#include <iostream>
#include <vector>
#include <map>
#include <functional>
#include <memory>
using namespace std;
class SoapDispatcher
{
public:
std::map<std::string, std::function<void(const std::string&)>> idSubscribers_;
std::function<void()> subscribe(const std::string& id, std::function<void(const std::string&)> callback)
{
idSubscribers_[id] = callback;
return [this, id]() { idSubscribers_.erase(id); };
}
};
void handleThresholdUpdate()
{
std::cout <<"1111111111111111111111111111111"<<std::endl;
}
class Scenario
{
std::function<void()> unsubscriber_;
std::shared_ptr<SoapDispatcher> soapDispatcher = make_shared<SoapDispatcher>();
public:
void test()
{
std::function<void()> unsubscriber_;
unsubscriber_ = soapDispatcher->subscribe
("2", [this, unsubscriber_](const std::string& data){
if (unsubscriber_)
{
unsubscriber_();
}
handleThresholdUpdate();
}
);
auto idIter = soapDispatcher->idSubscribers_.find("2");
if (soapDispatcher->idSubscribers_.end() != idIter)
{
idIter->second("data");
}
}
};
int main(){
std::shared_ptr<Scenario> scenario = make_shared<Scenario>();
scenario->test();
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8bWFwPgojaW5jbHVkZSA8ZnVuY3Rpb25hbD4KI2luY2x1ZGUgPG1lbW9yeT4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCmNsYXNzIFNvYXBEaXNwYXRjaGVyCnsKcHVibGljOgoKc3RkOjptYXA8c3RkOjpzdHJpbmcsIHN0ZDo6ZnVuY3Rpb248dm9pZChjb25zdCBzdGQ6OnN0cmluZyYpPj4gaWRTdWJzY3JpYmVyc187CgpzdGQ6OmZ1bmN0aW9uPHZvaWQoKT4gc3Vic2NyaWJlKGNvbnN0IHN0ZDo6c3RyaW5nJiBpZCwgc3RkOjpmdW5jdGlvbjx2b2lkKGNvbnN0IHN0ZDo6c3RyaW5nJik+IGNhbGxiYWNrKQp7CglpZFN1YnNjcmliZXJzX1tpZF0gPSBjYWxsYmFjazsKICAgIHJldHVybiBbdGhpcywgaWRdKCkgeyBpZFN1YnNjcm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