All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Public Types | Public Member Functions | Private Member Functions | Private Attributes | List of all members
icarus::ns::util::ChangeMonitor< T, Comp > Struct Template Reference

Helper to check if an object has changed. More...

#include <ChangeMonitor.h>

Inheritance diagram for icarus::ns::util::ChangeMonitor< T, Comp >:
icarus::ns::util::ThreadSafeChangeMonitor< T, Comp >

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_tupdate (Data_t const &currentObj)
 Returns the old object if different from newObj. More...
 
std::optional< Data_toperator() (Data_t const &currentObj)
 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_tfRefObj
 The last object seen. More...
 
Comparison_t fComp
 Comparison used for reference testing. More...
 

Detailed Description

template<typename T, typename Comp = std::equal_to<T>>
struct icarus::ns::util::ChangeMonitor< T, Comp >

Helper to check if an object has changed.

Template Parameters
Ttype of the object
Comptype of the comparison between T objects

This class can report if a value has changed from a previous check. The usage pattern is:

  1. a ChangeMonitor is created
  2. the monitor object is given a value, which becomes the new reference
  3. arbitrary processing happens
  4. the monitor object is given another value, which becomes the new reference; if this value is different from the previous reference, the previous reference is returned
  5. steps 2 and 3 can be reiterated

Example of usage:

// starts with no reference by default
// first check just establishes the reference
int var = 0;
monitor(var); // this is also a `update()`, which returns no value
// reference is 0, new value is 1: a change is detected
if (monitor(1)) {
std::cout << "Value has changed!" << std::endl;
}
var = 5; // this does not change the monitoring
// reference is now 1, new value is 1: no change is detected
if (monitor(1)) {
std::cout << "Value has changed again!" << std::endl;
}
// more complex syntax (C++17) allows accessing the old reference value;
// reference is now 1, new value is 2: change is detected
if (auto prevVal = monitor(2); prevVal) {
std::cout << "Value has changed from " << *prevVal << " to 2!"
<< std::endl;
}

A few observations:

A copy of the "current" T object is kept in this object.

Requirements for T:

Note
This implementation is not thread-safe. For a thread-safe change monitor, see icarus::ns::util::ThreadSafeChangeMonitor.

Definition at line 85 of file ChangeMonitor.h.

Member Typedef Documentation

template<typename T, typename Comp = std::equal_to<T>>
using icarus::ns::util::ChangeMonitor< T, Comp >::Comparison_t = Comp

Type of object for reference comparison.

Definition at line 88 of file ChangeMonitor.h.

template<typename T, typename Comp = std::equal_to<T>>
using icarus::ns::util::ChangeMonitor< T, Comp >::Data_t = T

Type of the object being monitored.

Definition at line 87 of file ChangeMonitor.h.

Constructor & Destructor Documentation

template<typename T, typename Comp = std::equal_to<T>>
icarus::ns::util::ChangeMonitor< T, Comp >::ChangeMonitor ( Comparison_t  comp = Comparison_t{})
inline

Default constructor: starts with no reference value.

Definition at line 91 of file ChangeMonitor.h.

91 {}): fComp(std::move(comp)) {}
Comparison_t fComp
Comparison used for reference testing.
template<typename T, typename Comp = std::equal_to<T>>
icarus::ns::util::ChangeMonitor< T, Comp >::ChangeMonitor ( Data_t const &  ref,
Comp  comp = Comp{} 
)
inline

Constructor: starts with ref as the reference value.

Definition at line 94 of file ChangeMonitor.h.

94  {})
95  : fRefObj(ref), fComp(std::move(comp)) {}
Comparison_t fComp
Comparison used for reference testing.
std::optional< Data_t > fRefObj
The last object seen.

Member Function Documentation

template<typename T, typename Comp = std::equal_to<T>>
bool icarus::ns::util::ChangeMonitor< T, Comp >::hasReference ( ) const
inline

Returns whether a reference value is present.

Definition at line 124 of file ChangeMonitor.h.

124 { return fRefObj.has_value(); }
std::optional< Data_t > fRefObj
The last object seen.
template<typename T, typename Comp = std::equal_to<T>>
std::optional<Data_t> icarus::ns::util::ChangeMonitor< T, Comp >::operator() ( Data_t const &  currentObj)
inline

As update().

Definition at line 120 of file ChangeMonitor.h.

121  { return update(currentObj); }
std::optional< Data_t > update(Data_t const &currentObj)
Returns the old object if different from newObj.
template<typename T, typename Comp = std::equal_to<T>>
Data_t const& icarus::ns::util::ChangeMonitor< T, Comp >::reference ( ) const
inline

Returns the reference value; undefined if hasReference() is false.

Definition at line 127 of file ChangeMonitor.h.

127 { return fRefObj.value(); }
std::optional< Data_t > fRefObj
The last object seen.
template<typename T, typename Comp = std::equal_to<T>>
bool icarus::ns::util::ChangeMonitor< T, Comp >::same ( Data_t const &  A,
Data_t const &  B 
) const
inlineprivate

Returns whether A and B represent the same value.

Definition at line 137 of file ChangeMonitor.h.

137 { return fComp(A, B); }
Comparison_t fComp
Comparison used for reference testing.
float A
Definition: dedx.py:137
template<typename T, typename Comp = std::equal_to<T>>
std::optional<Data_t> icarus::ns::util::ChangeMonitor< T, Comp >::update ( Data_t const &  currentObj)
inline

Returns the old object if different from newObj.

Parameters
currentObjthe current object value
Returns
the old reference if different from currentObj, or no value

If 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.

Note
"No value returned" technically means that the std::optional object returned has no value, i.e. has_value() is false.

Definition at line 111 of file ChangeMonitor.h.

112  {
113  if (fRefObj && same(currentObj, fRefObj.value())) return {};
114  auto lastObj = std::move(fRefObj);
115  fRefObj.emplace(currentObj);
116  return lastObj;
117  }
bool same(Data_t const &A, Data_t const &B) const
Returns whether A and B represent the same value.
std::optional< Data_t > fRefObj
The last object seen.

Member Data Documentation

template<typename T, typename Comp = std::equal_to<T>>
Comparison_t icarus::ns::util::ChangeMonitor< T, Comp >::fComp
private

Comparison used for reference testing.

Definition at line 134 of file ChangeMonitor.h.

template<typename T, typename Comp = std::equal_to<T>>
std::optional<Data_t> icarus::ns::util::ChangeMonitor< T, Comp >::fRefObj
private

The last object seen.

Definition at line 132 of file ChangeMonitor.h.


The documentation for this struct was generated from the following file: