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 --- include/newtype/impl/new_type_iterator_types.hpp | 24 +++++++ include/newtype/impl/type_traits_extensions.hpp | 90 ++++++++++++++++++++++++ include/newtype/new_type.hpp | 62 +++++++++++++++- 3 files changed, 175 insertions(+), 1 deletion(-) (limited to 'include') 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 -- cgit v1.2.3