OpenTTD Source 20261129-master-g72a8bdc861
OverflowSafeInt< T > Class Template Reference

Overflow safe template for integers, i.e. More...

#include <overflowsafe_type.hpp>

Public Types

using BaseType = T

Public Member Functions

constexpr OverflowSafeInt ()=default
 Construct an overflow-safe type instance with a default value.
constexpr OverflowSafeInt (std::integral auto value)
 Construct an overflow-safe type from an integral type.
template<class Tother>
constexpr OverflowSafeInt (const OverflowSafeInt< Tother > &value)
 Safely construct an overflow-safe type from an overflow-safe type with a different base type.
constexpr OverflowSafeInt & operator= (std::integral auto other)
 Safely assign an integral value to this overflow-safe instance.
constexpr OverflowSafeInt operator- () const
constexpr OverflowSafeInt & operator+= (const OverflowSafeInt &other)
 Safe implementation of addition.
constexpr OverflowSafeInt & operator-= (const OverflowSafeInt &other)
 Safe implementation of subtraction.
constexpr OverflowSafeInt & operator++ ()
constexpr OverflowSafeInt operator++ (int)
constexpr OverflowSafeInt & operator-- ()
constexpr OverflowSafeInt operator-- (int)
constexpr OverflowSafeInt & operator*= (const OverflowSafeInt &factor)
 Safe implementation of multiplication.
constexpr OverflowSafeInt & operator/= (const OverflowSafeInt &divisor)
 Divide operator.
constexpr OverflowSafeInt & operator%= (const OverflowSafeInt &divisor)
 Modulo operator.
constexpr OverflowSafeInt & operator<<= (const int shift)
constexpr OverflowSafeInt operator<< (const int shift) const
constexpr OverflowSafeInt & operator>>= (const int shift)
constexpr OverflowSafeInt operator>> (const int shift) const
constexpr operator T () const
constexpr BaseType base () const noexcept
 Get the underlying value of this instance.
constexpr OverflowSafeInt operator+ (const OverflowSafeInt &other) const
constexpr OverflowSafeInt operator+ (std::integral auto other) const
constexpr OverflowSafeInt operator- (const OverflowSafeInt &other) const
constexpr OverflowSafeInt operator- (std::integral auto other) const
constexpr OverflowSafeInt operator* (const OverflowSafeInt &factor) const
constexpr OverflowSafeInt operator* (std::integral auto factor) const
constexpr OverflowSafeInt operator/ (const OverflowSafeInt &divisor) const
constexpr OverflowSafeInt operator/ (std::integral auto divisor) const
constexpr OverflowSafeInt operator% (const OverflowSafeInt &divisor) const
constexpr OverflowSafeInt operator% (std::integral auto divisor) const
constexpr bool operator== (const OverflowSafeInt &other) const
constexpr bool operator== (std::integral auto other) const
constexpr auto operator<=> (const OverflowSafeInt &other) const
constexpr auto operator<=> (std::integral auto other) const

Static Public Member Functions

static constexpr OverflowSafeInt< T > max ()
static constexpr OverflowSafeInt< T > min ()

Static Private Member Functions

static constexpr T ConvertIfSafe (std::integral auto value)
 Convert from any integral type to the base type.

Private Attributes

T m_value {}
 The non-overflow safe backend to store the value in.

Static Private Attributes

static constexpr T T_MAX = std::numeric_limits<T>::max()
 Maximum value of this type.
static constexpr T T_MIN = std::numeric_limits<T>::min()
 Minimum value of this type.

Friends

constexpr OverflowSafeInt operator+ (std::integral auto lhs, const OverflowSafeInt &rhs) noexcept
constexpr OverflowSafeInt operator- (std::integral auto lhs, const OverflowSafeInt &rhs) noexcept
constexpr OverflowSafeInt operator* (std::integral auto lhs, const OverflowSafeInt &rhs) noexcept
constexpr OverflowSafeInt operator/ (std::integral auto lhs, const OverflowSafeInt &rhs) noexcept
constexpr OverflowSafeInt operator% (std::integral auto lhs, const OverflowSafeInt &rhs) noexcept

Detailed Description

template<class T>
class OverflowSafeInt< T >

Overflow safe template for integers, i.e.

integers that will never overflow when you multiply the maximum value with 2, or add 2, or subtract something from the minimum value, etc.

Parameters
Tthe type these integers are stored with.

Definition at line 29 of file overflowsafe_type.hpp.

Member Typedef Documentation

◆ BaseType

template<class T>
using OverflowSafeInt< T >::BaseType = T

