emlabcpp
modern opinionated embedded C++ library
|
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_val > | min () |
static bounded< T, min_val, max_val > | max () |
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) |
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.
|
inlineconstexpr |
|
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.
|
inlinestatic |
References emlabcpp::bounded< T, MinVal, MaxVal >::max_val.
|
inlinestatic |
References emlabcpp::bounded< T, MinVal, MaxVal >::min_val.
|
inlineexplicit |
|
inlineconstexpr |
|
inlineconstexpr |
Sum of two bounded types of same base type is bounded within appropiate ranges.
References emlabcpp::bounded< T, MinVal, MaxVal >::other.
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.
|
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.
|
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.
|
friend |
|
friend |
|
friend |
return emlabcpp::bounded< T, MinVal, MaxVal >::bounded { Val } |
OtherMax Value out of emlabcpp::bounded< T, MinVal, MaxVal >::bounds |
|
staticconstexpr |
|
staticconstexpr |
|
staticconstexpr |
|
staticconstexpr |
OtherMax emlabcpp::bounded< T, MinVal, MaxVal >::other |
emlabcpp::bounded< T, MinVal, MaxVal >::OtherMin |