OpenTTD Source  20241108-master-g80f628063a
OverflowSafeInt< T > Class Template Reference

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

#include <overflowsafe_type.hpp>

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. More...
 
constexpr OverflowSafeIntoperator-= (const OverflowSafeInt &other)
 Safe implementation of subtraction. More...
 
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. More...
 
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
 

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 Function Documentation

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

References OverflowSafeInt< T >::m_value.

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

References OverflowSafeInt< T >::m_value.

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

References OverflowSafeInt< T >::m_value.


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