Files CCore/inc/gadget/UIntSat.h CCore/src/gadget/UIntSat.cpp
This file contains a collection of tools to control an unsigned integer overflow.
UIntSat can be used to perform operations with the overflow control:
template <class UInt>
struct UIntSat
{
UInt value;
bool overflow;
// constructors
constexpr UIntSat(); // null value
constexpr UIntSat(UIntSat<UInt> a,UIntSat<UInt> b); // add two values
template <class T>
constexpr UIntSat(T value_); // convert from unsigned integer
// methods
bool operator ! () const { return overflow; }
friend constexpr UIntSat<UInt> operator + (UIntSat<UInt> a,UIntSat<UInt> b);
friend UIntSat<UInt> operator += (UIntSat<UInt> &a,UIntSat<UInt> b);
bool operator < (UInt lim) const;
bool operator <= (UInt lim) const;
bool operator > (UInt lim) const;
bool operator >= (UInt lim) const;
};
Flag overflow is set if the accumulated value is not representable by the given type. You can consider such value as "greater than the maximum UInt value". Further additive operations with such value produces only the same kind of value. You can sum, add to self and compare with a limit UIntSats. You can also use it in constant expressions. The most common usage is a buffer length calculation:
UIntSat<ulen> len=len1; len+=len2; len+=len3; if( len<=max_len ) { // ok } else { // not fit or even overflowed }
ULenSat is the typedef with the ulen parameter:
using ULenSat = UIntSat<ulen> ;
UIntConstAdd and UIntConstSub are Meta-constants.
template <class UInt,UInt A,UInt B> const UInt UIntConstAdd = .... ; template <class UInt,UInt A,UInt B> const UInt UIntConstSub = .... ;
UIntConstAdd has the value A+B if there is no overflow, otherwise not defined.
UIntConstSub has the value A-B if A >= B, otherwise not defined.
const ulen L1 = .... ; const ulen L2 = .... ; const ulen S = UIntConstAdd<ulen,L1,L2> ; // safe sum of two constants, not compilable in case of overflow