From b01d495beb586d8d18ef592d80f593ea1bbe10e2 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Fri, 28 Feb 2020 06:46:26 +0100 Subject: new_type: implement rbegin and crbegin --- doc/src/index.rst | 99 +++++++++++++++++ include/newtype/impl/new_type_iterator_types.hpp | 24 ++++ include/newtype/impl/type_traits_extensions.hpp | 90 +++++++++++++++ include/newtype/new_type.hpp | 62 ++++++++++- test/src/iterable_suite.cpp | 136 ++++++++++++++++++++++- 5 files changed, 409 insertions(+), 2 deletions(-) diff --git a/doc/src/index.rst b/doc/src/index.rst index cd016b9..a54d54c 100644 --- a/doc/src/index.rst +++ b/doc/src/index.rst @@ -99,6 +99,18 @@ Class template :cpp:class:`new_type` .. versionadded:: 1.1.0 + .. cpp:type:: reverse_iterator = typename BaseType::reverse_iterator + + :enablement: This type alias shall be defined iff. this :cpp:class:`new_type`'s :cpp:type:`base_type` has a member type :cpp:type:`reverse_iterator ` and the :cpp:var:`derivation clause ` contains :cpp:var:`Iterable`. + + .. versionadded:: 1.1.0 + + .. cpp:type:: const_reverse_iterator = typename BaseType::const_reverse_iterator + + :enablement: This type alias shall be defined iff. this :cpp:class:`new_type`'s :cpp:type:`base_type` has a member type :cpp:type:`const_reverse_iterator ` and the :cpp:var:`derivation clause ` contains :cpp:var:`Iterable`. + + .. versionadded:: 1.1.0 + **Static Data Members** .. cpp:var:: static derivation_clause_type constexpr derivation_clause = DerivationClause @@ -242,6 +254,39 @@ Class template :cpp:class:`new_type` .. versionadded:: 1.1.0 + .. cpp:function:: constexpr iterator rbegin() + + Get a reverse iterator to the beginning of the object contained by this :cpp:class:`new_type` + + :enablement: This function shall be available iff. + + a) this :cpp:class:`new_type`'s :cpp:var:`derivation clause ` contains :cpp:var:`Iterable` and + b) this :cpp:class:`new_type`'s :cpp:type:`base type ` has a non-static member function :cpp:func:`rbegin() ` that returns an instance of type :cpp:type:`reverse_iterator` + + .. versionadded:: 1.1.0 + + .. cpp:function:: constexpr iterator rbegin() const + + Get a constant reverse iterator to the beginning of the object contained by this :cpp:class:`new_type` + + :enablement: This function shall be available iff. + + a) this :cpp:class:`new_type`'s :cpp:var:`derivation clause ` contains :cpp:var:`Iterable` and + b) this :cpp:class:`new_type`'s :cpp:type:`base type ` has a non-static member function :cpp:func:`rbegin() const ` that returns an instance of type :cpp:type:`const_reverse_iterator` + + .. versionadded:: 1.1.0 + + .. cpp:function:: constexpr iterator crbegin() const + + Get a constant reverse iterator to the beginning of the object contained by this :cpp:class:`new_type` + + :enablement: This function shall be available iff. + + a) this :cpp:class:`new_type`'s :cpp:var:`derivation clause ` contains :cpp:var:`Iterable` and + b) this :cpp:class:`new_type`'s :cpp:type:`base type ` has a non-static member function :cpp:func:`crbegin() const ` that returns an instance of type :cpp:type:`const_reverse_iterator` + + .. versionadded:: 1.1.0 + :literal:`namespace`-level functions and function templates ----------------------------------------------------------- @@ -750,6 +795,60 @@ Iterators .. versionadded:: 1.1.0 +.. cpp:function:: template \ + constexpr new_type::reverse_iterator rbegin(new_type & obj) + + Get a reverse iterator to the beginning of the object contained by an instance of :cpp:class:`new_type` + + :tparam BaseType: |BaseTypeDoc| + :tparam TagType: |TagTypeDoc| + :tparam DerivationClause: |DerivationClauseDoc| + :param obj: The object to retrieve the iterator from + :returns: An iterator to the begining of the object of contained by :literal:`obj`. + :throws: Any exception + :enablement: This function shall be available iff. + + a) :cpp:var:`derivation clause ` contains :cpp:var:`Iterable` and + b) for the :cpp:class:`new_type`'s :cpp:type:`base type ` exists a namespace-level function :literal:`rbegin(BaseType &)` that returns an instance of type :cpp:type:`new_type::reverse_iterator` + + .. versionadded:: 1.1.0 + +.. cpp:function:: template \ + constexpr new_type::const_reverse_iterator rbegin(new_type const & obj) + + Get a constant reverse iterator to the beginning of the object contained by an instance of :cpp:class:`new_type` + + :tparam BaseType: |BaseTypeDoc| + :tparam TagType: |TagTypeDoc| + :tparam DerivationClause: |DerivationClauseDoc| + :param obj: The object to retrieve the iterator from + :returns: An iterator to the begining of the object of contained by :cpp:var:`obj`. + :throws: Any exception + :enablement: This function shall be available iff. + + a) this :cpp:class:`new_type`'s :cpp:var:`derivation clause ` contains :cpp:var:`Iterable` and + b) for the :cpp:class:`new_type`'s :cpp:type:`base type ` exists a namespace-level function :literal:`rbegin(BaseType const &)` that returns an instance of type :cpp:type:`new_type::const_reverse_iterator` + + .. versionadded:: 1.1.0 + +.. cpp:function:: template \ + constexpr new_type::const_reverse_iterator crbegin(new_type const & obj) + + Get a constant reverse iterator to the beginning of the object contained by an instance of :cpp:class:`new_type` + + :tparam BaseType: |BaseTypeDoc| + :tparam TagType: |TagTypeDoc| + :tparam DerivationClause: |DerivationClauseDoc| + :param obj: The object to retrieve the iterator from + :returns: An iterator to the begining of the object of contained by :cpp:var:`obj`. + :throws: Any exception + :enablement: This function shall be available iff. + + a) this :cpp:class:`new_type`'s :cpp:var:`derivation clause ` contains :cpp:var:`Iterable` and + b) for the :cpp:class:`new_type`'s :cpp:type:`base type ` exists a namespace-level function :literal:`crbegin(BaseType const &)` that returns an instance of type :cpp:type:`new_type::const_reverse_iterator` + + .. versionadded:: 1.1.0 + :cpp:class:`std::hash` Support ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/include/newtype/impl/new_type_iterator_types.hpp b/include/newtype/impl/new_type_iterator_types.hpp index 037f08d..2ea8274 100644 --- a/include/newtype/impl/new_type_iterator_types.hpp +++ b/include/newtype/impl/new_type_iterator_types.hpp @@ -30,10 +30,34 @@ namespace nt::impl using const_iterator = typename T::const_iterator; }; + template> + struct new_type_reverse_iterator + { + }; + + template + struct new_type_reverse_iterator> + { + using reverse_iterator = typename T::reverse_iterator; + }; + + template> + struct new_type_const_reverse_iterator + { + }; + + template + struct new_type_const_reverse_iterator> + { + using const_reverse_iterator = typename T::const_reverse_iterator; + }; + template struct new_type_iterator_types : new_type_iterator , new_type_const_iterator + , new_type_reverse_iterator + , new_type_const_reverse_iterator { }; diff --git a/include/newtype/impl/type_traits_extensions.hpp b/include/newtype/impl/type_traits_extensions.hpp index a9bd6af..f7dafef 100644 --- a/include/newtype/impl/type_traits_extensions.hpp +++ b/include/newtype/impl/type_traits_extensions.hpp @@ -578,6 +578,96 @@ namespace nt::impl auto constexpr has_cbegin_v = has_cbegin::value; } // namespace iterable_cbegin + inline namespace iterable_rbegin + { + template + struct has_free_rbegin : std::false_type + { + }; + + template + struct has_free_rbegin()))>> + : std::is_same()))>> + { + }; + + template + struct has_free_rbegin()))>> + : std::is_same()))>> + { + }; + + template + auto constexpr has_free_rbegin_v = has_free_rbegin::value; + + template + struct has_member_rbegin : std::false_type + { + }; + + template + struct has_member_rbegin().rbegin())>> + : std::is_same().rbegin())>> + { + }; + + template + struct has_member_rbegin().rbegin())>> + : std::is_same().rbegin())>> + { + }; + + template + auto constexpr has_member_rbegin_v = has_member_rbegin::value; + + template + struct has_rbegin : std::disjunction, has_member_rbegin> + { + }; + + template + auto constexpr has_rbegin_v = has_rbegin::value; + } // namespace iterable_rbegin + + inline namespace iterable_rbegin + { + template + struct has_free_crbegin : std::false_type + { + }; + + template + struct has_free_crbegin()))>> + : std::is_same()))>> + { + }; + + template + auto constexpr has_free_crbegin_v = has_free_crbegin::value; + + template + struct has_member_crbegin : std::false_type + { + }; + + template + struct has_member_crbegin().crbegin())>> + : std::is_same().crbegin())>> + { + }; + + template + auto constexpr has_member_crbegin_v = has_member_crbegin::value; + + template + struct has_crbegin : std::disjunction, has_member_crbegin> + { + }; + + template + auto constexpr has_crbegin_v = has_crbegin::value; + } // namespace iterable_rbegin + } // 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 24ac71a..36a3927 100644 --- a/include/newtype/new_type.hpp +++ b/include/newtype/new_type.hpp @@ -65,7 +65,7 @@ namespace nt template auto constexpr friend begin(new_type const & obj) - -> std::enable_if_t, + -> std::enable_if_t, typename new_type::const_iterator>; template @@ -73,6 +73,21 @@ namespace nt -> std::enable_if_t, typename new_type::const_iterator>; + template + auto constexpr friend rbegin(new_type & obj) + -> std::enable_if_t, + typename new_type::reverse_iterator>; + + template + auto constexpr friend rbegin(new_type const & obj) + -> std::enable_if_t, + typename new_type::const_reverse_iterator>; + + template + auto constexpr friend crbegin(new_type const & obj) + -> std::enable_if_t, + typename new_type::const_reverse_iterator>; + using super = impl::new_type_move_assignment; public: @@ -140,6 +155,27 @@ namespace nt { return this->m_value.cbegin(); } + + template * = nullptr> + auto constexpr rbegin() + -> std::enable_if_t, typename NewType::reverse_iterator> + { + return this->m_value.rbegin(); + } + + template + auto constexpr rbegin() const -> std::enable_if_t, + typename NewType::const_reverse_iterator> + { + return this->m_value.rbegin(); + } + + template + auto constexpr crbegin() const -> std::enable_if_t, + typename NewType::const_reverse_iterator> + { + return this->m_value.crbegin(); + } }; template @@ -350,6 +386,30 @@ namespace nt return cbegin(obj.m_value); } + template + auto constexpr rbegin(new_type & obj) + -> std::enable_if_t, + typename new_type::reverse_iterator> + { + return rbegin(obj.m_value); + } + + template + auto constexpr rbegin(new_type const & obj) + -> std::enable_if_t, + typename new_type::const_reverse_iterator> + { + return rbegin(obj.m_value); + } + + template + auto constexpr crbegin(new_type const & obj) + -> std::enable_if_t, + typename new_type::const_reverse_iterator> + { + return crbegin(obj.m_value); + } + } // namespace nt namespace std diff --git a/test/src/iterable_suite.cpp b/test/src/iterable_suite.cpp index 3bdb16b..778dc35 100644 --- a/test/src/iterable_suite.cpp +++ b/test/src/iterable_suite.cpp @@ -18,10 +18,15 @@ namespace { using iterator = void *; using const_iterator = void const *; + using reverse_iterator = void *; + using const_reverse_iterator = void const *; auto begin() -> iterator; auto begin() const -> const_iterator; auto cbegin() const -> const_iterator; + auto rbegin() -> reverse_iterator; + auto rbegin() const -> const_reverse_iterator; + auto crbegin() const -> const_reverse_iterator; }; } // namespace @@ -135,6 +140,117 @@ inline namespace cbegin_tests } // namespace cbegin_tests +inline namespace rbegin_tests +{ + + auto a_new__type_not_deriving_iterable_has_no_rbegin() -> void + { + using type_alias = nt::new_type; + ASSERT(!(nt::impl::has_rbegin_v)); + } + + auto a_new__type_based_on_a_non_iterable_type_deriving_iterable_has_no_rbegin() -> void + { + static_assert(!nt::impl::has_rbegin_v); + using type_alias = nt::new_type; + ASSERT(!(nt::impl::has_rbegin_v)); + } + + auto a_new__type_based_on_an_iterable_type_with_member_rbegin_deriving_iterable_has_member_rbegin() -> void + { + static_assert(nt::impl::has_member_rbegin_v); + using type_alias = nt::new_type; + ASSERT(nt::impl::has_member_rbegin_v); + } + + auto a_new__type_based_on_an_iterable_type_with_constant_member_rbegin_deriving_iterable_has_constant_member_rbegin() -> void + { + static_assert(nt::impl::has_member_rbegin_v); + using type_alias = nt::new_type; + ASSERT(nt::impl::has_member_rbegin_v); + } + + auto a_new__type_based_on_an_iterable_type_without_free_rbegin_deriving_iterable_has_no_free_rbegin() -> void + { + static_assert(!nt::impl::has_free_rbegin_v); + using type_alias = nt::new_type; + ASSERT(!nt::impl::has_free_rbegin_v); + } + + auto a_new__type_based_on_an_iterable_type_without_constant_free_rbegin_deriving_iterable_has_no_constant_free_rbegin() -> void + { + static_assert(!nt::impl::has_free_rbegin_v); + using type_alias = nt::new_type; + ASSERT(!nt::impl::has_free_rbegin_v); + } + + auto accessing_the_first_element_of_a_reverse_iterator_on_a_new__type_yields_the_same_value_as_accessing_it_through_an_unwrapped_type() + -> void + { + using type_alias = nt::new_type, struct tag, deriving(nt::Iterable)>; + auto weak = std::array{42, 21, 10}; + auto strong = type_alias{{42, 21, 10}}; + ASSERT_EQUAL(*weak.rbegin(), *strong.rbegin()); + } + + auto an_iterator_obtained_via_member_rbegin_compares_equal_to_an_iterator_obtained_via_free_rbegin() -> void + { + using type_alias = nt::new_type, struct tag, deriving(nt::Iterable)>; + auto instance = type_alias{{42, 21, 10}}; + ASSERT_EQUAL(rbegin(instance), instance.rbegin()); + } + +} // namespace rbegin_tests + +inline namespace crbegin_tests +{ + + auto a_new__type_not_deriving_iterable_has_no_crbegin() -> void + { + using type_alias = nt::new_type; + ASSERT(!(nt::impl::has_crbegin_v)); + } + + auto a_new__type_based_on_a_non_iterable_type_deriving_iterable_has_no_crbegin() -> void + { + static_assert(!nt::impl::has_crbegin_v); + using type_alias = nt::new_type; + ASSERT(!(nt::impl::has_crbegin_v)); + } + + auto a_new__type_based_on_an_iterable_type_with_member_crbegin_deriving_iterable_has_member_crbegin() -> void + { + static_assert(nt::impl::has_member_crbegin_v); + using type_alias = nt::new_type; + ASSERT(nt::impl::has_member_crbegin_v); + } + + auto a_new__type_based_on_an_iterable_type_without_free_crbegin_deriving_iterable_has_no_free_crbegin() -> void + { + static_assert(!nt::impl::has_free_crbegin_v); + using type_alias = nt::new_type; + ASSERT(!nt::impl::has_free_crbegin_v); + } + + auto + accessing_the_first_element_of_a_constant_reverse_iterator_on_a_new__type_yields_the_same_value_as_accessing_it_through_an_unwrapped_type() + -> void + { + using type_alias = nt::new_type, struct tag, deriving(nt::Iterable)>; + auto weak = std::array{42, 21, 10}; + auto strong = type_alias{{42, 21, 10}}; + ASSERT_EQUAL(*weak.crbegin(), *strong.crbegin()); + } + + auto an_iterator_obtained_via_member_crbegin_compares_equal_to_an_iterator_obtained_via_free_crbegin() -> void + { + using type_alias = nt::new_type, struct tag, deriving(nt::Iterable)>; + auto instance = type_alias{{42, 21, 10}}; + ASSERT_EQUAL(crbegin(instance), instance.crbegin()); + } + +} // namespace crbegin_tests + auto iterable_suite() -> std::pair { return { @@ -150,13 +266,31 @@ auto iterable_suite() -> std::pair KAWAII(accessing_the_first_element_of_an_iterator_on_a_new__type_yields_the_same_value_as_accessing_it_through_an_unwrapped_type), KAWAII(an_iterator_obtained_via_member_begin_compares_equal_to_an_iterator_obtained_via_free_begin), - /// 'begin' Tests + /// 'cbegin' Tests KAWAII(a_new__type_not_deriving_iterable_has_no_cbegin), KAWAII(a_new__type_based_on_a_non_iterable_type_deriving_iterable_has_no_cbegin), KAWAII(a_new__type_based_on_an_iterable_type_with_member_cbegin_deriving_iterable_has_member_cbegin), KAWAII(a_new__type_based_on_an_iterable_type_without_free_cbegin_deriving_iterable_has_no_free_cbegin), KAWAII(accessing_the_first_element_of_a_constant_iterator_on_a_new__type_yields_the_same_value_as_accessing_it_through_an_unwrapped_type), KAWAII(an_iterator_obtained_via_member_cbegin_compares_equal_to_an_iterator_obtained_via_free_cbegin), + + /// 'rbegin' Tests + KAWAII(a_new__type_not_deriving_iterable_has_no_rbegin), + KAWAII(a_new__type_based_on_a_non_iterable_type_deriving_iterable_has_no_rbegin), + KAWAII(a_new__type_based_on_an_iterable_type_with_member_rbegin_deriving_iterable_has_member_rbegin), + KAWAII(a_new__type_based_on_an_iterable_type_with_constant_member_rbegin_deriving_iterable_has_constant_member_rbegin), + KAWAII(a_new__type_based_on_an_iterable_type_without_free_rbegin_deriving_iterable_has_no_free_rbegin), + KAWAII(a_new__type_based_on_an_iterable_type_without_constant_free_rbegin_deriving_iterable_has_no_constant_free_rbegin), + KAWAII(accessing_the_first_element_of_a_reverse_iterator_on_a_new__type_yields_the_same_value_as_accessing_it_through_an_unwrapped_type), + KAWAII(an_iterator_obtained_via_member_rbegin_compares_equal_to_an_iterator_obtained_via_free_rbegin), + + /// 'cbegin' Tests + KAWAII(a_new__type_not_deriving_iterable_has_no_crbegin), + KAWAII(a_new__type_based_on_a_non_iterable_type_deriving_iterable_has_no_crbegin), + KAWAII(a_new__type_based_on_an_iterable_type_with_member_crbegin_deriving_iterable_has_member_crbegin), + KAWAII(a_new__type_based_on_an_iterable_type_without_free_crbegin_deriving_iterable_has_no_free_crbegin), + KAWAII(accessing_the_first_element_of_a_constant_reverse_iterator_on_a_new__type_yields_the_same_value_as_accessing_it_through_an_unwrapped_type), + KAWAII(an_iterator_obtained_via_member_crbegin_compares_equal_to_an_iterator_obtained_via_free_crbegin), // clang-format on }, "Iterable Tests"}; -- cgit v1.2.3