From a45e086dc57f51efb882bf864e78fe678238deea Mon Sep 17 00:00:00 2001 From: Ratan Gupta Date: Wed, 21 Feb 2018 19:03:13 +0530 Subject: Add callback contexts Add the notion of a callback context. This enables callbacks to have logic around the conditions they were invoked in. There are two context on which call back can be invoked 1) Startup: during startup all the call backs will be called 2) Signal: As part of condition match on the watched properties. Callback would behave differently based on the context. eg: eventCallback 1) Startup: Don't take any action. 2) Signal: Create the Dbus Object for the event. Change-Id: If455558798ac3e44bbd8a93de0ce1b09d2e308ae Signed-off-by: Ratan Gupta --- src/callback.hpp | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) (limited to 'src/callback.hpp') diff --git a/src/callback.hpp b/src/callback.hpp index 1d248bd..d476d97 100644 --- a/src/callback.hpp +++ b/src/callback.hpp @@ -25,8 +25,14 @@ class Callback Callback& operator=(Callback&&) = default; virtual ~Callback() = default; - /** @brief Run the callback. */ - virtual void operator()() = 0; + /** @brief Run the callback. + * @param[in] ctx - caller context + * Context could be Startup or Signal + * Startup: Callback is called as part of process startup. + * Signal: Callback is called as part of watch condition has been met. + * + */ + virtual void operator()(Context ctx) = 0; }; /** @class Conditional @@ -89,7 +95,7 @@ class IndexedCallback : public Callback : Callback(), index(callbackIndex) {} /** @brief Run the callback. */ - virtual void operator()() override = 0; + virtual void operator()(Context ctx) override = 0; protected: @@ -122,11 +128,11 @@ class GroupOfCallbacks : public Callback : graph(graphEntry) {} /** @brief Run the callbacks. */ - void operator()() override + void operator()(Context ctx) override { for (auto e : graph) { - (*CallbackAccess::get()[e])(); + (*CallbackAccess::get()[e])(ctx); } } @@ -154,11 +160,11 @@ class ConditionalCallback: public Callback : graph(graphEntry), condition(cond) {} /** @brief Run the callback if the condition is satisfied. */ - virtual void operator()() override + virtual void operator()(Context ctx) override { if (condition()) { - (*CallbackAccess::get()[graph[0]])(); + (*CallbackAccess::get()[graph[0]])(ctx); } } @@ -202,15 +208,15 @@ class DeferrableCallback : public ConditionalCallback delayInterval(delay), timer(nullptr) {} - void operator()() override + void operator()(Context ctx) override { if (!timer) { timer = std::make_unique( // **INDENT-OFF** - [this](auto & source) + [ctx, this](auto & source) { - this->ConditionalCallback::operator()(); + this->ConditionalCallback::operator()(ctx); }); // **INDENT-ON** timer->disable(); -- cgit v1.2.1