From 346b79d3b7dfe649dc33efe582a096418e354450 Mon Sep 17 00:00:00 2001 From: Michael Davidsaver Date: Mon, 19 Oct 2020 16:41:19 -0700 Subject: [PATCH] Add Subscription::shared_from_this() Allow event() handler to acquire a shared_ptr to eg. queue for later processing. --- src/clientmon.cpp | 6 ++++++ src/pvxs/client.h | 5 +++++ 2 files changed, 11 insertions(+) diff --git a/src/clientmon.cpp b/src/clientmon.cpp index 7716455..2e6b2f1 100644 --- a/src/clientmon.cpp +++ b/src/clientmon.cpp @@ -39,6 +39,7 @@ struct SubscriptionImpl : public OperationBase, public Subscription evevent ackTick; // const after exec() + std::weak_ptr self; // internal std::function onInit; std::function event; Value pvRequest; @@ -173,6 +174,10 @@ struct SubscriptionImpl : public OperationBase, public Subscription return ret; } + virtual std::shared_ptr shared_from_this() const override final { + return std::shared_ptr(self); + } + virtual bool cancel() override final { auto context = chan->context; decltype (event) junk; @@ -552,6 +557,7 @@ std::shared_ptr MonitorBuilder::exec() auto chan = Channel::build(ctx->shared_from_this(), _name); auto op = std::make_shared(Operation::Monitor, chan); + op->self = op; op->event = std::move(_event); op->onInit = std::move(_onInit); op->pvRequest = _buildReq(); diff --git a/src/pvxs/client.h b/src/pvxs/client.h index 2a69c21..f8bda66 100644 --- a/src/pvxs/client.h +++ b/src/pvxs/client.h @@ -193,6 +193,11 @@ public: * @endcode */ virtual Value pop() =0; + + //! Return strong internal reference which will not prevent + //! implicit cancellation when the last reference returned + //! by exec() is released. + virtual std::shared_ptr shared_from_this() const =0; }; //! Handle for entry in Channel cache