diff options
| author | Felix Morgner <felix.morgner@gmail.com> | 2020-01-02 15:42:12 +0100 |
|---|---|---|
| committer | Felix Morgner <felix.morgner@gmail.com> | 2020-01-02 15:42:12 +0100 |
| commit | 05abaa3d00393165e7794a150ad6c4b5ac5dd37e (patch) | |
| tree | bf31b1f546b8e04ba69f3750c095cda2575020a5 | |
| parent | 350a6e397f544d3c5444cbfd79d672b34cfd268c (diff) | |
| download | newtype-05abaa3d00393165e7794a150ad6c4b5ac5dd37e.tar.xz newtype-05abaa3d00393165e7794a150ad6c4b5ac5dd37e.zip | |
new_type: implement multiply-assignment
| -rw-r--r-- | doc/src/index.rst | 8 | ||||
| -rw-r--r-- | include/newtype/impl/type_traits_extensions.hpp | 61 | ||||
| -rw-r--r-- | include/newtype/new_type.hpp | 27 | ||||
| -rw-r--r-- | test/src/arithmetic_suite.cpp | 27 |
4 files changed, 118 insertions, 5 deletions
diff --git a/doc/src/index.rst b/doc/src/index.rst index 591a168..4c13412 100644 --- a/doc/src/index.rst +++ b/doc/src/index.rst @@ -320,6 +320,14 @@ Arithmetic Operators **enablement:** This operator shall be available iff. a) :cpp:type:`new_type<BaseType, TagType, DerivationClause>::base_type` is multipliable using the operator :literal:`*` and b) :cpp:type:`DerivationClause` includes :cpp:var:`Arithmetic`. +.. cpp:function:: template<typename BaseType, \ + typename TagType, \ + auto DerivationClause> \ + constexpr new_type<BaseType, TagType, DerivationClause> & operator*=(new_type<BaseType, TagType, DerivationClause> const & lhs, new_type<BaseType, TagType, DerivationClause> const & rhs) + + **noexcept specification:** This operator shall be noexcept iff. :cpp:type:`new_type<BaseType, TagType, DerivationClause>::base_type` is nothrow multiply-assignable. + + **enablement:** This operator shall be available iff. a) :cpp:type:`new_type<BaseType, TagType, DerivationClause>::base_type` is multiply-assignable using the operator :literal:`-=` and b) :cpp:type:`DerivationClause` includes :cpp:var:`Arithmetic`. .. cpp:function:: template<typename BaseType, \ typename TagType, \ diff --git a/include/newtype/impl/type_traits_extensions.hpp b/include/newtype/impl/type_traits_extensions.hpp index 5104306..6224d29 100644 --- a/include/newtype/impl/type_traits_extensions.hpp +++ b/include/newtype/impl/type_traits_extensions.hpp @@ -887,6 +887,67 @@ namespace nt::impl template<typename T> auto constexpr is_nothrow_subtract_assignable_v = is_nothrow_subtract_assignable<T>::value; + /** + * @brief A trait to test if a given type is multiply-assignable + * + * @tparam T The type to test + * @note This specialization forms the base case for non-multiply-assignable T + */ + template<typename T, typename = void> + struct is_multiply_assignable : std::false_type + { + }; + + /** + * @brief A trait to test if a given type is multiply-assignable + * + * @tparam T The type to test + * @note This specialization forms the case for multiply-assignable T + */ + template<typename T> + struct is_multiply_assignable<T, std::void_t<decltype(std::declval<T &>() *= std::declval<T const &>())>> : std::true_type + { + }; + + /** + * @brief A variable template to test if a given type is multiply-assignable + * + * @tparam T The type to test + */ + template<typename T> + auto constexpr is_multiply_assignable_v = is_multiply_assignable<T>::value; + + /** + * @brief A trait to test if a given type is noexcept multiply-assignable + * + * @tparam T The type to test + * @note This specialization forms the base case for non-noexcept multiply-assignable or non-multiply-assignable T + */ + template<typename T, typename = void> + struct is_nothrow_multiply_assignable : std::false_type + { + }; + + /** + * @brief A trait to test if a given type is noexcept multiply-assignable + * + * @tparam T The type to test + * @note This specialization forms the case for multiply-assignable T detemining if T is noexcept multiply-assignable + */ + template<typename T> + struct is_nothrow_multiply_assignable<T, std::void_t<decltype(std::declval<T &>() *= std::declval<T const &>())>> + : std::bool_constant<noexcept(std::declval<T &>() *= std::declval<T const &>())> + { + }; + + /** + * @brief A variable template to test if a given type is noexcept multiply-assignable + * + * @tparam T The type to test + */ + template<typename T> + auto constexpr is_nothrow_multiply_assignable_v = is_nothrow_multiply_assignable<T>::value; + } // namespace compound_arithmetic } // namespace nt::impl diff --git a/include/newtype/new_type.hpp b/include/newtype/new_type.hpp index 8d55f61..a7ab4af 100644 --- a/include/newtype/new_type.hpp +++ b/include/newtype/new_type.hpp @@ -50,6 +50,13 @@ namespace nt -> std::enable_if_t<DerivationClauseV(nt::Arithmetic) && impl::is_subtract_assignable_v<BaseTypeT>, new_type<BaseTypeT, TagTypeT, DerivationClauseV> &>; + template<typename BaseTypeT, typename TagTypeT, auto DerivationClauseV> + auto constexpr friend + operator*=(new_type<BaseTypeT, TagTypeT, DerivationClauseV> & lhs, + new_type<BaseTypeT, TagTypeT, DerivationClauseV> const & rhs) noexcept(impl::is_nothrow_multiply_assignable_v<BaseTypeT>) + -> std::enable_if_t<DerivationClauseV(nt::Arithmetic) && impl::is_multiply_assignable_v<BaseTypeT>, + new_type<BaseTypeT, TagTypeT, DerivationClauseV> &>; + using super = impl::new_type_move_assignment<BaseType, TagType>; public: @@ -436,6 +443,26 @@ namespace nt } /** + * @brief Multiply two instances of the same nt::new_type, modifying the left-hand side + * + * @note This operator is only available if the derivation clause of the passed in nt::new_type objects contains nt::Arithmetic and the base + * type is multiplyable. + * @param lhs The left-hand side of the multiplyition + * @param rhs The right-hand side of the multiplyition + * @return a reference to the the modified value + */ + template<typename BaseType, typename TagType, auto DerivationClause> + auto constexpr + operator*=(new_type<BaseType, TagType, DerivationClause> & lhs, + new_type<BaseType, TagType, DerivationClause> const & rhs) noexcept(impl::is_nothrow_multiply_assignable_v<BaseType>) + -> std::enable_if_t<DerivationClause(nt::Arithmetic) && impl::is_multiply_assignable_v<BaseType>, + new_type<BaseType, TagType, DerivationClause> &> + { + lhs.m_value *= rhs.m_value; + return lhs; + } + + /** * @brief Divide two instances of the same nt::new_type * * @note This operator is only available if the derivation clause of the passed in nt::new_type objects contains nt::Arithmetic and the base diff --git a/test/src/arithmetic_suite.cpp b/test/src/arithmetic_suite.cpp index 75a7f46..c7051e2 100644 --- a/test/src/arithmetic_suite.cpp +++ b/test/src/arithmetic_suite.cpp @@ -202,7 +202,7 @@ inline namespace division_tests } // namespace division_tests -inline namespace compound_addition +inline namespace compound_arithmetic { auto addition_assignment_of_two_instances_of_a_new__type_deriving_arithmetic_produces_an_instance_of_the_same_new__type() -> void @@ -239,7 +239,24 @@ inline namespace compound_addition ASSERT_EQUAL(elhs -= erhs, (lhs -= rhs).decay()); } -} // namespace compound_addition + auto multiplication_assignment_of_two_instances_of_a_new__type_deriving_arithmetic_produces_an_instance_of_the_same_new__type() -> void + { + using type_alias = nt::new_type<int, struct tag, deriving(nt::Arithmetic)>; + ASSERT((std::is_same_v<type_alias &, decltype(std::declval<type_alias &>() *= std::declval<type_alias const &>())>)); + } + + auto multiplication_assignment_of_two_instances_of_a_new__type_deriving_arithmetic_produces_the_correct_value_with_respect_to_the_base_type() + -> void + { + using type_alias = nt::new_type<int, struct tag, deriving(nt::Arithmetic)>; + auto elhs = 42; + auto erhs = 18; + auto lhs = type_alias{elhs}; + auto rhs = type_alias{erhs}; + ASSERT_EQUAL(elhs *= erhs, (lhs *= rhs).decay()); + } + +} // namespace compound_arithmetic auto arithmetic_suite() -> std::pair<cute::suite, std::string> { @@ -279,13 +296,13 @@ auto arithmetic_suite() -> std::pair<cute::suite, std::string> KAWAII(division_of_two_instances_of_a_new__type_deriving_arithmetic_produces_an_instance_of_the_same_new__type), KAWAII(division_of_two_instances_of_a_new__type_deriving_arithmetic_produces_the_correct_value_with_respect_to_the_base_type), - /// Compound Addition Tests + /// Compound Arithmetic Tests KAWAII(addition_assignment_of_two_instances_of_a_new__type_deriving_arithmetic_produces_an_instance_of_the_same_new__type), KAWAII(addition_assignment_of_two_instances_of_a_new__type_deriving_arithmetic_produces_the_correct_value_with_respect_to_the_base_type), - - /// Compound Addition Tests KAWAII(subtraction_assignment_of_two_instances_of_a_new__type_deriving_arithmetic_produces_an_instance_of_the_same_new__type), KAWAII(subtraction_assignment_of_two_instances_of_a_new__type_deriving_arithmetic_produces_the_correct_value_with_respect_to_the_base_type), + KAWAII(multiplication_assignment_of_two_instances_of_a_new__type_deriving_arithmetic_produces_an_instance_of_the_same_new__type), + KAWAII(multiplication_assignment_of_two_instances_of_a_new__type_deriving_arithmetic_produces_the_correct_value_with_respect_to_the_base_type), // clang-format on }, |
