fork(1) download
  1. #include <iostream>
  2. #include <vector>
  3. #include <map>
  4. #include <functional>
  5. #include <memory>
  6. using namespace std;
  7.  
  8. class SoapDispatcher
  9. {
  10. public:
  11.  
  12. std::map<std::string, std::function<void(const std::string&)>> idSubscribers_;
  13.  
  14. std::function<void()> subscribe(const std::string& id, std::function<void(const std::string&)> callback)
  15. {
  16. idSubscribers_[id] = callback;
  17. return [this, id]() { idSubscribers_.erase(id); };
  18. }
  19. };
  20.  
  21. void handleThresholdUpdate()
  22. {
  23. std::cout <<"1111111111111111111111111111111"<<std::endl;
  24. }
  25.  
  26. class Scenario
  27. {
  28. std::function<void()> unsubscriber_;
  29. std::shared_ptr<SoapDispatcher> soapDispatcher = make_shared<SoapDispatcher>();
  30.  
  31. public:
  32. void test()
  33. {
  34. std::function<void()> unsubscriber_;
  35. unsubscriber_ = soapDispatcher->subscribe
  36. ("2", [this, unsubscriber_](const std::string& data){
  37. if (unsubscriber_)
  38. {
  39. unsubscriber_();
  40. }
  41. handleThresholdUpdate();
  42. }
  43. );
  44. auto idIter = soapDispatcher->idSubscribers_.find("2");
  45. if (soapDispatcher->idSubscribers_.end() != idIter)
  46. {
  47. idIter->second("data");
  48. }
  49. }
  50.  
  51.  
  52. };
  53.  
  54.  
  55.  
  56. int main(){
  57. std::shared_ptr<Scenario> scenario = make_shared<Scenario>();
  58.  
  59. scenario->test();
  60.  
  61. }
  62.  
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
1111111111111111111111111111111