Chris@47: // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors Chris@47: // Licensed under the MIT License: Chris@47: // Chris@47: // Permission is hereby granted, free of charge, to any person obtaining a copy Chris@47: // of this software and associated documentation files (the "Software"), to deal Chris@47: // in the Software without restriction, including without limitation the rights Chris@47: // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell Chris@47: // copies of the Software, and to permit persons to whom the Software is Chris@47: // furnished to do so, subject to the following conditions: Chris@47: // Chris@47: // The above copyright notice and this permission notice shall be included in Chris@47: // all copies or substantial portions of the Software. Chris@47: // Chris@47: // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR Chris@47: // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, Chris@47: // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE Chris@47: // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER Chris@47: // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, Chris@47: // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN Chris@47: // THE SOFTWARE. Chris@47: Chris@47: // This file contains types which are intended to help detect incorrect usage at compile Chris@47: // time, but should then be optimized down to basic primitives (usually, integers) by the Chris@47: // compiler. Chris@47: Chris@47: #ifndef KJ_UNITS_H_ Chris@47: #define KJ_UNITS_H_ Chris@47: Chris@47: #if defined(__GNUC__) && !KJ_HEADER_WARNINGS Chris@47: #pragma GCC system_header Chris@47: #endif Chris@47: Chris@47: #include "common.h" Chris@47: Chris@47: namespace kj { Chris@47: Chris@47: // ======================================================================================= Chris@47: // IDs Chris@47: Chris@47: template Chris@47: struct Id { Chris@47: // A type-safe numeric ID. `UnderlyingType` is the underlying integer representation. `Label` Chris@47: // distinguishes this Id from other Id types. Sample usage: Chris@47: // Chris@47: // class Foo; Chris@47: // typedef Id FooId; Chris@47: // Chris@47: // class Bar; Chris@47: // typedef Id BarId; Chris@47: // Chris@47: // You can now use the FooId and BarId types without any possibility of accidentally using a Chris@47: // FooId when you really wanted a BarId or vice-versa. Chris@47: Chris@47: UnderlyingType value; Chris@47: Chris@47: inline constexpr Id(): value(0) {} Chris@47: inline constexpr explicit Id(int value): value(value) {} Chris@47: Chris@47: inline constexpr bool operator==(const Id& other) const { return value == other.value; } Chris@47: inline constexpr bool operator!=(const Id& other) const { return value != other.value; } Chris@47: inline constexpr bool operator<=(const Id& other) const { return value <= other.value; } Chris@47: inline constexpr bool operator>=(const Id& other) const { return value >= other.value; } Chris@47: inline constexpr bool operator< (const Id& other) const { return value < other.value; } Chris@47: inline constexpr bool operator> (const Id& other) const { return value > other.value; } Chris@47: }; Chris@47: Chris@47: // ======================================================================================= Chris@47: // Quantity and UnitRatio -- implement unit analysis via the type system Chris@47: Chris@47: template constexpr bool isIntegral() { return false; } Chris@47: template <> constexpr bool isIntegral() { return true; } Chris@47: template <> constexpr bool isIntegral() { return true; } Chris@47: template <> constexpr bool isIntegral() { return true; } Chris@47: template <> constexpr bool isIntegral() { return true; } Chris@47: template <> constexpr bool isIntegral() { return true; } Chris@47: template <> constexpr bool isIntegral() { return true; } Chris@47: template <> constexpr bool isIntegral() { return true; } Chris@47: template <> constexpr bool isIntegral() { return true; } Chris@47: template <> constexpr bool isIntegral() { return true; } Chris@47: template <> constexpr bool isIntegral() { return true; } Chris@47: template <> constexpr bool isIntegral() { return true; } Chris@47: Chris@47: template Chris@47: class UnitRatio { Chris@47: // A multiplier used to convert Quantities of one unit to Quantities of another unit. See Chris@47: // Quantity, below. Chris@47: // Chris@47: // Construct this type by dividing one Quantity by another of a different unit. Use this type Chris@47: // by multiplying it by a Quantity, or dividing a Quantity by it. Chris@47: Chris@47: static_assert(isIntegral(), "Underlying type for UnitRatio must be integer."); Chris@47: Chris@47: public: Chris@47: inline UnitRatio() {} Chris@47: Chris@47: constexpr explicit UnitRatio(Number unit1PerUnit2): unit1PerUnit2(unit1PerUnit2) {} Chris@47: // This constructor was intended to be private, but GCC complains about it being private in a Chris@47: // bunch of places that don't appear to even call it, so I made it public. Oh well. Chris@47: Chris@47: template Chris@47: inline constexpr UnitRatio(const UnitRatio& other) Chris@47: : unit1PerUnit2(other.unit1PerUnit2) {} Chris@47: Chris@47: template Chris@47: inline constexpr UnitRatio Chris@47: operator+(UnitRatio other) const { Chris@47: return UnitRatio( Chris@47: unit1PerUnit2 + other.unit1PerUnit2); Chris@47: } Chris@47: template Chris@47: inline constexpr UnitRatio Chris@47: operator-(UnitRatio other) const { Chris@47: return UnitRatio( Chris@47: unit1PerUnit2 - other.unit1PerUnit2); Chris@47: } Chris@47: Chris@47: template Chris@47: inline constexpr UnitRatio Chris@47: operator*(UnitRatio other) const { Chris@47: // U1 / U2 * U3 / U1 = U3 / U2 Chris@47: return UnitRatio( Chris@47: unit1PerUnit2 * other.unit1PerUnit2); Chris@47: } Chris@47: template Chris@47: inline constexpr UnitRatio Chris@47: operator*(UnitRatio other) const { Chris@47: // U1 / U2 * U2 / U3 = U1 / U3 Chris@47: return UnitRatio( Chris@47: unit1PerUnit2 * other.unit1PerUnit2); Chris@47: } Chris@47: Chris@47: template Chris@47: inline constexpr UnitRatio Chris@47: operator/(UnitRatio other) const { Chris@47: // (U1 / U2) / (U1 / U3) = U3 / U2 Chris@47: return UnitRatio( Chris@47: unit1PerUnit2 / other.unit1PerUnit2); Chris@47: } Chris@47: template Chris@47: inline constexpr UnitRatio Chris@47: operator/(UnitRatio other) const { Chris@47: // (U1 / U2) / (U3 / U2) = U1 / U3 Chris@47: return UnitRatio( Chris@47: unit1PerUnit2 / other.unit1PerUnit2); Chris@47: } Chris@47: Chris@47: template Chris@47: inline decltype(Number(1) / OtherNumber(1)) Chris@47: operator/(UnitRatio other) const { Chris@47: return unit1PerUnit2 / other.unit1PerUnit2; Chris@47: } Chris@47: Chris@47: inline bool operator==(UnitRatio other) const { return unit1PerUnit2 == other.unit1PerUnit2; } Chris@47: inline bool operator!=(UnitRatio other) const { return unit1PerUnit2 != other.unit1PerUnit2; } Chris@47: Chris@47: private: Chris@47: Number unit1PerUnit2; Chris@47: Chris@47: template Chris@47: friend class Quantity; Chris@47: template Chris@47: friend class UnitRatio; Chris@47: Chris@47: template Chris@47: friend inline constexpr UnitRatio Chris@47: operator*(N1, UnitRatio); Chris@47: }; Chris@47: Chris@47: template Chris@47: inline constexpr UnitRatio Chris@47: operator*(N1 n, UnitRatio r) { Chris@47: return UnitRatio(n * r.unit1PerUnit2); Chris@47: } Chris@47: Chris@47: template Chris@47: class Quantity { Chris@47: // A type-safe numeric quantity, specified in terms of some unit. Two Quantities cannot be used Chris@47: // in arithmetic unless they use the same unit. The `Unit` type parameter is only used to prevent Chris@47: // accidental mixing of units; this type is never instantiated and can very well be incomplete. Chris@47: // `Number` is the underlying primitive numeric type. Chris@47: // Chris@47: // Quantities support most basic arithmetic operators, intelligently handling units, and Chris@47: // automatically casting the underlying type in the same way that the compiler would. Chris@47: // Chris@47: // To convert a primitive number to a Quantity, multiply it by unit>(). Chris@47: // To convert a Quantity to a primitive number, divide it by unit>(). Chris@47: // To convert a Quantity of one unit to another unit, multiply or divide by a UnitRatio. Chris@47: // Chris@47: // The Quantity class is not well-suited to hardcore physics as it does not allow multiplying Chris@47: // one quantity by another. For example, multiplying meters by meters won't get you square Chris@47: // meters; it will get you a compiler error. It would be interesting to see if template Chris@47: // metaprogramming could properly deal with such things but this isn't needed for the present Chris@47: // use case. Chris@47: // Chris@47: // Sample usage: Chris@47: // Chris@47: // class SecondsLabel; Chris@47: // typedef Quantity Seconds; Chris@47: // constexpr Seconds SECONDS = unit(); Chris@47: // Chris@47: // class MinutesLabel; Chris@47: // typedef Quantity Minutes; Chris@47: // constexpr Minutes MINUTES = unit(); Chris@47: // Chris@47: // constexpr UnitRatio SECONDS_PER_MINUTE = Chris@47: // 60 * SECONDS / MINUTES; Chris@47: // Chris@47: // void waitFor(Seconds seconds) { Chris@47: // sleep(seconds / SECONDS); Chris@47: // } Chris@47: // void waitFor(Minutes minutes) { Chris@47: // waitFor(minutes * SECONDS_PER_MINUTE); Chris@47: // } Chris@47: // Chris@47: // void waitThreeMinutes() { Chris@47: // waitFor(3 * MINUTES); Chris@47: // } Chris@47: Chris@47: static_assert(isIntegral(), "Underlying type for Quantity must be integer."); Chris@47: Chris@47: public: Chris@47: inline constexpr Quantity() {} Chris@47: Chris@47: inline constexpr Quantity(MaxValue_): value(maxValue) {} Chris@47: inline constexpr Quantity(MinValue_): value(minValue) {} Chris@47: // Allow initialization from maxValue and minValue. Chris@47: // TODO(msvc): decltype(maxValue) and decltype(minValue) deduce unknown-type for these function Chris@47: // parameters, causing the compiler to complain of a duplicate constructor definition, so we Chris@47: // specify MaxValue_ and MinValue_ types explicitly. Chris@47: Chris@47: inline explicit constexpr Quantity(Number value): value(value) {} Chris@47: // This constructor was intended to be private, but GCC complains about it being private in a Chris@47: // bunch of places that don't appear to even call it, so I made it public. Oh well. Chris@47: Chris@47: template Chris@47: inline constexpr Quantity(const Quantity& other) Chris@47: : value(other.value) {} Chris@47: Chris@47: template Chris@47: inline constexpr Quantity Chris@47: operator+(const Quantity& other) const { Chris@47: return Quantity(value + other.value); Chris@47: } Chris@47: template Chris@47: inline constexpr Quantity Chris@47: operator-(const Quantity& other) const { Chris@47: return Quantity(value - other.value); Chris@47: } Chris@47: template Chris@47: inline constexpr Quantity Chris@47: operator*(OtherNumber other) const { Chris@47: static_assert(isIntegral(), "Multiplied Quantity by non-integer."); Chris@47: return Quantity(value * other); Chris@47: } Chris@47: template Chris@47: inline constexpr Quantity Chris@47: operator/(OtherNumber other) const { Chris@47: static_assert(isIntegral(), "Divided Quantity by non-integer."); Chris@47: return Quantity(value / other); Chris@47: } Chris@47: template Chris@47: inline constexpr decltype(Number(1) / OtherNumber(1)) Chris@47: operator/(const Quantity& other) const { Chris@47: return value / other.value; Chris@47: } Chris@47: template Chris@47: inline constexpr decltype(Number(1) % OtherNumber(1)) Chris@47: operator%(const Quantity& other) const { Chris@47: return value % other.value; Chris@47: } Chris@47: Chris@47: template Chris@47: inline constexpr Quantity Chris@47: operator*(const UnitRatio& ratio) const { Chris@47: return Quantity( Chris@47: value * ratio.unit1PerUnit2); Chris@47: } Chris@47: template Chris@47: inline constexpr Quantity Chris@47: operator/(const UnitRatio& ratio) const { Chris@47: return Quantity( Chris@47: value / ratio.unit1PerUnit2); Chris@47: } Chris@47: template Chris@47: inline constexpr Quantity Chris@47: operator%(const UnitRatio& ratio) const { Chris@47: return Quantity( Chris@47: value % ratio.unit1PerUnit2); Chris@47: } Chris@47: template Chris@47: inline constexpr UnitRatio Chris@47: operator/(const Quantity& other) const { Chris@47: return UnitRatio(value / other.value); Chris@47: } Chris@47: Chris@47: template Chris@47: inline constexpr bool operator==(const Quantity& other) const { Chris@47: return value == other.value; Chris@47: } Chris@47: template Chris@47: inline constexpr bool operator!=(const Quantity& other) const { Chris@47: return value != other.value; Chris@47: } Chris@47: template Chris@47: inline constexpr bool operator<=(const Quantity& other) const { Chris@47: return value <= other.value; Chris@47: } Chris@47: template Chris@47: inline constexpr bool operator>=(const Quantity& other) const { Chris@47: return value >= other.value; Chris@47: } Chris@47: template Chris@47: inline constexpr bool operator<(const Quantity& other) const { Chris@47: return value < other.value; Chris@47: } Chris@47: template Chris@47: inline constexpr bool operator>(const Quantity& other) const { Chris@47: return value > other.value; Chris@47: } Chris@47: Chris@47: template Chris@47: inline Quantity& operator+=(const Quantity& other) { Chris@47: value += other.value; Chris@47: return *this; Chris@47: } Chris@47: template Chris@47: inline Quantity& operator-=(const Quantity& other) { Chris@47: value -= other.value; Chris@47: return *this; Chris@47: } Chris@47: template Chris@47: inline Quantity& operator*=(OtherNumber other) { Chris@47: value *= other; Chris@47: return *this; Chris@47: } Chris@47: template Chris@47: inline Quantity& operator/=(OtherNumber other) { Chris@47: value /= other.value; Chris@47: return *this; Chris@47: } Chris@47: Chris@47: private: Chris@47: Number value; Chris@47: Chris@47: template Chris@47: friend class Quantity; Chris@47: Chris@47: template Chris@47: friend inline constexpr auto operator*(Number1 a, Quantity b) Chris@47: -> Quantity; Chris@47: Chris@47: template Chris@47: friend inline constexpr T unit(); Chris@47: }; Chris@47: Chris@47: template Chris@47: inline constexpr T unit() { return T(1); } Chris@47: // unit>() returns a Quantity of value 1. It also, intentionally, works on basic Chris@47: // numeric types. Chris@47: Chris@47: template Chris@47: inline constexpr auto operator*(Number1 a, Quantity b) Chris@47: -> Quantity { Chris@47: return Quantity(a * b.value); Chris@47: } Chris@47: Chris@47: template Chris@47: inline constexpr auto operator*(UnitRatio ratio, Chris@47: Quantity measure) Chris@47: -> decltype(measure * ratio) { Chris@47: return measure * ratio; Chris@47: } Chris@47: Chris@47: // ======================================================================================= Chris@47: // Absolute measures Chris@47: Chris@47: template Chris@47: class Absolute { Chris@47: // Wraps some other value -- typically a Quantity -- but represents a value measured based on Chris@47: // some absolute origin. For example, if `Duration` is a type representing a time duration, Chris@47: // Absolute might be a calendar date. Chris@47: // Chris@47: // Since Absolute represents measurements relative to some arbitrary origin, the only sensible Chris@47: // arithmetic to perform on them is addition and subtraction. Chris@47: Chris@47: // TODO(someday): Do the same automatic expansion of integer width that Quantity does? Doesn't Chris@47: // matter for our time use case, where we always use 64-bit anyway. Note that fixing this Chris@47: // would implicitly allow things like multiplying an Absolute by a UnitRatio to change its Chris@47: // units, which is actually totally logical and kind of neat. Chris@47: Chris@47: public: Chris@47: inline constexpr Absolute operator+(const T& other) const { return Absolute(value + other); } Chris@47: inline constexpr Absolute operator-(const T& other) const { return Absolute(value - other); } Chris@47: inline constexpr T operator-(const Absolute& other) const { return value - other.value; } Chris@47: Chris@47: inline Absolute& operator+=(const T& other) { value += other; return *this; } Chris@47: inline Absolute& operator-=(const T& other) { value -= other; return *this; } Chris@47: Chris@47: inline constexpr bool operator==(const Absolute& other) const { return value == other.value; } Chris@47: inline constexpr bool operator!=(const Absolute& other) const { return value != other.value; } Chris@47: inline constexpr bool operator<=(const Absolute& other) const { return value <= other.value; } Chris@47: inline constexpr bool operator>=(const Absolute& other) const { return value >= other.value; } Chris@47: inline constexpr bool operator< (const Absolute& other) const { return value < other.value; } Chris@47: inline constexpr bool operator> (const Absolute& other) const { return value > other.value; } Chris@47: Chris@47: private: Chris@47: T value; Chris@47: Chris@47: explicit constexpr Absolute(T value): value(value) {} Chris@47: Chris@47: template Chris@47: friend inline constexpr U origin(); Chris@47: }; Chris@47: Chris@47: template Chris@47: inline constexpr Absolute operator+(const T& a, const Absolute& b) { Chris@47: return b + a; Chris@47: } Chris@47: Chris@47: template struct UnitOf_ { typedef T Type; }; Chris@47: template struct UnitOf_> { typedef T Type; }; Chris@47: template Chris@47: using UnitOf = typename UnitOf_::Type; Chris@47: // UnitOf> is T. UnitOf is AnythingElse. Chris@47: Chris@47: template Chris@47: inline constexpr T origin() { return T(0 * unit>()); } Chris@47: // origin>() returns an Absolute of value 0. It also, intentionally, works on basic Chris@47: // numeric types. Chris@47: Chris@47: } // namespace kj Chris@47: Chris@47: #endif // KJ_UNITS_H_