annotate win64-msvc/include/kj/units.h @ 62:0994c39f1e94

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