diff options
| -rw-r--r-- | CMakeLists.txt | 1 | ||||
| -rw-r--r-- | doc/src/index.rst | 126 | ||||
| -rw-r--r-- | include/newtype/derivable.hpp | 7 | ||||
| -rw-r--r-- | include/newtype/impl/new_type_iterator_types.hpp | 42 | ||||
| -rw-r--r-- | include/newtype/impl/type_traits_extensions.hpp | 72 | ||||
| -rw-r--r-- | include/newtype/new_type.hpp | 80 | ||||
| -rw-r--r-- | test/include/iterable_suite.hpp | 11 | ||||
| -rw-r--r-- | test/src/driver.cpp | 2 | ||||
| -rw-r--r-- | test/src/iterable_suite.cpp | 61 |
9 files changed, 377 insertions, 25 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 37a3460..fe85a55 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -59,6 +59,7 @@ if(BUILD_TESTING) "${PROJECT_SOURCE_DIR}/test/src/equality_comparison_suite.cpp" "${PROJECT_SOURCE_DIR}/test/src/hash_suite.cpp" "${PROJECT_SOURCE_DIR}/test/src/io_operators_suite.cpp" + "${PROJECT_SOURCE_DIR}/test/src/iterable_suite.cpp" "${PROJECT_SOURCE_DIR}/test/src/new_type_constructor_suite.cpp" "${PROJECT_SOURCE_DIR}/test/src/relational_operators_suite.cpp" ) diff --git a/doc/src/index.rst b/doc/src/index.rst index 49db3b6..e8d50d6 100644 --- a/doc/src/index.rst +++ b/doc/src/index.rst @@ -28,7 +28,7 @@ In it, :cpp:class:`new_type` is used to create thre new strong aliases :literal: .. literalinclude:: ../../examples/src/basic_usage.cpp :language: c++ :linenos: - :name: new-type-usage-basic + :name: new-type-usage-basic :caption: Basic usage of :cpp:class:`new_type` However, using :cpp:class:`new_type` in this fashion seem quite cumbersome. @@ -76,7 +76,7 @@ Class template :cpp:class:`new_type` :tparam BaseType: |BaseTypeDoc| :tparam TagType: |TagTypeDoc| :tparam DerivationClause: |DerivationClauseDoc| - + .. versionadded:: 1.0.0 **Member Type Aliases** @@ -87,6 +87,18 @@ Class template :cpp:class:`new_type` .. cpp:type:: derivation_clause_type = decltype(DerivationClause) + .. cpp:type:: iterator = typename BaseType::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:`iterator <new_type::base_type::iterator>` and the :cpp:var:`derivation clause <derivation_clause>` contains :cpp:var:`Iterable`. + + .. versionadded:: 1.1.0 + + .. cpp:type:: const_iterator = typename BaseType::const_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_iterator <new_type::base_type::const_iterator>` and the :cpp:var:`derivation clause <derivation_clause>` contains :cpp:var:`Iterable`. + + .. versionadded:: 1.1.0 + **Static Data Members** .. cpp:var:: static derivation_clause_type constexpr derivation_clause = DerivationClause @@ -195,6 +207,30 @@ Class template :cpp:class:`new_type` :enablement: This operator shall be available iff. this :cpp:class:`new_type`'s :cpp:var:`derivation clause <derivation_clause>` contains :cpp:var:`Indirection` + **Iterators** + + .. cpp:function:: constexpr iterator begin() + + Get an 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 <derivation_clause>` contains :cpp:var:`Iterable` and + b) this :cpp:class:`new_type`'s :cpp:type:`base type <base_type>` has a non-static member function :cpp:func:`begin() <new_type::base_type::begin()>` that returns an instance of type :cpp:type:`iterator` + + .. versionadded:: 1.1.0 + + .. cpp:function:: constexpr iterator begin() const + + Get a constant 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 <derivation_clause>` contains :cpp:var:`Iterable` and + b) this :cpp:class:`new_type`'s :cpp:type:`base type <base_type>` has a non-static member function :cpp:func:`begin() const <new_type::base_type::begin()>` that returns an instance of type :cpp:type:`const_iterator` + + .. versionadded:: 1.1.0 + :literal:`namespace`-level functions and function templates ----------------------------------------------------------- @@ -233,7 +269,7 @@ Equality Comparison Operators :returns: The value returned by the comparison of object contained by :literal:`lhs` with an object of the :cpp:type:`base type <new_type::base_type>`. :throws: Any exception thrown by the comparison of object contained by :literal:`lhs` with an object of the :cpp:type:`base type <new_type::base_type>`. This operator shall be noexcept iff. :cpp:type:`new_type::base_type` is *nothrow equals-comparable*. :enablement: This operator shall be available iff. - + a. :cpp:type:`new_type::base_type` supports comparison using :literal:`==` and b. the :cpp:var:`derivation clause <DerivationClause>` contains :cpp:var:`EqBase` @@ -252,7 +288,7 @@ Equality Comparison Operators :returns: The value returned by the comparison of an object of :cpp:type:`base type <new_type::base_type>` with the object contained by :literal:`rhs`. :throws: Any exception thrown by the comparison of an object of :cpp:type:`base type <new_type::base_type>` with the object contained by :literal:`rhs`. This operator shall be noexcept iff. :cpp:type:`new_type::base_type` is *nothrow equals-comparable*. :enablement: This operator shall be available iff. - + a. :cpp:type:`new_type::base_type` supports comparison using :literal:`==` and b. the :cpp:var:`derivation clause <DerivationClause>` contains :cpp:var:`EqBase` @@ -308,7 +344,7 @@ Equality Comparison Operators :returns: The value returned by the comparison of an object of :cpp:type:`base type <new_type::base_type>` with the object contained by :literal:`rhs`. :throws: Any exception thrown by the comparison of an object of :cpp:type:`base type <new_type::base_type>` with the object contained by :literal:`rhs`. This operator shall be noexcept iff. :cpp:type:`new_type::base_type` is *nothrow not-equals-comparable*. :enablement: This operator shall be available iff. - + a. :cpp:type:`new_type::base_type` supports comparison using :literal:`!=` and b. the :cpp:var:`derivation clause <DerivationClause>` contains :cpp:var:`EqBase` @@ -464,7 +500,7 @@ Arithmetic Operators :returns: A new instance of :cpp:class:`new_type\<BaseType, TagType, DerivationClause>` containing the result of applying :literal:`+` to the objects contained by :literal:`lhs` and :literal:`rhs`. :throws: Any exception thrown by the addition operator of the objects contained by :literal:`lhs` and :literal:`rhs`. This operator shall be noexcept iff. - + a. :cpp:type:`new_type::base_type` is *nothrow addable* and b. :cpp:type:`new_type::base_type` is *nothrow copy-constructible* @@ -488,7 +524,7 @@ Arithmetic Operators :returns: A reference to the first argument containing the value modified by applying :literal:`+=` to the objects contained by :literal:`lhs` and :literal:`rhs`. :throws: Any exception thrown by the addition-assignment operator of the objects contained by :literal:`lhs` and :literal:`rhs`. This operator shall be noexcept iff. - + a. :cpp:type:`new_type::base_type` is *nothrow add-assignable* and b. :cpp:type:`new_type::base_type` is *nothrow copy-constructible* @@ -512,7 +548,7 @@ Arithmetic Operators :returns: A new instance of :cpp:class:`new_type\<BaseType, TagType, DerivationClause>` containing the result of applying :literal:`-` to the objects contained by :literal:`lhs` and :literal:`rhs`. :throws: Any exception thrown by the subtraction operator of the objects contained by :literal:`lhs` and :literal:`rhs`. This operator shall be noexcept iff. - + a. :cpp:type:`new_type::base_type` is *nothrow subtractable* and b. :cpp:type:`new_type::base_type` is *nothrow copy-constructible* @@ -536,7 +572,7 @@ Arithmetic Operators :returns: A reference to the first argument containing the value modified by applying :literal:`-=` to the objects contained by :literal:`lhs` and :literal:`rhs`. :throws: Any exception thrown by the subtraction-assignment operator of the objects contained by :literal:`lhs` and :literal:`rhs`. This operator shall be noexcept iff. - + a. :cpp:type:`new_type::base_type` is *nothrow subtract-assignable* and b. :cpp:type:`new_type::base_type` is *nothrow copy-constructible* @@ -560,7 +596,7 @@ Arithmetic Operators :returns: A new instance of :cpp:class:`new_type\<BaseType, TagType, DerivationClause>` containing the result of applying :literal:`*` to the objects contained by :literal:`lhs` and :literal:`rhs`. :throws: Any exception thrown by the multiplication operator of the objects contained by :literal:`lhs` and :literal:`rhs`. This operator shall be noexcept iff. - + a. :cpp:type:`new_type::base_type` is *nothrow multipliable* and b. :cpp:type:`new_type::base_type` is *nothrow copy-constructible* @@ -584,7 +620,7 @@ Arithmetic Operators :returns: A reference to the first argument containing the value modified by applying :literal:`*=` to the objects contained by :literal:`lhs` and :literal:`rhs`. :throws: Any exception thrown by the multiplication-assignment operator of the objects contained by :literal:`lhs` and :literal:`rhs`. This operator shall be noexcept iff. - + a. :cpp:type:`new_type::base_type` is *nothrow multiply-assignable* and b. :cpp:type:`new_type::base_type` is *nothrow copy-constructible* @@ -608,7 +644,7 @@ Arithmetic Operators :returns: A new instance of :cpp:class:`new_type\<BaseType, TagType, DerivationClause>` containing the result of applying :literal:`/` to the objects contained by :literal:`lhs` and :literal:`rhs`. :throws: Any exception thrown by the division operator of the objects contained by :literal:`lhs` and :literal:`rhs`. This operator shall be noexcept iff. - + a. :cpp:type:`new_type::base_type` is *nothrow dividable* and b. :cpp:type:`new_type::base_type` is *nothrow copy-constructible* @@ -631,7 +667,7 @@ Arithmetic Operators :returns: A reference to the first argument containing the value modified by applying :literal:`/=` to the objects contained by :literal:`lhs` and :literal:`rhs`. :throws: Any exception thrown by the division-assignment operator of the objects contained by :literal:`lhs` and :literal:`rhs`. This operator shall be noexcept iff. - + a. :cpp:type:`new_type::base_type` is *nothrow divide-assignable* and b. :cpp:type:`new_type::base_type` is *nothrow copy-constructible* @@ -642,6 +678,45 @@ Arithmetic Operators .. versionadded:: 1.0.0 +Iterators +~~~~~~~~~ + +.. cpp:function:: template<typename BaseType, typename TagType, auto DerivationClause> \ + constexpr new_type<BaseType, TagType, DerivationClause>::iterator begin(new_type<BaseType, TagType, DerivationClause> & obj) + + Get an 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 <DerivationClause>` contains :cpp:var:`Iterable` and + b) for the :cpp:class:`new_type`'s :cpp:type:`base type <BaseType>` exists a namespace-level function :literal:`begin(BaseType &)` that returns an instance of type :cpp:type:`new_type::iterator` + + .. versionadded:: 1.1.0 + +.. cpp:function:: template<typename BaseType, typename TagType, auto DerivationClause> \ + constexpr new_type<BaseType, TagType, DerivationClause>::const_iterator begin(new_type<BaseType, TagType, DerivationClause> const & obj) + + Get a constant 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 <DerivationClause>` contains :cpp:var:`Iterable` and + b) for the :cpp:class:`new_type`'s :cpp:type:`base type <BaseType>` exists a namespace-level function :literal:`begin(BaseType const &)` that returns an instance of type :cpp:type:`new_type::const_iterator` + + .. versionadded:: 1.1.0 + :cpp:class:`std::hash` Support ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -660,7 +735,7 @@ Arithmetic Operators :returns: The result of applying :cpp:class:`std::hash` to the object contained by :literal:`value` :throws: Any exception thrown by the call operator of the specialization of :cpp:class`std::hash` for the type of the object contained by :literal:`value`. :enablement: This operator shall be available iff. - + a. :cpp:type:`nt::new_type::base_type` is hashable and b. the :cpp:var:`derivation clause <DerivationClause>` contains :cpp:var:`Hash <nt::Hash>`. @@ -731,15 +806,18 @@ Standard derivation tags .. cpp:var:: auto constexpr Indirection = derivable<class indirection_tag>{} - .. .. cpp:function:: constexpr BaseType operator->() noexcept + This tag enables the derivation of the "member access through pointer" operator :cpp:func:`operator->() <constexpr BaseType new_type::operator->()()>` (both in :literal:`const` and non-:literal:`const` variants). + + .. versionadded:: 1.0.0 - .. **enablement:** This operator shall be available iff. this :cpp:class:`new_type`'s :cpp:var:`derivation_clause` contains :cpp:var:`Indirection` +.. cpp:var:: auto constexpr Iterable = derivable<class iterable_tag>{} - .. .. cpp:function:: constexpr BaseType const * operator->() const noexcept + This tag enables the derivation of the following "standard iterator functions": - This tag enables the derivation of the "member access through pointer" operator :cpp:func:`operator->() <constexpr BaseType new_type::operator->()()>` (both in :literal:`const` and non-:literal:`const` variants). + * :cpp:func:`begin() <constexpr typename BaseType::iterator new_type::begin()>` + * :cpp:func:`begin() const <constexpr typename BaseType::iterator new_type::begin() const>` - .. versionadded:: 1.0.0 + .. versionadded:: 1.1.0 .. cpp:var:: auto constexpr Read = derivable<class read_tag>{} @@ -750,7 +828,7 @@ Standard derivation tags .. cpp:var:: auto constexpr Relational = derivable<class relational_tag>{} This tag enables the derivation of the following relational operators: - + * :cpp:func:`operator\<(new_type const &, new_type const &) <template\<typename BaseType, typename TagType, auto DerivationClause> constexpr bool operator<(new_type<BaseType, TagType, DerivationClause> const &, new_type<BaseType, TagType, DerivationClause> const &)>` * :cpp:func:`operator>(new_type const &, new_type const &) <template\<typename BaseType, typename TagType, auto DerivationClause> constexpr bool operator>(new_type<BaseType, TagType, DerivationClause> const &, new_type<BaseType, TagType, DerivationClause> const &)>` * :cpp:func:`operator\<=(new_type const &, new_type const &) <template\<typename BaseType, typename TagType, auto DerivationClause> constexpr bool operator<=(new_type<BaseType, TagType, DerivationClause> const &, new_type<BaseType, TagType, DerivationClause> const &)>` @@ -774,7 +852,7 @@ Function template :cpp:func:`deriving` .. cpp:function:: template<typename... DerivableTags> \ constexpr derivation_clause<DerivableTags...> deriving(derivable<DerivableTags>... features) noexcept - + This function can be used to create a new :cpp:class:`derivation_clause` for use in the definitions of instances of :cpp:class:`new_type`. .. versionadded:: 1.0.0 @@ -811,8 +889,8 @@ Class template :cpp:class:`derivation_clause` Check if this :cpp:class:`derivation clause <derivation_clause>` contains the given derivation - :tparam DerivableTag: A tag uniquely identifying a derivation - + :tparam DerivableTag: A tag uniquely identifying a derivation + .. cpp:function:: template<typename DerivableTag, typename... RemainingDerivableTags> \ constexpr bool operator()(derivable<DerivableTag>, derivable<RemainingDerivableTags>...) const noexcept @@ -845,7 +923,7 @@ Class template :cpp:class:`derivation_clause` .. cpp:function:: template<typename... OtherDerivableTags> \ constexpr bool operator<(derivation_clause<OtherDerivableTags...> other) const noexcept - + Check if this :cpp:class:`derivation clause <derivation_clause>` is a subset of the one represented by :cpp:any:`other`. One :cpp:class:`derivation clause <derivation_clause>` is considered to be a subset of another iff. the list of derivations of this instance forms a proper subset of the list of derivations of the other. 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 @@ -61,6 +61,13 @@ namespace nt auto constexpr Indirection = derivable<struct indirection_tag>{}; /** + * @brief A tag to enable derivation of the iterator accessors + * + * @since 1.0.0 + */ + auto constexpr Iterable = derivable<struct iterable_tag>{}; + + /** * @brief A tag to enable derivation of the stream input operator * * @since 1.0.0 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 <type_traits> + +namespace nt::impl +{ + + template<typename T, bool = false, typename = std::void_t<>> + struct new_type_iterator + { + }; + + template<typename T> + struct new_type_iterator<T, true, std::void_t<typename T::iterator>> + { + using iterator = typename T::iterator; + }; + + template<typename T, bool = false, typename = std::void_t<>> + struct new_type_const_iterator + { + }; + + template<typename T> + struct new_type_const_iterator<T, true, std::void_t<typename T::const_iterator>> + { + using const_iterator = typename T::const_iterator; + }; + + template<typename T, bool Enabled> + struct new_type_iterator_types + : new_type_iterator<T, Enabled> + , new_type_const_iterator<T, Enabled> + { + }; + +} // 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 <cstddef> #include <functional> #include <iosfwd> +#include <iterator> #include <type_traits> namespace nt::impl @@ -1051,6 +1052,77 @@ namespace nt::impl } // namespace std_support + inline namespace iterable + { + template<typename T, typename = void> + struct has_std_begin : std::false_type + { + }; + + template<typename T> + struct has_std_begin<T, std::void_t<decltype(std::begin(std::declval<T &>()))>> + : std::is_same<typename T::iterator, std::remove_cvref_t<decltype(std::begin(std::declval<T &>()))>> + { + }; + + template<typename T> + struct has_std_begin<T const, std::void_t<decltype(std::begin(std::declval<T const &>()))>> + : std::is_same<typename T::const_iterator, std::remove_cvref_t<decltype(std::begin(std::declval<T const &>()))>> + { + }; + + template<typename T> + auto constexpr has_std_begin_v = has_std_begin<T>::value; + + template<typename T, typename = void> + struct has_free_begin : std::false_type + { + }; + + template<typename T> + struct has_free_begin<T, std::void_t<decltype(begin(std::declval<T &>()))>> + : std::is_same<typename T::iterator, std::remove_cvref_t<decltype(begin(std::declval<T &>()))>> + { + }; + + template<typename T> + struct has_free_begin<T const, std::void_t<decltype(begin(std::declval<T const &>()))>> + : std::is_same<typename T::const_iterator, std::remove_cvref_t<decltype(begin(std::declval<T const &>()))>> + { + }; + + template<typename T> + auto constexpr has_free_begin_v = has_free_begin<T>::value; + + template<typename T, typename = void> + struct has_member_begin : std::false_type + { + }; + + template<typename T> + struct has_member_begin<T, std::void_t<decltype(std::declval<T &>().begin())>> + : std::is_same<typename T::iterator, std::remove_cvref_t<decltype(std::declval<T &>().begin())>> + { + }; + + template<typename T> + struct has_member_begin<T const, std::void_t<decltype(std::declval<T const &>().begin())>> + : std::is_same<typename T::const_iterator, std::remove_cvref_t<decltype(std::declval<T const &>().begin())>> + { + }; + + template<typename T> + auto constexpr has_member_begin_v = has_member_begin<T>::value; + + template<typename T> + struct has_begin : std::disjunction<has_std_begin<T>, has_free_begin<T>, has_member_begin<T>> + { + }; + + template<typename T> + auto constexpr has_begin_v = has_begin<T>::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 <functional> #include <istream> +#include <iterator> #include <ostream> #include <type_traits> @@ -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<typename BaseType, typename TagType, auto DerivationClause = deriving()> - class new_type : impl::new_type_move_assignment<BaseType, TagType> + class new_type + : impl::new_type_move_assignment<BaseType, TagType> + , public impl::new_type_iterator_types<BaseType, DerivationClause(nt::Iterable)> { static_assert(!std::is_reference_v<BaseType>, "The base type must not be a reference type"); static_assert(!std::is_void_v<std::remove_cv_t<BaseType>>, "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<typename NewType = new_type, std::enable_if_t<NewType::derivation_clause(nt::Iterable)> * = nullptr> + auto constexpr begin() + -> std::enable_if_t<NewType::derivation_clause(nt::Iterable) && impl::has_begin_v<BaseType>, typename NewType::iterator> + { + if constexpr (impl::has_member_begin_v<BaseType>) + { + 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<typename NewType = new_type> + auto constexpr begin() const + -> std::enable_if_t<NewType::derivation_clause(nt::Iterable) && impl::has_begin_v<BaseType const>, typename NewType::const_iterator> + { + if constexpr (impl::has_member_begin_v<BaseType>) + { + 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<typename BaseType, typename TagType, auto DerivationClause, typename NewType = new_type<BaseType, TagType, DerivationClause>> + auto constexpr begin(new_type<BaseType, TagType, DerivationClause> & obj) + -> std::enable_if_t<DerivationClause(nt::Iterable) && impl::has_free_begin_v<BaseType>, 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<typename BaseType, typename TagType, auto DerivationClause, typename NewType = new_type<BaseType, TagType, DerivationClause>> + auto constexpr begin(new_type<BaseType, TagType, DerivationClause> const & obj) + -> std::enable_if_t<DerivationClause(nt::Iterable) && impl::has_free_begin_v<BaseType const>, typename NewType::const_iterator> + { + return begin(obj); + } + } // namespace nt namespace std diff --git a/test/include/iterable_suite.hpp b/test/include/iterable_suite.hpp new file mode 100644 index 0000000..c2bbc6e --- /dev/null +++ b/test/include/iterable_suite.hpp @@ -0,0 +1,11 @@ +#ifndef NEWTYPE_TEST_ITERABLE_SUITE_HPP +#define NEWTYPE_TEST_ITERABLE_SUITE_HPP + +#include <cute/cute_suite.h> + +#include <string> +#include <utility> + +auto iterable_suite() -> std::pair<cute::suite, std::string>; + +#endif
\ No newline at end of file diff --git a/test/src/driver.cpp b/test/src/driver.cpp index cfd6b90..a0e8904 100644 --- a/test/src/driver.cpp +++ b/test/src/driver.cpp @@ -4,6 +4,7 @@ #include "equality_comparison_suite.hpp" #include "hash_suite.hpp" #include "io_operators_suite.hpp" +#include "iterable_suite.hpp" #include "new_type_constructor_suite.hpp" #include "relational_operators_suite.hpp" @@ -61,6 +62,7 @@ int main(int argc, char ** argv) io_operators_suite(), arithmetic_suite(), hash_suite(), + iterable_suite(), }; auto selectors = get_test_selectors(suites); diff --git a/test/src/iterable_suite.cpp b/test/src/iterable_suite.cpp new file mode 100644 index 0000000..2470571 --- /dev/null +++ b/test/src/iterable_suite.cpp @@ -0,0 +1,61 @@ +#include "iterable_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 <array> +#include <iterator> + +namespace +{ + +} + +inline namespace begin_tests +{ + + auto a_new__type_not_deriving_iterable_has_no_begin() -> void + { + using type_alias = nt::new_type<int, struct tag>; + ASSERT(!(nt::impl::has_begin_v<type_alias>)); + } + + auto a_new__type_based_on_a_non_iterable_type_deriving_iterable_has_no_begin() -> void + { + static_assert(!nt::impl::has_begin_v<int>); + using type_alias = nt::new_type<int, struct tag, deriving(nt::Iterable)>; + ASSERT(!(nt::impl::has_begin_v<type_alias>)); + } + + auto a_new__type_based_on_an_iterable_type_with_member_begin_deriving_iterable_has_member_begin() -> void + { + static_assert(nt::impl::has_member_begin_v<std::array<int, 3>>); + using type_alias = nt::new_type<std::array<int, 3>, struct tag, deriving(nt::Iterable)>; + ASSERT(nt::impl::has_member_begin_v<type_alias>); + } + + auto a_new__type_based_on_an_iterable_type_with_constant_member_begin_deriving_iterable_has_constant_member_begin() -> void + { + static_assert(nt::impl::has_member_begin_v<std::array<int const, 3>>); + using type_alias = nt::new_type<std::array<int, 3>, struct tag, deriving(nt::Iterable)>; + ASSERT(nt::impl::has_member_begin_v<type_alias const>); + } + +} // namespace begin_tests + +auto iterable_suite() -> std::pair<cute::suite, std::string> +{ + return {{ + /// 'begin' Tests + KAWAII(a_new__type_not_deriving_iterable_has_no_begin), + KAWAII(a_new__type_based_on_a_non_iterable_type_deriving_iterable_has_no_begin), + KAWAII(a_new__type_based_on_an_iterable_type_with_member_begin_deriving_iterable_has_member_begin), + KAWAII(a_new__type_based_on_an_iterable_type_with_constant_member_begin_deriving_iterable_has_constant_member_begin), + }, + "Iterable Tests"}; +}
\ No newline at end of file |
