emlabcpp
modern opinionated embedded C++ library
emlabcpp::bounded< T, MinVal, MaxVal > Class Template Reference

The bounded class represents a wrapper over type T constrained between MinVal and MaxVal as compile-time constants. More...

#include <bounded.h>

Public Member Functions

template<typename U , U OtherMin, U OtherMax>
 requires (std::is_integral_v< U > &&std::is_integral_v< T > &&min_val<=OtherMin &&OtherMax<=max_val) const expr bounded(bounded< U
 Constructor that allows conversion from another bounded type with compatible bounds. More...
 
constexpr bounded ()
 
constexpr T operator* () const
 
 operator T () const
 
void rotate_right (T step)
 Rotation to the right increases the internal value by step modulo the range it is in. More...
 
void rotate_left (T step)
 Rotation to the left decreases the internal value by step modulo the range it is in. More...
 
template<T FromOther, T ToOther>
constexpr bounded< T, MinVal+FromOther, MaxVal+ToOther > operator+ (bounded< T, FromOther, ToOther > const &other) const
 Sum of two bounded types of same base type is bounded within appropiate ranges. More...
 

Static Public Member Functions

template<typename U >
static std::optional< bounded< T, min_val, max_val > > make (U val)
 Creates an optional bounded value if the input value is within the allowed range. More...
 
static bounded< T, min_val, max_valmin ()
 
static bounded< T, min_val, max_valmax ()
 

Public Attributes

 OtherMin
 
OtherMax other: val_( static_cast< T >( *other ) ) { } template < T Val > static constexpr bounded get() { static_assert( min_val <= Val && Val <= max_val
 
OtherMax Value out of bounds
 
return bounded { Val }
 

Static Public Attributes

static constexpr T min_val = MinVal
 
static constexpr T max_val = MaxVal
 
static constexpr T interval_range = static_cast< T >( 1 ) + max_val - min_val
 
static constexpr bool has_single_element = MinVal == MaxVal
 

Friends

template<typename U , U FromOther, U ToOther>
class bounded
 
constexpr friend auto operator<=> (bounded const &, bounded const &)=default
 
template<typename U >
constexpr friend auto operator<=> (bounded const &b, U const &val)
 

Detailed Description

template<typename T, T MinVal, T MaxVal>
class emlabcpp::bounded< T, MinVal, MaxVal >

The bounded class represents a wrapper over type T constrained between MinVal and MaxVal as compile-time constants.

This ensures that values of this type are always within the specified range, providing compile-time guarantees and eliminating the need for runtime checks within APIs.

This design shifts the responsibility of bounds checking from the API to the user, ensuring that only valid values are passed to the API. While bounds checks are still necessary, they are moved to the point of creation of the bounded type.

Constructor & Destructor Documentation

◆ bounded()

template<typename T , T MinVal, T MaxVal>
constexpr emlabcpp::bounded< T, MinVal, MaxVal >::bounded ( )
inlineconstexpr

Member Function Documentation

◆ make()

template<typename T , T MinVal, T MaxVal>
template<typename U >
static std::optional< bounded< T, min_val, max_val > > emlabcpp::bounded< T, MinVal, MaxVal >::make ( val)
inlinestatic

Creates an optional bounded value if the input value is within the allowed range.

Returns std::nullopt if the value is out of bounds.

References emlabcpp::bounded< T, MinVal, MaxVal >::max_val, and emlabcpp::bounded< T, MinVal, MaxVal >::min_val.

◆ max()

template<typename T , T MinVal, T MaxVal>
static bounded< T, min_val, max_val > emlabcpp::bounded< T, MinVal, MaxVal >::max ( )
inlinestatic

◆ min()

template<typename T , T MinVal, T MaxVal>
static bounded< T, min_val, max_val > emlabcpp::bounded< T, MinVal, MaxVal >::min ( )
inlinestatic

◆ operator T()

template<typename T , T MinVal, T MaxVal>
emlabcpp::bounded< T, MinVal, MaxVal >::operator T ( ) const
inlineexplicit

◆ operator*()

template<typename T , T MinVal, T MaxVal>
constexpr T emlabcpp::bounded< T, MinVal, MaxVal >::operator* ( ) const
inlineconstexpr

◆ operator+()

template<typename T , T MinVal, T MaxVal>
template<T FromOther, T ToOther>
constexpr bounded< T, MinVal + FromOther, MaxVal + ToOther > emlabcpp::bounded< T, MinVal, MaxVal >::operator+ ( bounded< T, FromOther, ToOther > const &  other) const
inlineconstexpr

Sum of two bounded types of same base type is bounded within appropiate ranges.

References emlabcpp::bounded< T, MinVal, MaxVal >::other.

◆ requires()

template<typename T , T MinVal, T MaxVal>
template<typename U , U OtherMin, U OtherMax>
emlabcpp::bounded< T, MinVal, MaxVal >::requires ( std::is_integral_v< U > &&std::is_integral_v< T > &&min_val<=OtherMin &&OtherMax<=  max_val) const

Constructor that allows conversion from another bounded type with compatible bounds.

◆ rotate_left()

template<typename T , T MinVal, T MaxVal>
void emlabcpp::bounded< T, MinVal, MaxVal >::rotate_left ( step)
inline

Rotation to the left decreases the internal value by step modulo the range it is in.

References emlabcpp::bounded< T, MinVal, MaxVal >::interval_range, and emlabcpp::bounded< T, MinVal, MaxVal >::min_val.

◆ rotate_right()

template<typename T , T MinVal, T MaxVal>
void emlabcpp::bounded< T, MinVal, MaxVal >::rotate_right ( step)
inline

Rotation to the right increases the internal value by step modulo the range it is in.

References emlabcpp::bounded< T, MinVal, MaxVal >::interval_range, and emlabcpp::bounded< T, MinVal, MaxVal >::min_val.

Friends And Related Function Documentation

◆ bounded

template<typename T , T MinVal, T MaxVal>
template<typename U , U FromOther, U ToOther>
friend class bounded
friend

◆ operator<=> [1/2]

template<typename T , T MinVal, T MaxVal>
constexpr friend auto operator<=> ( bounded< T, MinVal, MaxVal > const &  ,
bounded< T, MinVal, MaxVal > const &   
)
friend

◆ operator<=> [2/2]

template<typename T , T MinVal, T MaxVal>
template<typename U >
constexpr friend auto operator<=> ( bounded< T, MinVal, MaxVal > const &  b,
U const &  val 
)
friend

Member Data Documentation

◆ bounded

template<typename T , T MinVal, T MaxVal>
return emlabcpp::bounded< T, MinVal, MaxVal >::bounded { Val }

◆ bounds

template<typename T , T MinVal, T MaxVal>
OtherMax Value out of emlabcpp::bounded< T, MinVal, MaxVal >::bounds

◆ has_single_element

template<typename T , T MinVal, T MaxVal>
constexpr bool emlabcpp::bounded< T, MinVal, MaxVal >::has_single_element = MinVal == MaxVal
staticconstexpr

◆ interval_range

template<typename T , T MinVal, T MaxVal>
constexpr T emlabcpp::bounded< T, MinVal, MaxVal >::interval_range = static_cast< T >( 1 ) + max_val - min_val
staticconstexpr

◆ max_val

template<typename T , T MinVal, T MaxVal>
constexpr T emlabcpp::bounded< T, MinVal, MaxVal >::max_val = MaxVal
staticconstexpr

◆ min_val

template<typename T , T MinVal, T MaxVal>
constexpr T emlabcpp::bounded< T, MinVal, MaxVal >::min_val = MinVal
staticconstexpr

◆ other

template<typename T , T MinVal, T MaxVal>
OtherMax emlabcpp::bounded< T, MinVal, MaxVal >::other

◆ OtherMin

template<typename T , T MinVal, T MaxVal>
emlabcpp::bounded< T, MinVal, MaxVal >::OtherMin

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