Bug 1614879 [wpt PR 21750] - Set request mode for beacon request with non-cors-safeli...
[gecko.git] / mfbt / Saturate.h
blob87d54c95c71c60eb82e6d9f86b34c89ed9db1900
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 /* Provides saturation arithmetics for scalar types. */
9 #ifndef mozilla_Saturate_h
10 #define mozilla_Saturate_h
12 #include <limits>
13 #include <utility>
15 #include "mozilla/Attributes.h"
16 #include "mozilla/TypeTraits.h"
18 namespace mozilla {
19 namespace detail {
21 /**
22 * |SaturateOp<T>| wraps scalar values for saturation arithmetics. Usage:
24 * uint32_t value = 1;
26 * ++SaturateOp<uint32_t>(value); // value is 2
27 * --SaturateOp<uint32_t>(value); // value is 1
28 * --SaturateOp<uint32_t>(value); // value is 0
29 * --SaturateOp<uint32_t>(value); // value is still 0
31 * Please add new operators when required.
33 * |SaturateOp<T>| will saturate at the minimum and maximum values of
34 * type T. If you need other bounds, implement a clamped-type class and
35 * specialize the type traits accordingly.
37 template <typename T>
38 class SaturateOp {
39 public:
40 explicit SaturateOp(T& aValue) : mValue(aValue) {
41 // We should actually check for |std::is_scalar<T>::value| to be
42 // true, but this type trait is not available everywhere. Relax
43 // this assertion if you want to use floating point values as well.
44 static_assert(IsIntegral<T>::value,
45 "Integral type required in instantiation");
48 // Add and subtract operators
50 T operator+(const T& aRhs) const { return T(mValue) += aRhs; }
52 T operator-(const T& aRhs) const { return T(mValue) -= aRhs; }
54 // Compound operators
56 const T& operator+=(const T& aRhs) const {
57 const T min = std::numeric_limits<T>::min();
58 const T max = std::numeric_limits<T>::max();
60 if (aRhs > static_cast<T>(0)) {
61 mValue = (max - aRhs) < mValue ? max : mValue + aRhs;
62 } else {
63 mValue = (min - aRhs) > mValue ? min : mValue + aRhs;
65 return mValue;
68 const T& operator-=(const T& aRhs) const {
69 const T min = std::numeric_limits<T>::min();
70 const T max = std::numeric_limits<T>::max();
72 if (aRhs > static_cast<T>(0)) {
73 mValue = (min + aRhs) > mValue ? min : mValue - aRhs;
74 } else {
75 mValue = (max + aRhs) < mValue ? max : mValue - aRhs;
77 return mValue;
80 // Increment and decrement operators
82 const T& operator++() const // prefix
84 return operator+=(static_cast<T>(1));
87 T operator++(int) const // postfix
89 const T value(mValue);
90 operator++();
91 return value;
94 const T& operator--() const // prefix
96 return operator-=(static_cast<T>(1));
99 T operator--(int) const // postfix
101 const T value(mValue);
102 operator--();
103 return value;
106 private:
107 SaturateOp(const SaturateOp<T>&) = delete;
108 SaturateOp(SaturateOp<T>&&) = delete;
109 SaturateOp& operator=(const SaturateOp<T>&) = delete;
110 SaturateOp& operator=(SaturateOp<T>&&) = delete;
112 T& mValue;
116 * |Saturate<T>| is a value type for saturation arithmetics. It's
117 * built on top of |SaturateOp<T>|.
119 template <typename T>
120 class Saturate {
121 public:
122 Saturate() = default;
123 MOZ_IMPLICIT Saturate(const Saturate<T>&) = default;
125 MOZ_IMPLICIT Saturate(Saturate<T>&& aValue) {
126 mValue = std::move(aValue.mValue);
129 explicit Saturate(const T& aValue) : mValue(aValue) {}
131 const T& value() const { return mValue; }
133 // Compare operators
135 bool operator==(const Saturate<T>& aRhs) const {
136 return mValue == aRhs.mValue;
139 bool operator!=(const Saturate<T>& aRhs) const { return !operator==(aRhs); }
141 bool operator==(const T& aRhs) const { return mValue == aRhs; }
143 bool operator!=(const T& aRhs) const { return !operator==(aRhs); }
145 // Assignment operators
147 Saturate<T>& operator=(const Saturate<T>&) = default;
149 Saturate<T>& operator=(Saturate<T>&& aRhs) {
150 mValue = std::move(aRhs.mValue);
151 return *this;
154 // Add and subtract operators
156 Saturate<T> operator+(const Saturate<T>& aRhs) const {
157 Saturate<T> lhs(mValue);
158 return lhs += aRhs.mValue;
161 Saturate<T> operator+(const T& aRhs) const {
162 Saturate<T> lhs(mValue);
163 return lhs += aRhs;
166 Saturate<T> operator-(const Saturate<T>& aRhs) const {
167 Saturate<T> lhs(mValue);
168 return lhs -= aRhs.mValue;
171 Saturate<T> operator-(const T& aRhs) const {
172 Saturate<T> lhs(mValue);
173 return lhs -= aRhs;
176 // Compound operators
178 Saturate<T>& operator+=(const Saturate<T>& aRhs) {
179 SaturateOp<T>(mValue) += aRhs.mValue;
180 return *this;
183 Saturate<T>& operator+=(const T& aRhs) {
184 SaturateOp<T>(mValue) += aRhs;
185 return *this;
188 Saturate<T>& operator-=(const Saturate<T>& aRhs) {
189 SaturateOp<T>(mValue) -= aRhs.mValue;
190 return *this;
193 Saturate<T>& operator-=(const T& aRhs) {
194 SaturateOp<T>(mValue) -= aRhs;
195 return *this;
198 // Increment and decrement operators
200 Saturate<T>& operator++() // prefix
202 ++SaturateOp<T>(mValue);
203 return *this;
206 Saturate<T> operator++(int) // postfix
208 return Saturate<T>(SaturateOp<T>(mValue)++);
211 Saturate<T>& operator--() // prefix
213 --SaturateOp<T>(mValue);
214 return *this;
217 Saturate<T> operator--(int) // postfix
219 return Saturate<T>(SaturateOp<T>(mValue)--);
222 private:
223 T mValue;
226 } // namespace detail
228 typedef detail::Saturate<int8_t> SaturateInt8;
229 typedef detail::Saturate<int16_t> SaturateInt16;
230 typedef detail::Saturate<int32_t> SaturateInt32;
231 typedef detail::Saturate<uint8_t> SaturateUint8;
232 typedef detail::Saturate<uint16_t> SaturateUint16;
233 typedef detail::Saturate<uint32_t> SaturateUint32;
235 } // namespace mozilla
237 template <typename LhsT, typename RhsT>
238 bool operator==(LhsT aLhs, const mozilla::detail::Saturate<RhsT>& aRhs) {
239 return aRhs.operator==(static_cast<RhsT>(aLhs));
242 template <typename LhsT, typename RhsT>
243 bool operator!=(LhsT aLhs, const mozilla::detail::Saturate<RhsT>& aRhs) {
244 return !(aLhs == aRhs);
247 #endif // mozilla_Saturate_h