Skip to content

Commit

Permalink
improve API and make a new release
Browse files Browse the repository at this point in the history
  • Loading branch information
mhogomchungu committed Aug 14, 2018
1 parent 8ee81a9 commit fe956bb
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 47 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ cmake_minimum_required( VERSION 3.0 )

project( tasks )

set( LIB_VERSION "1.2.5" )
set( SO_VERSION "1.1.0" )
set( LIB_VERSION "1.2.6" )
set( SO_VERSION "2.0.0" )

add_definitions( -Wextra -Wall -pedantic )

Expand Down
23 changes: 12 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ Task::future<int>& foo = bar() ;
int r = foo.await() ;

```
**3. Example use an alternative use of .await() method of a future.**
**3. Example use of an alternative .await() method of a future.**

```c++

Expand Down Expand Up @@ -131,42 +131,43 @@ Task::future<int>& foo = Task::run( foo ) ;

```

**3. Creating a future that combines multiple functions. .get() and .queue() on the future will cause passed in functions to run sequentially and in the order they are specified. .await() and .then() will cause passed in functions to run concurrently.**
**3. Creating a future that combines arbitrary number of functions. .get() and .queue() on the future will cause passed in functions to run sequentially and in the order they are specified. .await() and .then() will cause passed in functions to run concurrently.**

```c++

void foo() ; //function prototype
void bar() ; //function prototype
void woof() ; //function prototype

Task::future<void>& foo = Task::run( foo,bar ) ;
Task::future<void>& foo = Task::run( foo,bar,woof ) ;

```

**4. Creating a future that combines multiple tasks and their continuations that take no argument. .get() and .queue() on the future will cause passed in functions to run sequentially and in the order they are specified. .await() and .then() will cause passed in functions to run concurrently.**
**4. Creating a future that combines arbitrary number of tasks and their continuations that take no argument. .get() and .queue() on the future will cause passed in functions to run sequentially and in the order they are specified. .await() and .then() will cause passed in functions to run concurrently.**

```c++

void foo() ; //function prototype
void bar() ; //function prototype

void cfoo() ; //function prototype
void cbar() ; //function prototype
void cfoo() ; //continuation function prototype
void cbar() ; //continuation function prototype

Task::future<void>& e = Task::run( Task::void_pair{ foo,cfoo },Task::void_pair{ bar,cbar } ) ;
Task::future<void>& e = Task::run( Task::make_pair( foo,cfoo ),Task::make_pair( bar,cbar ) ) ;

```

**5. Creating a future that combines multiple tasks and their continuations that takes an argument. .get() and .queue() on the future will cause passed in pairs to run sequentially and in the order they are specified. .await() and .then() will cause passed in pairs to run concurrently. The result of the future is undefined and a function that takes no argument should be used if .await() method of the future is called.**
**5. Creating a future that combines arbitrary number of tasks and their continuations that takes an argument. .get() and .queue() on the future will cause passed in pairs to run sequentially and in the order they are specified. .await() and .then() will cause passed in pairs to run concurrently. The result of the future is undefined and a function that takes no argument should be used if .await() method of the future is called.**

```c++

int foo() ; //function prototype
int bar() ; //function prototype

void cfoo( int ) ; //function prototype
void cbar( int ) ; //function prototype
void cfoo( int ) ; //continuation function prototype
void cbar( int ) ; //continuation function prototype

