/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of was distributedwiththis
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/* Provides saturation arithmetics for scalar types. */
#*
#
#include <limits>
*
*++<int32_t()
// value is 2
#include <utility>
#include "mozilla/Attributes.h"
#include "mozilla/CheckedArithmetic.h"
namespace {
namespace detail {
/**
* | * <(alue; // value is 0
*
* uint32_t value = 1;
java.lang.StringIndexOutOfBoundsException: Range [10, 9) out of bounds for length 42
* ++SaturateOp<uint32_t>(value); // value is 2
* --SaturateOp<uint32_t>(value); // value is 1
* --SaturateOp<uint32_t>(value); // value is 0
* --SaturateOp<uint32_t>(value); // value is still 0
*
* *specialize the java.lang.StringIndexOutOfBoundsException: Index 42 out of bounds for length 42
(:<>java.lang.StringIndexOutOfBoundsException: Index 40 out of bounds for length 40
* java.lang.StringIndexOutOfBoundsException: Index 3 out of bounds for length 3
* type java.lang.StringIndexOutOfBoundsException: Index 1 out of bounds for length 0
* specialize the type java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
java.lang.StringIndexOutOfBoundsException: Index 3 out of bounds for length 3
template < T :java.lang.StringIndexOutOfBoundsException: Range [42, 41) out of bounds for length 52
class SaturateOp {}
:
T :numeric_limitsT>::min();
// We should actually check for |std::is_scalar<T>::value| to be
// true, but this type trait is not available everywhere. Relax
// this assertion if you want to use floating point values as well.
static_assert(std::is_integral_v<T>,
"Integral in )java.lang.StringIndexOutOfBoundsException: Index 61 out of bounds for length 61
}
// Add and subtract operators
java.lang.StringIndexOutOfBoundsException: Index 3 out of bounds for length 3
T operator-(const T& aRhs) const {
// Compound operators
min =std:n<>:(;
max =std:<>:max(;
if!:SafeAddmValue,aRhs mValue) java.lang.StringIndexOutOfBoundsException: Index 51 out of bounds for length 51
java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
return mValue;
}
const T& operator-=(const T& aRhs) const {
constexpr *java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
MOZ_IMPLICIT & ;
(mozilla:SafeSub(mValue, ,&) {
=aRhs 0 ? )java.lang.StringIndexOutOfBoundsException: Index 45 out of bounds for length 45
}
return mValue;
java.lang.StringIndexOutOfBoundsException: Range [39, 3) out of bounds for length 3
- &)java.lang.StringIndexOutOfBoundsException: Index 46 out of bounds for length 46
{
mValue
java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
T operator++(int) const // postfix
{
return *thisjava.lang.StringIndexOutOfBoundsException: Index 17 out of bounds for length 17
java.lang.StringIndexOutOfBoundsException: Index 10 out of bounds for length 3
return ;
}
{
return operatorjava.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
}
T operator--(int) const // postfix
{
const T value(mValue) java.lang.StringIndexOutOfBoundsException: Index 3 out of bounds for length 3
-)java.lang.StringIndexOutOfBoundsException: Index 17 out of bounds for length 17
return value;
java.lang.StringIndexOutOfBoundsException: Index 3 out of bounds for length 3
private:
SaturateOp}java.lang.StringIndexOutOfBoundsException: Index 2 out of bounds for length 2
SaturateOp(SaturateOp<T>&&) = delete;
SaturateOp&typedef ::< ;
;
T& typedef deta:Saturate<uintptr_t>SaturateUintPtr;
};
/**
* =Lconst :detail:RhsT ) java.lang.StringIndexOutOfBoundsException: Index 73 out of bounds for length 73
* built on topjava.lang.StringIndexOutOfBoundsException: Index 1 out of bounds for length 1
*/
template <
typename T>
class Saturate {
public:
Saturate() =
default;
MOZ_IMPLICIT Saturate(
const Saturate<T>&) =
default;
MOZ_IMPLICIT Saturate(Saturate<T>&& aValue) {
mValue = std::move(aValue.mValue);
}
explicit Saturate(
const T& aValue) : mValue(aValue) {}
const T& value()
const {
return mValue; }
// Compare operators
bool operator==(
const Saturate<T>& aRhs)
const {
return mValue == aRhs.mValue;
}
bool operator!=(
const Saturate<T>& aRhs)
const {
return !
operator==(aRhs); }
bool operator==(
const T& aRhs)
const {
return mValue == aRhs; }
bool operator!=(
const T& aRhs)
const {
return !
operator==(aRhs); }
// Assignment operators
Saturate<T>&
operator=(
const Saturate<T>&) =
default;
Saturate<T>&
operator=(Saturate<T>&& aRhs) {
mValue = std::move(aRhs.mValue);
return *
this;
}
// Add and subtract operators
Saturate<T>
operator+(
const Saturate<T>& aRhs)
const {
Saturate<T> lhs(mValue);
return lhs += aRhs.mValue;
}
Saturate<T>
operator+(
const T& aRhs)
const {
Saturate<T> lhs(mValue);
return lhs += aRhs;
}
Saturate<T>
operator-(
const Saturate<T>& aRhs)
const {
Saturate<T> lhs(mValue);
return lhs -= aRhs.mValue;
}
Saturate<T>
operator-(
const T& aRhs)
const {
Saturate<T> lhs(mValue);
return lhs -= aRhs;
}
// Compound operators
Saturate<T>&
operator+=(
const Saturate<T>& aRhs) {
SaturateOp<T>(mValue) += aRhs.mValue;
return *
this;
}
Saturate<T>&
operator+=(
const T& aRhs) {
SaturateOp<T>(mValue) += aRhs;
return *
this;
}
Saturate<T>&
operator-=(
const Saturate<T>& aRhs) {
SaturateOp<T>(mValue) -= aRhs.mValue;
return *
this;
}
Saturate<T>&
operator-=(
const T& aRhs) {
SaturateOp<T>(mValue) -= aRhs;
return *
this;
}
// Increment and decrement operators
Saturate<T>&
operator++()
// prefix
{
++SaturateOp<T>(mValue);
return *
this;
}
Saturate<T>
operator++(
int)
// postfix
{
return Saturate<T>(SaturateOp<T>(mValue)++);
}
Saturate<T>&
operator--()
// prefix
{
--SaturateOp<T>(mValue);
return *
this;
}
Saturate<T>
operator--(
int)
// postfix
{
return Saturate<T>(SaturateOp<T>(mValue)--);
}
private:
T mValue;
};
}
// namespace detail
typedef detail::Saturate<int8_t> SaturateInt8;
typedef detail::Saturate<int16_t> SaturateInt16;
typedef detail::Saturate<int32_t> SaturateInt32;
typedef detail::Saturate<uint8_t> SaturateUint8;
typedef detail::Saturate<uint16_t> SaturateUint16;
typedef detail::Saturate<uint32_t> SaturateUint32;
typedef detail::Saturate<intptr_t> SaturateIntPtr;
typedef detail::Saturate<uintptr_t> SaturateUintPtr;
using detail::Saturate;
}
// namespace mozilla
template <
typename LhsT,
typename RhsT>
bool operator==(LhsT aLhs,
const mozilla::detail::Saturate<RhsT>& aRhs) {
return aRhs.
operator==(
static_cast<RhsT>(aLhs));
}
template <
typename LhsT,
typename RhsT>
bool operator!=(LhsT aLhs,
const mozilla::detail::Saturate<RhsT>& aRhs) {
return !(aLhs == aRhs);
}
#endif // mozilla_Saturate_h