fork 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::function<void()> subscribe(const std::string& id, std::function<void()> callback)
  13. {
  14. idSubscribers_[id] = callback;
  15. return [this, id]() { idSubscribers_.erase(id); };
  16. }
  17.  
  18. public:
  19.  
  20. std::map<std::string, std::function<void()>> idSubscribers_;
  21. };
  22.  
  23. void handleThresholdUpdate(string& ss)
  24. {
  25. std::cout <<"1111111111111111111111111111111"<< ss <<std::endl;
  26. }
  27.  
  28. class Scenario
  29. {
  30. public:
  31. void test()
  32. {
  33. std::function<void()> unsubscriber_;
  34. unsubscriber_ = soapDispatcher->subscribe
  35. ("2", [this, unsubscriber_](){
  36. if (unsubscriber_)
  37. {
  38. unsubscriber_();
  39. }
  40. string aa = "what's wrong";
  41. handleThresholdUpdate(aa);
  42. }
  43. );
  44. auto idIter = soapDispatcher->idSubscribers_.find("2");
  45. if (soapDispatcher->idSubscribers_.end() != idIter)
  46. {
  47. idIter->second();
  48. }
  49. }
  50.  
  51. public:
  52. std::function<void()> unsubscriber_;
  53. std::shared_ptr<SoapDispatcher> soapDispatcher = make_shared<SoapDispatcher>();
  54. };
  55.  
  56.  
  57.  
  58. int main(){
  59. std::shared_ptr<Scenario> scenario = make_shared<Scenario>();
  60.  
  61. scenario->test();
  62.  
  63. }
  64.  
Success #stdin #stdout 0.01s 5260KB
stdin
Standard input is empty
stdout
1111111111111111111111111111111what's wrong