fork download
  1. using namespace std;
  2. using namespace std::tr1;
  3.  
  4. template <typename T1> class Event {
  5. protected:
  6. vector<function<void(T1)>> functors;
  7. vector<Event<T1>*> events;
  8.  
  9. public:
  10. const static int ExpectedFunctorCount = 2;
  11.  
  12. Event () : functors(), events() {
  13. functors.reserve( ExpectedFunctorCount );
  14. events.reserve( ExpectedFunctorCount );
  15. }
  16.  
  17. void Add ( void(* func)( T1 ) ) {
  18. functors.push_back( function<void(T1)>( func ) );
  19. }
  20.  
  21. void Add ( function<void( T1 )> func ) {
  22. functors.push_back( func );
  23. }
  24.  
  25. void Invoke ( T1 a1 ) {
  26. for ( std::size_t f = 0; f < functors.size(); ++f ) {
  27. functors[f]( a1 );
  28. }
  29. for ( std::size_t f = 0; f < events.size(); ++f ) {
  30. (*events[f])( a1 );
  31. }
  32. }
  33.  
  34. void operator() ( T1 a1 ) {
  35. Invoke( a1 );
  36. }
  37.  
  38. size_t InvocationCount ( ) {
  39. return events.size( ) + functors.size( );
  40. }
  41.  
  42. void RemoveAt ( size_t index ) {
  43.  
  44. }
  45.  
  46. };
  47.  
  48. template <> class Event<void> {
  49. protected:
  50. std::vector<function<void(void)>> functors;
  51. std::vector<Event<void>*> events;
  52.  
  53. public:
  54. const static int ExpectedFunctorCount = 2;
  55.  
  56. Event () : functors(), events() {
  57. functors.reserve( ExpectedFunctorCount );
  58. events.reserve( ExpectedFunctorCount );
  59. }
  60.  
  61. void Add ( void(* func)( void ) ) {
  62. functors.push_back( function<void(void)>( func ) );
  63. }
  64.  
  65. void Add ( function<void( void )> func ) {
  66. functors.push_back( func );
  67. }
  68.  
  69. void Invoke ( void ) {
  70. for ( size_t f = 0; f < functors.size(); ++f ) {
  71. functors[f]( );
  72. }
  73. for ( size_t f = 0; f < events.size(); ++f ) {
  74. (*events[f])( );
  75. }
  76. }
  77.  
  78. void operator() ( void ) {
  79. Invoke( );
  80. }
  81.  
  82. size_t InvocationCount ( ) {
  83. return events.size( ) + functors.size( );
  84. }
  85.  
  86. void RemoveAt ( size_t index ) {
  87.  
  88. }
  89.  
  90. };
  91.  
  92. void VoidTest () {
  93. cout << "INTO THE VOID" << endl;
  94. }
  95.  
  96. void IntTest ( int num ) {
  97. cout << "Got myself a " << num << " !" << endl;
  98. }
  99.  
  100. int main ( int argc, char* argv[] ) {
  101.  
  102. Event<void> VoidEv;
  103. Event<int> IntEv;
  104.  
  105. IntEv.Add( IntTest );
  106. IntEv( 20 );
  107.  
  108. return 0;
  109. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Events, go go!
compilation info
prog.cpp:2:22: error: 'tr1' is not a namespace-name
prog.cpp:2:25: error: expected namespace-name before ';' token
prog.cpp:6:5: error: 'vector' does not name a type
prog.cpp:7:2: error: 'vector' does not name a type
prog.cpp:21:13: error: 'function' has not been declared
prog.cpp:21:21: error: expected ',' or '...' before '<' token
prog.cpp:38:2: error: 'size_t' does not name a type
prog.cpp:42:18: error: 'size_t' has not been declared
prog.cpp: In constructor 'Event<T1>::Event()':
prog.cpp:12:13: error: class 'Event<T1>' does not have any field named 'functors'
prog.cpp:12:25: error: class 'Event<T1>' does not have any field named 'events'
prog.cpp:13:3: error: 'functors' was not declared in this scope
prog.cpp:14:3: error: 'events' was not declared in this scope
prog.cpp: In member function 'void Event<T1>::Add(void (*)(T1))':
prog.cpp:18:3: error: 'functors' was not declared in this scope
prog.cpp:18:23: error: 'function' was not declared in this scope
prog.cpp:18:32: error: expected primary-expression before 'void'
prog.cpp: In member function 'void Event<T1>::Add(int)':
prog.cpp:22:3: error: 'functors' was not declared in this scope
prog.cpp:22:23: error: 'func' was not declared in this scope
prog.cpp: In member function 'void Event<T1>::Invoke(T1)':
prog.cpp:26:9: error: 'size_t' is not a member of 'std'
prog.cpp:26:21: error: expected ';' before 'f'
prog.cpp:26:28: error: 'f' was not declared in this scope
prog.cpp:26:32: error: 'functors' was not declared in this scope
prog.cpp:29:9: error: 'size_t' is not a member of 'std'
prog.cpp:29:21: error: expected ';' before 'f'
prog.cpp:29:28: error: 'f' was not declared in this scope
prog.cpp:29:32: error: 'events' was not declared in this scope
prog.cpp: At global scope:
prog.cpp:50:2: error: 'vector' in namespace 'std' does not name a type
prog.cpp:51:2: error: 'vector' in namespace 'std' does not name a type
prog.cpp:65:13: error: 'function' has not been declared
prog.cpp:65:21: error: expected ',' or '...' before '<' token
prog.cpp:82:2: error: 'size_t' does not name a type
prog.cpp:86:18: error: 'size_t' has not been declared
prog.cpp: In constructor 'Event<void>::Event()':
prog.cpp:56:13: error: class 'Event<void>' does not have any field named 'functors'
prog.cpp:56:25: error: class 'Event<void>' does not have any field named 'events'
prog.cpp:57:3: error: 'functors' was not declared in this scope
prog.cpp:58:3: error: 'events' was not declared in this scope
prog.cpp: In member function 'void Event<void>::Add(void (*)())':
prog.cpp:62:3: error: 'functors' was not declared in this scope
prog.cpp:62:23: error: 'function' was not declared in this scope
prog.cpp:62:32: error: expected primary-expression before 'void'
prog.cpp: In member function 'void Event<void>::Add(int)':
prog.cpp:66:3: error: 'functors' was not declared in this scope
prog.cpp:66:23: error: 'func' was not declared in this scope
prog.cpp: In member function 'void Event<void>::Invoke()':
prog.cpp:70:9: error: 'size_t' was not declared in this scope
prog.cpp:70:16: error: expected ';' before 'f'
prog.cpp:70:23: error: 'f' was not declared in this scope
prog.cpp:70:27: error: 'functors' was not declared in this scope
prog.cpp:73:16: error: expected ';' before 'f'
prog.cpp:73:23: error: 'f' was not declared in this scope
prog.cpp:73:27: error: 'events' was not declared in this scope
prog.cpp: In function 'void VoidTest()':
prog.cpp:93:2: error: 'cout' was not declared in this scope
prog.cpp:93:29: error: 'endl' was not declared in this scope
prog.cpp: In function 'void IntTest(int)':
prog.cpp:97:2: error: 'cout' was not declared in this scope
prog.cpp:97:44: error: 'endl' was not declared in this scope
stdout
Standard output is empty