array_iterator.h 1.2 KB

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