aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFelix Morgner <felix.morgner@gmail.com>2019-12-31 16:54:49 +0100
committerFelix Morgner <felix.morgner@gmail.com>2019-12-31 16:54:55 +0100
commit9c95106af2fbaa9f5f8f605ee7df54cc90356df6 (patch)
treed3a65f48c4b63a63fa4615ab5f0c94c01680121c
parent1cce84ccacccba82362ef16f8207a4e6d9fbc1fe (diff)
downloadnewtype-9c95106af2fbaa9f5f8f605ee7df54cc90356df6.tar.xz
newtype-9c95106af2fbaa9f5f8f605ee7df54cc90356df6.zip
new_type: implement addition via nt::Arithmetic
-rw-r--r--CMakeLists.txt3
-rw-r--r--doc/src/index.rst12
-rw-r--r--include/newtype/impl/type_traits_extensions.hpp66
-rw-r--r--include/newtype/new_type.hpp21
-rw-r--r--test/include/arithmetic_suite.hpp11
-rw-r--r--test/src/arithmetic_suite.cpp53
-rw-r--r--test/src/driver.cpp2
7 files changed, 167 insertions, 1 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 8903e3c..8d9d1fe 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -50,8 +50,9 @@ if(BUILD_TESTING)
add_executable("${PROJECT_NAME}_tests"
"${PROJECT_SOURCE_DIR}/test/src/driver.cpp"
- "${PROJECT_SOURCE_DIR}/test/src/derivation_clause_suite.cpp"
+ "${PROJECT_SOURCE_DIR}/test/src/arithmetic_suite.cpp"
"${PROJECT_SOURCE_DIR}/test/src/conversion_suite.cpp"
+ "${PROJECT_SOURCE_DIR}/test/src/derivation_clause_suite.cpp"
"${PROJECT_SOURCE_DIR}/test/src/equality_comparison_suite.cpp"
"${PROJECT_SOURCE_DIR}/test/src/io_operators_suite.cpp"
"${PROJECT_SOURCE_DIR}/test/src/new_type_constructor_suite.cpp"
diff --git a/doc/src/index.rst b/doc/src/index.rst
index 60c6fdb..6c341c2 100644
--- a/doc/src/index.rst
+++ b/doc/src/index.rst
@@ -272,6 +272,18 @@ Stream I/O Operators
.. versionadded:: 1.0.0
+Arithmetic Operators
+~~~~~~~~~~~~~~~~~~~~
+
+.. 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 input operator shall be noexcept iff. :cpp:type:`new_type<BaseType, TagType, DerivationClause>::base_type` is nothrow addable as well as nothrow copy-constructible.
+
+ **enablement:** This operator shall be available iff. a) :cpp:type:`new_type<BaseType, TagType, DerivationClause>::base_type` is addable using the operator :literal:`+` and b) :cpp:type:`DerivationClause` includes :cpp:var:`Arithmetic`.
+
Header :literal:`<newtype/derivable.hpp>`
=========================================
diff --git a/include/newtype/impl/type_traits_extensions.hpp b/include/newtype/impl/type_traits_extensions.hpp
index ae0c7d5..d6dbfd6 100644
--- a/include/newtype/impl/type_traits_extensions.hpp
+++ b/include/newtype/impl/type_traits_extensions.hpp
@@ -513,6 +513,72 @@ namespace nt::impl
} // namespace iostreamable
+ inline namespace arithmetic
+ {
+
+ /**
+ * @brief A trait to test if a given type is addable
+ *
+ * @tparam T The type to test
+ * @note This specialization forms the base case for non-addable T
+ */
+ template<typename T, typename = void>
+ struct is_addable : std::false_type
+ {
+ };
+
+ /**
+ * @brief A trait to test if a given type is input streamable
+ *
+ * @tparam T The type to test
+ * @note This specialization forms the case for addable T
+ */
+ template<typename T>
+ struct is_addable<T, std::void_t<decltype(std::declval<T const &>() + std::declval<T const &>())>> : std::true_type
+ {
+ };
+
+ /**
+ * @brief A variable template to test if a given type is addable
+ *
+ * @tparam T The type to test
+ */
+ template<typename T>
+ auto constexpr is_addable_v = is_addable<T>::value;
+
+ /**
+ * @brief A trait to test if a given type is noexcept addable
+ *
+ * @tparam T The type to test
+ * @note This specialization forms the base case for non-noexcept addable or non-addable T
+ */
+ template<typename T, typename = void>
+ struct is_nothrow_addable : std::false_type
+ {
+ };
+
+ /**
+ * @brief A trait to test if a given type is noexcept addable
+ *
+ * @tparam T The type to test
+ * @note This specialization forms the case for addable T detemining if T is noexcept addable
+ */
+ template<typename T>
+ struct is_nothrow_addable<T, std::void_t<decltype(std::declval<T const &>() + std::declval<T const &>())>>
+ : std::bool_constant<noexcept(std::declval<T const &>() + std::declval<T const &>())>
+ {
+ };
+
+ /**
+ * @brief A variable template to test if a given type is noexcept addable
+ *
+ * @tparam T The type to test
+ */
+ template<typename T>
+ auto constexpr is_nothrow_addable_v = is_nothrow_addable<T>::value;
+
+ } // namespace arithmetic
+
} // namespace nt::impl
#endif \ No newline at end of file
diff --git a/include/newtype/new_type.hpp b/include/newtype/new_type.hpp
index 77b531e..8228a94 100644
--- a/include/newtype/new_type.hpp
+++ b/include/newtype/new_type.hpp
@@ -325,6 +325,27 @@ namespace nt
{
return input >> target.m_value;
}
+
+ /// @section Arithmetic operators
+
+ /**
+ * @brief Add 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
+ * type is addable.
+ * @param lhs The left-hand side of the addition
+ * @param rhs The right-hand side of the addition
+ * @return a new instance of the same nt::new_type
+ */
+ template<typename BaseType, typename TagType, auto DerivationClause>
+ auto constexpr
+ operator+(new_type<BaseType, TagType, DerivationClause> const & lhs, new_type<BaseType, TagType, DerivationClause> const & rhs) noexcept(
+ impl::is_nothrow_addable_v<BaseType> && std::is_nothrow_copy_constructible_v<BaseType>)
+ -> std::enable_if_t<DerivationClause(nt::Arithmetic) && impl::is_addable_v<BaseType>, new_type<BaseType, TagType, DerivationClause>>
+ {
+ return {lhs.decay() + rhs.decay()};
+ }
+
} // namespace nt
#endif
diff --git a/test/include/arithmetic_suite.hpp b/test/include/arithmetic_suite.hpp
new file mode 100644
index 0000000..1eb4af8
--- /dev/null
+++ b/test/include/arithmetic_suite.hpp
@@ -0,0 +1,11 @@
+#ifndef NEWTYPE_TEST_ARITHMETIC_SUITE_HPP
+#define NEWTYPE_TEST_ARITHMETIC_SUITE_HPP
+
+#include <cute/cute_suite.h>
+
+#include <string>
+#include <utility>
+
+auto arithmetic_suite() -> std::pair<cute::suite, std::string>;
+
+#endif \ No newline at end of file
diff --git a/test/src/arithmetic_suite.cpp b/test/src/arithmetic_suite.cpp
new file mode 100644
index 0000000..4541125
--- /dev/null
+++ b/test/src/arithmetic_suite.cpp
@@ -0,0 +1,53 @@
+#include "conversion_suite.hpp"
+#include "kawaii.hpp"
+#include "newtype/derivable.hpp"
+#include "newtype/deriving.hpp"
+#include "newtype/impl/type_traits_extensions.hpp"
+#include "newtype/new_type.hpp"
+
+#include <cute/cute.h>
+
+#include <type_traits>
+
+inline namespace addition_tests
+{
+
+ auto a_new__type_not_deriving_arithmetic_is_not_addable_with_instances_of_itself() -> void
+ {
+ using type_alias = nt::new_type<int, struct tag>;
+ ASSERT(!(nt::impl::is_addable_v<type_alias>));
+ }
+
+ auto a_new__type_deriving_arithmetic_is_addable_with_instances_of_itself() -> void
+ {
+ using type_alias = nt::new_type<int, struct tag, deriving(nt::Arithmetic)>;
+ ASSERT(nt::impl::is_addable_v<type_alias>);
+ }
+
+ auto addition_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 const &>() + std::declval<type_alias const &>())>));
+ }
+
+ auto addition_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 lhs = type_alias{24};
+ auto rhs = type_alias{18};
+ ASSERT_EQUAL(24 + 18, (lhs + rhs).decay());
+ }
+
+} // namespace addition_tests
+
+auto arithmetic_suite() -> std::pair<cute::suite, std::string>
+{
+ return {{
+ /// Addition Tests
+ KAWAII(a_new__type_not_deriving_arithmetic_is_not_addable_with_instances_of_itself),
+ KAWAII(a_new__type_deriving_arithmetic_is_addable_with_instances_of_itself),
+ KAWAII(addition_of_two_instances_of_a_new__type_deriving_arithmetic_produces_an_instance_of_the_same_new__type),
+ KAWAII(addition_of_two_instances_of_a_new__type_deriving_arithmetic_produces_the_correct_value_with_respect_to_the_base_type),
+ },
+ "Arithmetic Operators Tests"};
+} \ No newline at end of file
diff --git a/test/src/driver.cpp b/test/src/driver.cpp
index c650dda..c93f157 100644
--- a/test/src/driver.cpp
+++ b/test/src/driver.cpp
@@ -1,3 +1,4 @@
+#include "arithmetic_suite.hpp"
#include "conversion_suite.hpp"
#include "derivation_clause_suite.hpp"
#include "equality_comparison_suite.hpp"
@@ -57,6 +58,7 @@ int main(int argc, char ** argv)
equality_comparison_suite(),
relational_operators_suite(),
io_operators_suite(),
+ arithmetic_suite(),
};
auto selectors = get_test_selectors(suites);