Task::future<int>& e = Task::run( Task::pair<int>{ foo,cfoo },Task::pair<int>{ bar,cbar } ) ;
Task::future<int>& e = Task::run( Task::make_pair( foo,cfoo ),Task::make_pair( bar,cbar ) ) ;
```
Further documentation of how to use the library is here[1] and here[2].
Expand Down
30 changes: 15 additions & 15 deletions example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,9 @@ static void _testing_multiple_tasks()
auto ra2 = [](){ _print( "r2" ) ; } ;
auto ra3 = [](){ _print( "r3" ) ; } ;

Task::future<void>& e = Task::run( Task::void_pair{ fna1,ra1 },
Task::void_pair{ fna2,ra2 },
Task::void_pair{ fna3,ra3 } ) ;
Task::future<void>& e = Task::run( Task::make_pair( fna1,ra1 ),
Task::make_pair( fna2,ra2 ),
Task::make_pair( fna3,ra3 ) ) ;

e.await() ;

Expand All @@ -217,9 +217,9 @@ static void _testing_multiple_tasks()
auto r2 = []( int ){ _print( "r2" ) ; } ;
auto r3 = []( int ){ _print( "r3" ) ; } ;

Task::future<int>& s = Task::run( Task::pair<int>{ fn1,r1 },
Task::pair<int>{ fn2,r2 },
Task::pair<int>{ fn3,r3 } ) ;
Task::future<int>& s = Task::run( Task::make_pair( fn1,r1 ),
Task::make_pair( fn2,r2 ),
Task::make_pair( fn3,r3 ) ) ;

s.then( _testing_multiple_tasks_with_start ) ;
}
Expand Down Expand Up @@ -263,9 +263,9 @@ static void _testing_multiple_tasks_with_start()
auto r2 = [ = ]( int ){ _print( "r2" ) ; e->count() ; } ;
auto r3 = [ = ]( int ){ _print( "r3" ) ; e->count() ; } ;

Task::future<int>& s = Task::run( Task::pair<int>{ fn1,r1 },
Task::pair<int>{ fn2,r2 },
Task::pair<int>{ fn3,r3 } ) ;
Task::future<int>& s = Task::run( Task::make_pair( fn1,r1 ),
Task::make_pair( fn2,r2 ),
Task::make_pair( fn3,r3 ) ) ;

s.start() ;
}
Expand All @@ -282,9 +282,9 @@ static void _testing_queue_with_no_results()
auto ra2 = [](){ _print( "r2" ) ; } ;
auto ra3 = [](){ _print( "r3" ) ; } ;

Task::future<void>& e = Task::run( Task::void_pair{ fna1,ra1 },
Task::void_pair{ fna2,ra2 },
Task::void_pair{ fna3,ra3 } ) ;
Task::future<void>& e = Task::run( Task::make_pair( fna1,ra1 ),
Task::make_pair( fna2,ra2 ),
Task::make_pair( fna3,ra3 ) ) ;

e.queue( _testing_queue_with_results ) ;
}
Expand All @@ -301,9 +301,9 @@ static void _testing_queue_with_results()
auto r2 = [ = ]( int ){ _print( "r2" ) ; } ;
auto r3 = [ = ]( int ){ _print( "r3" ) ; } ;

Task::future<int>& s = Task::run( Task::pair<int>{ fn1,r1 },
Task::pair<int>{ fn2,r2 },
Task::pair<int>{ fn3,r3 } ) ;
Task::future<int>& s = Task::run( Task::make_pair( fn1,r1 ),
Task::make_pair( fn2,r2 ),
Task::make_pair( fn3,r3 ) ) ;
s.queue( _test_run_then ) ;
}

Expand Down
64 changes: 45 additions & 19 deletions task.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* copyright: 2014-2017
* copyright: 2014-2018
* name : Francis Banyikwa
* email: [email protected]
*
Expand Down Expand Up @@ -106,11 +106,6 @@

namespace Task
{
template< typename T >
using pair = std::pair< std::function< T() >,std::function< void( T ) > > ;

using void_pair = std::pair< std::function< void() >,std::function< void() > > ;

template< typename T >
class future;

Expand All @@ -122,6 +117,37 @@ namespace Task
void add( Task::future< T >&,Task::future< T >&,std::function< void( T ) >&& ) ;
}

template< typename T >
struct pair{
pair( std::function< T() > first,std::function< void( T ) > second ) :
value( std::make_pair( std::move( first ),std::move( second ) ) )
{
}
std::pair< std::function< T() >,std::function< void( T ) > > value ;
};

template<>
struct pair<void>{
pair( std::function< void() > first,std::function< void() > second ) :
value( std::make_pair( std::move( first ),std::move( second ) ) )
{
}
std::pair< std::function< void() >,std::function< void() > > value ;
};

template< typename E,typename F >
pair<typename std::result_of<E()>::type> make_pair( E e,F f )
{
return pair<typename std::result_of<E()>::type>( std::move( e ),std::move( f ) ) ;
}

template< typename E,typename F,
typename std::enable_if<std::is_void<typename std::result_of<E()>::type>::value>::type>
pair< void > make_pair( E e,F f )
{
return pair< void >( std::move( e ),std::move( f ) ) ;
}

class Thread : public QThread
{
Q_OBJECT
Expand Down Expand Up @@ -350,7 +376,7 @@ namespace Task
};

template<>
class future< void > : private QObject
class future< void > : private QObject
{
public:
void then( std::function< void() > function )
Expand Down Expand Up @@ -653,14 +679,14 @@ namespace Task
template< typename E,typename F,typename ... T >
void add_pair( Task::future< E >& f,F&& s,T&& ... t )
{
add( f,Task::detail::run( std::move( s.first ) ),std::move( s.second ) ) ;
add( f,Task::detail::run( std::move( s.value.first ) ),std::move( s.value.second ) ) ;
add_pair( f,std::forward<T>( t ) ... ) ;
}

template< typename F,typename ... T >
void add_pair_void( Task::future< void >& f,F&& s,T&& ... t )
{
add_void( f,Task::detail::run( std::move( s.first ) ),std::move( s.second ) ) ;
add_void( f,Task::detail::run( std::move( s.value.first ) ),std::move( s.value.second ) ) ;
add_pair_void( f,std::forward<T>( t ) ... ) ;
}

Expand Down Expand Up @@ -705,7 +731,7 @@ namespace Task
}

template< typename ... T >
Task::future< void >& run( void_pair s,T ... t )
Task::future< void >& run( pair< void > s,T ... t )
{
auto& e = Task::detail::future< void >() ;
Task::detail::add_pair_void( e,std::move( s ),std::move( t ) ... ) ;
Expand Down Expand Up @@ -1032,27 +1058,27 @@ auto ra1 = [](){ std::cout << "r1" << std::endl ; } ;
auto ra2 = [](){ std::cout << "r2" << std::endl ; } ;
auto ra3 = [](){ std::cout << "r3" << std::endl ; } ;

Task::future<void>& e = Task::run( Task::void_pair{ fna1,ra1 },
Task::void_pair{ fna2,ra2 },
Task::void_pair{ fna3,ra3 } ) ;
Task::future<void>& e = Task::run( Task::make_pair( fna1,ra1 ),
Task::make_pair( fna2,ra2 ),
Task::make_pair( fna3,ra3 ) ) ;

e.await() ;

std::cout<< "Testing multiple tasks with continuation arguments" << std::endl ;

auto fn1 = [](){ _printThreadID(); return 0 ;} ;
auto fn2 = [](){ _printThreadID(); return 0 ;} ;
auto fn1 = [](){ _printThreadID(); return 0 ; } ;
auto fn2 = [](){ _printThreadID(); return 0 ; } ;
auto fn3 = [](){ _printThreadID(); return 0 ; } ;

auto r1 = []( int ){ std::cout << "r1" << std::endl ; } ;
auto r2 = []( int ){ std::cout << "r2" << std::endl ; } ;
auto r3 = []( int ){ std::cout << "r3" << std::endl ; } ;

Task::future<int>& s = Task::run( Task::pair<int>{ fn1,r1 },
Task::pair<int>{ fn2,r2 },
Task::pair<int>{ fn3,r3 } ) ;
Task::future<int>& s = Task::run( Task::make_pair( fn1,r1 ),
Task::make_pair( fn2,r2 ),
Task::make_pair( fn3,r3 ) ) ;

s.then( [](){ QCoreApplication::quit() ;} ) ;
s.then( [](){ QCoreApplication::quit() ; } ) ;

#endif //end example block

Expand Down

0 comments on commit fe956bb

Please sign in to comment.