aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
Diffstat (limited to 'include')
-rw-r--r--include/newtype/derivable.hpp7
-rw-r--r--include/newtype/impl/new_type_iterator_types.hpp42
-rw-r--r--include/newtype/impl/type_traits_extensions.hpp72
-rw-r--r--include/newtype/new_type.hpp80
4 files changed, 200 insertions, 1 deletions
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