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