Horizon
iteration_proxy.hpp
1 #pragma once
2 
3 #include <cstddef> // size_t
4 #include <string> // string, to_string
5 
6 #include <nlohmann/detail/value_t.hpp>
7 
8 namespace nlohmann
9 {
10 namespace detail
11 {
13 template<typename IteratorType> class iteration_proxy
14 {
15  private:
17  class iteration_proxy_internal
18  {
19  private:
21  IteratorType anchor;
23  std::size_t array_index = 0;
24 
25  public:
26  explicit iteration_proxy_internal(IteratorType it) noexcept : anchor(it) {}
27 
29  iteration_proxy_internal& operator*()
30  {
31  return *this;
32  }
33 
35  iteration_proxy_internal& operator++()
36  {
37  ++anchor;
38  ++array_index;
39 
40  return *this;
41  }
42 
44  bool operator!=(const iteration_proxy_internal& o) const noexcept
45  {
46  return anchor != o.anchor;
47  }
48 
50  std::string key() const
51  {
52  assert(anchor.m_object != nullptr);
53 
54  switch (anchor.m_object->type())
55  {
56  // use integer array index as key
57  case value_t::array:
58  return std::to_string(array_index);
59 
60  // use key from the object
61  case value_t::object:
62  return anchor.key();
63 
64  // use an empty key for all primitive types
65  default:
66  return "";
67  }
68  }
69 
71  typename IteratorType::reference value() const
72  {
73  return anchor.value();
74  }
75  };
76 
78  typename IteratorType::reference container;
79 
80  public:
82  explicit iteration_proxy(typename IteratorType::reference cont) noexcept
83  : container(cont) {}
84 
86  iteration_proxy_internal begin() noexcept
87  {
88  return iteration_proxy_internal(container.begin());
89  }
90 
92  iteration_proxy_internal end() noexcept
93  {
94  return iteration_proxy_internal(container.end());
95  }
96 };
97 }
98 }
nlohmann::detail::value_t::object
object (unordered set of name/value pairs)
nlohmann
namespace for Niels Lohmann
Definition: adl_serializer.hpp:8
nlohmann::detail::iteration_proxy::begin
iteration_proxy_internal begin() noexcept
return iterator begin (needed for range-based for)
Definition: iteration_proxy.hpp:86
nlohmann::detail::iteration_proxy::end
iteration_proxy_internal end() noexcept
return iterator end (needed for range-based for)
Definition: iteration_proxy.hpp:92
nlohmann::detail::value_t::string
string value
nlohmann::detail::value_t::array
array (ordered collection of values)
nlohmann::detail::iteration_proxy::iteration_proxy
iteration_proxy(typename IteratorType::reference cont) noexcept
construct iteration proxy from a container
Definition: iteration_proxy.hpp:82
p2t::operator*
Point operator*(double s, const Point &a)
Multiply point by scalar.
Definition: shapes.h:245