1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
#include <iostream>
#include <functional>
#include <queue>
#include <string>
struct task_queue
{
template < typename CALLABLE, typename... ARGS >
void run_when_asked( CALLABLE fn, ARGS&&... args )
{ pending.push( std::bind( fn, args... ) ) ; }
void execute_pending()
{
while( !pending.empty() )
{
pending.front()() ;
pending.pop() ;
}
}
std::queue< std::function< void() > > pending ;
};
int free_fun( int a, int b )
{
std::cout << "free_fun(" << a << ',' << b << ")\n" ;
return a+b ;
}
struct function_object
{
void operator() ( const std::string& str ) const
{ std::cout << "function_object()(" << str << ")\n" ; }
};
struct some_class
{
double mem_fun( double a, double b ) const
{
std::cout << "some_class::mem_fun( (" << this << "), " << a << ", " << b << " )\n" ;
return a+b+v ;
}
int v = 7 ;
};
int main()
{
task_queue tq ;
tq.run_when_asked( free_fun, 1, 2 ) ;
function_object f ;
tq.run_when_asked( f, "hello world" ) ;
some_class obj ;
tq.run_when_asked( &some_class::mem_fun, std::ref(obj), 3.4, 5.6 ) ;
tq.run_when_asked( [] ( int a, int b )
{ std::cout << "closure(" << a << ',' << b << ")\n" ; },
5, 6 ) ;
tq.execute_pending() ;
}
|