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