recursive_iterator_base.hpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #pragma once
  2. #include "../end_aware_iterator.hpp"
  3. #include "recursive_iterator_traits.hpp"
  4. namespace iterator { namespace detail {
  5. template <typename Iterator, typename = void> class recursive_iterator_base;
  6. /**
  7. * @class recursive_iterator_base
  8. * @brief A thin wrapper around end_aware_iterator for the purposes of
  9. * template metaprogramming.
  10. */
  11. template <typename Iterator, typename>
  12. class recursive_iterator_base : public end_aware_iterator<Iterator> {
  13. public:
  14. using super = end_aware_iterator<Iterator>;
  15. protected:
  16. using recursive_category = terminal_layer_tag_t;
  17. public:
  18. using super::super;
  19. recursive_iterator_base(super const & iter) : super(iter) {}
  20. recursive_iterator_base(super && iter) : super(std::move(iter)) {}
  21. recursive_iterator_base() = default;
  22. operator super() const { return *this; }
  23. protected:
  24. typename super::reference get() const { return super::operator*(); }
  25. };
  26. /**
  27. * @class recursive_iterator_base
  28. * @brief An SFINAE specialization of recursive_iterator_base for associative
  29. * containers
  30. *
  31. * Because it is possible for recursive iterator to step over multiple layers
  32. * of associative containers, the return type is made into a tuple, so that
  33. * the caller does not need to write something like
  34. * `it->second.second.second'. Instead, the return type is a tuple of
  35. * references, so that the caller can write code like `std::get<3>(*it)'.
  36. *
  37. * For example, the ref type for std::map<int, std::map<float, double> >
  38. * with this would be std::tuple<int const&, float const&, double &>.
  39. */
  40. template <typename Iterator>
  41. class recursive_iterator_base<Iterator, is_associative_t<Iterator>>
  42. : public end_aware_iterator<Iterator> {
  43. public:
  44. using super = end_aware_iterator<Iterator>;
  45. using first_type = decltype((std::declval<Iterator>()->first));
  46. using second_type = decltype((std::declval<Iterator>()->second));
  47. protected:
  48. using recursive_category = continue_layer_tag_t;
  49. public:
  50. using value_type = std::tuple<first_type, second_type>;
  51. using reference = std::tuple<first_type &, second_type &>;
  52. public:
  53. using super::super;
  54. recursive_iterator_base(super const & iter) : super(iter) {}
  55. recursive_iterator_base(super && iter) : super(std::move(iter)) {}
  56. recursive_iterator_base() = default;
  57. operator super() const { return *this; }
  58. protected:
  59. /**
  60. * An alternative function to operator*(), which allows single layer
  61. * recursive iterators (on associative containers) to return the
  62. * underlying value/reference type, and nested containers to propogate
  63. * a tuple or a pair as necessary.
  64. */
  65. reference get() const {
  66. auto & pair = super::operator*();
  67. return std::tie(pair.first, pair.second);
  68. }
  69. };
  70. }}