Definition at line 50 of file overflowsafe_type.hpp.

Constructor & Destructor Documentation

◆ OverflowSafeInt() [1/2]

template<class T>
OverflowSafeInt< T >::OverflowSafeInt ( std::integral auto value)
inlineconstexpr

Construct an overflow-safe type from an integral type.

Parameters
valueThe value.

Definition at line 61 of file overflowsafe_type.hpp.

◆ OverflowSafeInt() [2/2]

template<class T>
template<class Tother>
OverflowSafeInt< T >::OverflowSafeInt ( const OverflowSafeInt< Tother > & value)
inlineconstexpr

Safely construct an overflow-safe type from an overflow-safe type with a different base type.

Note
if the type cannot be converted safetly then a static_assert will be triggered.
Template Parameters
TotherThe base type of the original type.
Parameters
valueThe original overflow-safe type.

Definition at line 70 of file overflowsafe_type.hpp.

Member Function Documentation

◆ base()

template<class T>
BaseType OverflowSafeInt< T >::base ( ) const
inlineconstexprnoexcept

Get the underlying value of this instance.

Returns
The underlying value.

Definition at line 258 of file overflowsafe_type.hpp.

◆ ConvertIfSafe()

template<class T>
constexpr T OverflowSafeInt< T >::ConvertIfSafe ( std::integral auto value)
inlinestaticconstexprprivate

Convert from any integral type to the base type.

Note
if the type cannot be converted safely then a static_assert will be triggered.
Parameters
valueThe value to be converted.
Returns
The value converted to the base type.

Definition at line 40 of file overflowsafe_type.hpp.

Referenced by OverflowSafeInt< int64_t >::operator<=>(), OverflowSafeInt< int64_t >::operator=(), and OverflowSafeInt< int64_t >::operator==().

◆ max()

template<class T>
constexpr OverflowSafeInt< T > OverflowSafeInt< T >::max ( )
inlinestaticconstexpr

Definition at line 251 of file overflowsafe_type.hpp.

◆ min()

template<class T>
constexpr OverflowSafeInt< T > OverflowSafeInt< T >::min ( )
inlinestaticconstexpr

Definition at line 252 of file overflowsafe_type.hpp.

◆ operator T()

template<class T>
OverflowSafeInt< T >::operator T ( ) const
inlineconstexpr

Definition at line 249 of file overflowsafe_type.hpp.

◆ operator%() [1/2]

template<class T>
OverflowSafeInt OverflowSafeInt< T >::operator% ( const OverflowSafeInt< T > & divisor) const
inlineconstexpr

Modulo operator.

Parameters
divisorThe divisor to modulo with.
Returns
The result of the modulo.

Definition at line 221 of file overflowsafe_type.hpp.

◆ operator%() [2/2]

template<class T>
OverflowSafeInt OverflowSafeInt< T >::operator% ( std::integral auto divisor) const
inlineconstexpr

Modulo operator.

Parameters
divisorThe divisor to modulo with.
Returns
The result of the modulo.

Definition at line 222 of file overflowsafe_type.hpp.

◆ operator%=()

template<class T>
OverflowSafeInt & OverflowSafeInt< T >::operator%= ( const OverflowSafeInt< T > & divisor)
inlineconstexpr

Modulo operator.

Parameters
divisorThe divisor to modulo with.
Returns
This instance.

Definition at line 214 of file overflowsafe_type.hpp.

◆ operator*() [1/2]

template<class T>
OverflowSafeInt OverflowSafeInt< T >::operator* ( const OverflowSafeInt< T > & factor) const
inlineconstexpr

Safely multiply operator.

Parameters
factorThe factor to mulitply with.
Returns
The result of the multiplication.

Definition at line 189 of file overflowsafe_type.hpp.

◆ operator*() [2/2]

template<class T>
OverflowSafeInt OverflowSafeInt< T >::operator* ( std::integral auto factor) const
inlineconstexpr

Safely multiply operator.

Parameters
factorThe factor to mulitply with.
Returns
The result of the multiplication.

Definition at line 190 of file overflowsafe_type.hpp.

◆ operator*=()

template<class T>
OverflowSafeInt & OverflowSafeInt< T >::operator*= ( const OverflowSafeInt< T > & factor)
inlineconstexpr

Safe implementation of multiplication.

Parameters
factorthe factor to multiply this with.
Returns
Reference to this instance.
Note
when the multiplication would yield more than T_MAX (or less than T_MIN), it will be T_MAX (respectively T_MIN).

Definition at line 159 of file overflowsafe_type.hpp.

◆ operator+() [1/2]

