annotate win32-mingw/include/kj/units.h @ 146:206f0eb279b8

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