From dd6dfef7ab786571f223386374f8e8f83bae38fa Mon Sep 17 00:00:00 2001 From: Dominik Berner Date: Wed, 24 Mar 2021 08:32:35 +0100 Subject: [PATCH 1/6] Move unit cast to own file --- CHANGELOG.md | 1 + include/SI/detail/unit.h | 39 +----------------------------- include/SI/detail/unit_cast.h | 45 +++++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 38 deletions(-) create mode 100644 include/SI/detail/unit_cast.h diff --git a/CHANGELOG.md b/CHANGELOG.md index 950e3299..85997865 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## 2.0.5 +* Move helper function such as `unit_cast` to own file for easier maintenance * Fixed typos in documentation ## 2.0.4 diff --git a/include/SI/detail/unit.h b/include/SI/detail/unit.h index 903345ea..54531924 100644 --- a/include/SI/detail/unit.h +++ b/include/SI/detail/unit.h @@ -18,6 +18,7 @@ #endif #include "detail.h" +#include "unit_cast.h" #include #include @@ -25,10 +26,6 @@ /// Namespace containing all SI units namespace SI::detail { -// forward declarations -template -constexpr auto unit_cast(const _rhs_T &rhs); - template struct unit_with_common_ratio; /// @todo add in-place unit_cast for move operators @@ -499,40 +496,6 @@ operator/(const _type &lhs, static_cast<_type>(_ratio::den)))}); } -/// helper template to check if a type is a unit_t (false for all other types) -template struct is_unit_t : std::false_type {}; - -/// template specialisation to check if a type is a unit_t (true if unit_t) -template -struct is_unit_t> - : std::true_type {}; - -/// non-const specialisation of check above -template -struct is_unit_t> : std::true_type {}; - -template -inline constexpr bool is_unit_t_v = is_unit_t<_type>::value; - -/// function to cast between two units of the same type -template -constexpr auto unit_cast(const _rhs_T &rhs) { - // using static assert instead of std::enable if in order to be able to - // forward declare this function easier - static_assert( - is_unit_t_v<_rhs_T> || - std::is_base_of< - unit_t<_rhs_T::symbol::value, typename _rhs_T::exponent, - typename _rhs_T::internal_type, typename _rhs_T::ratio>, - _rhs_T>::value, - "is of type unit_t or a derived class"); - using conversion_ratio = - std::ratio_divide; - - return _target_type( - ((rhs.value() * conversion_ratio::num) / conversion_ratio::den)); -} - template struct unit_with_common_ratio { static_assert(is_unit_t_v<_unit_lhs>, "only supported for SI::unit_t"); diff --git a/include/SI/detail/unit_cast.h b/include/SI/detail/unit_cast.h new file mode 100644 index 00000000..e0376d13 --- /dev/null +++ b/include/SI/detail/unit_cast.h @@ -0,0 +1,45 @@ +#pragma once + +#include + +namespace SI::detail { + +// forward declaration +template +struct unit_t; + +/// helper template to check if a type is a unit_t (false for all other +/// types) +template struct is_unit_t : std::false_type {}; + +/// template specialisation to check if a type is a unit_t (true if unit_t) +template +struct is_unit_t> + : std::true_type {}; + +/// non-const specialisation of check above +template +struct is_unit_t> : std::true_type {}; + +template +inline constexpr bool is_unit_t_v = is_unit_t<_type>::value; + +/// function to cast between two units of the same type +template +constexpr auto unit_cast(const _rhs_T &rhs) { + // using static assert instead of std::enable if in order to be able to + // forward declare this function easier + static_assert( + is_unit_t_v<_rhs_T> || + std::is_base_of< + unit_t<_rhs_T::symbol::value, typename _rhs_T::exponent, + typename _rhs_T::internal_type, typename _rhs_T::ratio>, + _rhs_T>::value, + "is of type unit_t or a derived class"); + using conversion_ratio = + std::ratio_divide; + + return _target_type( + ((rhs.value() * conversion_ratio::num) / conversion_ratio::den)); +} +} // namespace SI::detail \ No newline at end of file From eb057841ecf237a248f27b60d3234e90396747f5 Mon Sep 17 00:00:00 2001 From: Dominik Berner Date: Wed, 24 Mar 2021 08:44:29 +0100 Subject: [PATCH 2/6] Move cross unit operations to own file --- CHANGELOG.md | 3 +- include/SI/detail/cross_unit_operations.h | 49 +++++++++++++++++ include/SI/detail/detail.h | 20 +++++++ include/SI/detail/operator_helpers.h | 1 + include/SI/detail/unit.h | 53 ------------------- include/SI/detail/unit_cast.h | 36 ++++++------- test/src/detail_tests/detail_tests.cc | 2 + .../unit_helper_functions_tests.cc | 1 + .../detail_tests/unit_t_conversions_tests.cc | 1 + 9 files changed, 92 insertions(+), 74 deletions(-) create mode 100644 include/SI/detail/cross_unit_operations.h diff --git a/CHANGELOG.md b/CHANGELOG.md index 85997865..d04eaf6e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,8 @@ ## 2.0.5 -* Move helper function such as `unit_cast` to own file for easier maintenance +* Moved helper function such as `unit_cast` and `unit_with_common_ratio` to own file for easier maintenance +* Moved cross-unit-operations to a separate file for easier maintenance * Fixed typos in documentation ## 2.0.4 diff --git a/include/SI/detail/cross_unit_operations.h b/include/SI/detail/cross_unit_operations.h new file mode 100644 index 00000000..d8aff25e --- /dev/null +++ b/include/SI/detail/cross_unit_operations.h @@ -0,0 +1,49 @@ +#pragma once + +#include "detail.h" + +namespace SI::detail { +// forward declaration +template +struct unit_t; + +/// divide a value of a certain unit with another value of a possibly +/// different type resulting in a new type, the resulting exponent is +/// specified by resulting unit using a variadic template to simplify usage of +/// implementation the internal type of the result is the internal type of lhs +template