fiber
Loading...
Searching...
No Matches
fiber::Tick< UInt, MAX_TICK > Class Template Reference

An overflow aware integer type for hardware timer/counter that are used as clocks. More...

#include <Tick.hpp>

Public Types

using rep = UInt
 

Public Member Functions

constexpr Tick ()=default
 
constexpr Tick (const Tick &)=default
 
constexpr Tickoperator= (const Tick &)=default
 
template<class T>
requires (!is_clocktick<T> && std::is_unsigned_v<T>)
constexpr Tick (const T v)
 Safely constructs a Tick from the input value, so that the value wraps around after the MAX_TICK.
 
template<class T>
requires (!is_clocktick<T> && std::is_signed_v<T>)
constexpr Tick (const T v)
 
template<std::integral T>
constexpr Tickoperator= (const T v)
 
void operator+= (const Tick other)
 
void operator-= (const Tick other)
 
void operator*= (const Tick other)
 
void operator/= (const Tick other)
 
void operator%= (const Tick other)
 
template<std::integral Int>
constexpr operator Int () const
 

Static Public Member Functions

static constexpr Tick< UInt, MAX_TICK > reinterpret (UInt v)
 Reinterprets the integer value into the clock tick. this assumes that the passed value is smaller or equal than the overflow value.
 

Public Attributes

UInt value = UInt(0)
 

Static Public Attributes

static constexpr UInt max_tick = MAX_TICK
 
static constexpr UInt modulo = static_cast<UInt>(max_tick + static_cast<UInt>(1))
 
static constexpr bool modulo_is_power_of_two = (max_tick & modulo) == static_cast<UInt>(0)
 
static constexpr bool max_tick_is_limit = std::numeric_limits<UInt>::max() == max_tick
 

Friends

constexpr Tick operator+ (const Tick lhs, const Tick rhs)
 addition that wrapps around MAX_TICKS
 
constexpr Tick operator- (Tick lhs, Tick rhs)
 subtraction that wrapps around MAX_TICKS
 
constexpr Tick operator+ (const Tick v)
 unary + operator is just the identity function
 
constexpr Tick operator- (const Tick v)
 negation that wrapps around MAX_TICKS
 
constexpr Tick operator* (const Tick lhs, const Tick rhs)
 multiplication that wrapps around MAX_TICKS
 
constexpr Tick operator/ (const Tick lhs, const Tick rhs)
 division that wrapps around MAX_TICKS
 
constexpr Tick operator% (const Tick lhs, const Tick rhs)
 remainder/modulo opteration thta wraps around MAX_TICKS
 
constexpr bool operator== (const Tick lhs, const Tick rhs)
 
constexpr bool operator!= (const Tick lhs, const Tick rhs)
 
constexpr bool operator<= (const Tick lhs, const Tick rhs)
 
constexpr bool operator>= (const Tick lhs, const Tick rhs)
 
constexpr bool operator< (const Tick lhs, const Tick rhs)
 
constexpr bool operator> (const Tick lhs, const Tick rhs)
 
OStreamoperator<< (OStream &stream, const Tick tick)
 

Detailed Description

template<std::unsigned_integral UInt, UInt MAX_TICK>
class fiber::Tick< UInt, MAX_TICK >

An overflow aware integer type for hardware timer/counter that are used as clocks.

An overflow aware tick class template to simulate the behaviour of hardware timers in microcontrollers for use in Duration and TimePoint types.

  • Allows <, <=, >= and > comparisons at and around the overflow point.
  • Allows +, -, *, / and % operations at and around the overflow point.

Note: The maximum comparison range is half the MAX_TICK range

Template Parameters
UIntAn unsigned integer type
MAX_TICKThe maximum tick just before the overflow
UIntThe underlieing integer type
MAX_TICKThe largest/last value before the overflow.

Member Typedef Documentation

◆ rep

template<std::unsigned_integral UInt, UInt MAX_TICK>
using fiber::Tick< UInt, MAX_TICK >::rep = UInt

Constructor & Destructor Documentation

◆ Tick() [1/4]

template<std::unsigned_integral UInt, UInt MAX_TICK>
fiber::Tick< UInt, MAX_TICK >::Tick ( )
constexprdefault

◆ Tick() [2/4]

template<std::unsigned_integral UInt, UInt MAX_TICK>
fiber::Tick< UInt, MAX_TICK >::Tick ( const Tick< UInt, MAX_TICK > & )
constexprdefault

