A C++ template library for embedded applications
Designed and maintained by
Aster Consulting Ltd

cyclic_value

Provides a value that cycles between two limits.
Supports increments, decrements and arbitrary advance.

template <typename T, const T FIRST = T(), const T LAST = T()>
class cyclic_value

Constructor

cyclic_value<int> value;
Creates a cyclic value of type int with default constructed initial defined limits.

cyclic_value<int, N, M> value;
Creates a cyclic value of type int with initial defined limits of N and M.

cyclic_value<int> value(N, M);
Creates a cyclic value of type int with initial defined limits of N and M.

Modifiers

cyclic_value& operator ++();
cyclic_value& operator ++(int);
Increments the value. If the value is at the last value then is set to the first.

cyclic_value& operator --();
cyclic_value& operator --(int);
Decrements the value. If the value is at the first value then is set to the last.

void advance(int n);
Advances the value by the specified amount, wrapping if necessary.

Operations

void set(const T& first, const T& last);
Sets the new first and last values. Sets the current value to first.

void to_first();
Sets the current value to the first value

void to_last();
Sets the current value to the last value

template <const T OTHER_FIRST, const T OTHER_LAST>
void swap(cyclic_value<T, OTHER_FIRST, OTHER_LAST>& other);
Swaps with another cyclic value.

Operators

operator T();
operator const T() const;
Conversion operators to T.

cyclic_value& operator =(T t);
Sets the current value to t.

Non-member functions

template <typename T,
          const T LHS_FIRST, const T LHS_LAST,
          const T RHS_FIRST, const T RHS_LAST>
void swap(cyclic_value<T, LHS_FIRST, LHS_LAST>& lhs,
          cyclic_value<T, RHS_FIRST, RHS_LAST>& rhs)
Swaps two cyclic values.


template <typename T,
          const T LHS_FIRST, const T LHS_LAST,
          const T RHS_FIRST, const T RHS_LAST>
bool operator == (const cyclic_value<T, LHS_FIRST, LHS_LAST>& lhs,
                  const cyclic_value<T, RHS_FIRST, RHS_LAST>& rhs)
Checks equality of two cyclic values.


template <typename T,
          const T LHS_FIRST, const T LHS_LAST,
          const T RHS_FIRST, const T RHS_LAST>
bool operator != (const cyclic_value<T, LHS_FIRST, RHS_LAST>& lhs,
                  const cyclic_value<T, RHS_FIRST, RHS_LAST>& rhs)
Checks inequality of two cyclic values.
cyclic_value.h