annotate win64-msvc/include/kj/units.h @ 169:223a55898ab9 tip default

Add null config files
author Chris Cannam <cannam@all-day-breakfast.com>
date Mon, 02 Mar 2020 14:03:47 +0000
parents b4bfdf10c4b3
children
rev   line source
cannam@148 1 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
cannam@148 2 // Licensed under the MIT License:
cannam@148 3 //
cannam@148 4 // Permission is hereby granted, free of charge, to any person obtaining a copy
cannam@148 5 // of this software and associated documentation files (the "Software"), to deal
cannam@148 6 // in the Software without restriction, including without limitation the rights
cannam@148 7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
cannam@148 8 // copies of the Software, and to permit persons to whom the Software is
cannam@148 9 // furnished to do so, subject to the following conditions:
cannam@148 10 //
cannam@148 11 // The above copyright notice and this permission notice shall be included in
cannam@148 12 // all copies or substantial portions of the Software.
cannam@148 13 //
cannam@148 14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
cannam@148 15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
cannam@148 16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
cannam@148 17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
cannam@148 18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
cannam@148 19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
cannam@148 20 // THE SOFTWARE.
cannam@148 21
cannam@148 22 // This file contains types which are intended to help detect incorrect usage at compile
cannam@148 23 // time, but should then be optimized down to basic primitives (usually, integers) by the
cannam@148 24 // compiler.
cannam@148 25
cannam@148 26 #ifndef KJ_UNITS_H_
cannam@148 27 #define KJ_UNITS_H_
cannam@148 28
cannam@148 29 #if defined(__GNUC__) && !KJ_HEADER_WARNINGS
cannam@148 30 #pragma GCC system_header
cannam@148 31 #endif
cannam@148 32
cannam@148 33 #include "common.h"
cannam@148 34 #include <inttypes.h>
cannam@148 35
cannam@148 36 namespace kj {
cannam@148 37
cannam@148 38 // =======================================================================================
cannam@148 39 // IDs
cannam@148 40
cannam@148 41 template <typename UnderlyingType, typename Label>
cannam@148 42 struct Id {
cannam@148 43 // A type-safe numeric ID. `UnderlyingType` is the underlying integer representation. `Label`
cannam@148 44 // distinguishes this Id from other Id types. Sample usage:
cannam@148 45 //
cannam@148 46 // class Foo;
cannam@148 47 // typedef Id<uint, Foo> FooId;
cannam@148 48 //
cannam@148 49 // class Bar;
cannam@148 50 // typedef Id<uint, Bar> BarId;
cannam@148 51 //
cannam@148 52 // You can now use the FooId and BarId types without any possibility of accidentally using a
cannam@148 53 // FooId when you really wanted a BarId or vice-versa.
cannam@148 54
cannam@148 55 UnderlyingType value;
cannam@148 56
cannam@148 57 inline constexpr Id(): value(0) {}
cannam@148 58 inline constexpr explicit Id(int value): value(value) {}
cannam@148 59
cannam@148 60 inline constexpr bool operator==(const Id& other) const { return value == other.value; }
cannam@148 61 inline constexpr bool operator!=(const Id& other) const { return value != other.value; }
cannam@148 62 inline constexpr bool operator<=(const Id& other) const { return value <= other.value; }
cannam@148 63 inline constexpr bool operator>=(const Id& other) const { return value >= other.value; }
cannam@148 64 inline constexpr bool operator< (const Id& other) const { return value < other.value; }
cannam@148 65 inline constexpr bool operator> (const Id& other) const { return value > other.value; }
cannam@148 66 };
cannam@148 67
cannam@148 68 // =======================================================================================
cannam@148 69 // Quantity and UnitRatio -- implement unit analysis via the type system
cannam@148 70
cannam@148 71 struct Unsafe_ {};
cannam@148 72 constexpr Unsafe_ unsafe = Unsafe_();
cannam@148 73 // Use as a parameter to constructors that are unsafe to indicate that you really do mean it.
cannam@148 74
cannam@148 75 template <uint64_t maxN, typename T>
cannam@148 76 class Bounded;
cannam@148 77 template <uint value>
cannam@148 78 class BoundedConst;
cannam@148 79
cannam@148 80 template <typename T> constexpr bool isIntegral() { return false; }
cannam@148 81 template <> constexpr bool isIntegral<char>() { return true; }
cannam@148 82 template <> constexpr bool isIntegral<signed char>() { return true; }
cannam@148 83 template <> constexpr bool isIntegral<short>() { return true; }
cannam@148 84 template <> constexpr bool isIntegral<int>() { return true; }
cannam@148 85 template <> constexpr bool isIntegral<long>() { return true; }
cannam@148 86 template <> constexpr bool isIntegral<long long>() { return true; }
cannam@148 87 template <> constexpr bool isIntegral<unsigned char>() { return true; }
cannam@148 88 template <> constexpr bool isIntegral<unsigned short>() { return true; }
cannam@148 89 template <> constexpr bool isIntegral<unsigned int>() { return true; }
cannam@148 90 template <> constexpr bool isIntegral<unsigned long>() { return true; }
cannam@148 91 template <> constexpr bool isIntegral<unsigned long long>() { return true; }
cannam@148 92
cannam@148 93 template <typename T>
cannam@148 94 struct IsIntegralOrBounded_ { static constexpr bool value = isIntegral<T>(); };
cannam@148 95 template <uint64_t m, typename T>
cannam@148 96 struct IsIntegralOrBounded_<Bounded<m, T>> { static constexpr bool value = true; };
cannam@148 97 template <uint v>
cannam@148 98 struct IsIntegralOrBounded_<BoundedConst<v>> { static constexpr bool value = true; };
cannam@148 99
cannam@148 100 template <typename T>
cannam@148 101 inline constexpr bool isIntegralOrBounded() { return IsIntegralOrBounded_<T>::value; }
cannam@148 102
cannam@148 103 template <typename Number, typename Unit1, typename Unit2>
cannam@148 104 class UnitRatio {
cannam@148 105 // A multiplier used to convert Quantities of one unit to Quantities of another unit. See
cannam@148 106 // Quantity, below.
cannam@148 107 //
cannam@148 108 // Construct this type by dividing one Quantity by another of a different unit. Use this type
cannam@148 109 // by multiplying it by a Quantity, or dividing a Quantity by it.
cannam@148 110
cannam@148 111 static_assert(isIntegralOrBounded<Number>(),
cannam@148 112 "Underlying type for UnitRatio must be integer.");
cannam@148 113
cannam@148 114 public:
cannam@148 115 inline UnitRatio() {}
cannam@148 116
cannam@148 117 constexpr UnitRatio(Number unit1PerUnit2, decltype(unsafe)): unit1PerUnit2(unit1PerUnit2) {}
cannam@148 118 // This constructor was intended to be private, but GCC complains about it being private in a
cannam@148 119 // bunch of places that don't appear to even call it, so I made it public. Oh well.
cannam@148 120
cannam@148 121 template <typename OtherNumber>
cannam@148 122 inline constexpr UnitRatio(const UnitRatio<OtherNumber, Unit1, Unit2>& other)
cannam@148 123 : unit1PerUnit2(other.unit1PerUnit2) {}
cannam@148 124
cannam@148 125 template <typename OtherNumber>
cannam@148 126 inline constexpr UnitRatio<decltype(Number()+OtherNumber()), Unit1, Unit2>
cannam@148 127 operator+(UnitRatio<OtherNumber, Unit1, Unit2> other) const {
cannam@148 128 return UnitRatio<decltype(Number()+OtherNumber()), Unit1, Unit2>(
cannam@148 129 unit1PerUnit2 + other.unit1PerUnit2, unsafe);
cannam@148 130 }
cannam@148 131 template <typename OtherNumber>
cannam@148 132 inline constexpr UnitRatio<decltype(Number()-OtherNumber()), Unit1, Unit2>
cannam@148 133 operator-(UnitRatio<OtherNumber, Unit1, Unit2> other) const {
cannam@148 134 return UnitRatio<decltype(Number()-OtherNumber()), Unit1, Unit2>(
cannam@148 135 unit1PerUnit2 - other.unit1PerUnit2, unsafe);
cannam@148 136 }
cannam@148 137
cannam@148 138 template <typename OtherNumber, typename Unit3>
cannam@148 139 inline constexpr UnitRatio<decltype(Number()*OtherNumber()), Unit3, Unit2>
cannam@148 140 operator*(UnitRatio<OtherNumber, Unit3, Unit1> other) const {
cannam@148 141 // U1 / U2 * U3 / U1 = U3 / U2
cannam@148 142 return UnitRatio<decltype(Number()*OtherNumber()), Unit3, Unit2>(
cannam@148 143 unit1PerUnit2 * other.unit1PerUnit2, unsafe);
cannam@148 144 }
cannam@148 145 template <typename OtherNumber, typename Unit3>
cannam@148 146 inline constexpr UnitRatio<decltype(Number()*OtherNumber()), Unit1, Unit3>
cannam@148 147 operator*(UnitRatio<OtherNumber, Unit2, Unit3> other) const {
cannam@148 148 // U1 / U2 * U2 / U3 = U1 / U3
cannam@148 149 return UnitRatio<decltype(Number()*OtherNumber()), Unit1, Unit3>(
cannam@148 150 unit1PerUnit2 * other.unit1PerUnit2, unsafe);
cannam@148 151 }
cannam@148 152
cannam@148 153 template <typename OtherNumber, typename Unit3>
cannam@148 154 inline constexpr UnitRatio<decltype(Number()*OtherNumber()), Unit3, Unit2>
cannam@148 155 operator/(UnitRatio<OtherNumber, Unit1, Unit3> other) const {
cannam@148 156 // (U1 / U2) / (U1 / U3) = U3 / U2
cannam@148 157 return UnitRatio<decltype(Number()*OtherNumber()), Unit3, Unit2>(
cannam@148 158 unit1PerUnit2 / other.unit1PerUnit2, unsafe);
cannam@148 159 }
cannam@148 160 template <typename OtherNumber, typename Unit3>
cannam@148 161 inline constexpr UnitRatio<decltype(Number()*OtherNumber()), Unit1, Unit3>
cannam@148 162 operator/(UnitRatio<OtherNumber, Unit3, Unit2> other) const {
cannam@148 163 // (U1 / U2) / (U3 / U2) = U1 / U3
cannam@148 164 return UnitRatio<decltype(Number()*OtherNumber()), Unit1, Unit3>(
cannam@148 165 unit1PerUnit2 / other.unit1PerUnit2, unsafe);
cannam@148 166 }
cannam@148 167
cannam@148 168 template <typename OtherNumber>
cannam@148 169 inline decltype(Number() / OtherNumber())
cannam@148 170 operator/(UnitRatio<OtherNumber, Unit1, Unit2> other) const {
cannam@148 171 return unit1PerUnit2 / other.unit1PerUnit2;
cannam@148 172 }
cannam@148 173
cannam@148 174 inline bool operator==(UnitRatio other) const { return unit1PerUnit2 == other.unit1PerUnit2; }
cannam@148 175 inline bool operator!=(UnitRatio other) const { return unit1PerUnit2 != other.unit1PerUnit2; }
cannam@148 176
cannam@148 177 private:
cannam@148 178 Number unit1PerUnit2;
cannam@148 179
cannam@148 180 template <typename OtherNumber, typename OtherUnit>
cannam@148 181 friend class Quantity;
cannam@148 182 template <typename OtherNumber, typename OtherUnit1, typename OtherUnit2>
cannam@148 183 friend class UnitRatio;
cannam@148 184
cannam@148 185 template <typename N1, typename N2, typename U1, typename U2, typename>
cannam@148 186 friend inline constexpr UnitRatio<decltype(N1() * N2()), U1, U2>
cannam@148 187 operator*(N1, UnitRatio<N2, U1, U2>);
cannam@148 188 };
cannam@148 189
cannam@148 190 template <typename N1, typename N2, typename U1, typename U2,
cannam@148 191 typename = EnableIf<isIntegralOrBounded<N1>() && isIntegralOrBounded<N2>()>>
cannam@148 192 inline constexpr UnitRatio<decltype(N1() * N2()), U1, U2>
cannam@148 193 operator*(N1 n, UnitRatio<N2, U1, U2> r) {
cannam@148 194 return UnitRatio<decltype(N1() * N2()), U1, U2>(n * r.unit1PerUnit2, unsafe);
cannam@148 195 }
cannam@148 196
cannam@148 197 template <typename Number, typename Unit>
cannam@148 198 class Quantity {
cannam@148 199 // A type-safe numeric quantity, specified in terms of some unit. Two Quantities cannot be used
cannam@148 200 // in arithmetic unless they use the same unit. The `Unit` type parameter is only used to prevent
cannam@148 201 // accidental mixing of units; this type is never instantiated and can very well be incomplete.
cannam@148 202 // `Number` is the underlying primitive numeric type.
cannam@148 203 //
cannam@148 204 // Quantities support most basic arithmetic operators, intelligently handling units, and
cannam@148 205 // automatically casting the underlying type in the same way that the compiler would.
cannam@148 206 //
cannam@148 207 // To convert a primitive number to a Quantity, multiply it by unit<Quantity<N, U>>().
cannam@148 208 // To convert a Quantity to a primitive number, divide it by unit<Quantity<N, U>>().
cannam@148 209 // To convert a Quantity of one unit to another unit, multiply or divide by a UnitRatio.
cannam@148 210 //
cannam@148 211 // The Quantity class is not well-suited to hardcore physics as it does not allow multiplying
cannam@148 212 // one quantity by another. For example, multiplying meters by meters won't get you square
cannam@148 213 // meters; it will get you a compiler error. It would be interesting to see if template
cannam@148 214 // metaprogramming could properly deal with such things but this isn't needed for the present
cannam@148 215 // use case.
cannam@148 216 //
cannam@148 217 // Sample usage:
cannam@148 218 //
cannam@148 219 // class SecondsLabel;
cannam@148 220 // typedef Quantity<double, SecondsLabel> Seconds;
cannam@148 221 // constexpr Seconds SECONDS = unit<Seconds>();
cannam@148 222 //
cannam@148 223 // class MinutesLabel;
cannam@148 224 // typedef Quantity<double, MinutesLabel> Minutes;
cannam@148 225 // constexpr Minutes MINUTES = unit<Minutes>();
cannam@148 226 //
cannam@148 227 // constexpr UnitRatio<double, SecondsLabel, MinutesLabel> SECONDS_PER_MINUTE =
cannam@148 228 // 60 * SECONDS / MINUTES;
cannam@148 229 //
cannam@148 230 // void waitFor(Seconds seconds) {
cannam@148 231 // sleep(seconds / SECONDS);
cannam@148 232 // }
cannam@148 233 // void waitFor(Minutes minutes) {
cannam@148 234 // waitFor(minutes * SECONDS_PER_MINUTE);
cannam@148 235 // }
cannam@148 236 //
cannam@148 237 // void waitThreeMinutes() {
cannam@148 238 // waitFor(3 * MINUTES);
cannam@148 239 // }
cannam@148 240
cannam@148 241 static_assert(isIntegralOrBounded<Number>(),
cannam@148 242 "Underlying type for Quantity must be integer.");
cannam@148 243
cannam@148 244 public:
cannam@148 245 inline constexpr Quantity() = default;
cannam@148 246
cannam@148 247 inline constexpr Quantity(MaxValue_): value(maxValue) {}
cannam@148 248 inline constexpr Quantity(MinValue_): value(minValue) {}
cannam@148 249 // Allow initialization from maxValue and minValue.
cannam@148 250 // TODO(msvc): decltype(maxValue) and decltype(minValue) deduce unknown-type for these function
cannam@148 251 // parameters, causing the compiler to complain of a duplicate constructor definition, so we
cannam@148 252 // specify MaxValue_ and MinValue_ types explicitly.
cannam@148 253
cannam@148 254 inline constexpr Quantity(Number value, decltype(unsafe)): value(value) {}
cannam@148 255 // This constructor was intended to be private, but GCC complains about it being private in a
cannam@148 256 // bunch of places that don't appear to even call it, so I made it public. Oh well.
cannam@148 257
cannam@148 258 template <typename OtherNumber>
cannam@148 259 inline constexpr Quantity(const Quantity<OtherNumber, Unit>& other)
cannam@148 260 : value(other.value) {}
cannam@148 261
cannam@148 262 template <typename OtherNumber>
cannam@148 263 inline Quantity& operator=(const Quantity<OtherNumber, Unit>& other) {
cannam@148 264 value = other.value;
cannam@148 265 return *this;
cannam@148 266 }
cannam@148 267
cannam@148 268 template <typename OtherNumber>
cannam@148 269 inline constexpr Quantity<decltype(Number() + OtherNumber()), Unit>
cannam@148 270 operator+(const Quantity<OtherNumber, Unit>& other) const {
cannam@148 271 return Quantity<decltype(Number() + OtherNumber()), Unit>(value + other.value, unsafe);
cannam@148 272 }
cannam@148 273 template <typename OtherNumber>
cannam@148 274 inline constexpr Quantity<decltype(Number() - OtherNumber()), Unit>
cannam@148 275 operator-(const Quantity<OtherNumber, Unit>& other) const {
cannam@148 276 return Quantity<decltype(Number() - OtherNumber()), Unit>(value - other.value, unsafe);
cannam@148 277 }
cannam@148 278 template <typename OtherNumber, typename = EnableIf<isIntegralOrBounded<OtherNumber>()>>
cannam@148 279 inline constexpr Quantity<decltype(Number() * OtherNumber()), Unit>
cannam@148 280 operator*(OtherNumber other) const {
cannam@148 281 return Quantity<decltype(Number() * other), Unit>(value * other, unsafe);
cannam@148 282 }
cannam@148 283 template <typename OtherNumber, typename = EnableIf<isIntegralOrBounded<OtherNumber>()>>
cannam@148 284 inline constexpr Quantity<decltype(Number() / OtherNumber()), Unit>
cannam@148 285 operator/(OtherNumber other) const {
cannam@148 286 return Quantity<decltype(Number() / other), Unit>(value / other, unsafe);
cannam@148 287 }
cannam@148 288 template <typename OtherNumber>
cannam@148 289 inline constexpr decltype(Number() / OtherNumber())
cannam@148 290 operator/(const Quantity<OtherNumber, Unit>& other) const {
cannam@148 291 return value / other.value;
cannam@148 292 }
cannam@148 293 template <typename OtherNumber>
cannam@148 294 inline constexpr Quantity<decltype(Number() % OtherNumber()), Unit>
cannam@148 295 operator%(const Quantity<OtherNumber, Unit>& other) const {
cannam@148 296 return Quantity<decltype(Number() % OtherNumber()), Unit>(value % other.value, unsafe);
cannam@148 297 }
cannam@148 298
cannam@148 299 template <typename OtherNumber, typename OtherUnit>
cannam@148 300 inline constexpr Quantity<decltype(Number() * OtherNumber()), OtherUnit>
cannam@148 301 operator*(UnitRatio<OtherNumber, OtherUnit, Unit> ratio) const {
cannam@148 302 return Quantity<decltype(Number() * OtherNumber()), OtherUnit>(
cannam@148 303 value * ratio.unit1PerUnit2, unsafe);
cannam@148 304 }
cannam@148 305 template <typename OtherNumber, typename OtherUnit>
cannam@148 306 inline constexpr Quantity<decltype(Number() / OtherNumber()), OtherUnit>
cannam@148 307 operator/(UnitRatio<OtherNumber, Unit, OtherUnit> ratio) const {
cannam@148 308 return Quantity<decltype(Number() / OtherNumber()), OtherUnit>(
cannam@148 309 value / ratio.unit1PerUnit2, unsafe);
cannam@148 310 }
cannam@148 311 template <typename OtherNumber, typename OtherUnit>
cannam@148 312 inline constexpr Quantity<decltype(Number() % OtherNumber()), Unit>
cannam@148 313 operator%(UnitRatio<OtherNumber, Unit, OtherUnit> ratio) const {
cannam@148 314 return Quantity<decltype(Number() % OtherNumber()), Unit>(
cannam@148 315 value % ratio.unit1PerUnit2, unsafe);
cannam@148 316 }
cannam@148 317 template <typename OtherNumber, typename OtherUnit>
cannam@148 318 inline constexpr UnitRatio<decltype(Number() / OtherNumber()), Unit, OtherUnit>
cannam@148 319 operator/(Quantity<OtherNumber, OtherUnit> other) const {
cannam@148 320 return UnitRatio<decltype(Number() / OtherNumber()), Unit, OtherUnit>(
cannam@148 321 value / other.value, unsafe);
cannam@148 322 }
cannam@148 323
cannam@148 324 template <typename OtherNumber>
cannam@148 325 inline constexpr bool operator==(const Quantity<OtherNumber, Unit>& other) const {
cannam@148 326 return value == other.value;
cannam@148 327 }
cannam@148 328 template <typename OtherNumber>
cannam@148 329 inline constexpr bool operator!=(const Quantity<OtherNumber, Unit>& other) const {
cannam@148 330 return value != other.value;
cannam@148 331 }
cannam@148 332 template <typename OtherNumber>
cannam@148 333 inline constexpr bool operator<=(const Quantity<OtherNumber, Unit>& other) const {
cannam@148 334 return value <= other.value;
cannam@148 335 }
cannam@148 336 template <typename OtherNumber>
cannam@148 337 inline constexpr bool operator>=(const Quantity<OtherNumber, Unit>& other) const {
cannam@148 338 return value >= other.value;
cannam@148 339 }
cannam@148 340 template <typename OtherNumber>
cannam@148 341 inline constexpr bool operator<(const Quantity<OtherNumber, Unit>& other) const {
cannam@148 342 return value < other.value;
cannam@148 343 }
cannam@148 344 template <typename OtherNumber>
cannam@148 345 inline constexpr bool operator>(const Quantity<OtherNumber, Unit>& other) const {
cannam@148 346 return value > other.value;
cannam@148 347 }
cannam@148 348
cannam@148 349 template <typename OtherNumber>
cannam@148 350 inline Quantity& operator+=(const Quantity<OtherNumber, Unit>& other) {
cannam@148 351 value += other.value;
cannam@148 352 return *this;
cannam@148 353 }
cannam@148 354 template <typename OtherNumber>
cannam@148 355 inline Quantity& operator-=(const Quantity<OtherNumber, Unit>& other) {
cannam@148 356 value -= other.value;
cannam@148 357 return *this;
cannam@148 358 }
cannam@148 359 template <typename OtherNumber>
cannam@148 360 inline Quantity& operator*=(OtherNumber other) {
cannam@148 361 value *= other;
cannam@148 362 return *this;
cannam@148 363 }
cannam@148 364 template <typename OtherNumber>
cannam@148 365 inline Quantity& operator/=(OtherNumber other) {
cannam@148 366 value /= other.value;
cannam@148 367 return *this;
cannam@148 368 }
cannam@148 369
cannam@148 370 private:
cannam@148 371 Number value;
cannam@148 372
cannam@148 373 template <typename OtherNumber, typename OtherUnit>
cannam@148 374 friend class Quantity;
cannam@148 375
cannam@148 376 template <typename Number1, typename Number2, typename Unit2>
cannam@148 377 friend inline constexpr auto operator*(Number1 a, Quantity<Number2, Unit2> b)
cannam@148 378 -> Quantity<decltype(Number1() * Number2()), Unit2>;
cannam@148 379 };
cannam@148 380
cannam@148 381 template <typename T> struct Unit_ {
cannam@148 382 static inline constexpr T get() { return T(1); }
cannam@148 383 };
cannam@148 384 template <typename T, typename U>
cannam@148 385 struct Unit_<Quantity<T, U>> {
cannam@148 386 static inline constexpr Quantity<decltype(Unit_<T>::get()), U> get() {
cannam@148 387 return Quantity<decltype(Unit_<T>::get()), U>(Unit_<T>::get(), unsafe);
cannam@148 388 }
cannam@148 389 };
cannam@148 390
cannam@148 391 template <typename T>
cannam@148 392 inline constexpr auto unit() -> decltype(Unit_<T>::get()) { return Unit_<T>::get(); }
cannam@148 393 // unit<Quantity<T, U>>() returns a Quantity of value 1. It also, intentionally, works on basic
cannam@148 394 // numeric types.
cannam@148 395
cannam@148 396 template <typename Number1, typename Number2, typename Unit>
cannam@148 397 inline constexpr auto operator*(Number1 a, Quantity<Number2, Unit> b)
cannam@148 398 -> Quantity<decltype(Number1() * Number2()), Unit> {
cannam@148 399 return Quantity<decltype(Number1() * Number2()), Unit>(a * b.value, unsafe);
cannam@148 400 }
cannam@148 401
cannam@148 402 template <typename Number1, typename Number2, typename Unit, typename Unit2>
cannam@148 403 inline constexpr auto operator*(UnitRatio<Number1, Unit2, Unit> ratio,
cannam@148 404 Quantity<Number2, Unit> measure)
cannam@148 405 -> decltype(measure * ratio) {
cannam@148 406 return measure * ratio;
cannam@148 407 }
cannam@148 408
cannam@148 409 // =======================================================================================
cannam@148 410 // Absolute measures
cannam@148 411
cannam@148 412 template <typename T, typename Label>
cannam@148 413 class Absolute {
cannam@148 414 // Wraps some other value -- typically a Quantity -- but represents a value measured based on
cannam@148 415 // some absolute origin. For example, if `Duration` is a type representing a time duration,
cannam@148 416 // Absolute<Duration, UnixEpoch> might be a calendar date.
cannam@148 417 //
cannam@148 418 // Since Absolute represents measurements relative to some arbitrary origin, the only sensible
cannam@148 419 // arithmetic to perform on them is addition and subtraction.
cannam@148 420
cannam@148 421 // TODO(someday): Do the same automatic expansion of integer width that Quantity does? Doesn't
cannam@148 422 // matter for our time use case, where we always use 64-bit anyway. Note that fixing this
cannam@148 423 // would implicitly allow things like multiplying an Absolute by a UnitRatio to change its
cannam@148 424 // units, which is actually totally logical and kind of neat.
cannam@148 425
cannam@148 426 public:
cannam@148 427 inline constexpr Absolute operator+(const T& other) const { return Absolute(value + other); }
cannam@148 428 inline constexpr Absolute operator-(const T& other) const { return Absolute(value - other); }
cannam@148 429 inline constexpr T operator-(const Absolute& other) const { return value - other.value; }
cannam@148 430
cannam@148 431 inline Absolute& operator+=(const T& other) { value += other; return *this; }
cannam@148 432 inline Absolute& operator-=(const T& other) { value -= other; return *this; }
cannam@148 433
cannam@148 434 inline constexpr bool operator==(const Absolute& other) const { return value == other.value; }
cannam@148 435 inline constexpr bool operator!=(const Absolute& other) const { return value != other.value; }
cannam@148 436 inline constexpr bool operator<=(const Absolute& other) const { return value <= other.value; }
cannam@148 437 inline constexpr bool operator>=(const Absolute& other) const { return value >= other.value; }
cannam@148 438 inline constexpr bool operator< (const Absolute& other) const { return value < other.value; }
cannam@148 439 inline constexpr bool operator> (const Absolute& other) const { return value > other.value; }
cannam@148 440
cannam@148 441 private:
cannam@148 442 T value;
cannam@148 443
cannam@148 444 explicit constexpr Absolute(T value): value(value) {}
cannam@148 445
cannam@148 446 template <typename U>
cannam@148 447 friend inline constexpr U origin();
cannam@148 448 };
cannam@148 449
cannam@148 450 template <typename T, typename Label>
cannam@148 451 inline constexpr Absolute<T, Label> operator+(const T& a, const Absolute<T, Label>& b) {
cannam@148 452 return b + a;
cannam@148 453 }
cannam@148 454
cannam@148 455 template <typename T> struct UnitOf_ { typedef T Type; };
cannam@148 456 template <typename T, typename Label> struct UnitOf_<Absolute<T, Label>> { typedef T Type; };
cannam@148 457 template <typename T>
cannam@148 458 using UnitOf = typename UnitOf_<T>::Type;
cannam@148 459 // UnitOf<Absolute<T, U>> is T. UnitOf<AnythingElse> is AnythingElse.
cannam@148 460
cannam@148 461 template <typename T>
cannam@148 462 inline constexpr T origin() { return T(0 * unit<UnitOf<T>>()); }
cannam@148 463 // origin<Absolute<T, U>>() returns an Absolute of value 0. It also, intentionally, works on basic
cannam@148 464 // numeric types.
cannam@148 465
cannam@148 466 // =======================================================================================
cannam@148 467 // Overflow avoidance
cannam@148 468
cannam@148 469 template <uint64_t n, uint accum = 0>
cannam@148 470 struct BitCount_ {
cannam@148 471 static constexpr uint value = BitCount_<(n >> 1), accum + 1>::value;
cannam@148 472 };
cannam@148 473 template <uint accum>
cannam@148 474 struct BitCount_<0, accum> {
cannam@148 475 static constexpr uint value = accum;
cannam@148 476 };
cannam@148 477
cannam@148 478 template <uint64_t n>
cannam@148 479 inline constexpr uint bitCount() { return BitCount_<n>::value; }
cannam@148 480 // Number of bits required to represent the number `n`.
cannam@148 481
cannam@148 482 template <uint bitCountBitCount> struct AtLeastUInt_ {
cannam@148 483 static_assert(bitCountBitCount < 7, "don't know how to represent integers over 64 bits");
cannam@148 484 };
cannam@148 485 template <> struct AtLeastUInt_<0> { typedef uint8_t Type; };
cannam@148 486 template <> struct AtLeastUInt_<1> { typedef uint8_t Type; };
cannam@148 487 template <> struct AtLeastUInt_<2> { typedef uint8_t Type; };
cannam@148 488 template <> struct AtLeastUInt_<3> { typedef uint8_t Type; };
cannam@148 489 template <> struct AtLeastUInt_<4> { typedef uint16_t Type; };
cannam@148 490 template <> struct AtLeastUInt_<5> { typedef uint32_t Type; };
cannam@148 491 template <> struct AtLeastUInt_<6> { typedef uint64_t Type; };
cannam@148 492
cannam@148 493 template <uint bits>
cannam@148 494 using AtLeastUInt = typename AtLeastUInt_<bitCount<max(bits, 1) - 1>()>::Type;
cannam@148 495 // AtLeastUInt<n> is an unsigned integer of at least n bits. E.g. AtLeastUInt<12> is uint16_t.
cannam@148 496
cannam@148 497 // -------------------------------------------------------------------
cannam@148 498
cannam@148 499 template <uint value>
cannam@148 500 class BoundedConst {
cannam@148 501 // A constant integer value on which we can do bit size analysis.
cannam@148 502
cannam@148 503 public:
cannam@148 504 BoundedConst() = default;
cannam@148 505
cannam@148 506 inline constexpr uint unwrap() const { return value; }
cannam@148 507
cannam@148 508 #define OP(op, check) \
cannam@148 509 template <uint other> \
cannam@148 510 inline constexpr BoundedConst<(value op other)> \
cannam@148 511 operator op(BoundedConst<other>) const { \
cannam@148 512 static_assert(check, "overflow in BoundedConst arithmetic"); \
cannam@148 513 return BoundedConst<(value op other)>(); \
cannam@148 514 }
cannam@148 515 #define COMPARE_OP(op) \
cannam@148 516 template <uint other> \
cannam@148 517 inline constexpr bool operator op(BoundedConst<other>) const { \
cannam@148 518 return value op other; \
cannam@148 519 }
cannam@148 520
cannam@148 521 OP(+, value + other >= value)
cannam@148 522 OP(-, value - other <= value)
cannam@148 523 OP(*, value * other / other == value)
cannam@148 524 OP(/, true) // div by zero already errors out; no other division ever overflows
cannam@148 525 OP(%, true) // mod by zero already errors out; no other modulus ever overflows
cannam@148 526 OP(<<, value << other >= value)
cannam@148 527 OP(>>, true) // right shift can't overflow
cannam@148 528 OP(&, true) // bitwise ops can't overflow
cannam@148 529 OP(|, true) // bitwise ops can't overflow
cannam@148 530
cannam@148 531 COMPARE_OP(==)
cannam@148 532 COMPARE_OP(!=)
cannam@148 533 COMPARE_OP(< )
cannam@148 534 COMPARE_OP(> )
cannam@148 535 COMPARE_OP(<=)
cannam@148 536 COMPARE_OP(>=)
cannam@148 537 #undef OP
cannam@148 538 #undef COMPARE_OP
cannam@148 539 };
cannam@148 540
cannam@148 541 template <uint64_t m, typename T>
cannam@148 542 struct Unit_<Bounded<m, T>> {
cannam@148 543 static inline constexpr BoundedConst<1> get() { return BoundedConst<1>(); }
cannam@148 544 };
cannam@148 545
cannam@148 546 template <uint value>
cannam@148 547 struct Unit_<BoundedConst<value>> {
cannam@148 548 static inline constexpr BoundedConst<1> get() { return BoundedConst<1>(); }
cannam@148 549 };
cannam@148 550
cannam@148 551 template <uint value>
cannam@148 552 inline constexpr BoundedConst<value> bounded() {
cannam@148 553 return BoundedConst<value>();
cannam@148 554 }
cannam@148 555
cannam@148 556 template <uint64_t a, uint64_t b>
cannam@148 557 static constexpr uint64_t boundedAdd() {
cannam@148 558 static_assert(a + b >= a, "possible overflow detected");
cannam@148 559 return a + b;
cannam@148 560 }
cannam@148 561 template <uint64_t a, uint64_t b>
cannam@148 562 static constexpr uint64_t boundedSub() {
cannam@148 563 static_assert(a - b <= a, "possible underflow detected");
cannam@148 564 return a - b;
cannam@148 565 }
cannam@148 566 template <uint64_t a, uint64_t b>
cannam@148 567 static constexpr uint64_t boundedMul() {
cannam@148 568 static_assert(a * b / b == a, "possible overflow detected");
cannam@148 569 return a * b;
cannam@148 570 }
cannam@148 571 template <uint64_t a, uint64_t b>
cannam@148 572 static constexpr uint64_t boundedLShift() {
cannam@148 573 static_assert(a << b >= a, "possible overflow detected");
cannam@148 574 return a << b;
cannam@148 575 }
cannam@148 576
cannam@148 577 template <uint a, uint b>
cannam@148 578 inline constexpr BoundedConst<kj::min(a, b)> min(BoundedConst<a>, BoundedConst<b>) {
cannam@148 579 return bounded<kj::min(a, b)>();
cannam@148 580 }
cannam@148 581 template <uint a, uint b>
cannam@148 582 inline constexpr BoundedConst<kj::max(a, b)> max(BoundedConst<a>, BoundedConst<b>) {
cannam@148 583 return bounded<kj::max(a, b)>();
cannam@148 584 }
cannam@148 585 // We need to override min() and max() between constants because the ternary operator in the
cannam@148 586 // default implementation would complain.
cannam@148 587
cannam@148 588 // -------------------------------------------------------------------
cannam@148 589
cannam@148 590 template <uint64_t maxN, typename T>
cannam@148 591 class Bounded {
cannam@148 592 public:
cannam@148 593 static_assert(maxN <= T(kj::maxValue), "possible overflow detected");
cannam@148 594
cannam@148 595 Bounded() = default;
cannam@148 596
cannam@148 597 Bounded(const Bounded& other) = default;
cannam@148 598 template <typename OtherInt, typename = EnableIf<isIntegral<OtherInt>()>>
cannam@148 599 inline constexpr Bounded(OtherInt value): value(value) {
cannam@148 600 static_assert(OtherInt(maxValue) <= maxN, "possible overflow detected");
cannam@148 601 }
cannam@148 602 template <uint64_t otherMax, typename OtherT>
cannam@148 603 inline constexpr Bounded(const Bounded<otherMax, OtherT>& other)
cannam@148 604 : value(other.value) {
cannam@148 605 static_assert(otherMax <= maxN, "possible overflow detected");
cannam@148 606 }
cannam@148 607 template <uint otherValue>
cannam@148 608 inline constexpr Bounded(BoundedConst<otherValue>)
cannam@148 609 : value(otherValue) {
cannam@148 610 static_assert(otherValue <= maxN, "overflow detected");
cannam@148 611 }
cannam@148 612
cannam@148 613 Bounded& operator=(const Bounded& other) = default;
cannam@148 614 template <typename OtherInt, typename = EnableIf<isIntegral<OtherInt>()>>
cannam@148 615 Bounded& operator=(OtherInt other) {
cannam@148 616 static_assert(OtherInt(maxValue) <= maxN, "possible overflow detected");
cannam@148 617 value = other;
cannam@148 618 return *this;
cannam@148 619 }
cannam@148 620 template <uint64_t otherMax, typename OtherT>
cannam@148 621 inline Bounded& operator=(const Bounded<otherMax, OtherT>& other) {
cannam@148 622 static_assert(otherMax <= maxN, "possible overflow detected");
cannam@148 623 value = other.value;
cannam@148 624 return *this;
cannam@148 625 }
cannam@148 626 template <uint otherValue>
cannam@148 627 inline Bounded& operator=(BoundedConst<otherValue>) {
cannam@148 628 static_assert(otherValue <= maxN, "overflow detected");
cannam@148 629 value = otherValue;
cannam@148 630 return *this;
cannam@148 631 }
cannam@148 632
cannam@148 633 inline constexpr T unwrap() const { return value; }
cannam@148 634
cannam@148 635 #define OP(op, newMax) \
cannam@148 636 template <uint64_t otherMax, typename otherT> \
cannam@148 637 inline constexpr Bounded<newMax, decltype(T() op otherT())> \
cannam@148 638 operator op(const Bounded<otherMax, otherT>& other) const { \
cannam@148 639 return Bounded<newMax, decltype(T() op otherT())>(value op other.value, unsafe); \
cannam@148 640 }
cannam@148 641 #define COMPARE_OP(op) \
cannam@148 642 template <uint64_t otherMax, typename OtherT> \
cannam@148 643 inline constexpr bool operator op(const Bounded<otherMax, OtherT>& other) const { \
cannam@148 644 return value op other.value; \
cannam@148 645 }
cannam@148 646
cannam@148 647 OP(+, (boundedAdd<maxN, otherMax>()))
cannam@148 648 OP(*, (boundedMul<maxN, otherMax>()))
cannam@148 649 OP(/, maxN)
cannam@148 650 OP(%, otherMax - 1)
cannam@148 651
cannam@148 652 // operator- is intentionally omitted because we mostly use this with unsigned types, and
cannam@148 653 // subtraction requires proof that subtrahend is not greater than the minuend.
cannam@148 654
cannam@148 655 COMPARE_OP(==)
cannam@148 656 COMPARE_OP(!=)
cannam@148 657 COMPARE_OP(< )
cannam@148 658 COMPARE_OP(> )
cannam@148 659 COMPARE_OP(<=)
cannam@148 660 COMPARE_OP(>=)
cannam@148 661
cannam@148 662 #undef OP
cannam@148 663 #undef COMPARE_OP
cannam@148 664
cannam@148 665 template <uint64_t newMax, typename ErrorFunc>
cannam@148 666 inline Bounded<newMax, T> assertMax(ErrorFunc&& func) const {
cannam@148 667 // Assert that the number is no more than `newMax`. Otherwise, call `func`.
cannam@148 668 static_assert(newMax < maxN, "this bounded size assertion is redundant");
cannam@148 669 if (KJ_UNLIKELY(value > newMax)) func();
cannam@148 670 return Bounded<newMax, T>(value, unsafe);
cannam@148 671 }
cannam@148 672
cannam@148 673 template <uint64_t otherMax, typename OtherT, typename ErrorFunc>
cannam@148 674 inline Bounded<maxN, decltype(T() - OtherT())> subtractChecked(
cannam@148 675 const Bounded<otherMax, OtherT>& other, ErrorFunc&& func) const {
cannam@148 676 // Subtract a number, calling func() if the result would underflow.
cannam@148 677 if (KJ_UNLIKELY(value < other.value)) func();
cannam@148 678 return Bounded<maxN, decltype(T() - OtherT())>(value - other.value, unsafe);
cannam@148 679 }
cannam@148 680
cannam@148 681 template <uint otherValue, typename ErrorFunc>
cannam@148 682 inline Bounded<maxN - otherValue, T> subtractChecked(
cannam@148 683 BoundedConst<otherValue>, ErrorFunc&& func) const {
cannam@148 684 // Subtract a number, calling func() if the result would underflow.
cannam@148 685 static_assert(otherValue <= maxN, "underflow detected");
cannam@148 686 if (KJ_UNLIKELY(value < otherValue)) func();
cannam@148 687 return Bounded<maxN - otherValue, T>(value - otherValue, unsafe);
cannam@148 688 }
cannam@148 689
cannam@148 690 template <uint64_t otherMax, typename OtherT>
cannam@148 691 inline Maybe<Bounded<maxN, decltype(T() - OtherT())>> trySubtract(
cannam@148 692 const Bounded<otherMax, OtherT>& other) const {
cannam@148 693 // Subtract a number, calling func() if the result would underflow.
cannam@148 694 if (value < other.value) {
cannam@148 695 return nullptr;
cannam@148 696 } else {
cannam@148 697 return Bounded<maxN, decltype(T() - OtherT())>(value - other.value, unsafe);
cannam@148 698 }
cannam@148 699 }
cannam@148 700
cannam@148 701 template <uint otherValue>
cannam@148 702 inline Maybe<Bounded<maxN - otherValue, T>> trySubtract(BoundedConst<otherValue>) const {
cannam@148 703 // Subtract a number, calling func() if the result would underflow.
cannam@148 704 if (value < otherValue) {
cannam@148 705 return nullptr;
cannam@148 706 } else {
cannam@148 707 return Bounded<maxN - otherValue, T>(value - otherValue, unsafe);
cannam@148 708 }
cannam@148 709 }
cannam@148 710
cannam@148 711 inline constexpr Bounded(T value, decltype(unsafe)): value(value) {}
cannam@148 712 template <uint64_t otherMax, typename OtherT>
cannam@148 713 inline constexpr Bounded(Bounded<otherMax, OtherT> value, decltype(unsafe))
cannam@148 714 : value(value.value) {}
cannam@148 715 // Mainly for internal use.
cannam@148 716 //
cannam@148 717 // Only use these as a last resort, with ample commentary on why you think it's safe.
cannam@148 718
cannam@148 719 private:
cannam@148 720 T value;
cannam@148 721
cannam@148 722 template <uint64_t, typename>
cannam@148 723 friend class Bounded;
cannam@148 724 };
cannam@148 725
cannam@148 726 template <typename Number>
cannam@148 727 inline constexpr Bounded<Number(kj::maxValue), Number> bounded(Number value) {
cannam@148 728 return Bounded<Number(kj::maxValue), Number>(value, unsafe);
cannam@148 729 }
cannam@148 730
cannam@148 731 inline constexpr Bounded<1, uint8_t> bounded(bool value) {
cannam@148 732 return Bounded<1, uint8_t>(value, unsafe);
cannam@148 733 }
cannam@148 734
cannam@148 735 template <uint bits, typename Number>
cannam@148 736 inline constexpr Bounded<maxValueForBits<bits>(), Number> assumeBits(Number value) {
cannam@148 737 return Bounded<maxValueForBits<bits>(), Number>(value, unsafe);
cannam@148 738 }
cannam@148 739
cannam@148 740 template <uint bits, uint64_t maxN, typename T>
cannam@148 741 inline constexpr Bounded<maxValueForBits<bits>(), T> assumeBits(Bounded<maxN, T> value) {
cannam@148 742 return Bounded<maxValueForBits<bits>(), T>(value, unsafe);
cannam@148 743 }
cannam@148 744
cannam@148 745 template <uint bits, typename Number, typename Unit>
cannam@148 746 inline constexpr auto assumeBits(Quantity<Number, Unit> value)
cannam@148 747 -> Quantity<decltype(assumeBits<bits>(value / unit<Quantity<Number, Unit>>())), Unit> {
cannam@148 748 return Quantity<decltype(assumeBits<bits>(value / unit<Quantity<Number, Unit>>())), Unit>(
cannam@148 749 assumeBits<bits>(value / unit<Quantity<Number, Unit>>()), unsafe);
cannam@148 750 }
cannam@148 751
cannam@148 752 template <uint64_t maxN, typename Number>
cannam@148 753 inline constexpr Bounded<maxN, Number> assumeMax(Number value) {
cannam@148 754 return Bounded<maxN, Number>(value, unsafe);
cannam@148 755 }
cannam@148 756
cannam@148 757 template <uint64_t newMaxN, uint64_t maxN, typename T>
cannam@148 758 inline constexpr Bounded<newMaxN, T> assumeMax(Bounded<maxN, T> value) {
cannam@148 759 return Bounded<newMaxN, T>(value, unsafe);
cannam@148 760 }
cannam@148 761
cannam@148 762 template <uint64_t maxN, typename Number, typename Unit>
cannam@148 763 inline constexpr auto assumeMax(Quantity<Number, Unit> value)
cannam@148 764 -> Quantity<decltype(assumeMax<maxN>(value / unit<Quantity<Number, Unit>>())), Unit> {
cannam@148 765 return Quantity<decltype(assumeMax<maxN>(value / unit<Quantity<Number, Unit>>())), Unit>(
cannam@148 766 assumeMax<maxN>(value / unit<Quantity<Number, Unit>>()), unsafe);
cannam@148 767 }
cannam@148 768
cannam@148 769 template <uint maxN, typename Number>
cannam@148 770 inline constexpr Bounded<maxN, Number> assumeMax(BoundedConst<maxN>, Number value) {
cannam@148 771 return assumeMax<maxN>(value);
cannam@148 772 }
cannam@148 773
cannam@148 774 template <uint newMaxN, uint64_t maxN, typename T>
cannam@148 775 inline constexpr Bounded<newMaxN, T> assumeMax(BoundedConst<maxN>, Bounded<maxN, T> value) {
cannam@148 776 return assumeMax<maxN>(value);
cannam@148 777 }
cannam@148 778
cannam@148 779 template <uint maxN, typename Number, typename Unit>
cannam@148 780 inline constexpr auto assumeMax(Quantity<BoundedConst<maxN>, Unit>, Quantity<Number, Unit> value)
cannam@148 781 -> decltype(assumeMax<maxN>(value)) {
cannam@148 782 return assumeMax<maxN>(value);
cannam@148 783 }
cannam@148 784
cannam@148 785 template <uint64_t newMax, uint64_t maxN, typename T, typename ErrorFunc>
cannam@148 786 inline Bounded<newMax, T> assertMax(Bounded<maxN, T> value, ErrorFunc&& errorFunc) {
cannam@148 787 // Assert that the bounded value is less than or equal to the given maximum, calling errorFunc()
cannam@148 788 // if not.
cannam@148 789 static_assert(newMax < maxN, "this bounded size assertion is redundant");
cannam@148 790 return value.template assertMax<newMax>(kj::fwd<ErrorFunc>(errorFunc));
cannam@148 791 }
cannam@148 792
cannam@148 793 template <uint64_t newMax, uint64_t maxN, typename T, typename Unit, typename ErrorFunc>
cannam@148 794 inline Quantity<Bounded<newMax, T>, Unit> assertMax(
cannam@148 795 Quantity<Bounded<maxN, T>, Unit> value, ErrorFunc&& errorFunc) {
cannam@148 796 // Assert that the bounded value is less than or equal to the given maximum, calling errorFunc()
cannam@148 797 // if not.
cannam@148 798 static_assert(newMax < maxN, "this bounded size assertion is redundant");
cannam@148 799 return (value / unit<decltype(value)>()).template assertMax<newMax>(
cannam@148 800 kj::fwd<ErrorFunc>(errorFunc)) * unit<decltype(value)>();
cannam@148 801 }
cannam@148 802
cannam@148 803 template <uint newMax, uint64_t maxN, typename T, typename ErrorFunc>
cannam@148 804 inline Bounded<newMax, T> assertMax(
cannam@148 805 BoundedConst<newMax>, Bounded<maxN, T> value, ErrorFunc&& errorFunc) {
cannam@148 806 return assertMax<newMax>(value, kj::mv(errorFunc));
cannam@148 807 }
cannam@148 808
cannam@148 809 template <uint newMax, uint64_t maxN, typename T, typename Unit, typename ErrorFunc>
cannam@148 810 inline Quantity<Bounded<newMax, T>, Unit> assertMax(
cannam@148 811 Quantity<BoundedConst<newMax>, Unit>,
cannam@148 812 Quantity<Bounded<maxN, T>, Unit> value, ErrorFunc&& errorFunc) {
cannam@148 813 return assertMax<newMax>(value, kj::mv(errorFunc));
cannam@148 814 }
cannam@148 815
cannam@148 816 template <uint64_t newBits, uint64_t maxN, typename T, typename ErrorFunc = ThrowOverflow>
cannam@148 817 inline Bounded<maxValueForBits<newBits>(), T> assertMaxBits(
cannam@148 818 Bounded<maxN, T> value, ErrorFunc&& errorFunc = ErrorFunc()) {
cannam@148 819 // Assert that the bounded value requires no more than the given number of bits, calling
cannam@148 820 // errorFunc() if not.
cannam@148 821 return assertMax<maxValueForBits<newBits>()>(value, kj::fwd<ErrorFunc>(errorFunc));
cannam@148 822 }
cannam@148 823
cannam@148 824 template <uint64_t newBits, uint64_t maxN, typename T, typename Unit,
cannam@148 825 typename ErrorFunc = ThrowOverflow>
cannam@148 826 inline Quantity<Bounded<maxValueForBits<newBits>(), T>, Unit> assertMaxBits(
cannam@148 827 Quantity<Bounded<maxN, T>, Unit> value, ErrorFunc&& errorFunc = ErrorFunc()) {
cannam@148 828 // Assert that the bounded value requires no more than the given number of bits, calling
cannam@148 829 // errorFunc() if not.
cannam@148 830 return assertMax<maxValueForBits<newBits>()>(value, kj::fwd<ErrorFunc>(errorFunc));
cannam@148 831 }
cannam@148 832
cannam@148 833 template <typename newT, uint64_t maxN, typename T>
cannam@148 834 inline constexpr Bounded<maxN, newT> upgradeBound(Bounded<maxN, T> value) {
cannam@148 835 return value;
cannam@148 836 }
cannam@148 837
cannam@148 838 template <typename newT, uint64_t maxN, typename T, typename Unit>
cannam@148 839 inline constexpr Quantity<Bounded<maxN, newT>, Unit> upgradeBound(
cannam@148 840 Quantity<Bounded<maxN, T>, Unit> value) {
cannam@148 841 return value;
cannam@148 842 }
cannam@148 843
cannam@148 844 template <uint64_t maxN, typename T, typename Other, typename ErrorFunc>
cannam@148 845 inline auto subtractChecked(Bounded<maxN, T> value, Other other, ErrorFunc&& errorFunc)
cannam@148 846 -> decltype(value.subtractChecked(other, kj::fwd<ErrorFunc>(errorFunc))) {
cannam@148 847 return value.subtractChecked(other, kj::fwd<ErrorFunc>(errorFunc));
cannam@148 848 }
cannam@148 849
cannam@148 850 template <typename T, typename U, typename Unit, typename ErrorFunc>
cannam@148 851 inline auto subtractChecked(Quantity<T, Unit> value, Quantity<U, Unit> other, ErrorFunc&& errorFunc)
cannam@148 852 -> Quantity<decltype(subtractChecked(T(), U(), kj::fwd<ErrorFunc>(errorFunc))), Unit> {
cannam@148 853 return subtractChecked(value / unit<Quantity<T, Unit>>(),
cannam@148 854 other / unit<Quantity<U, Unit>>(),
cannam@148 855 kj::fwd<ErrorFunc>(errorFunc))
cannam@148 856 * unit<Quantity<T, Unit>>();
cannam@148 857 }
cannam@148 858
cannam@148 859 template <uint64_t maxN, typename T, typename Other>
cannam@148 860 inline auto trySubtract(Bounded<maxN, T> value, Other other)
cannam@148 861 -> decltype(value.trySubtract(other)) {
cannam@148 862 return value.trySubtract(other);
cannam@148 863 }
cannam@148 864
cannam@148 865 template <typename T, typename U, typename Unit>
cannam@148 866 inline auto trySubtract(Quantity<T, Unit> value, Quantity<U, Unit> other)
cannam@148 867 -> Maybe<Quantity<decltype(subtractChecked(T(), U(), int())), Unit>> {
cannam@148 868 return trySubtract(value / unit<Quantity<T, Unit>>(),
cannam@148 869 other / unit<Quantity<U, Unit>>())
cannam@148 870 .map([](decltype(subtractChecked(T(), U(), int())) x) {
cannam@148 871 return x * unit<Quantity<T, Unit>>();
cannam@148 872 });
cannam@148 873 }
cannam@148 874
cannam@148 875 template <uint64_t aN, uint64_t bN, typename A, typename B>
cannam@148 876 inline constexpr Bounded<kj::min(aN, bN), WiderType<A, B>>
cannam@148 877 min(Bounded<aN, A> a, Bounded<bN, B> b) {
cannam@148 878 return Bounded<kj::min(aN, bN), WiderType<A, B>>(kj::min(a.unwrap(), b.unwrap()), unsafe);
cannam@148 879 }
cannam@148 880 template <uint64_t aN, uint64_t bN, typename A, typename B>
cannam@148 881 inline constexpr Bounded<kj::max(aN, bN), WiderType<A, B>>
cannam@148 882 max(Bounded<aN, A> a, Bounded<bN, B> b) {
cannam@148 883 return Bounded<kj::max(aN, bN), WiderType<A, B>>(kj::max(a.unwrap(), b.unwrap()), unsafe);
cannam@148 884 }
cannam@148 885 // We need to override min() and max() because:
cannam@148 886 // 1) WiderType<> might not choose the correct bounds.
cannam@148 887 // 2) One of the two sides of the ternary operator in the default implementation would fail to
cannam@148 888 // typecheck even though it is OK in practice.
cannam@148 889
cannam@148 890 // -------------------------------------------------------------------
cannam@148 891 // Operators between Bounded and BoundedConst
cannam@148 892
cannam@148 893 #define OP(op, newMax) \
cannam@148 894 template <uint64_t maxN, uint cvalue, typename T> \
cannam@148 895 inline constexpr Bounded<(newMax), decltype(T() op uint())> operator op( \
cannam@148 896 Bounded<maxN, T> value, BoundedConst<cvalue>) { \
cannam@148 897 return Bounded<(newMax), decltype(T() op uint())>(value.unwrap() op cvalue, unsafe); \
cannam@148 898 }
cannam@148 899
cannam@148 900 #define REVERSE_OP(op, newMax) \
cannam@148 901 template <uint64_t maxN, uint cvalue, typename T> \
cannam@148 902 inline constexpr Bounded<(newMax), decltype(uint() op T())> operator op( \
cannam@148 903 BoundedConst<cvalue>, Bounded<maxN, T> value) { \
cannam@148 904 return Bounded<(newMax), decltype(uint() op T())>(cvalue op value.unwrap(), unsafe); \
cannam@148 905 }
cannam@148 906
cannam@148 907 #define COMPARE_OP(op) \
cannam@148 908 template <uint64_t maxN, uint cvalue, typename T> \
cannam@148 909 inline constexpr bool operator op(Bounded<maxN, T> value, BoundedConst<cvalue>) { \
cannam@148 910 return value.unwrap() op cvalue; \
cannam@148 911 } \
cannam@148 912 template <uint64_t maxN, uint cvalue, typename T> \
cannam@148 913 inline constexpr bool operator op(BoundedConst<cvalue>, Bounded<maxN, T> value) { \
cannam@148 914 return cvalue op value.unwrap(); \
cannam@148 915 }
cannam@148 916
cannam@148 917 OP(+, (boundedAdd<maxN, cvalue>()))
cannam@148 918 REVERSE_OP(+, (boundedAdd<maxN, cvalue>()))
cannam@148 919
cannam@148 920 OP(*, (boundedMul<maxN, cvalue>()))
cannam@148 921 REVERSE_OP(*, (boundedAdd<maxN, cvalue>()))
cannam@148 922
cannam@148 923 OP(/, maxN / cvalue)
cannam@148 924 REVERSE_OP(/, cvalue) // denominator could be 1
cannam@148 925
cannam@148 926 OP(%, cvalue - 1)
cannam@148 927 REVERSE_OP(%, maxN - 1)
cannam@148 928
cannam@148 929 OP(<<, (boundedLShift<maxN, cvalue>()))
cannam@148 930 REVERSE_OP(<<, (boundedLShift<cvalue, maxN>()))
cannam@148 931
cannam@148 932 OP(>>, maxN >> cvalue)
cannam@148 933 REVERSE_OP(>>, cvalue >> maxN)
cannam@148 934
cannam@148 935 OP(&, maxValueForBits<bitCount<maxN>()>() & cvalue)
cannam@148 936 REVERSE_OP(&, maxValueForBits<bitCount<maxN>()>() & cvalue)
cannam@148 937
cannam@148 938 OP(|, maxN | cvalue)
cannam@148 939 REVERSE_OP(|, maxN | cvalue)
cannam@148 940
cannam@148 941 COMPARE_OP(==)
cannam@148 942 COMPARE_OP(!=)
cannam@148 943 COMPARE_OP(< )
cannam@148 944 COMPARE_OP(> )
cannam@148 945 COMPARE_OP(<=)
cannam@148 946 COMPARE_OP(>=)
cannam@148 947
cannam@148 948 #undef OP
cannam@148 949 #undef REVERSE_OP
cannam@148 950 #undef COMPARE_OP
cannam@148 951
cannam@148 952 template <uint64_t maxN, uint cvalue, typename T>
cannam@148 953 inline constexpr Bounded<cvalue, decltype(uint() - T())>
cannam@148 954 operator-(BoundedConst<cvalue>, Bounded<maxN, T> value) {
cannam@148 955 // We allow subtraction of a variable from a constant only if the constant is greater than or
cannam@148 956 // equal to the maximum possible value of the variable. Since the variable could be zero, the
cannam@148 957 // result can be as large as the constant.
cannam@148 958 //
cannam@148 959 // We do not allow subtraction of a constant from a variable because there's never a guarantee it
cannam@148 960 // won't underflow (unless the constant is zero, which is silly).
cannam@148 961 static_assert(cvalue >= maxN, "possible underflow detected");
cannam@148 962 return Bounded<cvalue, decltype(uint() - T())>(cvalue - value.unwrap(), unsafe);
cannam@148 963 }
cannam@148 964
cannam@148 965 template <uint64_t aN, uint b, typename A>
cannam@148 966 inline constexpr Bounded<kj::min(aN, b), A> min(Bounded<aN, A> a, BoundedConst<b>) {
cannam@148 967 return Bounded<kj::min(aN, b), A>(kj::min(b, a.unwrap()), unsafe);
cannam@148 968 }
cannam@148 969 template <uint64_t aN, uint b, typename A>
cannam@148 970 inline constexpr Bounded<kj::min(aN, b), A> min(BoundedConst<b>, Bounded<aN, A> a) {
cannam@148 971 return Bounded<kj::min(aN, b), A>(kj::min(a.unwrap(), b), unsafe);
cannam@148 972 }
cannam@148 973 template <uint64_t aN, uint b, typename A>
cannam@148 974 inline constexpr Bounded<kj::max(aN, b), A> max(Bounded<aN, A> a, BoundedConst<b>) {
cannam@148 975 return Bounded<kj::max(aN, b), A>(kj::max(b, a.unwrap()), unsafe);
cannam@148 976 }
cannam@148 977 template <uint64_t aN, uint b, typename A>
cannam@148 978 inline constexpr Bounded<kj::max(aN, b), A> max(BoundedConst<b>, Bounded<aN, A> a) {
cannam@148 979 return Bounded<kj::max(aN, b), A>(kj::max(a.unwrap(), b), unsafe);
cannam@148 980 }
cannam@148 981 // We need to override min() between a Bounded and a constant since:
cannam@148 982 // 1) WiderType<> might choose BoundedConst over a 1-byte Bounded, which is wrong.
cannam@148 983 // 2) To clamp the bounds of the output type.
cannam@148 984 // 3) Same ternary operator typechecking issues.
cannam@148 985
cannam@148 986 // -------------------------------------------------------------------
cannam@148 987
cannam@148 988 template <uint64_t maxN, typename T>
cannam@148 989 class SafeUnwrapper {
cannam@148 990 public:
cannam@148 991 inline explicit constexpr SafeUnwrapper(Bounded<maxN, T> value): value(value.unwrap()) {}
cannam@148 992
cannam@148 993 template <typename U, typename = EnableIf<isIntegral<U>()>>
cannam@148 994 inline constexpr operator U() const {
cannam@148 995 static_assert(maxN <= U(maxValue), "possible truncation detected");
cannam@148 996 return value;
cannam@148 997 }
cannam@148 998
cannam@148 999 inline constexpr operator bool() const {
cannam@148 1000 static_assert(maxN <= 1, "possible truncation detected");
cannam@148 1001 return value;
cannam@148 1002 }
cannam@148 1003
cannam@148 1004 private:
cannam@148 1005 T value;
cannam@148 1006 };
cannam@148 1007
cannam@148 1008 template <uint64_t maxN, typename T>
cannam@148 1009 inline constexpr SafeUnwrapper<maxN, T> unbound(Bounded<maxN, T> bounded) {
cannam@148 1010 // Unwraps the bounded value, returning a value that can be implicitly cast to any integer type.
cannam@148 1011 // If this implicit cast could truncate, a compile-time error will be raised.
cannam@148 1012 return SafeUnwrapper<maxN, T>(bounded);
cannam@148 1013 }
cannam@148 1014
cannam@148 1015 template <uint64_t value>
cannam@148 1016 class SafeConstUnwrapper {
cannam@148 1017 public:
cannam@148 1018 template <typename T, typename = EnableIf<isIntegral<T>()>>
cannam@148 1019 inline constexpr operator T() const {
cannam@148 1020 static_assert(value <= T(maxValue), "this operation will truncate");
cannam@148 1021 return value;
cannam@148 1022 }
cannam@148 1023
cannam@148 1024 inline constexpr operator bool() const {
cannam@148 1025 static_assert(value <= 1, "this operation will truncate");
cannam@148 1026 return value;
cannam@148 1027 }
cannam@148 1028 };
cannam@148 1029
cannam@148 1030 template <uint value>
cannam@148 1031 inline constexpr SafeConstUnwrapper<value> unbound(BoundedConst<value>) {
cannam@148 1032 return SafeConstUnwrapper<value>();
cannam@148 1033 }
cannam@148 1034
cannam@148 1035 template <typename T, typename U>
cannam@148 1036 inline constexpr T unboundAs(U value) {
cannam@148 1037 return unbound(value);
cannam@148 1038 }
cannam@148 1039
cannam@148 1040 template <uint64_t requestedMax, uint64_t maxN, typename T>
cannam@148 1041 inline constexpr T unboundMax(Bounded<maxN, T> value) {
cannam@148 1042 // Explicitly ungaurd expecting a value that is at most `maxN`.
cannam@148 1043 static_assert(maxN <= requestedMax, "possible overflow detected");
cannam@148 1044 return value.unwrap();
cannam@148 1045 }
cannam@148 1046
cannam@148 1047 template <uint64_t requestedMax, uint value>
cannam@148 1048 inline constexpr uint unboundMax(BoundedConst<value>) {
cannam@148 1049 // Explicitly ungaurd expecting a value that is at most `maxN`.
cannam@148 1050 static_assert(value <= requestedMax, "overflow detected");
cannam@148 1051 return value;
cannam@148 1052 }
cannam@148 1053
cannam@148 1054 template <uint bits, typename T>
cannam@148 1055 inline constexpr auto unboundMaxBits(T value) ->
cannam@148 1056 decltype(unboundMax<maxValueForBits<bits>()>(value)) {
cannam@148 1057 // Explicitly ungaurd expecting a value that fits into `bits` bits.
cannam@148 1058 return unboundMax<maxValueForBits<bits>()>(value);
cannam@148 1059 }
cannam@148 1060
cannam@148 1061 #define OP(op) \
cannam@148 1062 template <uint64_t maxN, typename T, typename U> \
cannam@148 1063 inline constexpr auto operator op(T a, SafeUnwrapper<maxN, U> b) -> decltype(a op (T)b) { \
cannam@148 1064 return a op (AtLeastUInt<sizeof(T)*8>)b; \
cannam@148 1065 } \
cannam@148 1066 template <uint64_t maxN, typename T, typename U> \
cannam@148 1067 inline constexpr auto operator op(SafeUnwrapper<maxN, U> b, T a) -> decltype((T)b op a) { \
cannam@148 1068 return (AtLeastUInt<sizeof(T)*8>)b op a; \
cannam@148 1069 } \
cannam@148 1070 template <uint64_t value, typename T> \
cannam@148 1071 inline constexpr auto operator op(T a, SafeConstUnwrapper<value> b) -> decltype(a op (T)b) { \
cannam@148 1072 return a op (AtLeastUInt<sizeof(T)*8>)b; \
cannam@148 1073 } \
cannam@148 1074 template <uint64_t value, typename T> \
cannam@148 1075 inline constexpr auto operator op(SafeConstUnwrapper<value> b, T a) -> decltype((T)b op a) { \
cannam@148 1076 return (AtLeastUInt<sizeof(T)*8>)b op a; \
cannam@148 1077 }
cannam@148 1078
cannam@148 1079 OP(+)
cannam@148 1080 OP(-)
cannam@148 1081 OP(*)
cannam@148 1082 OP(/)
cannam@148 1083 OP(%)
cannam@148 1084 OP(<<)
cannam@148 1085 OP(>>)
cannam@148 1086 OP(&)
cannam@148 1087 OP(|)
cannam@148 1088 OP(==)
cannam@148 1089 OP(!=)
cannam@148 1090 OP(<=)
cannam@148 1091 OP(>=)
cannam@148 1092 OP(<)
cannam@148 1093 OP(>)
cannam@148 1094
cannam@148 1095 #undef OP
cannam@148 1096
cannam@148 1097 // -------------------------------------------------------------------
cannam@148 1098
cannam@148 1099 template <uint64_t maxN, typename T>
cannam@148 1100 class Range<Bounded<maxN, T>> {
cannam@148 1101 public:
cannam@148 1102 inline constexpr Range(Bounded<maxN, T> begin, Bounded<maxN, T> end)
cannam@148 1103 : inner(unbound(begin), unbound(end)) {}
cannam@148 1104 inline explicit constexpr Range(Bounded<maxN, T> end)
cannam@148 1105 : inner(unbound(end)) {}
cannam@148 1106
cannam@148 1107 class Iterator {
cannam@148 1108 public:
cannam@148 1109 Iterator() = default;
cannam@148 1110 inline explicit Iterator(typename Range<T>::Iterator inner): inner(inner) {}
cannam@148 1111
cannam@148 1112 inline Bounded<maxN, T> operator* () const { return Bounded<maxN, T>(*inner, unsafe); }
cannam@148 1113 inline Iterator& operator++() { ++inner; return *this; }
cannam@148 1114
cannam@148 1115 inline bool operator==(const Iterator& other) const { return inner == other.inner; }
cannam@148 1116 inline bool operator!=(const Iterator& other) const { return inner != other.inner; }
cannam@148 1117
cannam@148 1118 private:
cannam@148 1119 typename Range<T>::Iterator inner;
cannam@148 1120 };
cannam@148 1121
cannam@148 1122 inline Iterator begin() const { return Iterator(inner.begin()); }
cannam@148 1123 inline Iterator end() const { return Iterator(inner.end()); }
cannam@148 1124
cannam@148 1125 private:
cannam@148 1126 Range<T> inner;
cannam@148 1127 };
cannam@148 1128
cannam@148 1129 template <typename T, typename U>
cannam@148 1130 class Range<Quantity<T, U>> {
cannam@148 1131 public:
cannam@148 1132 inline constexpr Range(Quantity<T, U> begin, Quantity<T, U> end)
cannam@148 1133 : inner(begin / unit<Quantity<T, U>>(), end / unit<Quantity<T, U>>()) {}
cannam@148 1134 inline explicit constexpr Range(Quantity<T, U> end)
cannam@148 1135 : inner(end / unit<Quantity<T, U>>()) {}
cannam@148 1136
cannam@148 1137 class Iterator {
cannam@148 1138 public:
cannam@148 1139 Iterator() = default;
cannam@148 1140 inline explicit Iterator(typename Range<T>::Iterator inner): inner(inner) {}
cannam@148 1141
cannam@148 1142 inline Quantity<T, U> operator* () const { return *inner * unit<Quantity<T, U>>(); }
cannam@148 1143 inline Iterator& operator++() { ++inner; return *this; }
cannam@148 1144
cannam@148 1145 inline bool operator==(const Iterator& other) const { return inner == other.inner; }
cannam@148 1146 inline bool operator!=(const Iterator& other) const { return inner != other.inner; }
cannam@148 1147
cannam@148 1148 private:
cannam@148 1149 typename Range<T>::Iterator inner;
cannam@148 1150 };
cannam@148 1151
cannam@148 1152 inline Iterator begin() const { return Iterator(inner.begin()); }
cannam@148 1153 inline Iterator end() const { return Iterator(inner.end()); }
cannam@148 1154
cannam@148 1155 private:
cannam@148 1156 Range<T> inner;
cannam@148 1157 };
cannam@148 1158
cannam@148 1159 template <uint value>
cannam@148 1160 inline constexpr Range<Bounded<value, uint>> zeroTo(BoundedConst<value> end) {
cannam@148 1161 return Range<Bounded<value, uint>>(end);
cannam@148 1162 }
cannam@148 1163
cannam@148 1164 template <uint value, typename Unit>
cannam@148 1165 inline constexpr Range<Quantity<Bounded<value, uint>, Unit>>
cannam@148 1166 zeroTo(Quantity<BoundedConst<value>, Unit> end) {
cannam@148 1167 return Range<Quantity<Bounded<value, uint>, Unit>>(end);
cannam@148 1168 }
cannam@148 1169
cannam@148 1170 } // namespace kj
cannam@148 1171
cannam@148 1172 #endif // KJ_UNITS_H_