recursive_iterator_base.hpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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<
  42. Iterator,
  43. typename std::enable_if<std::is_const<typename end_aware_iterator<
  44. Iterator>::value_type::first_type>::value>::type>
  45. : public end_aware_iterator<Iterator> {
  46. public:
  47. using super = end_aware_iterator<Iterator>;
  48. using first_type = decltype((std::declval<Iterator>()->first));
  49. using second_type = decltype((std::declval<Iterator>()->second));
  50. protected:
  51. using recursive_category = continue_layer_tag_t;
  52. public:
  53. using value_type = std::tuple<first_type, second_type>;
  54. using reference = std::tuple<first_type &, second_type &>;
  55. public:
  56. using super::super;
  57. recursive_iterator_base(super const & iter) : super(iter) {}
  58. recursive_iterator_base(super && iter) : super(std::move(iter)) {}
  59. recursive_iterator_base() = default;
  60. operator super() const { return *this; }
  61. protected:
  62. /**
  63. * An alternative function to operator*(), which allows single layer
  64. * recursive iterators (on associative containers) to return the
  65. * underlying value/reference type, and nested containers to propogate
  66. * a tuple or a pair as necessary.
  67. */
  68. reference get() const {
  69. auto & pair = super::operator*();
  70. return std::tie(pair.first, pair.second);
  71. }
  72. };
  73. }}