fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <utility>
  4.  
  5. void A() {
  6. std::cout << "I'm an A" << std::endl;
  7. }
  8.  
  9. void B() {
  10. std::cout << "I'm a B" << std::endl;
  11. }
  12.  
  13. void C() {
  14. std::cout << "I'm a C" << std::endl;
  15. }
  16.  
  17. class processor {
  18. private:
  19. std::vector<void (*)()> funcs;
  20. std::vector<std::pair<int, int>> links;
  21. public:
  22. void add_func(void (*func)()) { funcs.push_back(func); }
  23. void link(int from, int to) { links.push_back({from, to}); }
  24. void call(int indx) {
  25. // Make the call
  26. std::cout << "generic preprocessing" << std::endl;
  27. funcs.at(indx)();
  28. std::cout << "generic postprocessing" << std::endl;
  29.  
  30. // Call any links
  31. for(auto it : links) {
  32. if(it.first == indx) { call(it.second); }
  33. }
  34. }
  35. };
  36.  
  37. int main() {
  38. processor p;
  39. p.add_func(A);
  40. p.add_func(B);
  41. p.add_func(C);
  42.  
  43. p.link(0, 1); // A -> B
  44. p.link(1, 2); // B -> C
  45.  
  46. p.call(0); // Call A
  47.  
  48. return 0;
  49. }
Success #stdin #stdout 0s 4912KB
stdin
Standard input is empty
stdout
generic preprocessing
I'm an A
generic postprocessing
generic preprocessing
I'm a B
generic postprocessing
generic preprocessing
I'm a C
generic postprocessing