fork download
  1. #include <iostream>
  2. #include <memory>
  3. #include <vector>
  4. #include <functional>
  5. using namespace std;
  6.  
  7. class Strategy;
  8. struct ScheduledEvent {
  9. function<void(Strategy*)> tocall;
  10. void perform(Strategy*u) { tocall(u); }
  11. ScheduledEvent(function<void(Strategy*)> func) : tocall(func) { }
  12. };
  13.  
  14. class Strategy {
  15. vector<unique_ptr<ScheduledEvent>> heap_eventlist;
  16. public:
  17. void schedule_function(function<void(Strategy*)> func) {
  18. // Puts the scheduled event built with the strategy function onto a list
  19. heap_eventlist.emplace_back(std::make_unique<ScheduledEvent>(func));
  20. }
  21. void do_it() {
  22. for (auto &x:heap_eventlist)
  23. x->perform(this);
  24. }
  25. virtual void f() { cout <<"Strategy"<<endl; }
  26. };
  27.  
  28. struct BasicAlgo : Strategy {
  29. string b="BasicAlgo";
  30. void ff() { cout << b<<endl; }
  31. };
  32.  
  33.  
  34. int main() {
  35. BasicAlgo g;
  36. g.schedule_function (&BasicAlgo::f);
  37. g.schedule_function ([](Strategy *x)->void { BasicAlgo*b=dynamic_cast<BasicAlgo*>(x); if (b) b->ff(); });
  38. g.do_it();
  39.  
  40. return 0;
  41. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
Strategy
BasicAlgo