fork download
  1. class furrovineexport Dispatcher {
  2. protected:
  3. typedef std::unique_ptr<DispatchBase> TDispatch;
  4. typedef std::list<TDispatch> TDispatchList;
  5. TDispatchList work;
  6. std::mutex thislock;
  7.  
  8. public:
  9. Dispatcher () {
  10.  
  11. }
  12.  
  13. template <typename R> void Add ( std::function<R ( )> f ) {
  14. thislock.lock();
  15. work.push_back( TDispatch( new Dispatch0<R>( f ) ) );
  16. thislock.unlock();
  17. }
  18.  
  19. template <typename R, typename T1> void Add ( std::function<R (T1)> f, T1 t1 ) {
  20. thislock.lock();
  21. work.push_back( TDispatch( new Dispatch1<R, T1>( f, t1 ) ) );
  22. thislock.unlock();
  23. }
  24.  
  25. template <typename R, typename T1, typename T2> void Add ( std::function<R (T1)> f, T1 t1, T2 t2 ) {
  26. thislock.lock();
  27. work.push_back(
  28. TDispatch( new Dispatch2<R, T1, T2>( f,
  29. std::forward<T1>( t1 ),
  30. std::forward<T2>( t2 ) ) ) );
  31. thislock.unlock();
  32. }
  33.  
  34. template <typename R, typename T1, typename T2, typename T3> void Add ( std::function<R (T1)> f, T1 t1, T2 t2, T3 t3 ) {
  35. thislock.lock();
  36. work.push_back( TDispatch(
  37. new Dispatch3<R, T1, T2, T3>( f,
  38. std::forward<T1>( t1 ),
  39. std::forward<T2>( t2 ),
  40. std::forward<T3>( t3 ) ) ) );
  41. thislock.unlock();
  42. }
  43.  
  44. template <typename R, typename T1, typename T2, typename T3, typename T4> void Add ( std::function<R (T1)> f, T1 t1, T2 t2, T3 t3 ) {
  45. thislock.lock();
  46. work.push_back( TDispatch(
  47. new Dispatch3<R, T1, T2, T3>( f,
  48. std::forward<T1>( t1 ),
  49. std::forward<T2>( t2 ),
  50. std::forward<T3>( t3 ),
  51. std::forward<T4>( t4 ) ) ) );
  52. thislock.unlock();
  53. }
  54.  
  55. void Do () {
  56. thislock.lock();
  57. TDispatchList dispatchwork = std::move( work );
  58. work.clear();
  59. thislock.unlock();
  60. auto d = dispatchwork.begin();
  61. while ( d != dispatchwork.end( ) ) {
  62. (*d)->Invoke( );
  63. ++d;
  64. }
  65. }
  66.  
  67. void operator () () {
  68. Do();
  69. }
  70. };
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty