/* * ValueWatchDog.hpp * * Created on: 12 окт. 2019 г. * Author: titov */ #ifndef SOURCE_SCHEMATIC_VALUEWATCHDOG_HPP_ #define SOURCE_SCHEMATIC_VALUEWATCHDOG_HPP_ #include "../systemic/Timer.hh" #include "../systemic/IValue.hpp" #include "../systemic/IStatus.hh" namespace schematic { template struct ValueWatchDogBase : public systemic::IStatus { protected: typedef systemic::IValue Value; typedef BaseType Base; ValueWatchDogBase( Value & value, float timeout_in_second ); public: virtual operator bool() const; protected: Value & value; const systemic::time_t timeout; mutable Base previous_value; mutable systemic::Timer timer; }; template struct ValueWatchDogObser2Obser : public ValueWatchDogBase { typedef systemic::IValue Value; typedef BaseType Base; ValueWatchDogObser2Obser( Value & value, float timeout_in_second ) : ValueWatchDogBase( value, timeout_in_second ) {} operator bool() const; }; template struct ValueWatchDogProcess2Obser : public ValueWatchDogBase { typedef systemic::IValue Value; typedef BaseType Base; ValueWatchDogProcess2Obser( Value & value, float timeout_in_second ) : ValueWatchDogBase( value, timeout_in_second ) {} void process(); }; //template //struct ValueWatchDogAsync2Obser : public systemic::IStatus, public IValueSet { // // ValueWatchDogAsync2Obser( float timeout_in_second ); // // operator bool() const; // IValueSet & operator=( BaseType val ); // //protected: // const systemic::time_t timeout; // // Base previous_value; // systemic::Timer timer; //}; //template //struct ValueWatchDogAsync2Process : public IValueSet { // // ValueWatchDogAsync2Obser( IValueSet & output, float timeout_in_second ); // // void process(); // IValueSet & operator=( BaseType val ); // //protected: // IValueSet & output; // const systemic::time_t timeout; // // Base previous_value; // systemic::Timer timer; //}; } template inline schematic::ValueWatchDogBase::ValueWatchDogBase( Value & value, float timeout_in_second ) : value(value), timeout(systemic::seconds2time(timeout_in_second)), previous_value(value), timer() { timer.start(timeout); } template inline schematic::ValueWatchDogBase::operator bool() const { return timer.delayElapsed(); } template inline schematic::ValueWatchDogObser2Obser::operator bool() const { Base temp_value = ValueWatchDogBase::value; if( ValueWatchDogBase::previous_value != temp_value ) { ValueWatchDogBase::previous_value = temp_value; ValueWatchDogBase::timer.start( ValueWatchDogBase::timeout ); } return ValueWatchDogBase::operator bool(); } template inline void schematic::ValueWatchDogProcess2Obser::process() { typename ValueWatchDogBase::Base temp_value = ValueWatchDogBase::value; if( ValueWatchDogBase::previous_value != temp_value ) { ValueWatchDogBase::previous_value = temp_value; ValueWatchDogBase::timer.start(ValueWatchDogBase::timeout); } } #endif /* SOURCE_SCHEMATIC_VALUEWATCHDOG_HPP_ */