recursive_iterator_layer.hpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #pragma once
  2. #include "recursive_iterator_base.hpp"
  3. #include "recursive_iterator_traits.hpp"
  4. namespace iterator { namespace detail {
  5. /**
  6. * @class recursive_iterator_layer
  7. * @brief A single layer for recursing down a nested collection. Represents
  8. * non-associative containers.
  9. *
  10. * Provides dispatch/overloading for types and functions of recursive_iterator
  11. * chains to resolve ambiguous typedefs and operators.
  12. *
  13. * @see recursive_iterator_impl
  14. * @see bounded_recursive_iterator_impl
  15. * @tparam Iterator The underlying iterator type of the layer
  16. * @tparam RecursiveIterator_NextLayer The next layer, either a
  17. * recursive_iterator_impl, or a bounded_recursive_iterator_impl
  18. */
  19. template <typename Iterator, typename RecursiveIterator_NextLayer>
  20. class recursive_iterator_layer : public recursive_iterator_base<Iterator>,
  21. public RecursiveIterator_NextLayer {
  22. public:
  23. using super = RecursiveIterator_NextLayer;
  24. using layer = recursive_iterator_base<Iterator>;
  25. protected:
  26. using recursive_category = continue_layer_tag_t;
  27. public:
  28. recursive_iterator_layer() = default;
  29. explicit recursive_iterator_layer(layer v) : recursive_iterator_layer() {
  30. assign(v);
  31. update();
  32. }
  33. template <typename OIter, typename Rec>
  34. recursive_iterator_layer(recursive_iterator_layer<OIter, Rec> const & other)
  35. : layer(static_cast<recursive_iterator_base<OIter> const &>(other)),
  36. super(static_cast<Rec const &>(other)) {}
  37. decltype(auto) operator*() const { return super::get(); }
  38. decltype(auto) operator->() const { return super::operator->(); }
  39. bool operator==(recursive_iterator_layer const & other) const {
  40. return (static_cast<layer const &>(*this) == other) &&
  41. (static_cast<super const &>(*this) == other);
  42. }
  43. protected:
  44. decltype(auto) get() const { return operator*(); }
  45. /**
  46. * Advance the iterator step. If the next layer has reached the end, then
  47. * we advance this iterator until it reaches either its own end, or a
  48. * non-empty subcollection to start iterating over.
  49. */
  50. void next() {
  51. super::next();
  52. update();
  53. }
  54. /**
  55. * Update the underlying iterator and propogate updates down the chain so
  56. * that if there is data available, the iterator is in a dereferencable
  57. * state.
  58. */
  59. void assign(layer v) {
  60. static_cast<layer &>(*this) = v;
  61. if (!v.at_end()) { super::assign(make_end_aware_iterator(*v)); }
  62. }
  63. void update() {
  64. layer & self = static_cast<layer &>(*this);
  65. while (super::at_end() && !(++self).at_end()) {
  66. super::assign(make_end_aware_iterator(*self));
  67. }
  68. }
  69. using layer::at_end;
  70. };
  71. }}