| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- //
- // recursive_iterator_meta.hpp
- // iterator
- //
- // Created by Sam Jaffe on 2/21/17.
- //
- #pragma once
- #include "flatten_iterator_layer.hpp"
- #include "recursive_iterator_base.hpp"
- #include "recursive_iterator_layer.hpp"
- #include "recursive_iterator_traits.hpp"
- namespace iterator { namespace detail {
- template <typename Iterator, typeclass = typeclass_t<Iterator>::value>
- class recursive_iterator_impl;
- /**
- * A bounded_recursive_iterator_impl where N == Max is always a terminal node.
- */
- template <typename Iterator, std::size_t N, std::size_t Max,
- typeclass = std::conditional_t<N == Max, typeclass_t<void>,
- typeclass_t<Iterator>>::value>
- class bounded_recursive_iterator_impl;
- /**
- * @class recursive_iterator_impl
- * @brief The default (terminal) implementation of an unbounded recursive
- * iterator.
- *
- * @see recursive_iterator_base
- * @tparam Iterator The iterator type being processed, such as
- * std::vector<int>::iterator
- */
- template <typename Iterator>
- class recursive_iterator_impl<Iterator, typeclass::TERMINAL>
- : public recursive_iterator_base<Iterator> {
- public:
- using super = recursive_iterator_base<Iterator>;
- public:
- using super::super;
- recursive_iterator_impl() = default;
- protected:
- void next() { super::operator++(); }
- void assign(super eat) { static_cast<super &>(*this) = eat; }
- };
- /**
- * @class recursive_iterator_impl
- *
- * An SFINAE specialization of bounded_recursive_iterator_impl for
- * non-associative container types.
- * @see recursive_iterator_layer
- */
- template <typename Iterator>
- class recursive_iterator_impl<Iterator, typeclass::CONTAINER>
- : public recursive_iterator_layer<
- Iterator, recursive_iterator_impl<value_iterator<Iterator>>> {
- public:
- using next_layer = recursive_iterator_impl<value_iterator<Iterator>>;
- using super = recursive_iterator_layer<Iterator, next_layer>;
- public:
- /**
- * A special override of operator* that allows collections like
- * std::vector<std::vector<std::map<K, V>>> still use the value/reference
- * type of the map. Works only for nested collections with one associative
- * container at the bottom level.
- */
- auto operator*() const -> decltype(*(next_layer &)(*this)) {
- return next_layer::operator*();
- }
- using super::super;
- recursive_iterator_impl() = default;
- };
- /**
- * @class recursive_iterator_impl
- *
- * An SFINAE specialization of bounded_recursive_iterator_impl for
- * associative container types.
- * @see flatten_iterator_layer
- */
- template <typename Iterator>
- class recursive_iterator_impl<Iterator, typeclass::ASSOCIATIVE_CONTAINER>
- : public flatten_iterator_layer<
- Iterator, recursive_iterator_impl<mapped_iterator<Iterator>>> {
- public:
- using next_layer = recursive_iterator_impl<mapped_iterator<Iterator>>;
- using super = flatten_iterator_layer<Iterator, next_layer>;
- public:
- using super::super;
- recursive_iterator_impl() = default;
- };
- /**
- * @class bounded_recursive_iterator_impl
- * @brief The default (terminal) implementation of a recursive iterator up to
- * Max levels deep.
- *
- * @see recursive_iterator_base
- * @tparam Iterator The iterator type being processed, such as
- * std::vector<int>::iterator
- * @tparam N The current layer of depth, starts at 1.
- * @tparam Max The maximum recursive depth to dive down, in case you need to
- * process some sub-collection in a specific manner.
- */
- template <typename Iterator, std::size_t N, std::size_t Max>
- class bounded_recursive_iterator_impl<Iterator, N, Max, typeclass::TERMINAL>
- : public recursive_iterator_base<Iterator> {
- public:
- using super = recursive_iterator_base<Iterator>;
- public:
- using super::super;
- bounded_recursive_iterator_impl() = default;
- protected:
- void next() { super::operator++(); }
- void assign(super eat) { static_cast<super &>(*this) = eat; }
- };
- /**
- * @class bounded_recursive_iterator_impl
- *
- * An SFINAE specialization of bounded_recursive_iterator_impl for
- * non-associative container types.
- * @see recursive_iterator_layer
- */
- template <typename Iterator, std::size_t N, std::size_t Max>
- class bounded_recursive_iterator_impl<Iterator, N, Max, typeclass::CONTAINER>
- : public recursive_iterator_layer<
- Iterator, bounded_recursive_iterator_impl<value_iterator<Iterator>,
- N + 1, Max>> {
- public:
- using next_layer =
- bounded_recursive_iterator_impl<value_iterator<Iterator>, N + 1, Max>;
- using super = recursive_iterator_layer<Iterator, next_layer>;
- public:
- /**
- * A special override of operator* that allows collections like
- * std::vector<std::vector<std::map<K, V>>> still use the value/reference
- * type of the map. Works only for nested collections with one associative
- * container at the bottom/Max level.
- */
- auto operator*() const -> decltype(*(next_layer &)(*this)) {
- return next_layer::operator*();
- }
- using super::super;
- bounded_recursive_iterator_impl() = default;
- };
- /**
- * @class bounded_recursive_iterator_impl
- *
- * An SFINAE specialization of bounded_recursive_iterator_impl for
- * associative container types.
- * @see flatten_iterator_layer
- */
- template <typename Iterator, std::size_t N, std::size_t Max>
- class bounded_recursive_iterator_impl<Iterator, N, Max,
- typeclass::ASSOCIATIVE_CONTAINER>
- : public flatten_iterator_layer<
- Iterator, bounded_recursive_iterator_impl<mapped_iterator<Iterator>,
- N + 1, Max>> {
- public:
- using next_layer =
- bounded_recursive_iterator_impl<mapped_iterator<Iterator>, N + 1, Max>;
- using super = flatten_iterator_layer<Iterator, next_layer>;
- public:
- using super::super;
- bounded_recursive_iterator_impl() = default;
- };
- }}
|