OpenTTD Source 20250205-master-gfd85ab1e2c
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 (const OverflowSafeInt &other)
 
constexpr OverflowSafeInt (const T int_)
 
constexpr OverflowSafeIntoperator= (const OverflowSafeInt &other)
 
constexpr OverflowSafeIntoperator= (T other)
 
constexpr OverflowSafeInt operator- () const
 
constexpr OverflowSafeIntoperator+= (const OverflowSafeInt &other)
 Safe implementation of addition.
 
constexpr OverflowSafeIntoperator-= (const OverflowSafeInt &other)
 Safe implementation of subtraction.
 
constexpr OverflowSafeInt operator+ (const OverflowSafeInt &other) const
 
constexpr OverflowSafeInt operator+ (const int other) const
 
constexpr OverflowSafeInt operator+ (const uint other) const
 
constexpr OverflowSafeInt operator- (const OverflowSafeInt &other) const
 
constexpr OverflowSafeInt operator- (const int other) const
 
constexpr OverflowSafeInt operator- (const uint other) const
 
constexpr OverflowSafeIntoperator++ ()
 
constexpr OverflowSafeIntoperator-- ()
 
constexpr OverflowSafeInt operator++ (int)
 
constexpr OverflowSafeInt operator-- (int)
 
constexpr OverflowSafeIntoperator*= (const int factor)
 Safe implementation of multiplication.
 
constexpr OverflowSafeInt operator* (const int64_t factor) const
 
constexpr OverflowSafeInt operator* (const int factor) const
 
constexpr OverflowSafeInt operator* (const uint factor) const
 
constexpr OverflowSafeInt operator* (const uint16_t factor) const
 
constexpr OverflowSafeInt operator* (const uint8_t factor) const
 
constexpr OverflowSafeIntoperator/= (const int64_t divisor)
 
constexpr OverflowSafeInt operator/ (const OverflowSafeInt &divisor) const
 
constexpr OverflowSafeInt operator/ (const int divisor) const
 
constexpr OverflowSafeInt operator/ (const uint divisor) const
 
constexpr OverflowSafeIntoperator%= (const int divisor)
 
constexpr OverflowSafeInt operator% (const int divisor) const
 
constexpr OverflowSafeIntoperator<<= (const int shift)
 
constexpr OverflowSafeInt operator<< (const int shift) const
 
constexpr OverflowSafeIntoperator>>= (const int shift)
 
constexpr OverflowSafeInt operator>> (const int shift) const
 
constexpr bool operator== (const OverflowSafeInt &other) const
 
constexpr bool operator!= (const OverflowSafeInt &other) const
 
constexpr bool operator> (const OverflowSafeInt &other) const
 
constexpr bool operator>= (const OverflowSafeInt &other) const
 
constexpr bool operator< (const OverflowSafeInt &other) const
 
constexpr bool operator<= (const OverflowSafeInt &other) const
 
constexpr bool operator== (const int other) const
 
constexpr bool operator!= (const int other) const
 
constexpr bool operator> (const int other) const
 
constexpr bool operator>= (const int other) const
 
constexpr bool operator< (const int other) const
 
constexpr bool operator<= (const int other) const
 
constexpr operator T () const
 
BaseType base () const noexcept
 

Static Public Member Functions

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

Private Attributes

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()
 
static constexpr T T_MIN = std::numeric_limits<T>::min()
 

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 38 of file overflowsafe_type.hpp.

Constructor & Destructor Documentation

◆ OverflowSafeInt() [1/3]

template<class T >
constexpr OverflowSafeInt< T >::OverflowSafeInt ( )
inlineconstexpr

Definition at line 40 of file overflowsafe_type.hpp.

◆ OverflowSafeInt() [2/3]

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

Definition at line 42 of file overflowsafe_type.hpp.

◆ OverflowSafeInt() [3/3]

template<class T >
constexpr OverflowSafeInt< T >::OverflowSafeInt ( const T  int_)
inlineconstexpr

Definition at line 43 of file overflowsafe_type.hpp.

Member Function Documentation

◆ base()

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

Definition at line 184 of file overflowsafe_type.hpp.

◆ max()

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

Definition at line 181 of file overflowsafe_type.hpp.

◆ min()

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

Definition at line 182 of file overflowsafe_type.hpp.

◆ operator T()

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

Definition at line 179 of file overflowsafe_type.hpp.

◆ operator!=() [1/2]

template<class T >
constexpr bool OverflowSafeInt< T >::operator!= ( const int  other) const
inlineconstexpr

Definition at line 173 of file overflowsafe_type.hpp.

◆ operator!=() [2/2]

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

Definition at line 165 of file overflowsafe_type.hpp.

◆ operator%()

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

Definition at line 155 of file overflowsafe_type.hpp.

◆ operator%=()

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

Definition at line 154 of file overflowsafe_type.hpp.