◆ Tick() [3/4]

template<std::unsigned_integral UInt, UInt MAX_TICK>
template<class T>
requires (!is_clocktick<T> && std::is_unsigned_v<T>)
fiber::Tick< UInt, MAX_TICK >::Tick ( const T v)
inlineconstexpr

Safely constructs a Tick from the input value, so that the value wraps around after the MAX_TICK.

Compile time optimisations:

  • If the passed type T is fully representable within MAX_TICK: compile to a simple assignment.
  • Fallback 1: MAX_TICK + 1 is a power of two: compiles to an bitwise-and in addition to the assignment.
  • Fallback 2: T is fully representable within 2 * MAX_TICK: compiles to a conditional subtraction.
  • Fallback 3: applies the modulo operator
Template Parameters
TGeneric unsigned integer type
Parameters
vunsigned integer value

◆ Tick() [4/4]

template<std::unsigned_integral UInt, UInt MAX_TICK>
template<class T>
requires (!is_clocktick<T> && std::is_signed_v<T>)
fiber::Tick< UInt, MAX_TICK >::Tick ( const T v)
inlineconstexpr

Member Function Documentation

◆ operator Int()

template<std::unsigned_integral UInt, UInt MAX_TICK>
template<std::integral Int>
fiber::Tick< UInt, MAX_TICK >::operator Int ( ) const
inlineexplicitconstexpr

◆ operator%=()

template<std::unsigned_integral UInt, UInt MAX_TICK>
void fiber::Tick< UInt, MAX_TICK >::operator%= ( const Tick< UInt, MAX_TICK > other)
inline

◆ operator*=()

template<std::unsigned_integral UInt, UInt MAX_TICK>
void fiber::Tick< UInt, MAX_TICK >::operator*= ( const Tick< UInt, MAX_TICK > other)
inline

◆ operator+=()

template<std::unsigned_integral UInt, UInt MAX_TICK>
void fiber::Tick< UInt, MAX_TICK >::operator+= ( const Tick< UInt, MAX_TICK > other)
inline

◆ operator-=()

template<std::unsigned_integral UInt, UInt MAX_TICK>
void fiber::Tick< UInt, MAX_TICK >::operator-= ( const Tick< UInt, MAX_TICK > other)
inline

◆ operator/=()

template<std::unsigned_integral UInt, UInt MAX_TICK>
void fiber::Tick< UInt, MAX_TICK >::operator/= ( const Tick< UInt, MAX_TICK > other)
inline

◆ operator=() [1/2]

template<std::unsigned_integral UInt, UInt MAX_TICK>
template<std::integral T>
Tick & fiber::Tick< UInt, MAX_TICK >::operator= ( const T v)
inlineconstexpr

◆ operator=() [2/2]

template<std::unsigned_integral UInt, UInt MAX_TICK>
Tick & fiber::Tick< UInt, MAX_TICK >::operator= ( const Tick< UInt, MAX_TICK > & )
constexprdefault

◆ reinterpret()

template<std::unsigned_integral UInt, UInt MAX_TICK>
static constexpr Tick< UInt, MAX_TICK > fiber::Tick< UInt, MAX_TICK >::reinterpret ( UInt v)
inlinestaticconstexpr

Reinterprets the integer value into the clock tick. this assumes that the passed value is smaller or equal than the overflow value.

Parameters
van integer
Returns
a Tick
Exceptions
Iffull assertions are enabled, throws if the passed value is larger than the modulo

Friends And Related Symbol Documentation

◆ operator!=

template<std::unsigned_integral UInt, UInt MAX_TICK>
bool operator!= ( const Tick< UInt, MAX_TICK > lhs,
const Tick< UInt, MAX_TICK > rhs )
friend

◆ operator%

template<std::unsigned_integral UInt, UInt MAX_TICK>
Tick operator% ( const Tick< UInt, MAX_TICK > lhs,
const Tick< UInt, MAX_TICK > rhs )
friend

remainder/modulo opteration thta wraps around MAX_TICKS

◆ operator*

template<std::unsigned_integral UInt, UInt MAX_TICK>
Tick operator* ( const Tick< UInt, MAX_TICK > lhs,
const Tick< UInt, MAX_TICK > rhs )
friend

multiplication that wrapps around MAX_TICKS

