annotate osx/include/kj/units.h @ 49:3ab5a40c4e3b

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