◆ operator*() [1/5]

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

Definition at line 142 of file overflowsafe_type.hpp.

◆ operator*() [2/5]

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

Definition at line 141 of file overflowsafe_type.hpp.

◆ operator*() [3/5]

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

Definition at line 143 of file overflowsafe_type.hpp.

◆ operator*() [4/5]

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

Definition at line 144 of file overflowsafe_type.hpp.

◆ operator*() [5/5]

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

Definition at line 145 of file overflowsafe_type.hpp.

◆ operator*=()

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

Safe implementation of multiplication.

Parameters
factorthe factor to multiply this with.
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 115 of file overflowsafe_type.hpp.

References OverflowSafeInt< T >::m_value.

◆ operator+() [1/3]

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

Definition at line 98 of file overflowsafe_type.hpp.

◆ operator+() [2/3]

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

Definition at line 97 of file overflowsafe_type.hpp.

◆ operator+() [3/3]

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

Definition at line 99 of file overflowsafe_type.hpp.

◆ operator++() [1/2]

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

Definition at line 104 of file overflowsafe_type.hpp.

◆ operator++() [2/2]

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

Definition at line 106 of file overflowsafe_type.hpp.

◆ operator+=()

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

Safe implementation of addition.

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

Definition at line 55 of file overflowsafe_type.hpp.

References OverflowSafeInt< T >::m_value.

◆ operator-() [1/4]

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

Definition at line 48 of file overflowsafe_type.hpp.

◆ operator-() [2/4]

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

Definition at line 101 of file overflowsafe_type.hpp.

◆ operator-() [3/4]

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

Definition at line 100 of file overflowsafe_type.hpp.

◆ operator-() [4/4]

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

Definition at line 102 of file overflowsafe_type.hpp.

◆ operator--() [1/2]

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

Definition at line 105 of file overflowsafe_type.hpp.

◆ operator--() [2/2]

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

Definition at line 107 of file overflowsafe_type.hpp.

◆ operator-=()

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

Safe implementation of subtraction.

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

Definition at line 78 of file overflowsafe_type.hpp.

References OverflowSafeInt< T >::m_value.

◆ operator/() [1/3]

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

Definition at line 150 of file overflowsafe_type.hpp.

◆ operator/() [2/3]

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

Definition at line 149 of file overflowsafe_type.hpp.

◆ operator/() [3/3]

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

Definition at line 151 of file overflowsafe_type.hpp.

◆ operator/=()

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

Definition at line 148 of file overflowsafe_type.hpp.

◆ operator<() [1/2]

template<class T >
constexpr bool OverflowSafeInt< T >::operator< ( const int  other) const
inlineconstexpr

Definition at line 176 of file overflowsafe_type.hpp.

◆ operator<() [2/2]

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

Definition at line 168 of file overflowsafe_type.hpp.

◆ operator<<()

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

Definition at line 159 of file overflowsafe_type.hpp.

◆ operator<<=()

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

Definition at line 158 of file overflowsafe_type.hpp.

◆ operator<=() [1/2]

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

Definition at line 177 of file overflowsafe_type.hpp.

◆ operator<=() [2/2]

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

Definition at line 169 of file overflowsafe_type.hpp.

◆ operator=() [1/2]

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

Definition at line 45 of file overflowsafe_type.hpp.

◆ operator=() [2/2]

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

Definition at line 46 of file overflowsafe_type.hpp.

◆ operator==() [1/2]

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

Definition at line 172 of file overflowsafe_type.hpp.

◆ operator==() [2/2]

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

Definition at line 164 of file overflowsafe_type.hpp.

◆ operator>() [1/2]

template<class T >
constexpr bool OverflowSafeInt< T >::operator> ( const int  other) const
inlineconstexpr

Definition at line 174 of file overflowsafe_type.hpp.

◆ operator>() [2/2]

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

Definition at line 166 of file overflowsafe_type.hpp.

◆ operator>=() [1/2]

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

Definition at line 175 of file overflowsafe_type.hpp.

◆ operator>=() [2/2]

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

Definition at line 167 of file overflowsafe_type.hpp.

◆ operator>>()

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

Definition at line 161 of file overflowsafe_type.hpp.

◆ operator>>=()

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

Definition at line 160 of file overflowsafe_type.hpp.

Field Documentation

◆ m_value

template<class T >
T OverflowSafeInt< T >::m_value
private

The non-overflow safe backend to store the value in.

Definition at line 36 of file overflowsafe_type.hpp.

Referenced by OverflowSafeInt< T >::operator*=(), OverflowSafeInt< T >::operator+=(), and OverflowSafeInt< T >::operator-=().

◆ T_MAX

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

Definition at line 32 of file overflowsafe_type.hpp.

◆ T_MIN

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

Definition at line 33 of file overflowsafe_type.hpp.


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