RAVENNAKIT
Loading...
Searching...
No Matches
rav::SafeInt< T > Class Template Reference

A safe integer type that checks for overflow and underflow when performing arithmetic operations. More...

#include <safe_int.hpp>

Public Member Functions

 SafeInt ()=default
 SafeInt (T value)
 Constructs a safe_int from a value.
T value () const
SafeIntError error () const
tl::expected< T, SafeIntError > expected () const
template<class U>
SafeInt & operator+= (U b)
 Adds a value to the safe_int, returning an error if the operation would overflow or underflow.
template<class U>
SafeInt operator+ (U b) const
 Adds a value to the safe_int, returning a new safe_int with the result.
template<class U>
SafeInt & operator-= (U b)
 Subtracts a value from the safe_int, returning an error if the operation would overflow or underflow.
template<class U>
SafeInt operator- (U b) const
 Subtracts a value from the safe_int, returning a new safe_int with the result.
template<class U>
SafeInt & operator*= (U b)
 Multiplies the safe_int by a scalar, returning an error if the operation would overflow or underflow.
template<class U>
SafeInt operator* (U b) const
 Multiplies the safe_int by a scalar, returning a new safe_int with the result.
template<class U>
SafeInt & operator/= (U b)
 Divides the safe_int by a scalar, returning an error if the operation would overflow or underflow.
template<class U>
SafeInt operator/ (U b) const
 Divides the safe_int by a scalar, returning a new safe_int with the result.

Static Public Member Functions

template<class U>
static tl::expected< T, SafeIntError > add (T a, U b)
 Adds two values, returning an error if the operation would overflow or underflow.
template<class U>
static tl::expected< T, SafeIntError > sub (T a, U b)
 Subtracts two values, returning an error if the operation would overflow or underflow.
template<class U>
static tl::expected< T, SafeIntError > mul (T a, U b)
 Multiplies two values, returning an error if the operation would overflow or underflow.
template<class U>
static tl::expected< T, SafeIntError > div (T a, U b)
 Divides two values, returning an error if the operation would overflow or underflow.

Detailed Description

template<class T>
class rav::SafeInt< T >

A safe integer type that checks for overflow and underflow when performing arithmetic operations.

Template Parameters
TThe type of the integer.

Definition at line 40 of file safe_int.hpp.

Constructor & Destructor Documentation

◆ SafeInt() [1/2]

template<class T>
rav::SafeInt< T >::SafeInt ( )
default

◆ SafeInt() [2/2]

template<class T>
rav::SafeInt< T >::SafeInt ( T value)
inlineexplicit

Constructs a safe_int from a value.

Parameters
valueThe value to store.

Definition at line 50 of file safe_int.hpp.

Member Function Documentation

◆ add()

template<class T>
template<class U>
tl::expected< T, SafeIntError > rav::SafeInt< T >::add ( T a,
U b )
inlinestatic

Adds two values, returning an error if the operation would overflow or underflow.

Template Parameters
UThe type of the values to add.
Parameters
aThe first value.
bThe second value.
Returns
The sum of the two values, or an error if the operation would overflow or underflow.

Definition at line 195 of file safe_int.hpp.

◆ div()

template<class T>
template<class U>
tl::expected< T, SafeIntError > rav::SafeInt< T >::div ( T a,
U b )
inlinestatic

Divides two values, returning an error if the operation would overflow or underflow.

Template Parameters
UThe type of the values to divide.
Parameters
aThe first value.
bThe second value.
Returns
The quotient of the two values, or an error if the operation would overflow, underflow, or divide by zero.

Definition at line 272 of file safe_int.hpp.

◆ error()

template<class T>
SafeIntError rav::SafeInt< T >::error ( ) const
inlinenodiscard
Returns
The error state of the safe_int.
Exceptions
Whenthe value is not in an error state.

Definition at line 64 of file safe_int.hpp.

◆ expected()

template<class T>
tl::expected< T, SafeIntError > rav::SafeInt< T >::expected ( ) const
inline
Returns
The value stored in the safe_int.

Definition at line 71 of file safe_int.hpp.

◆ mul()

