From ce4fbe111da9375e9614e018547e90092df6ec4b Mon Sep 17 00:00:00 2001 From: Brad Bishop Date: Tue, 6 Jun 2017 23:58:09 -0400 Subject: expose watch callbacks Allow watch class users to explicitly invoke the watch callback. Since watches and callbacks share a common pool of state all watches must complete their initialization prior to invoking their callback methods. Change-Id: I62ebad64da88a145f3d5006b07c01381b0eb6728 Signed-off-by: Brad Bishop --- src/propertywatch.hpp | 12 +++++++++--- src/propertywatchimpl.hpp | 15 +++++++++++---- src/watch.hpp | 9 ++++++++- 3 files changed, 28 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/propertywatch.hpp b/src/propertywatch.hpp index 3eb05df..55f6e5b 100644 --- a/src/propertywatch.hpp +++ b/src/propertywatch.hpp @@ -37,8 +37,8 @@ class PropertyWatch : public Watch virtual ~PropertyWatch() = default; PropertyWatch( const PropertyIndex& watchIndex, - Callback* cb = nullptr) - : Watch(), index(watchIndex), callback(cb), alreadyRan(false) {} + Callback* callback = nullptr) + : Watch(), index(watchIndex), cb(callback), alreadyRan(false) {} /** @brief Start the watch. * @@ -46,6 +46,12 @@ class PropertyWatch : public Watch */ void start() override; + /** @brief Run the watch callback method. + * + * Watch callback interface implementation for PropertyWatch. + */ + void callback() override; + /** @brief Update properties. * * Subclasses to query the properties specified by the index @@ -89,7 +95,7 @@ class PropertyWatch : public Watch const PropertyIndex& index; /** @brief Optional callback method. */ - Callback* const callback; + Callback* const cb; /** @brief The start method should only be invoked once. */ bool alreadyRan; diff --git a/src/propertywatchimpl.hpp b/src/propertywatchimpl.hpp index d0153e4..67bbaff 100644 --- a/src/propertywatchimpl.hpp +++ b/src/propertywatchimpl.hpp @@ -108,6 +108,16 @@ void PropertyWatch::start() alreadyRan = true; } +template +void PropertyWatch::callback() +{ + // Invoke callback if present. + if (this->alreadyRan && this->cb) + { + (*this->cb)(); + } +} + template void PropertyWatchOfType::updateProperties( const std::string& busName, @@ -144,10 +154,7 @@ void PropertyWatchOfType::propertiesChanged( std::get<2>(item->second).get() = p.second.template get(); // Invoke callback if present. - if (this->alreadyRan && this->callback) - { - (*this->callback)(); - } + this->callback(); } } diff --git a/src/watch.hpp b/src/watch.hpp index 1ba671d..e36af9c 100644 --- a/src/watch.hpp +++ b/src/watch.hpp @@ -15,7 +15,10 @@ namespace monitoring * or initialization. Typical implementations might register dbus * callbacks or perform queries. * - * Watches of any type can be started. + * The callback method is invoked by main() on all watches of any + * type at application startup, after all watches have performed + * their setup. Typical implementations will forward the call + * to their associated callback. */ class Watch { @@ -29,6 +32,10 @@ class Watch /** @brief Start the watch. */ virtual void start() = 0; + + /** @brief Invoke the callback associated with the watch. */ + virtual void callback() = 0; + }; } // namespace monitoring -- cgit v1.2.1