array_iterator.h 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #pragma once
  2. #include <iterator>
  3. #include <jvalidate/detail/deref_proxy.h>
  4. namespace jvalidate::adapter::detail {
  5. /**
  6. * @brief An iterator for binding JSON values of type Array - which are
  7. * congruent to a vector<JSON>.
  8. *
  9. * @tparam It The underlying iterator type being operated on
  10. * @tparam Adapter The owning adapter type, must fulfill the following
  11. * contracts:
  12. * - is constructible from the value_type of It
  13. * Additionally, Adapter is expected to conform to the jvalidate::Adapter
  14. * concept.
  15. */
  16. template <typename It, typename Adapter> class JsonArrayIterator : public It {
  17. public:
  18. using value_type = Adapter;
  19. using reference = Adapter;
  20. using pointer = ::jvalidate::detail::DerefProxy<reference>;
  21. using difference_type = std::ptrdiff_t;
  22. using iterator_category = std::forward_iterator_tag;
  23. JsonArrayIterator() = default; // Sentinel for handling null objects
  24. JsonArrayIterator(It it) : It(it) {}
  25. reference operator*() const { return Adapter(It::operator*()); }
  26. pointer operator->() const { return {operator*()}; }
  27. JsonArrayIterator operator++(int) {
  28. auto tmp = *this;
  29. ++*this;
  30. return tmp;
  31. }
  32. JsonArrayIterator & operator++() {
  33. It::operator++();
  34. return *this;
  35. }
  36. };
  37. }