Add Subscription::shared_from_this()

Allow event() handler to acquire a shared_ptr<Subscription>
to eg. queue for later processing.
This commit is contained in:
Michael Davidsaver
2020-10-19 16:41:19 -07:00
parent fb9f356457
commit 346b79d3b7
2 changed files with 11 additions and 0 deletions
+6
View File
@@ -39,6 +39,7 @@ struct SubscriptionImpl : public OperationBase, public Subscription
evevent ackTick;
// const after exec()
std::weak_ptr<SubscriptionImpl> self; // internal
std::function<void (const Value&)> onInit;
std::function<void(Subscription&)> event;
Value pvRequest;
@@ -173,6 +174,10 @@ struct SubscriptionImpl : public OperationBase, public Subscription
return ret;
}
virtual std::shared_ptr<Subscription> shared_from_this() const override final {
return std::shared_ptr<Subscription>(self);
}
virtual bool cancel() override final {
auto context = chan->context;
decltype (event) junk;
@@ -552,6 +557,7 @@ std::shared_ptr<Subscription> MonitorBuilder::exec()
auto chan = Channel::build(ctx->shared_from_this(), _name);
auto op = std::make_shared<SubscriptionImpl>(Operation::Monitor, chan);
op->self = op;
op->event = std::move(_event);
op->onInit = std::move(_onInit);
op->pvRequest = _buildReq();