Helper to check if an object has changed. More...
#include <ChangeMonitor.h>
Public Types | |
using | Data_t = T |
Type of the object being monitored. More... | |
using | Comparison_t = Comp |
Type of object for reference comparison. More... | |
Public Member Functions | |
ChangeMonitor (Comparison_t comp=Comparison_t{}) | |
Default constructor: starts with no reference value. More... | |
ChangeMonitor (Data_t const &ref, Comp comp=Comp{}) | |
Constructor: starts with ref as the reference value. More... | |
std::optional< Data_t > | update (Data_t const ¤tObj) |
Returns the old object if different from newObj . More... | |
std::optional< Data_t > | operator() (Data_t const ¤tObj) |
As update() . More... | |
bool | hasReference () const |
Returns whether a reference value is present. More... | |
Data_t const & | reference () const |
Returns the reference value; undefined if hasReference() is false . More... | |
Private Member Functions | |
bool | same (Data_t const &A, Data_t const &B) const |
Returns whether A and B represent the same value. More... | |
Private Attributes | |
std::optional< Data_t > | fRefObj |
The last object seen. More... | |
Comparison_t | fComp |
Comparison used for reference testing. More... | |
Helper to check if an object has changed.
T | type of the object |
Comp | type of the comparison between T objects |
This class can report if a value has changed from a previous check. The usage pattern is:
ChangeMonitor
is createdExample of usage:
A few observations:
update()
will not report any change; a constructor is available to establish a reference at construction time;var
which established the reference is later changed, the monitor is oblivious to that change.A copy of the "current" T
object is kept in this object.
Requirements for T
:
Comp
is default)icarus::ns::util::ThreadSafeChangeMonitor
. Definition at line 85 of file ChangeMonitor.h.
using icarus::ns::util::ChangeMonitor< T, Comp >::Comparison_t = Comp |
Type of object for reference comparison.
Definition at line 88 of file ChangeMonitor.h.
using icarus::ns::util::ChangeMonitor< T, Comp >::Data_t = T |
Type of the object being monitored.
Definition at line 87 of file ChangeMonitor.h.
|
inline |
Default constructor: starts with no reference value.
Definition at line 91 of file ChangeMonitor.h.
|
inline |
Constructor: starts with ref
as the reference value.
Definition at line 94 of file ChangeMonitor.h.
|
inline |
Returns whether a reference value is present.
Definition at line 124 of file ChangeMonitor.h.
|
inline |
As update()
.
Definition at line 120 of file ChangeMonitor.h.
|
inline |
Returns the reference value; undefined if hasReference()
is false
.
Definition at line 127 of file ChangeMonitor.h.
|
inlineprivate |
Returns whether A
and B
represent the same value.
Definition at line 137 of file ChangeMonitor.h.
|
inline |
Returns the old object if different from newObj
.
currentObj | the current object value |
currentObj
, or no valueIf there is no reference value, no value is returned. Otherwise, the reference is updated and the value of the old reference is returned. After each call, the reference value will be equivalent to currentObj
.
std::optional
object returned has no value, i.e. has_value()
is false
. Definition at line 111 of file ChangeMonitor.h.
|
private |
Comparison used for reference testing.
Definition at line 134 of file ChangeMonitor.h.
|
private |
The last object seen.
Definition at line 132 of file ChangeMonitor.h.