From 52d5330cbb866f050a8f0736c139586c8a22c387 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Fri, 21 Feb 2020 21:19:45 +0100 Subject: new_type: begin Iterable implementation --- include/newtype/derivable.hpp | 7 +++ include/newtype/impl/new_type_iterator_types.hpp | 42 +++++++++++++ include/newtype/impl/type_traits_extensions.hpp | 72 +++++++++++++++++++++ include/newtype/new_type.hpp | 80 +++++++++++++++++++++++- 4 files changed, 200 insertions(+), 1 deletion(-) create mode 100644 include/newtype/impl/new_type_iterator_types.hpp (limited to 'include') diff --git a/include/newtype/derivable.hpp b/include/newtype/derivable.hpp index 19c79d9..faa844d 100644 --- a/include/newtype/derivable.hpp +++ b/include/newtype/derivable.hpp @@ -60,6 +60,13 @@ namespace nt */ auto constexpr Indirection = derivable{}; + /** + * @brief A tag to enable derivation of the iterator accessors + * + * @since 1.0.0 + */ + auto constexpr Iterable = derivable{}; + /** * @brief A tag to enable derivation of the stream input operator * diff --git a/include/newtype/impl/new_type_iterator_types.hpp b/include/newtype/impl/new_type_iterator_types.hpp new file mode 100644 index 0000000..037f08d --- /dev/null +++ b/include/newtype/impl/new_type_iterator_types.hpp @@ -0,0 +1,42 @@ +#ifndef NEWTYPE_IMPL_NEW_TYPE_ITERATOR_TYPES_HPP +#define NEWTYPE_IMPL_NEW_TYPE_ITERATOR_TYPES_HPP + +#include "newtype/version.hpp" + +#include + +namespace nt::impl +{ + + template> + struct new_type_iterator + { + }; + + template + struct new_type_iterator> + { + using iterator = typename T::iterator; + }; + + template> + struct new_type_const_iterator + { + }; + + template + struct new_type_const_iterator> + { + using const_iterator = typename T::const_iterator; + }; + + template + struct new_type_iterator_types + : new_type_iterator + , new_type_const_iterator + { + }; + +} // namespace nt::impl + +#endif \ No newline at end of file diff --git a/include/newtype/impl/type_traits_extensions.hpp b/include/newtype/impl/type_traits_extensions.hpp index 1f46fb4..7096578 100644 --- a/include/newtype/impl/type_traits_extensions.hpp +++ b/include/newtype/impl/type_traits_extensions.hpp @@ -6,6 +6,7 @@ #include #include #include +#include #include namespace nt::impl @@ -1051,6 +1052,77 @@ namespace nt::impl } // namespace std_support + inline namespace iterable + { + template + struct has_std_begin : std::false_type + { + }; + + template + struct has_std_begin()))>> + : std::is_same()))>> + { + }; + + template + struct has_std_begin()))>> + : std::is_same()))>> + { + }; + + template + auto constexpr has_std_begin_v = has_std_begin::value; + + template + struct has_free_begin : std::false_type + { + }; + + template + struct has_free_begin()))>> + : std::is_same()))>> + { + }; + + template + struct has_free_begin()))>> + : std::is_same()))>> + { + }; + + template + auto constexpr has_free_begin_v = has_free_begin::value; + + template + struct has_member_begin : std::false_type + { + }; + + template + struct has_member_begin().begin())>> + : std::is_same().begin())>> + { + }; + + template + struct has_member_begin().begin())>> + : std::is_same().begin())>> + { + }; + + template + auto constexpr has_member_begin_v = has_member_begin::value; + + template + struct has_begin : std::disjunction, has_free_begin, has_member_begin> + { + }; + + template + auto constexpr has_begin_v = has_begin::value; + } // namespace iterable + } // 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 ed1de44..0aa1a2e 100644 --- a/include/newtype/new_type.hpp +++ b/include/newtype/new_type.hpp @@ -3,12 +3,14 @@ #include "newtype/derivable.hpp" #include "newtype/deriving.hpp" +#include "newtype/impl/new_type_iterator_types.hpp" #include "newtype/impl/new_type_storage.hpp" #include "newtype/impl/type_traits_extensions.hpp" #include "newtype/version.hpp" #include #include +#include #include #include @@ -26,7 +28,9 @@ namespace nt * @tparam DervivationClause An nt::derivation_clause describing which features shall be automatically derived for the new type alias */ template - class new_type : impl::new_type_move_assignment + class new_type + : impl::new_type_move_assignment + , public impl::new_type_iterator_types { static_assert(!std::is_reference_v, "The base type must not be a reference type"); static_assert(!std::is_void_v>, "The base type must not be possibly cv-qualified void"); @@ -209,6 +213,51 @@ namespace nt { return std::addressof(this->m_value); } + + /// @section Iterators + + /** + * @brief Get the begin iterator of the base type + * + * @return An iterator to the beginning of the base type sequence + * @throw Any exception thrown by the overload of 'begin' selected + */ + template * = nullptr> + auto constexpr begin() + -> std::enable_if_t, typename NewType::iterator> + { + if constexpr (impl::has_member_begin_v) + { + return this->m_value.begin(); + } + else + { + using std::begin; + return begin(this->m_value); + } + } + + /** + * @brief Get the begin iterator of the base type + * + * @note Overload for constant instances + * @return An iterator to the beginning of the base type sequence + * @throw Any exception thrown by the overload of 'begin' selected + */ + template + auto constexpr begin() const + -> std::enable_if_t, typename NewType::const_iterator> + { + if constexpr (impl::has_member_begin_v) + { + return this->m_value.begin(); + } + else + { + using std::begin; + return begin(this->m_value); + } + } }; /// @section Equality comparison operators @@ -569,6 +618,35 @@ namespace nt return lhs; } + /// @section Free Iterator Accessors + + /** + * @brief Get the begin iterator of the base type + * + * @return An iterator to the beginning of the base type sequence + * @throw Any exception thrown by the overload of 'begin' selected + */ + template> + auto constexpr begin(new_type & obj) + -> std::enable_if_t, typename NewType::iterator> + { + return begin(obj); + } + + /** + * @brief Get the begin iterator of the base type + * + * @note Overload for constant instances + * @return An iterator to the beginning of the base type sequence + * @throw Any exception thrown by the overload of 'begin' selected + */ + template> + auto constexpr begin(new_type const & obj) + -> std::enable_if_t, typename NewType::const_iterator> + { + return begin(obj); + } + } // namespace nt namespace std -- cgit v1.2.3