#ifndef Magnum_Math_Unit_h #define Magnum_Math_Unit_h /* This file is part of Magnum. Copyright © 2010, 2011, 2012, 2013 Vladimír Vondruš Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /** @file * @brief Class Magnum::Math::Unit */ #include "Math/MathTypeTraits.h" namespace Magnum { namespace Math { /** @brief Base class for units @tparam T Underlying data type @see Deg, Rad */ template class Derived, class T> class Unit { template class, class> friend class Unit; public: typedef T Type; /**< @brief Underlying data type */ /** @brief Default constructor */ inline constexpr /*implicit*/ Unit(): value(T(0)) {} /** @brief Explicit conversion from unitless type */ inline constexpr explicit Unit(T value): value(value) {} /** @brief Construct from another underlying type */ template inline constexpr explicit Unit(Unit value): value(value.value) {} /** @brief Explicit conversion to underlying type */ inline constexpr explicit operator T() const { return value; } /** @brief Equality comparison */ inline constexpr bool operator==(Unit other) const { return MathTypeTraits::equals(value, other.value); } /** @brief Non-equality comparison */ inline constexpr bool operator!=(Unit other) const { return !operator==(other); } /** @brief Less than comparison */ inline constexpr bool operator<(Unit other) const { return value < other.value; } /** @brief Greater than comparison */ inline constexpr bool operator>(Unit other) const { return value > other.value; } /** @brief Less than or equal comparison */ inline constexpr bool operator<=(Unit other) const { return !operator>(other); } /** @brief Greater than or equal comparison */ inline constexpr bool operator>=(Unit other) const { return !operator<(other); } /** @brief Negated value */ inline constexpr Unit operator-() const { return Unit(-value); } /** @brief Add and assign value */ inline Unit& operator+=(Unit other) { value += other.value; return *this; } /** @brief Add value */ inline constexpr Unit operator+(Unit other) const { return Unit(value + other.value); } /** @brief Subtract and assign value */ inline Unit& operator-=(Unit other) { value -= other.value; return *this; } /** @brief Subtract value */ inline constexpr Unit operator-(Unit other) const { return Unit(value - other.value); } /** @brief Multiply with number and assign */ inline Unit& operator*=(T number) { value *= number; return *this; } /** @brief Multiply with number */ inline constexpr Unit operator*(T number) const { return Unit(value*number); } /** @brief Divide with number and assign */ inline Unit& operator/=(T number) { value /= number; return *this; } /** @brief Divide with number */ inline constexpr Unit operator/(T number) const { return Unit(value/number); } /** @brief Ratio of two values */ inline constexpr T operator/(Unit other) const { return value/other.value; } private: T value; }; /** @relates Unit @brief Multiply number with value */ template class Derived, class T> inline constexpr Unit operator*(typename std::common_type::type number, const Unit& value) { return value*number; } }} #endif