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