template<class T>
OverflowSafeInt OverflowSafeInt< T >::operator+ ( const OverflowSafeInt< T > & other) const
inlineconstexpr

Safe add operator.

Parameters
otherThe other value to add.
Returns
The result of the addition.

Definition at line 134 of file overflowsafe_type.hpp.

◆ operator+() [2/2]

template<class T>
OverflowSafeInt OverflowSafeInt< T >::operator+ ( std::integral auto other) const
inlineconstexpr

Safe add operator.

Parameters
otherThe other value to add.
Returns
The result of the addition.

Definition at line 135 of file overflowsafe_type.hpp.

◆ operator++() [1/2]

template<class T>
OverflowSafeInt & OverflowSafeInt< T >::operator++ ( )
inlineconstexpr

Definition at line 147 of file overflowsafe_type.hpp.

◆ operator++() [2/2]

template<class T>
OverflowSafeInt OverflowSafeInt< T >::operator++ ( int )
inlineconstexpr

Definition at line 148 of file overflowsafe_type.hpp.

◆ operator+=()

template<class T>
OverflowSafeInt & OverflowSafeInt< T >::operator+= ( const OverflowSafeInt< T > & other)
inlineconstexpr

Safe implementation of addition.

Parameters
otherthe amount to add
Returns
Reference to this instance.
Note
when the addition would yield more than T_MAX, it will be T_MAX.

Definition at line 87 of file overflowsafe_type.hpp.

◆ operator-() [1/3]

template<class T>
OverflowSafeInt OverflowSafeInt< T >::operator- ( ) const
inlineconstexpr

Definition at line 79 of file overflowsafe_type.hpp.

◆ operator-() [2/3]

template<class T>
OverflowSafeInt OverflowSafeInt< T >::operator- ( const OverflowSafeInt< T > & other) const
inlineconstexpr

Safe subtract operator.

Parameters
otherThe other value to subtract.
Returns
The result of the subtraction.

Definition at line 143 of file overflowsafe_type.hpp.

◆ operator-() [3/3]

template<class T>
OverflowSafeInt OverflowSafeInt< T >::operator- ( std::integral auto other) const
inlineconstexpr

Safe subtract operator.

Parameters
otherThe other value to subtract.
Returns
The result of the subtraction.

Definition at line 144 of file overflowsafe_type.hpp.

◆ operator--() [1/2]

template<class T>
OverflowSafeInt & OverflowSafeInt< T >::operator-- ( )
inlineconstexpr

Definition at line 149 of file overflowsafe_type.hpp.

◆ operator--() [2/2]

template<class T>
OverflowSafeInt OverflowSafeInt< T >::operator-- ( int )
inlineconstexpr

Definition at line 150 of file overflowsafe_type.hpp.

◆ operator-=()

template<class T>
OverflowSafeInt & OverflowSafeInt< T >::operator-= ( const OverflowSafeInt< T > & other)
inlineconstexpr

Safe implementation of subtraction.

Parameters
otherthe amount to subtract
Returns
Reference to this instance.
Note
when the subtraction would yield less than T_MIN, it will be T_MIN.

Definition at line 111 of file overflowsafe_type.hpp.

◆ operator/() [1/2]

template<class T>
OverflowSafeInt OverflowSafeInt< T >::operator/ ( const OverflowSafeInt< T > & divisor) const
inlineconstexpr

Divide operator.

Parameters
divisorThe divisor to divide with.
Returns
The result of the division.

Definition at line 205 of file overflowsafe_type.hpp.

◆ operator/() [2/2]

template<class T>
OverflowSafeInt OverflowSafeInt< T >::operator/ ( std::integral auto divisor) const
inlineconstexpr

Divide operator.

Parameters
divisorThe divisor to divide with.
Returns
The result of the division.

Definition at line 206 of file overflowsafe_type.hpp.

◆ operator/=()

template<class T>
OverflowSafeInt & OverflowSafeInt< T >::operator/= ( const OverflowSafeInt< T > & divisor)
inlineconstexpr

Divide operator.

Parameters
divisorThe divisor to divide with.
Returns
This instance.

Definition at line 198 of file overflowsafe_type.hpp.

◆ operator<<()

template<class T>
OverflowSafeInt OverflowSafeInt< T >::operator<< ( const int shift) const
inlineconstexpr

Definition at line 227 of file overflowsafe_type.hpp.

◆ operator<<=()

template<class T>
OverflowSafeInt & OverflowSafeInt< T >::operator<<= ( const int shift)
inlineconstexpr

Definition at line 226 of file overflowsafe_type.hpp.

