|
OpenTTD Source 20261129-master-g72a8bdc861
|
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 |
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.
| T | the type these integers are stored with. |
Definition at line 29 of file overflowsafe_type.hpp.
| using OverflowSafeInt< T >::BaseType = T |
Definition at line 50 of file overflowsafe_type.hpp.
|
inlineconstexpr |
Construct an overflow-safe type from an integral type.
| value | The value. |
Definition at line 61 of file overflowsafe_type.hpp.
|
inlineconstexpr |
Safely construct an overflow-safe type from an overflow-safe type with a different base type.
| Tother | The base type of the original type. |
| value | The original overflow-safe type. |
Definition at line 70 of file overflowsafe_type.hpp.
|
inlineconstexprnoexcept |
Get the underlying value of this instance.
Definition at line 258 of file overflowsafe_type.hpp.
|
inlinestaticconstexprprivate |
Convert from any integral type to the base type.
| value | The value to be converted. |
Definition at line 40 of file overflowsafe_type.hpp.
Referenced by OverflowSafeInt< int64_t >::operator<=>(), OverflowSafeInt< int64_t >::operator=(), and OverflowSafeInt< int64_t >::operator==().
|
inlinestaticconstexpr |
Definition at line 251 of file overflowsafe_type.hpp.
|
inlinestaticconstexpr |
Definition at line 252 of file overflowsafe_type.hpp.
|
inlineconstexpr |
Definition at line 249 of file overflowsafe_type.hpp.
|
inlineconstexpr |
Modulo operator.
| divisor | The divisor to modulo with. |
Definition at line 221 of file overflowsafe_type.hpp.
|
inlineconstexpr |
Modulo operator.
| divisor | The divisor to modulo with. |
Definition at line 222 of file overflowsafe_type.hpp.
|
inlineconstexpr |
Modulo operator.
| divisor | The divisor to modulo with. |
Definition at line 214 of file overflowsafe_type.hpp.
|
inlineconstexpr |
Safely multiply operator.
| factor | The factor to mulitply with. |
Definition at line 189 of file overflowsafe_type.hpp.
|
inlineconstexpr |
Safely multiply operator.
| factor | The factor to mulitply with. |
Definition at line 190 of file overflowsafe_type.hpp.
|
inlineconstexpr |
Safe implementation of multiplication.
| factor | the factor to multiply this with. |
Definition at line 159 of file overflowsafe_type.hpp.
|
inlineconstexpr |
Safe add operator.
| other | The other value to add. |
Definition at line 134 of file overflowsafe_type.hpp.
|
inlineconstexpr |
Safe add operator.
| other | The other value to add. |
Definition at line 135 of file overflowsafe_type.hpp.
|
inlineconstexpr |
Definition at line 147 of file overflowsafe_type.hpp.
|
inlineconstexpr |
Definition at line 148 of file overflowsafe_type.hpp.
|
inlineconstexpr |
Safe implementation of addition.
| other | the amount to add |
Definition at line 87 of file overflowsafe_type.hpp.
|
inlineconstexpr |
Definition at line 79 of file overflowsafe_type.hpp.
|
inlineconstexpr |
Safe subtract operator.
| other | The other value to subtract. |
Definition at line 143 of file overflowsafe_type.hpp.
|
inlineconstexpr |
Safe subtract operator.
| other | The other value to subtract. |
Definition at line 144 of file overflowsafe_type.hpp.
|
inlineconstexpr |
Definition at line 149 of file overflowsafe_type.hpp.
|
inlineconstexpr |
Definition at line 150 of file overflowsafe_type.hpp.
|
inlineconstexpr |
Safe implementation of subtraction.
| other | the amount to subtract |
Definition at line 111 of file overflowsafe_type.hpp.
|
inlineconstexpr |
Divide operator.
| divisor | The divisor to divide with. |
Definition at line 205 of file overflowsafe_type.hpp.
|
inlineconstexpr |
Divide operator.
| divisor | The divisor to divide with. |
Definition at line 206 of file overflowsafe_type.hpp.
|
inlineconstexpr |
Divide operator.
| divisor | The divisor to divide with. |
Definition at line 198 of file overflowsafe_type.hpp.
|
inlineconstexpr |
Definition at line 227 of file overflowsafe_type.hpp.
|
inlineconstexpr |
Definition at line 226 of file overflowsafe_type.hpp.
|
inlineconstexpr |
Comparison operator.
| other | The other value to compare with. |
Definition at line 245 of file overflowsafe_type.hpp.
|
inlineconstexpr |
Comparison operator.
| other | The other value to compare with. |
Definition at line 246 of file overflowsafe_type.hpp.
|
inlineconstexpr |
Safely assign an integral value to this overflow-safe instance.
| other | The value to assign. |
Definition at line 77 of file overflowsafe_type.hpp.
|
inlineconstexpr |
Equality comparison operator.
| other | The other value to compare with. |
true if the other value is the same as this value.. Definition at line 236 of file overflowsafe_type.hpp.
|
inlineconstexpr |
Equality comparison operator.
| other | The other value to compare with. |
true if the other value is the same as this value.. Definition at line 237 of file overflowsafe_type.hpp.
|
inlineconstexpr |
Definition at line 229 of file overflowsafe_type.hpp.
|
inlineconstexpr |
Definition at line 228 of file overflowsafe_type.hpp.
|
friend |
Operators for when an OverflowSafeInt is on the right-hand-side of an operation with an integral value.
| lhs | the integral value. |
| rhs | the overflow-safe value. |
Definition at line 270 of file overflowsafe_type.hpp.
|
friend |
Operators for when an OverflowSafeInt is on the right-hand-side of an operation with an integral value.
| lhs | the integral value. |
| rhs | the overflow-safe value. |
Definition at line 268 of file overflowsafe_type.hpp.
|
friend |
Operators for when an OverflowSafeInt is on the right-hand-side of an operation with an integral value.
| lhs | the integral value. |
| rhs | the overflow-safe value. |
Definition at line 266 of file overflowsafe_type.hpp.
|
friend |
Operators for when an OverflowSafeInt is on the right-hand-side of an operation with an integral value.
| lhs | the integral value. |
| rhs | the overflow-safe value. |
Definition at line 267 of file overflowsafe_type.hpp.
|
friend |
Operators for when an OverflowSafeInt is on the right-hand-side of an operation with an integral value.
| lhs | the integral value. |
| rhs | the overflow-safe value. |
Definition at line 269 of file overflowsafe_type.hpp.
|
private |
The non-overflow safe backend to store the value in.
Definition at line 48 of file overflowsafe_type.hpp.
Referenced by OverflowSafeInt< int64_t >::operator%=(), OverflowSafeInt< int64_t >::operator*=(), OverflowSafeInt< int64_t >::operator+=(), OverflowSafeInt< int64_t >::operator-=(), OverflowSafeInt< int64_t >::operator/=(), OverflowSafeInt< int64_t >::operator<=>(), OverflowSafeInt< int64_t >::operator<=>(), and OverflowSafeInt< int64_t >::operator==().
|
staticconstexprprivate |
Maximum value of this type.
Definition at line 31 of file overflowsafe_type.hpp.
|
staticconstexprprivate |
Minimum value of this type.
Definition at line 32 of file overflowsafe_type.hpp.