// The MIT License (MIT) // // Copyright (c) 2018 Mateusz Pusz // // 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. #include "units/dimensions/velocity.h" #include "units/dimensions/frequency.h" #include "units/dimensions/area.h" #include "units/math.h" #include #include using namespace units; namespace { template class my_value { T value_{}; public: my_value() = default; constexpr my_value(T v) : value_(std::move(v)) {} [[nodiscard]] constexpr my_value operator-() const { return my_value(-value_); } [[nodiscard]] friend constexpr my_value operator+(my_value lhs, my_value rhs) { return my_value(lhs.value_ + rhs.value_); } [[nodiscard]] friend constexpr my_value operator-(my_value lhs, my_value rhs) { return my_value(lhs.value_ - rhs.value_); } [[nodiscard]] friend constexpr my_value operator*(my_value lhs, my_value rhs) { return my_value(lhs.value_ * rhs.value_); } [[nodiscard]] friend constexpr my_value operator/(my_value lhs, my_value rhs) { return my_value(lhs.value_ / rhs.value_); } [[nodiscard]] friend constexpr bool operator==(my_value lhs, my_value rhs) { return lhs.value_ == rhs.value_; } [[nodiscard]] friend constexpr bool operator!=(my_value lhs, my_value rhs) { return !(lhs == rhs); } [[nodiscard]] friend constexpr bool operator<(my_value lhs, my_value rhs) { return lhs.value_ < rhs.value_; } [[nodiscard]] friend constexpr bool operator>(my_value lhs, my_value rhs) { return rhs < lhs; } [[nodiscard]] friend constexpr bool operator<=(my_value lhs, my_value rhs) { return !(rhs < lhs); } [[nodiscard]] friend constexpr bool operator>=(my_value lhs, my_value rhs) { return !(lhs < rhs); } constexpr operator const T&() const & { return value_; } }; } // namespace namespace units { template inline constexpr bool treat_as_floating_point> = std::is_floating_point_v; template struct quantity_values> { static constexpr my_value zero() { return my_value(0); } static constexpr my_value max() { return std::numeric_limits::max(); } static constexpr my_value min() { return std::numeric_limits::lowest(); } }; } // namespace units namespace std { template struct common_type, my_value> : std::type_identity>> { }; template struct common_type, U> : common_type { }; template struct common_type> : common_type { }; } // namespace std namespace { static_assert(units::Scalar>); static_assert(std::convertible_to, float>); static_assert(std::convertible_to>); using namespace units; // class invariants // constexpr quantity q; // should a static_assert // constexpr quantity> error(0m); // should trigger a static_assert // constexpr quantity error(0); // should trigger a static_assert // constexpr quantity>, int> error(0); // should trigger a static_assert // member types static_assert(std::is_same_v::rep, int>); static_assert(std::is_same_v::rep, double>); static_assert(std::is_same_v::unit, metre>); static_assert(std::is_same_v::unit, kilometre>); // constructors using my_int = my_value; using my_double = my_value; static_assert(quantity().count() == 0); constexpr quantity km{1000}; static_assert(km.count() == 1000); static_assert(quantity(km).count() == km.count()); static_assert(quantity(1).count() == 1); static_assert(quantity(my_value(1)).count() == 1); static_assert(quantity(1).count() == my_int{1}); // static_assert(quantity(1.0).count() == 1); // should not compile // static_assert(quantity(my_value(1.0)).count() == 1); // should not compile // static_assert(quantity(1.0).count() == 1); // should not compile static_assert(quantity(1.0).count() == 1.0); static_assert(quantity(my_value(1.0)).count() == 1.0); static_assert(quantity(1).count() == 1.0); static_assert(quantity(my_value(1)).count() == 1.0); static_assert(quantity(3.14).count() == 3.14); static_assert(quantity(1.0).count() == my_double{1.0}); static_assert(quantity(1).count() == my_double{1.0}); static_assert(quantity(3.14).count() == my_double{3.14}); static_assert(quantity(km).count() == 1000); // static_assert(quantity(quantity(3.14)).count() == 3); // should not compile static_assert(quantity(quantity_cast>(3.14m)).count() == 3); // static_assert(quantity(quantity(1000.0)).count() == 1000); // should not compile // static_assert(quantity(1000.0m).count() == 1000); // should not compile static_assert(quantity(1000.0m).count() == 1000.0); static_assert(quantity(quantity(1000.0)).count() == 1000.0); static_assert(quantity(1000.0m).count() == my_double{1000.0}); static_assert(quantity(km).count() == 1000.0); static_assert(quantity(km).count() == my_double{1000.0}); static_assert(quantity(1km).count() == 1000); // static_assert(quantity(1_s).count() == 1); // should not compile // static_assert(quantity(1010m).count() == 1); // should not compile static_assert(quantity(quantity_cast>(1010m)).count() == 1); // assignment operator static_assert([]() { quantity l1(1), l2(2); return l2 = l1; }().count() == 1); // static member functions static_assert(quantity::zero().count() == 0); static_assert(quantity::min().count() == std::numeric_limits::lowest()); static_assert(quantity::max().count() == std::numeric_limits::max()); static_assert(quantity::zero().count() == 0.0); static_assert(quantity::min().count() == std::numeric_limits::lowest()); static_assert(quantity::max().count() == std::numeric_limits::max()); static_assert(quantity::zero().count() == my_int{0}); static_assert(quantity::min().count() == my_int{std::numeric_limits::lowest()}); static_assert(quantity::max().count() == my_int{std::numeric_limits::max()}); static_assert(quantity::zero().count() == my_double{0.0}); static_assert(quantity::min().count() == my_double{std::numeric_limits::lowest()}); static_assert(quantity::max().count() == my_double{std::numeric_limits::max()}); // unary member operators static_assert((+km).count() == 1000); static_assert((-km).count() == -1000); static_assert((+(-km)).count() == -1000); static_assert((-(-km)).count() == 1000); // binary member operators static_assert([](auto v) { auto vv = v++; return std::make_pair(v, vv); }(km) == std::make_pair(quantity(1001), quantity(1000))); static_assert([](auto v) { auto vv = ++v; return std::make_pair(v, vv); }(km) == std::make_pair(quantity(1001), quantity(1001))); static_assert([](auto v) { auto vv = v--; return std::make_pair(v, vv); }(km) == std::make_pair(quantity(999), quantity(1000))); static_assert([](auto v) { auto vv = --v; return std::make_pair(v, vv); }(km) == std::make_pair(quantity(999), quantity(999))); // compound assignment static_assert((1m += 1m).count() == 2); static_assert((2m -= 1m).count() == 1); static_assert((1m *= 2).count() == 2); static_assert((2m /= 2).count() == 1); static_assert((7m %= 2).count() == 1); static_assert((7m %= 2m).count() == 1); // static_assert((7.m %= 2.).count() == 1); // should not compile // static_assert((7.m %= 2).count() == 1); // should not compile // static_assert((7m %= 2.).count() == 1); // should not compile static_assert((7m %= 2m).count() == 1); // static_assert((7.m %= 2.m).count() == 1); // should not compile // static_assert((7.m %= 2m).count() == 1); // should not compile // static_assert((7m %= 2.m).count() == 1); // should not compile // non-member arithmetic operators static_assert(std::is_same_v() + quantity()), quantity>); static_assert(std::is_same_v() + quantity()), quantity>); static_assert(std::is_same_v() + quantity()), quantity>); static_assert(std::is_same_v() - quantity()), quantity>); static_assert(std::is_same_v() - quantity()), quantity>); static_assert(std::is_same_v() * 1.0), quantity>); static_assert(std::is_same_v()), quantity>); static_assert(std::is_same_v() * quantity()), quantity>); static_assert(std::is_same_v()), quantity>); static_assert(std::is_same_v() / 1.0), quantity>); static_assert(std::is_same_v() / quantity()), double>); static_assert(std::is_same_v() / quantity()), double>); static_assert(std::is_same_v() / quantity()), quantity>); static_assert(std::is_same_v() % short(1)), quantity>); static_assert(std::is_same_v() % quantity(1)), quantity>); static_assert((1m + km).count() == 1001); static_assert((1m + 1km).count() == 1001); static_assert((km - 1m).count() == 999); static_assert((1km - 1m).count() == 999); static_assert((2m * 2).count() == 4); static_assert((3 * 3m).count() == 9); static_assert((4m / 2).count() == 2); static_assert(4m / 2m == 2); static_assert(4km / 2000m == 2); static_assert((7m % 2).count() == 1); static_assert((7m % 2m).count() == 1); static_assert((7km % 2000m).count() == 1000); // comparators static_assert(2m + 1m == 3m); static_assert(!(2m + 2m == 3m)); static_assert(2m + 2m != 3m); static_assert(!(2m + 2m != 4m)); static_assert(2m > 1m); static_assert(!(1m > 1m)); static_assert(1m < 2m); static_assert(!(2m < 2m)); static_assert(2m >= 1m); static_assert(2m >= 2m); static_assert(!(2m >= 3m)); static_assert(1m <= 2m); static_assert(2m <= 2m); static_assert(!(3m <= 2m)); static_assert(3m == 3.0m); static_assert(3m != 3.14m); static_assert(2m > 1.0m); static_assert(1.0m < 2m); static_assert(2.0m >= 1m); static_assert(1m <= 2.0m); static_assert(1000m == 1km); static_assert(1001m != 1km); static_assert(1001m > 1km); static_assert(999m < 1km); static_assert(1000m >= 1km); static_assert(1000m <= 1km); // is_quantity static_assert(Quantity>); // common_quantity static_assert(std::is_same_v, quantity>, quantity>); static_assert(std::is_same_v, quantity>, quantity>); static_assert(std::is_same_v, quantity>, quantity>); // quantity_cast static_assert(std::is_same_v>>(2km))::unit, metre>); static_assert(std::is_same_v>, ratio<1>>>(2km))::unit, metre>); // static_assert(quantity_cast(2km).count() == 2000); // should not compile static_assert(quantity_cast>(2km).count() == 2000); static_assert(quantity_cast>(2000m).count() == 2); // time // static_assert(1s == 1m); // should not compile static_assert(1h == 3600s); // length static_assert(1km == 1000m); static_assert(1km + 1m == 1001m); static_assert(10km / 5km == 2); static_assert(10km / 2 == 5km); // velocity static_assert(10m / 5s == 2mps); static_assert(10 / 5s * 1m == 2mps); static_assert(1km / 1s == 1000mps); static_assert(2kmph * 2h == 4km); static_assert(2km / 2kmph == 1h); static_assert(std::is_same_v(2m)), decltype(4sq_m)>); } // namespace