◆ operator<=>() [1/2]

template<class T>
auto OverflowSafeInt< T >::operator<=> ( const OverflowSafeInt< T > & other) const
inlineconstexpr

Comparison operator.

Parameters
otherThe other value to compare with.
Returns
The comparison result.

Definition at line 245 of file overflowsafe_type.hpp.

◆ operator<=>() [2/2]

template<class T>
auto OverflowSafeInt< T >::operator<=> ( std::integral auto other) const
inlineconstexpr

Comparison operator.

Parameters
otherThe other value to compare with.
Returns
The comparison result.

Definition at line 246 of file overflowsafe_type.hpp.

◆ operator=()

template<class T>
OverflowSafeInt & OverflowSafeInt< T >::operator= ( std::integral auto other)
inlineconstexpr

Safely assign an integral value to this overflow-safe instance.

Parameters
otherThe value to assign.
Returns
This instance.

Definition at line 77 of file overflowsafe_type.hpp.

◆ operator==() [1/2]

template<class T>
bool OverflowSafeInt< T >::operator== ( const OverflowSafeInt< T > & other) const
inlineconstexpr

Equality comparison operator.

Parameters
otherThe other value to compare with.
Returns
true if the other value is the same as this value..

Definition at line 236 of file overflowsafe_type.hpp.

◆ operator==() [2/2]

template<class T>
bool OverflowSafeInt< T >::operator== ( std::integral auto other) const
inlineconstexpr

Equality comparison operator.

Parameters
otherThe other value to compare with.
Returns
true if the other value is the same as this value..

Definition at line 237 of file overflowsafe_type.hpp.

◆ operator>>()

template<class T>
OverflowSafeInt OverflowSafeInt< T >::operator>> ( const int shift) const
inlineconstexpr

Definition at line 229 of file overflowsafe_type.hpp.

◆ operator>>=()

template<class T>
OverflowSafeInt & OverflowSafeInt< T >::operator>>= ( const int shift)
inlineconstexpr

Definition at line 228 of file overflowsafe_type.hpp.

◆ operator%

template<class T>
OverflowSafeInt operator% ( std::integral auto lhs,
const OverflowSafeInt< T > & rhs )
friend

Operators for when an OverflowSafeInt is on the right-hand-side of an operation with an integral value.

Parameters
lhsthe integral value.
rhsthe overflow-safe value.
Returns
The result of the operation.

Definition at line 270 of file overflowsafe_type.hpp.

◆ operator*

template<class T>
OverflowSafeInt operator* ( std::integral auto lhs,
const OverflowSafeInt< T > & rhs )
friend

Operators for when an OverflowSafeInt is on the right-hand-side of an operation with an integral value.

Parameters
lhsthe integral value.
rhsthe overflow-safe value.
Returns
The result of the operation.

Definition at line 268 of file overflowsafe_type.hpp.

◆ operator+

template<class T>
OverflowSafeInt operator+ ( std::integral auto lhs,
const OverflowSafeInt< T > & rhs )
friend

Operators for when an OverflowSafeInt is on the right-hand-side of an operation with an integral value.

Parameters
lhsthe integral value.
rhsthe overflow-safe value.
Returns
The result of the operation.

Definition at line 266 of file overflowsafe_type.hpp.

◆ operator-

template<class T>
OverflowSafeInt operator- ( std::integral auto lhs,
const OverflowSafeInt< T > & rhs )
friend

Operators for when an OverflowSafeInt is on the right-hand-side of an operation with an integral value.

Parameters
lhsthe integral value.
rhsthe overflow-safe value.
Returns
The result of the operation.

Definition at line 267 of file overflowsafe_type.hpp.

◆ operator/

template<class T>
OverflowSafeInt operator/ ( std::integral auto lhs,
const OverflowSafeInt< T > & rhs )
friend

Operators for when an OverflowSafeInt is on the right-hand-side of an operation with an integral value.

Parameters
lhsthe integral value.
rhsthe overflow-safe value.
Returns
The result of the operation.

Definition at line 269 of file overflowsafe_type.hpp.

Field Documentation

◆ m_value

◆ T_MAX

template<class T>
T OverflowSafeInt< T >::T_MAX = std::numeric_limits<T>::max()
staticconstexprprivate

Maximum value of this type.

Definition at line 31 of file overflowsafe_type.hpp.

◆ T_MIN

template<class T>
T OverflowSafeInt< T >::T_MIN = std::numeric_limits<T>::min()
staticconstexprprivate

Minimum value of this type.

Definition at line 32 of file overflowsafe_type.hpp.


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