annotate win32-mingw/include/kj/units.h @ 60:95867ba8caa8

Update PortAudio build for Win64
author Chris Cannam
date Tue, 10 Jan 2017 11:28:20 +0000
parents 37d53a7e8262
children eccd51b72864
rev   line source
Chris@50 1 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
Chris@50 2 // Licensed under the MIT License:
Chris@50 3 //
Chris@50 4 // Permission is hereby granted, free of charge, to any person obtaining a copy
Chris@50 5 // of this software and associated documentation files (the "Software"), to deal
Chris@50 6 // in the Software without restriction, including without limitation the rights
Chris@50 7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
Chris@50 8 // copies of the Software, and to permit persons to whom the Software is
Chris@50 9 // furnished to do so, subject to the following conditions:
Chris@50 10 //
Chris@50 11 // The above copyright notice and this permission notice shall be included in
Chris@50 12 // all copies or substantial portions of the Software.
Chris@50 13 //
Chris@50 14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Chris@50 15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Chris@50 16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Chris@50 17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Chris@50 18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Chris@50 19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
Chris@50 20 // THE SOFTWARE.
Chris@50 21
Chris@50 22 // This file contains types which are intended to help detect incorrect usage at compile
Chris@50 23 // time, but should then be optimized down to basic primitives (usually, integers) by the
Chris@50 24 // compiler.
Chris@50 25
Chris@50 26 #ifndef KJ_UNITS_H_
Chris@50 27 #define KJ_UNITS_H_
Chris@50 28
Chris@50 29 #if defined(__GNUC__) && !KJ_HEADER_WARNINGS
Chris@50 30 #pragma GCC system_header
Chris@50 31 #endif
Chris@50 32
Chris@50 33 #include "common.h"
Chris@50 34
Chris@50 35 namespace kj {
Chris@50 36
Chris@50 37 // =======================================================================================
Chris@50 38 // IDs
Chris@50 39
Chris@50 40 template <typename UnderlyingType, typename Label>
Chris@50 41 struct Id {
Chris@50 42 // A type-safe numeric ID. `UnderlyingType` is the underlying integer representation. `Label`
Chris@50 43 // distinguishes this Id from other Id types. Sample usage:
Chris@50 44 //
Chris@50 45 // class Foo;
Chris@50 46 // typedef Id<uint, Foo> FooId;
Chris@50 47 //
Chris@50 48 // class Bar;
Chris@50 49 // typedef Id<uint, Bar> BarId;
Chris@50 50 //
Chris@50 51 // You can now use the FooId and BarId types without any possibility of accidentally using a
Chris@50 52 // FooId when you really wanted a BarId or vice-versa.
Chris@50 53
Chris@50 54 UnderlyingType value;
Chris@50 55
Chris@50 56 inline constexpr Id(): value(0) {}
Chris@50 57 inline constexpr explicit Id(int value): value(value) {}
Chris@50 58
Chris@50 59 inline constexpr bool operator==(const Id& other) const { return value == other.value; }
Chris@50 60 inline constexpr bool operator!=(const Id& other) const { return value != other.value; }
Chris@50 61 inline constexpr bool operator<=(const Id& other) const { return value <= other.value; }
Chris@50 62 inline constexpr bool operator>=(const Id& other) const { return value >= other.value; }
Chris@50 63 inline constexpr bool operator< (const Id& other) const { return value < other.value; }
Chris@50 64 inline constexpr bool operator> (const Id& other) const { return value > other.value; }
Chris@50 65 };
Chris@50 66
Chris@50 67 // =======================================================================================
Chris@50 68 // Quantity and UnitRatio -- implement unit analysis via the type system
Chris@50 69
Chris@50 70 template <typename T> constexpr bool isIntegral() { return false; }
Chris@50 71 template <> constexpr bool isIntegral<char>() { return true; }
Chris@50 72 template <> constexpr bool isIntegral<signed char>() { return true; }
Chris@50 73 template <> constexpr bool isIntegral<short>() { return true; }
Chris@50 74 template <> constexpr bool isIntegral<int>() { return true; }
Chris@50 75 template <> constexpr bool isIntegral<long>() { return true; }
Chris@50 76 template <> constexpr bool isIntegral<long long>() { return true; }
Chris@50 77 template <> constexpr bool isIntegral<unsigned char>() { return true; }
Chris@50 78 template <> constexpr bool isIntegral<unsigned short>() { return true; }
Chris@50 79 template <> constexpr bool isIntegral<unsigned int>() { return true; }
Chris@50 80 template <> constexpr bool isIntegral<unsigned long>() { return true; }
Chris@50 81 template <> constexpr bool isIntegral<unsigned long long>() { return true; }
Chris@50 82
Chris@50 83 template <typename Number, typename Unit1, typename Unit2>
Chris@50 84 class UnitRatio {
Chris@50 85 // A multiplier used to convert Quantities of one unit to Quantities of another unit. See
Chris@50 86 // Quantity, below.
Chris@50 87 //
Chris@50 88 // Construct this type by dividing one Quantity by another of a different unit. Use this type
Chris@50 89 // by multiplying it by a Quantity, or dividing a Quantity by it.
Chris@50 90
Chris@50 91 static_assert(isIntegral<Number>(), "Underlying type for UnitRatio must be integer.");
Chris@50 92
Chris@50 93 public:
Chris@50 94 inline UnitRatio() {}
Chris@50 95
Chris@50 96 constexpr explicit UnitRatio(Number unit1PerUnit2): unit1PerUnit2(unit1PerUnit2) {}
Chris@50 97 // This constructor was intended to be private, but GCC complains about it being private in a
Chris@50 98 // bunch of places that don't appear to even call it, so I made it public. Oh well.
Chris@50 99
Chris@50 100 template <typename OtherNumber>
Chris@50 101 inline constexpr UnitRatio(const UnitRatio<OtherNumber, Unit1, Unit2>& other)
Chris@50 102 : unit1PerUnit2(other.unit1PerUnit2) {}
Chris@50 103
Chris@50 104 template <typename OtherNumber>
Chris@50 105 inline constexpr UnitRatio<decltype(Number(1)+OtherNumber(1)), Unit1, Unit2>
Chris@50 106 operator+(UnitRatio<OtherNumber, Unit1, Unit2> other) const {
Chris@50 107 return UnitRatio<decltype(Number(1)+OtherNumber(1)), Unit1, Unit2>(
Chris@50 108 unit1PerUnit2 + other.unit1PerUnit2);
Chris@50 109 }
Chris@50 110 template <typename OtherNumber>
Chris@50 111 inline constexpr UnitRatio<decltype(Number(1)-OtherNumber(1)), Unit1, Unit2>
Chris@50 112 operator-(UnitRatio<OtherNumber, Unit1, Unit2> other) const {
Chris@50 113 return UnitRatio<decltype(Number(1)-OtherNumber(1)), Unit1, Unit2>(
Chris@50 114 unit1PerUnit2 - other.unit1PerUnit2);
Chris@50 115 }
Chris@50 116
Chris@50 117 template <typename OtherNumber, typename Unit3>
Chris@50 118 inline constexpr UnitRatio<decltype(Number(1)*OtherNumber(1)), Unit3, Unit2>
Chris@50 119 operator*(UnitRatio<OtherNumber, Unit3, Unit1> other) const {
Chris@50 120 // U1 / U2 * U3 / U1 = U3 / U2
Chris@50 121 return UnitRatio<decltype(Number(1)*OtherNumber(1)), Unit3, Unit2>(
Chris@50 122 unit1PerUnit2 * other.unit1PerUnit2);
Chris@50 123 }
Chris@50 124 template <typename OtherNumber, typename Unit3>
Chris@50 125 inline constexpr UnitRatio<decltype(Number(1)*OtherNumber(1)), Unit1, Unit3>
Chris@50 126 operator*(UnitRatio<OtherNumber, Unit2, Unit3> other) const {
Chris@50 127 // U1 / U2 * U2 / U3 = U1 / U3
Chris@50 128 return UnitRatio<decltype(Number(1)*OtherNumber(1)), Unit1, Unit3>(
Chris@50 129 unit1PerUnit2 * other.unit1PerUnit2);
Chris@50 130 }
Chris@50 131
Chris@50 132 template <typename OtherNumber, typename Unit3>
Chris@50 133 inline constexpr UnitRatio<decltype(Number(1)*OtherNumber(1)), Unit3, Unit2>
Chris@50 134 operator/(UnitRatio<OtherNumber, Unit1, Unit3> other) const {
Chris@50 135 // (U1 / U2) / (U1 / U3) = U3 / U2
Chris@50 136 return UnitRatio<decltype(Number(1)*OtherNumber(1)), Unit3, Unit2>(
Chris@50 137 unit1PerUnit2 / other.unit1PerUnit2);
Chris@50 138 }
Chris@50 139 template <typename OtherNumber, typename Unit3>
Chris@50 140 inline constexpr UnitRatio<decltype(Number(1)*OtherNumber(1)), Unit1, Unit3>
Chris@50 141 operator/(UnitRatio<OtherNumber, Unit3, Unit2> other) const {
Chris@50 142 // (U1 / U2) / (U3 / U2) = U1 / U3
Chris@50 143 return UnitRatio<decltype(Number(1)*OtherNumber(1)), Unit1, Unit3>(
Chris@50 144 unit1PerUnit2 / other.unit1PerUnit2);
Chris@50 145 }
Chris@50 146
Chris@50 147 template <typename OtherNumber>
Chris@50 148 inline decltype(Number(1) / OtherNumber(1))
Chris@50 149 operator/(UnitRatio<OtherNumber, Unit1, Unit2> other) const {
Chris@50 150 return unit1PerUnit2 / other.unit1PerUnit2;
Chris@50 151 }
Chris@50 152
Chris@50 153 inline bool operator==(UnitRatio other) const { return unit1PerUnit2 == other.unit1PerUnit2; }
Chris@50 154 inline bool operator!=(UnitRatio other) const { return unit1PerUnit2 != other.unit1PerUnit2; }
Chris@50 155
Chris@50 156 private:
Chris@50 157 Number unit1PerUnit2;
Chris@50 158
Chris@50 159 template <typename OtherNumber, typename OtherUnit>
Chris@50 160 friend class Quantity;
Chris@50 161 template <typename OtherNumber, typename OtherUnit1, typename OtherUnit2>
Chris@50 162 friend class UnitRatio;
Chris@50 163
Chris@50 164 template <typename N1, typename N2, typename U1, typename U2>
Chris@50 165 friend inline constexpr UnitRatio<decltype(N1(1) * N2(1)), U1, U2>
Chris@50 166 operator*(N1, UnitRatio<N2, U1, U2>);
Chris@50 167 };
Chris@50 168
Chris@50 169 template <typename N1, typename N2, typename U1, typename U2>
Chris@50 170 inline constexpr UnitRatio<decltype(N1(1) * N2(1)), U1, U2>
Chris@50 171 operator*(N1 n, UnitRatio<N2, U1, U2> r) {
Chris@50 172 return UnitRatio<decltype(N1(1) * N2(1)), U1, U2>(n * r.unit1PerUnit2);
Chris@50 173 }
Chris@50 174
Chris@50 175 template <typename Number, typename Unit>
Chris@50 176 class Quantity {
Chris@50 177 // A type-safe numeric quantity, specified in terms of some unit. Two Quantities cannot be used
Chris@50 178 // in arithmetic unless they use the same unit. The `Unit` type parameter is only used to prevent
Chris@50 179 // accidental mixing of units; this type is never instantiated and can very well be incomplete.
Chris@50 180 // `Number` is the underlying primitive numeric type.
Chris@50 181 //
Chris@50 182 // Quantities support most basic arithmetic operators, intelligently handling units, and
Chris@50 183 // automatically casting the underlying type in the same way that the compiler would.
Chris@50 184 //
Chris@50 185 // To convert a primitive number to a Quantity, multiply it by unit<Quantity<N, U>>().
Chris@50 186 // To convert a Quantity to a primitive number, divide it by unit<Quantity<N, U>>().
Chris@50 187 // To convert a Quantity of one unit to another unit, multiply or divide by a UnitRatio.
Chris@50 188 //
Chris@50 189 // The Quantity class is not well-suited to hardcore physics as it does not allow multiplying
Chris@50 190 // one quantity by another. For example, multiplying meters by meters won't get you square
Chris@50 191 // meters; it will get you a compiler error. It would be interesting to see if template
Chris@50 192 // metaprogramming could properly deal with such things but this isn't needed for the present
Chris@50 193 // use case.
Chris@50 194 //
Chris@50 195 // Sample usage:
Chris@50 196 //
Chris@50 197 // class SecondsLabel;
Chris@50 198 // typedef Quantity<double, SecondsLabel> Seconds;
Chris@50 199 // constexpr Seconds SECONDS = unit<Seconds>();
Chris@50 200 //
Chris@50 201 // class MinutesLabel;
Chris@50 202 // typedef Quantity<double, MinutesLabel> Minutes;
Chris@50 203 // constexpr Minutes MINUTES = unit<Minutes>();
Chris@50 204 //
Chris@50 205 // constexpr UnitRatio<double, SecondsLabel, MinutesLabel> SECONDS_PER_MINUTE =
Chris@50 206 // 60 * SECONDS / MINUTES;
Chris@50 207 //
Chris@50 208 // void waitFor(Seconds seconds) {
Chris@50 209 // sleep(seconds / SECONDS);
Chris@50 210 // }
Chris@50 211 // void waitFor(Minutes minutes) {
Chris@50 212 // waitFor(minutes * SECONDS_PER_MINUTE);
Chris@50 213 // }
Chris@50 214 //
Chris@50 215 // void waitThreeMinutes() {
Chris@50 216 // waitFor(3 * MINUTES);
Chris@50 217 // }
Chris@50 218
Chris@50 219 static_assert(isIntegral<Number>(), "Underlying type for Quantity must be integer.");
Chris@50 220
Chris@50 221 public:
Chris@50 222 inline constexpr Quantity() {}
Chris@50 223
Chris@50 224 inline constexpr Quantity(MaxValue_): value(maxValue) {}
Chris@50 225 inline constexpr Quantity(MinValue_): value(minValue) {}
Chris@50 226 // Allow initialization from maxValue and minValue.
Chris@50 227 // TODO(msvc): decltype(maxValue) and decltype(minValue) deduce unknown-type for these function
Chris@50 228 // parameters, causing the compiler to complain of a duplicate constructor definition, so we
Chris@50 229 // specify MaxValue_ and MinValue_ types explicitly.
Chris@50 230
Chris@50 231 inline explicit constexpr Quantity(Number value): value(value) {}
Chris@50 232 // This constructor was intended to be private, but GCC complains about it being private in a
Chris@50 233 // bunch of places that don't appear to even call it, so I made it public. Oh well.
Chris@50 234
Chris@50 235 template <typename OtherNumber>
Chris@50 236 inline constexpr Quantity(const Quantity<OtherNumber, Unit>& other)
Chris@50 237 : value(other.value) {}
Chris@50 238
Chris@50 239 template <typename OtherNumber>
Chris@50 240 inline constexpr Quantity<decltype(Number(1) + OtherNumber(1)), Unit>
Chris@50 241 operator+(const Quantity<OtherNumber, Unit>& other) const {
Chris@50 242 return Quantity<decltype(Number(1) + OtherNumber(1)), Unit>(value + other.value);
Chris@50 243 }
Chris@50 244 template <typename OtherNumber>
Chris@50 245 inline constexpr Quantity<decltype(Number(1) - OtherNumber(1)), Unit>
Chris@50 246 operator-(const Quantity<OtherNumber, Unit>& other) const {
Chris@50 247 return Quantity<decltype(Number(1) - OtherNumber(1)), Unit>(value - other.value);
Chris@50 248 }
Chris@50 249 template <typename OtherNumber>
Chris@50 250 inline constexpr Quantity<decltype(Number(1) * OtherNumber(1)), Unit>
Chris@50 251 operator*(OtherNumber other) const {
Chris@50 252 static_assert(isIntegral<OtherNumber>(), "Multiplied Quantity by non-integer.");
Chris@50 253 return Quantity<decltype(Number(1) * other), Unit>(value * other);
Chris@50 254 }
Chris@50 255 template <typename OtherNumber>
Chris@50 256 inline constexpr Quantity<decltype(Number(1) / OtherNumber(1)), Unit>
Chris@50 257 operator/(OtherNumber other) const {
Chris@50 258 static_assert(isIntegral<OtherNumber>(), "Divided Quantity by non-integer.");
Chris@50 259 return Quantity<decltype(Number(1) / other), Unit>(value / other);
Chris@50 260 }
Chris@50 261 template <typename OtherNumber>
Chris@50 262 inline constexpr decltype(Number(1) / OtherNumber(1))
Chris@50 263 operator/(const Quantity<OtherNumber, Unit>& other) const {
Chris@50 264 return value / other.value;
Chris@50 265 }
Chris@50 266 template <typename OtherNumber>
Chris@50 267 inline constexpr decltype(Number(1) % OtherNumber(1))
Chris@50 268 operator%(const Quantity<OtherNumber, Unit>& other) const {
Chris@50 269 return value % other.value;
Chris@50 270 }
Chris@50 271
Chris@50 272 template <typename OtherNumber, typename OtherUnit>
Chris@50 273 inline constexpr Quantity<decltype(Number(1) * OtherNumber(1)), OtherUnit>
Chris@50 274 operator*(const UnitRatio<OtherNumber, OtherUnit, Unit>& ratio) const {
Chris@50 275 return Quantity<decltype(Number(1) * OtherNumber(1)), OtherUnit>(
Chris@50 276 value * ratio.unit1PerUnit2);
Chris@50 277 }
Chris@50 278 template <typename OtherNumber, typename OtherUnit>
Chris@50 279 inline constexpr Quantity<decltype(Number(1) / OtherNumber(1)), OtherUnit>
Chris@50 280 operator/(const UnitRatio<OtherNumber, Unit, OtherUnit>& ratio) const {
Chris@50 281 return Quantity<decltype(Number(1) / OtherNumber(1)), OtherUnit>(
Chris@50 282 value / ratio.unit1PerUnit2);
Chris@50 283 }
Chris@50 284 template <typename OtherNumber, typename OtherUnit>
Chris@50 285 inline constexpr Quantity<decltype(Number(1) % OtherNumber(1)), Unit>
Chris@50 286 operator%(const UnitRatio<OtherNumber, Unit, OtherUnit>& ratio) const {
Chris@50 287 return Quantity<decltype(Number(1) % OtherNumber(1)), Unit>(
Chris@50 288 value % ratio.unit1PerUnit2);
Chris@50 289 }
Chris@50 290 template <typename OtherNumber, typename OtherUnit>
Chris@50 291 inline constexpr UnitRatio<decltype(Number(1) / OtherNumber(1)), Unit, OtherUnit>
Chris@50 292 operator/(const Quantity<OtherNumber, OtherUnit>& other) const {
Chris@50 293 return UnitRatio<decltype(Number(1) / OtherNumber(1)), Unit, OtherUnit>(value / other.value);
Chris@50 294 }
Chris@50 295
Chris@50 296 template <typename OtherNumber>
Chris@50 297 inline constexpr bool operator==(const Quantity<OtherNumber, Unit>& other) const {
Chris@50 298 return value == other.value;
Chris@50 299 }
Chris@50 300 template <typename OtherNumber>
Chris@50 301 inline constexpr bool operator!=(const Quantity<OtherNumber, Unit>& other) const {
Chris@50 302 return value != other.value;
Chris@50 303 }
Chris@50 304 template <typename OtherNumber>
Chris@50 305 inline constexpr bool operator<=(const Quantity<OtherNumber, Unit>& other) const {
Chris@50 306 return value <= other.value;
Chris@50 307 }
Chris@50 308 template <typename OtherNumber>
Chris@50 309 inline constexpr bool operator>=(const Quantity<OtherNumber, Unit>& other) const {
Chris@50 310 return value >= other.value;
Chris@50 311 }
Chris@50 312 template <typename OtherNumber>
Chris@50 313 inline constexpr bool operator<(const Quantity<OtherNumber, Unit>& other) const {
Chris@50 314 return value < other.value;
Chris@50 315 }
Chris@50 316 template <typename OtherNumber>
Chris@50 317 inline constexpr bool operator>(const Quantity<OtherNumber, Unit>& other) const {
Chris@50 318 return value > other.value;
Chris@50 319 }
Chris@50 320
Chris@50 321 template <typename OtherNumber>
Chris@50 322 inline Quantity& operator+=(const Quantity<OtherNumber, Unit>& other) {
Chris@50 323 value += other.value;
Chris@50 324 return *this;
Chris@50 325 }
Chris@50 326 template <typename OtherNumber>
Chris@50 327 inline Quantity& operator-=(const Quantity<OtherNumber, Unit>& other) {
Chris@50 328 value -= other.value;
Chris@50 329 return *this;
Chris@50 330 }
Chris@50 331 template <typename OtherNumber>
Chris@50 332 inline Quantity& operator*=(OtherNumber other) {
Chris@50 333 value *= other;
Chris@50 334 return *this;
Chris@50 335 }
Chris@50 336 template <typename OtherNumber>
Chris@50 337 inline Quantity& operator/=(OtherNumber other) {
Chris@50 338 value /= other.value;
Chris@50 339 return *this;
Chris@50 340 }
Chris@50 341
Chris@50 342 private:
Chris@50 343 Number value;
Chris@50 344
Chris@50 345 template <typename OtherNumber, typename OtherUnit>
Chris@50 346 friend class Quantity;
Chris@50 347
Chris@50 348 template <typename Number1, typename Number2, typename Unit2>
Chris@50 349 friend inline constexpr auto operator*(Number1 a, Quantity<Number2, Unit2> b)
Chris@50 350 -> Quantity<decltype(Number1(1) * Number2(1)), Unit2>;
Chris@50 351
Chris@50 352 template <typename T>
Chris@50 353 friend inline constexpr T unit();
Chris@50 354 };
Chris@50 355
Chris@50 356 template <typename T>
Chris@50 357 inline constexpr T unit() { return T(1); }
Chris@50 358 // unit<Quantity<T, U>>() returns a Quantity of value 1. It also, intentionally, works on basic
Chris@50 359 // numeric types.
Chris@50 360
Chris@50 361 template <typename Number1, typename Number2, typename Unit>
Chris@50 362 inline constexpr auto operator*(Number1 a, Quantity<Number2, Unit> b)
Chris@50 363 -> Quantity<decltype(Number1(1) * Number2(1)), Unit> {
Chris@50 364 return Quantity<decltype(Number1(1) * Number2(1)), Unit>(a * b.value);
Chris@50 365 }
Chris@50 366
Chris@50 367 template <typename Number1, typename Number2, typename Unit, typename Unit2>
Chris@50 368 inline constexpr auto operator*(UnitRatio<Number1, Unit2, Unit> ratio,
Chris@50 369 Quantity<Number2, Unit> measure)
Chris@50 370 -> decltype(measure * ratio) {
Chris@50 371 return measure * ratio;
Chris@50 372 }
Chris@50 373
Chris@50 374 // =======================================================================================
Chris@50 375 // Absolute measures
Chris@50 376
Chris@50 377 template <typename T, typename Label>
Chris@50 378 class Absolute {
Chris@50 379 // Wraps some other value -- typically a Quantity -- but represents a value measured based on
Chris@50 380 // some absolute origin. For example, if `Duration` is a type representing a time duration,
Chris@50 381 // Absolute<Duration, UnixEpoch> might be a calendar date.
Chris@50 382 //
Chris@50 383 // Since Absolute represents measurements relative to some arbitrary origin, the only sensible
Chris@50 384 // arithmetic to perform on them is addition and subtraction.
Chris@50 385
Chris@50 386 // TODO(someday): Do the same automatic expansion of integer width that Quantity does? Doesn't
Chris@50 387 // matter for our time use case, where we always use 64-bit anyway. Note that fixing this
Chris@50 388 // would implicitly allow things like multiplying an Absolute by a UnitRatio to change its
Chris@50 389 // units, which is actually totally logical and kind of neat.
Chris@50 390
Chris@50 391 public:
Chris@50 392 inline constexpr Absolute operator+(const T& other) const { return Absolute(value + other); }
Chris@50 393 inline constexpr Absolute operator-(const T& other) const { return Absolute(value - other); }
Chris@50 394 inline constexpr T operator-(const Absolute& other) const { return value - other.value; }
Chris@50 395
Chris@50 396 inline Absolute& operator+=(const T& other) { value += other; return *this; }
Chris@50 397 inline Absolute& operator-=(const T& other) { value -= other; return *this; }
Chris@50 398
Chris@50 399 inline constexpr bool operator==(const Absolute& other) const { return value == other.value; }
Chris@50 400 inline constexpr bool operator!=(const Absolute& other) const { return value != other.value; }
Chris@50 401 inline constexpr bool operator<=(const Absolute& other) const { return value <= other.value; }
Chris@50 402 inline constexpr bool operator>=(const Absolute& other) const { return value >= other.value; }
Chris@50 403 inline constexpr bool operator< (const Absolute& other) const { return value < other.value; }
Chris@50 404 inline constexpr bool operator> (const Absolute& other) const { return value > other.value; }
Chris@50 405
Chris@50 406 private:
Chris@50 407 T value;
Chris@50 408
Chris@50 409 explicit constexpr Absolute(T value): value(value) {}
Chris@50 410
Chris@50 411 template <typename U>
Chris@50 412 friend inline constexpr U origin();
Chris@50 413 };
Chris@50 414
Chris@50 415 template <typename T, typename Label>
Chris@50 416 inline constexpr Absolute<T, Label> operator+(const T& a, const Absolute<T, Label>& b) {
Chris@50 417 return b + a;
Chris@50 418 }
Chris@50 419
Chris@50 420 template <typename T> struct UnitOf_ { typedef T Type; };
Chris@50 421 template <typename T, typename Label> struct UnitOf_<Absolute<T, Label>> { typedef T Type; };
Chris@50 422 template <typename T>
Chris@50 423 using UnitOf = typename UnitOf_<T>::Type;
Chris@50 424 // UnitOf<Absolute<T, U>> is T. UnitOf<AnythingElse> is AnythingElse.
Chris@50 425
Chris@50 426 template <typename T>
Chris@50 427 inline constexpr T origin() { return T(0 * unit<UnitOf<T>>()); }
Chris@50 428 // origin<Absolute<T, U>>() returns an Absolute of value 0. It also, intentionally, works on basic
Chris@50 429 // numeric types.
Chris@50 430
Chris@50 431 } // namespace kj
Chris@50 432
Chris@50 433 #endif // KJ_UNITS_H_