unkeyed_iterator.hpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. //
  2. // unkeyed_iterator.hpp
  3. // iterator
  4. //
  5. // Created by Sam Jaffe on 2/20/17.
  6. //
  7. #pragma once
  8. #include "proxy.h"
  9. namespace iterator {
  10. /**
  11. * At first glance, this class seems like it acts as an iterator that would be
  12. * used in the C++ equivalent of Java's Map::values() function. However,
  13. * notices that we do not assume that the input iterator is a map iterator.
  14. * The useful feature of this iterator - and the source of its name - is its
  15. * ability to be composed with recursive_iterator. This means that you can
  16. * have something crazy like a four-level map and then do:
  17. * \code
  18. * std::map<int, std::map<std::string, std::map<int, BigType>>> object;
  19. * auto const rit = make_recursive_iterator(object);
  20. * for (unkeyed_iterator<decltype(rit)> it = rit, end = {}; it != end; ++it) {
  21. * // Process only BigType, discarding all of the keys that we need to walk
  22. * }
  23. * \endcode
  24. */
  25. template <typename Iterator>
  26. class unkeyed_iterator : public proxy<Iterator, unkeyed_iterator<Iterator>> {
  27. public:
  28. using proxy<Iterator, unkeyed_iterator<Iterator>>::proxy;
  29. decltype(auto) dereference() const {
  30. using value_type = typename Iterator::value_type;
  31. constexpr auto index = std::tuple_size<value_type>::value - 1;
  32. return std::get<index>(*this->impl());
  33. }
  34. };
  35. template <typename It> unkeyed_iterator(It) -> unkeyed_iterator<It>;
  36. }
  37. MAKE_ITERATOR_FACADE_TYPEDEFS_T(::iterator::unkeyed_iterator);