diff --git a/src/core/include/units/generic/angle.h b/src/core/include/units/generic/angle.h index 11017a68..3fafecaf 100644 --- a/src/core/include/units/generic/angle.h +++ b/src/core/include/units/generic/angle.h @@ -32,7 +32,9 @@ namespace units { -struct radian : named_unit {}; +struct degree : named_unit {}; +struct rotation : named_scaled_unit(), degree> {}; +struct radian : named_scaled_unit() / pi, rotation> {}; template struct dim_angle : base_dimension<"A", U> {}; @@ -55,6 +57,22 @@ constexpr auto operator"" _q_rad(unsigned long long l) } constexpr auto operator"" _q_rad(long double l) { return angle(l); } +// rot +constexpr auto operator"" _q_rot(unsigned long long l) +{ + gsl_ExpectsAudit(std::in_range(l)); + return angle(static_cast(l)); +} +constexpr auto operator"" _q_rot(long double l) { return angle(l); } + +// deg +constexpr auto operator"" _q_deg(unsigned long long l) +{ + gsl_ExpectsAudit(std::in_range(l)); + return angle(static_cast(l)); +} +constexpr auto operator"" _q_deg(long double l) { return angle(l); } + } // namespace literals #endif // UNITS_NO_LITERALS @@ -64,6 +82,8 @@ constexpr auto operator"" _q_rad(long double l) { return angle, radian>{}; +inline constexpr auto rot = reference, rotation>{}; +inline constexpr auto deg = reference, degree>{}; } // namespace angle_references @@ -76,3 +96,18 @@ using namespace angle_references; #endif // UNITS_NO_REFERENCES } // namespace units + +#ifndef UNITS_NO_ALIASES + +namespace units::aliases::inline angle { + +template +using rad = units::angle; +template +using rot = units::angle; +template +using deg = units::angle; + +} // namespace units::aliases::inline angle + +#endif // UNITS_NO_ALIASES diff --git a/test/unit_test/static/CMakeLists.txt b/test/unit_test/static/CMakeLists.txt index f3cb7def..632592f8 100644 --- a/test/unit_test/static/CMakeLists.txt +++ b/test/unit_test/static/CMakeLists.txt @@ -23,9 +23,11 @@ cmake_minimum_required(VERSION 3.2) add_library(unit_tests_static_truncating quantity_test.cpp) + if(NOT ${projectPrefix}LIBCXX) target_sources(unit_tests_static_truncating PRIVATE quantity_kind_test.cpp quantity_point_kind_test.cpp) endif() + target_link_libraries(unit_tests_static_truncating PRIVATE mp-units::mp-units) target_compile_options( unit_tests_static_truncating PRIVATE $,/wd4242 /wd4244,-Wno-conversion> @@ -33,6 +35,7 @@ target_compile_options( add_library( unit_tests_static + angle_test.cpp cgs_test.cpp chrono_test.cpp concepts_test.cpp diff --git a/test/unit_test/static/angle_test.cpp b/test/unit_test/static/angle_test.cpp new file mode 100644 index 00000000..d936909a --- /dev/null +++ b/test/unit_test/static/angle_test.cpp @@ -0,0 +1,52 @@ +// 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 +#include + +namespace { + +using namespace units::references; + +static_assert(360. * deg == 1. * rot); +static_assert(360 * deg == 1 * rot); +static_assert(std::numbers::pi * 2 * rad == 1. * rot); + +} // namespace + +namespace { + +using namespace units::literals; + +static_assert(360._q_deg == 1._q_rot); +static_assert(360_q_deg == 1_q_rot); +static_assert(std::numbers::pi * quantity_cast(2._q_rad) == quantity_cast(1._q_rot)); + +} // namespace + +namespace { + +static_assert(units::aliases::deg<>(360.) == units::aliases::rot<>(1.)); +static_assert(units::aliases::deg(360) == units::aliases::rot(1)); +static_assert(std::numbers::pi * units::aliases::rad<>(2.) == units::aliases::rot<>(1.)); + +} // namespace