diff --git a/src/include/units/area.h b/src/include/units/area.h new file mode 100644 index 00000000..f9a97a71 --- /dev/null +++ b/src/include/units/area.h @@ -0,0 +1,73 @@ +// 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. + +#pragma once + +#include + +namespace units { + + struct dimension_area : make_dimension_t> {}; + template<> struct upcasting_traits> : upcast_to {}; + + template + concept bool Area = Quantity && std::experimental::ranges::Same; + + template + using area = quantity; + + struct square_millimeter : derived_unit {}; + template<> struct upcasting_traits> : upcast_to {}; + + struct square_centimeter : derived_unit {}; + template<> struct upcasting_traits> : upcast_to {}; + + struct square_meter : derived_unit {}; + template<> struct upcasting_traits> : upcast_to {}; + + struct square_kilometer : derived_unit {}; + template<> struct upcasting_traits> : upcast_to {}; + + struct square_foot : derived_unit {}; + template<> struct upcasting_traits> : upcast_to {}; + + inline namespace literals { + + // sq_mm + constexpr auto operator""_sq_mm(unsigned long long l) { return area(l); } + constexpr auto operator""_sq_mm(long double l) { return area(l); } + + // sq_cm + constexpr auto operator""_sq_cm(unsigned long long l) { return area(l); } + constexpr auto operator""_sq_cm(long double l) { return area(l); } + + // sq_m + constexpr auto operator""_sq_m(unsigned long long l) { return area(l); } + constexpr auto operator""_sq_m(long double l) { return area(l); } + + // sq_km + constexpr auto operator""_sq_km(unsigned long long l) { return area(l); } + constexpr auto operator""_sq_km(long double l) { return area(l); } + + } // namespace literals + +} // namespace units diff --git a/test/test_units.cpp b/test/test_units.cpp index 17ffbb48..15a6980a 100644 --- a/test/test_units.cpp +++ b/test/test_units.cpp @@ -31,6 +31,7 @@ #include #include +#include #include @@ -93,4 +94,10 @@ namespace { // static_assert(2000_m / 2_kmph == 1_h); // should not compile static_assert(quantity_cast>(2000_m) / 2_kmph == 1_h); + // area + + static_assert(1_m * 1_m == 1_sq_m); + static_assert(10_km * 10_km == 100_sq_km); + static_assert(1_sq_m == 10'000_sq_cm); + } // namespace