template<class T>
template<class U>
tl::expected< T, SafeIntError > rav::SafeInt< T >::mul ( T a,
U b )
inlinestatic

Multiplies two values, returning an error if the operation would overflow or underflow.

Template Parameters
UThe type of the values to multiply.
Parameters
aThe first value.
bThe second value.
Returns
The product of the two values, or an error if the operation would overflow or underflow.

Definition at line 231 of file safe_int.hpp.

◆ operator*()

template<class T>
template<class U>
SafeInt rav::SafeInt< T >::operator* ( U b) const
inline

Multiplies the safe_int by a scalar, returning a new safe_int with the result.

Template Parameters
UThe type of the scalar.
Parameters
bThe scalar to multiply by.
Returns
A new safe_int with the result.

Definition at line 153 of file safe_int.hpp.

◆ operator*=()

template<class T>
template<class U>
SafeInt & rav::SafeInt< T >::operator*= ( U b)
inline

Multiplies the safe_int by a scalar, returning an error if the operation would overflow or underflow.

Template Parameters
UThe type of the scalar.
Parameters
bThe scalar to multiply by.
Returns
A reference to the safe_int.

Definition at line 138 of file safe_int.hpp.

◆ operator+()

template<class T>
template<class U>
SafeInt rav::SafeInt< T >::operator+ ( U b) const
inline

Adds a value to the safe_int, returning a new safe_int with the result.

Template Parameters
UThe type of the value to add.
Parameters
bThe value to add.
Returns
A new safe_int with the result.

Definition at line 97 of file safe_int.hpp.

◆ operator+=()

template<class T>
template<class U>
SafeInt & rav::SafeInt< T >::operator+= ( U b)
inline

Adds a value to the safe_int, returning an error if the operation would overflow or underflow.

Template Parameters
UThe type of the value to add.
Parameters
bThe value to add.
Returns
A reference to the safe_int.

Definition at line 82 of file safe_int.hpp.

◆ operator-()

template<class T>
template<class U>
SafeInt rav::SafeInt< T >::operator- ( U b) const
inline

Subtracts a value from the safe_int, returning a new safe_int with the result.

Template Parameters
UThe type of the value to subtract.
Parameters
bThe value to subtract.
Returns
A new safe_int with the result.

Definition at line 125 of file safe_int.hpp.

◆ operator-=()

template<class T>
template<class U>
SafeInt & rav::SafeInt< T >::operator-= ( U b)
inline

Subtracts a value from the safe_int, returning an error if the operation would overflow or underflow.

Template Parameters
UThe type of the value to subtract.
Parameters
bThe value to subtract.
Returns
A reference to the safe_int.

Definition at line 110 of file safe_int.hpp.

◆ operator/()

template<class T>
template<class U>
SafeInt rav::SafeInt< T >::operator/ ( U b) const
inline

Divides the safe_int by a scalar, returning a new safe_int with the result.

Template Parameters
UThe type of the scalar.
Parameters
bThe scalar to divide by.
Returns
A new safe_int with the result.

Definition at line 181 of file safe_int.hpp.

◆ operator/=()

template<class T>
template<class U>
SafeInt & rav::SafeInt< T >::operator/= ( U b)
inline

Divides the safe_int by a scalar, returning an error if the operation would overflow or underflow.

Template Parameters
UThe type of the scalar.
Parameters
bThe scalar to divide by.
Returns
A reference to the safe_int.

Definition at line 166 of file safe_int.hpp.

◆ sub()

template<class T>
template<class U>
tl::expected< T, SafeIntError > rav::SafeInt< T >::sub ( T a,
U b )
inlinestatic

Subtracts two values, returning an error if the operation would overflow or underflow.

Template Parameters
UThe type of the values to subtract.
Parameters
aThe first value.
bThe second value.
Returns
The difference of the two values, or an error if the operation would overflow or underflow.

Definition at line 213 of file safe_int.hpp.

◆ value()

template<class T>
T rav::SafeInt< T >::value ( ) const
inline
Returns
The value stored in the safe_int.
Exceptions
Whenthe value is in an error state.

Definition at line 56 of file safe_int.hpp.


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