From fbc34c8b981e42c3f0f31876841f815394251fbd Mon Sep 17 00:00:00 2001 From: Mateusz Pusz Date: Wed, 8 Feb 2023 21:05:31 -0800 Subject: [PATCH] refactor: `get_associated_quantity.h` moved to a dedicated header file --- src/core/CMakeLists.txt | 1 + .../mp_units/bits/get_associated_quantity.h | 54 +++++++++++++++++++ src/core/include/mp_units/reference.h | 33 +----------- 3 files changed, 56 insertions(+), 32 deletions(-) create mode 100644 src/core/include/mp_units/bits/get_associated_quantity.h diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 4a98c3be..5514290f 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -43,6 +43,7 @@ add_library( include/mp_units/bits/algorithm.h include/mp_units/bits/dimension_concepts.h include/mp_units/bits/expression_template.h + include/mp_units/bits/get_associated_quantity.h include/mp_units/bits/magnitude.h include/mp_units/bits/math_concepts.h include/mp_units/bits/prime.h diff --git a/src/core/include/mp_units/bits/get_associated_quantity.h b/src/core/include/mp_units/bits/get_associated_quantity.h new file mode 100644 index 00000000..e45f9144 --- /dev/null +++ b/src/core/include/mp_units/bits/get_associated_quantity.h @@ -0,0 +1,54 @@ +// 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 +#include +#include + +namespace mp_units::detail { + +template +[[nodiscard]] consteval auto get_associated_quantity(power) +{ + return pow(get_associated_quantity(U{})); +} + +template +[[nodiscard]] consteval auto get_associated_quantity(type_list) +{ + return (dimensionless * ... * get_associated_quantity(Us{})); +} + +template +[[nodiscard]] consteval auto get_associated_quantity(U) +{ + if constexpr (requires { U::reference_unit; }) + return get_associated_quantity(U::reference_unit); + else if constexpr (requires { typename U::_num_; }) + return get_associated_quantity(typename U::_num_{}) / get_associated_quantity(typename U::_den_{}); + else if constexpr (requires { U::quantity_spec; }) + return U::quantity_spec; +} + +} // namespace mp_units::detail diff --git a/src/core/include/mp_units/reference.h b/src/core/include/mp_units/reference.h index 303d6304..ef3ad9ad 100644 --- a/src/core/include/mp_units/reference.h +++ b/src/core/include/mp_units/reference.h @@ -22,43 +22,13 @@ #pragma once +#include #include #include #include -#include namespace mp_units { -namespace detail { - -template -[[nodiscard]] consteval auto get_associated_quantity(U); - -template -[[nodiscard]] consteval auto get_associated_quantity(power) -{ - return get_associated_quantity(U{}); -} - -template -[[nodiscard]] consteval auto get_associated_quantity(type_list) -{ - return (dimensionless * ... * get_associated_quantity(Us{})); -} - -template -[[nodiscard]] consteval auto get_associated_quantity(U) -{ - if constexpr (requires { U::reference_unit; }) - return get_associated_quantity(U::reference_unit); - else if constexpr (requires { typename U::_num_; }) - return get_associated_quantity(typename U::_num_{}) / get_associated_quantity(typename U::_den_{}); - else if constexpr (requires { U::base_quantity; }) - return U::base_quantity; -} - -} // namespace detail - [[nodiscard]] consteval QuantitySpec auto get_quantity_spec(AssociatedUnit auto u) { return detail::get_associated_quantity(u); @@ -78,7 +48,6 @@ template return U; } - template Rep> class quantity;