Supports compile time optimisations

  • if the MAX_TICKS is the largest representable number (aka. natural overflow)
  • if MAX_TICKS + 1 is a power of two: multiplication and bitwise addition.
  • else falls back to addition with actual modulo operation - Tip: avoid or pray the compiler can optimise it away

◆ operator+ [1/2]

template<std::unsigned_integral UInt, UInt MAX_TICK>
Tick operator+ ( const Tick< UInt, MAX_TICK > lhs,
const Tick< UInt, MAX_TICK > rhs )
friend

addition that wrapps around MAX_TICKS

Supports compile time optimisations

  • if the MAX_TICKS is the largest representable number (aka. natural overflow)
  • if MAX_TICKS + 1 is a power of two
  • else falls back to addition with conditional modulo subtraction

◆ operator+ [2/2]

template<std::unsigned_integral UInt, UInt MAX_TICK>
Tick operator+ ( const Tick< UInt, MAX_TICK > v)
friend

unary + operator is just the identity function

◆ operator- [1/2]

template<std::unsigned_integral UInt, UInt MAX_TICK>
Tick operator- ( const Tick< UInt, MAX_TICK > v)
friend

negation that wrapps around MAX_TICKS

Supports compile time optimisations

  • if the MAX_TICKS is the largest representable number (aka. natural overflow)
  • else falls back to subtraction from modulo

◆ operator- [2/2]

template<std::unsigned_integral UInt, UInt MAX_TICK>
Tick operator- ( Tick< UInt, MAX_TICK > lhs,
Tick< UInt, MAX_TICK > rhs )
friend

subtraction that wrapps around MAX_TICKS

Supports compile time optimisations

  • if the MAX_TICKS is the largest representable number (aka. natural overflow)
  • else falls back to subtraction with conditional modulo addition

◆ operator/

template<std::unsigned_integral UInt, UInt MAX_TICK>
Tick operator/ ( const Tick< UInt, MAX_TICK > lhs,
const Tick< UInt, MAX_TICK > rhs )
friend

division that wrapps around MAX_TICKS

◆ operator<

template<std::unsigned_integral UInt, UInt MAX_TICK>
bool operator< ( const Tick< UInt, MAX_TICK > lhs,
const Tick< UInt, MAX_TICK > rhs )
friend

◆ operator<<

template<std::unsigned_integral UInt, UInt MAX_TICK>
OStream & operator<< ( OStream & stream,
const Tick< UInt, MAX_TICK > tick )
friend

◆ operator<=

template<std::unsigned_integral UInt, UInt MAX_TICK>
bool operator<= ( const Tick< UInt, MAX_TICK > lhs,
const Tick< UInt, MAX_TICK > rhs )
friend

◆ operator==

template<std::unsigned_integral UInt, UInt MAX_TICK>
bool operator== ( const Tick< UInt, MAX_TICK > lhs,
const Tick< UInt, MAX_TICK > rhs )
friend

◆ operator>

template<std::unsigned_integral UInt, UInt MAX_TICK>
bool operator> ( const Tick< UInt, MAX_TICK > lhs,
const Tick< UInt, MAX_TICK > rhs )
friend

◆ operator>=

template<std::unsigned_integral UInt, UInt MAX_TICK>
bool operator>= ( const Tick< UInt, MAX_TICK > lhs,
const Tick< UInt, MAX_TICK > rhs )
friend

Member Data Documentation

◆ max_tick

template<std::unsigned_integral UInt, UInt MAX_TICK>
UInt fiber::Tick< UInt, MAX_TICK >::max_tick = MAX_TICK
staticconstexpr

◆ max_tick_is_limit

template<std::unsigned_integral UInt, UInt MAX_TICK>
bool fiber::Tick< UInt, MAX_TICK >::max_tick_is_limit = std::numeric_limits<UInt>::max() == max_tick
staticconstexpr

◆ modulo

template<std::unsigned_integral UInt, UInt MAX_TICK>
UInt fiber::Tick< UInt, MAX_TICK >::modulo = static_cast<UInt>(max_tick + static_cast<UInt>(1))
staticconstexpr

◆ modulo_is_power_of_two

template<std::unsigned_integral UInt, UInt MAX_TICK>
bool fiber::Tick< UInt, MAX_TICK >::modulo_is_power_of_two = (max_tick & modulo) == static_cast<UInt>(0)
staticconstexpr

◆ value

template<std::unsigned_integral UInt, UInt MAX_TICK>
UInt fiber::Tick< UInt, MAX_TICK >::value = UInt(0)

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