/* * ObservedValue_wCallback.hpp * * Created on: Oct 16, 2015 * Author: heber */ #ifndef OBSERVEDVALUE_WCALLBACK_HPP_ #define OBSERVEDVALUE_WCALLBACK_HPP_ // include config.h #ifdef HAVE_CONFIG_H #include #endif #include "CodePatterns/Assert.hpp" #include "CodePatterns/ObservedValue.hpp" #include "CodePatterns/Observer/Observable.hpp" #include #include #include #include /** We derive from ObservedValue in order to tell owning instance about * subjectKilled() having been called. */ template class ObservedValue_wCallback : public ObservedValue { public: ObservedValue_wCallback( const Observable * const _owner, const boost::function &_recalcMethod, const std::string &_name, const T &_initialvalue, const Observable::channels_t &_channels, const boost::function &_callback) : ObservedValue(_owner, _recalcMethod, _name, _initialvalue, _channels), signedOnChannels(std::max((size_t)1,_channels.size())), callback(_callback), CallbackIsGone(false) {} virtual ~ObservedValue_wCallback() {} /** Function is called by callback owner to inform about its destruction. * * \note callback must not be used after this */ void noteCallBackIsGone() { CallbackIsGone = true; } protected: virtual void subjectKilled(Observable *publisher) { ASSERT(signedOnChannels > 0, "ObservedValue_wCallback::subjectKilled() - signedOnChannels is already zero."); if ((--signedOnChannels) == 0) { ObservedValue::subjectKilled(publisher); if (!CallbackIsGone) callback(); } } private: //!> if we are signOn() to multiple channels, count down before callback size_t signedOnChannels; //!> callback function to tell other entity about subjectKilled const boost::function callback; //!> is callback still alive or not bool CallbackIsGone; }; #endif /* OBSERVEDVALUE_WCALLBACK_HPP_ */