Template Class StateTracker

Class Documentation

template<typename T>
class StateTracker

Requires: std::copy_constructible<T> && std::assignable_from<T&, T>

Caller-owned successive samples without synchronization.

Accessors borrow this tracker’s storage and must not race update(). update() assigns previous before current; throwing T assignment can leave partial state rather than rolling back.

Template Parameters:

T – Copy-constructible, assignable sample type; optional queries add their own constraints.

Public Functions

inline explicit StateTracker(T initial)

Initializes previous and current to the same sample.

Parameters:

initial – Value copied to previous and moved to current; type failures propagate.

inline void update(T next)

Shifts current into previous, then installs the next sample.

Parameters:

next – New current value, moved into owned storage. The object is unsynchronized. Assignment failures propagate and can leave a partial shift; no rollback is performed. Existing references still name this storage.

inline const T &current() const

Declaration: [[nodiscard]]

Borrows the current sample.

Returns:

Current sample; initially equal to previous. The reference lives with this tracker and observes later assignments; copy it to retain an independent value. It must not race update().

inline const T &previous() const

Declaration: [[nodiscard]]

Borrows the previous sample.

Returns:

Sample preceding the last successful update; initially equal to current. The reference lives with this tracker and observes later assignments; copy it to retain an independent value. It must not race update().

inline bool changed() const

Declaration: [[nodiscard]]

Requires: std::equality_comparable<T>

Compares the current and previous samples.

Returns:

True when current != previous, not a latched unread-change flag. Requires equality-comparable T. Comparison failures propagate; no sample is changed.

inline auto delta() const

Declaration: [[nodiscard]]

Requires: requires(const T& a, const T& b) { a - b; }

Computes current minus previous using T’s subtraction.

See also

State utilities

Returns:

The expression result by value; its units/ownership follow that result type. Available only when subtraction is well-formed. Overflow and exceptions follow T; this adds no saturation or validation.