iteration_proxy.hpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. // __ _____ _____ _____
  2. // __| | __| | | | JSON for Modern C++
  3. // | | |__ | | | | | | version 3.12.0
  4. // |_____|_____|_____|_|___| https://github.com/nlohmann/json
  5. //
  6. // SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
  7. // SPDX-License-Identifier: MIT
  8. #pragma once
  9. #include <cstddef> // size_t
  10. #include <iterator> // forward_iterator_tag
  11. #include <tuple> // tuple_size, get, tuple_element
  12. #include <utility> // move
  13. #if JSON_HAS_RANGES
  14. #include <ranges> // enable_borrowed_range
  15. #endif
  16. #include <nlohmann/detail/abi_macros.hpp>
  17. #include <nlohmann/detail/meta/type_traits.hpp>
  18. #include <nlohmann/detail/string_utils.hpp>
  19. #include <nlohmann/detail/value_t.hpp>
  20. NLOHMANN_JSON_NAMESPACE_BEGIN
  21. namespace detail
  22. {
  23. template<typename IteratorType> class iteration_proxy_value
  24. {
  25. public:
  26. using difference_type = std::ptrdiff_t;
  27. using value_type = iteration_proxy_value;
  28. using pointer = value_type *;
  29. using reference = value_type &;
  30. using iterator_category = std::forward_iterator_tag;
  31. using string_type = typename std::remove_cv< typename std::remove_reference<decltype( std::declval<IteratorType>().key() ) >::type >::type;
  32. private:
  33. /// the iterator
  34. IteratorType anchor{};
  35. /// an index for arrays (used to create key names)
  36. std::size_t array_index = 0;
  37. /// last stringified array index
  38. mutable std::size_t array_index_last = 0;
  39. /// a string representation of the array index
  40. mutable string_type array_index_str = "0";
  41. /// an empty string (to return a reference for primitive values)
  42. string_type empty_str{};
  43. public:
  44. explicit iteration_proxy_value() = default;
  45. explicit iteration_proxy_value(IteratorType it, std::size_t array_index_ = 0)
  46. noexcept(std::is_nothrow_move_constructible<IteratorType>::value
  47. && std::is_nothrow_default_constructible<string_type>::value)
  48. : anchor(std::move(it))
  49. , array_index(array_index_)
  50. {}
  51. iteration_proxy_value(iteration_proxy_value const&) = default;
  52. iteration_proxy_value& operator=(iteration_proxy_value const&) = default;
  53. // older GCCs are a bit fussy and require explicit noexcept specifiers on defaulted functions
  54. iteration_proxy_value(iteration_proxy_value&&)
  55. noexcept(std::is_nothrow_move_constructible<IteratorType>::value
  56. && std::is_nothrow_move_constructible<string_type>::value) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor,cppcoreguidelines-noexcept-move-operations)
  57. iteration_proxy_value& operator=(iteration_proxy_value&&)
  58. noexcept(std::is_nothrow_move_assignable<IteratorType>::value
  59. && std::is_nothrow_move_assignable<string_type>::value) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor,cppcoreguidelines-noexcept-move-operations)
  60. ~iteration_proxy_value() = default;
  61. /// dereference operator (needed for range-based for)
  62. const iteration_proxy_value& operator*() const
  63. {
  64. return *this;
  65. }
  66. /// increment operator (needed for range-based for)
  67. iteration_proxy_value& operator++()
  68. {
  69. ++anchor;
  70. ++array_index;
  71. return *this;
  72. }
  73. iteration_proxy_value operator++(int)& // NOLINT(cert-dcl21-cpp)
  74. {
  75. auto tmp = iteration_proxy_value(anchor, array_index);
  76. ++anchor;
  77. ++array_index;
  78. return tmp;
  79. }
  80. /// equality operator (needed for InputIterator)
  81. bool operator==(const iteration_proxy_value& o) const
  82. {
  83. return anchor == o.anchor;
  84. }
  85. /// inequality operator (needed for range-based for)
  86. bool operator!=(const iteration_proxy_value& o) const
  87. {
  88. return anchor != o.anchor;
  89. }
  90. /// return key of the iterator
  91. const string_type& key() const
  92. {
  93. JSON_ASSERT(anchor.m_object != nullptr);
  94. switch (anchor.m_object->type())
  95. {
  96. // use integer array index as key
  97. case value_t::array:
  98. {
  99. if (array_index != array_index_last)
  100. {
  101. int_to_string( array_index_str, array_index );
  102. array_index_last = array_index;
  103. }
  104. return array_index_str;
  105. }
  106. // use key from the object
  107. case value_t::object:
  108. return anchor.key();
  109. // use an empty key for all primitive types
  110. case value_t::null:
  111. case value_t::string:
  112. case value_t::boolean:
  113. case value_t::number_integer:
  114. case value_t::number_unsigned:
  115. case value_t::number_float:
  116. case value_t::binary:
  117. case value_t::discarded:
  118. default:
  119. return empty_str;
  120. }
  121. }
  122. /// return value of the iterator
  123. typename IteratorType::reference value() const
  124. {
  125. return anchor.value();
  126. }
  127. };
  128. /// proxy class for the items() function
  129. template<typename IteratorType> class iteration_proxy
  130. {
  131. private:
  132. /// the container to iterate
  133. typename IteratorType::pointer container = nullptr;
  134. public:
  135. explicit iteration_proxy() = default;
  136. /// construct iteration proxy from a container
  137. explicit iteration_proxy(typename IteratorType::reference cont) noexcept
  138. : container(&cont) {}
  139. iteration_proxy(iteration_proxy const&) = default;
  140. iteration_proxy& operator=(iteration_proxy const&) = default;
  141. iteration_proxy(iteration_proxy&&) noexcept = default;
  142. iteration_proxy& operator=(iteration_proxy&&) noexcept = default;
  143. ~iteration_proxy() = default;
  144. /// return iterator begin (needed for range-based for)
  145. iteration_proxy_value<IteratorType> begin() const noexcept
  146. {
  147. return iteration_proxy_value<IteratorType>(container->begin());
  148. }
  149. /// return iterator end (needed for range-based for)
  150. iteration_proxy_value<IteratorType> end() const noexcept
  151. {
  152. return iteration_proxy_value<IteratorType>(container->end());
  153. }
  154. };
  155. // Structured Bindings Support
  156. // For further reference see https://blog.tartanllama.xyz/structured-bindings/
  157. // And see https://github.com/nlohmann/json/pull/1391
  158. template<std::size_t N, typename IteratorType, enable_if_t<N == 0, int> = 0>
  159. auto get(const nlohmann::detail::iteration_proxy_value<IteratorType>& i) -> decltype(i.key())
  160. {
  161. return i.key();
  162. }
  163. // Structured Bindings Support
  164. // For further reference see https://blog.tartanllama.xyz/structured-bindings/
  165. // And see https://github.com/nlohmann/json/pull/1391
  166. template<std::size_t N, typename IteratorType, enable_if_t<N == 1, int> = 0>
  167. auto get(const nlohmann::detail::iteration_proxy_value<IteratorType>& i) -> decltype(i.value())
  168. {
  169. return i.value();
  170. }
  171. } // namespace detail
  172. NLOHMANN_JSON_NAMESPACE_END
  173. // The Addition to the STD Namespace is required to add
  174. // Structured Bindings Support to the iteration_proxy_value class
  175. // For further reference see https://blog.tartanllama.xyz/structured-bindings/
  176. // And see https://github.com/nlohmann/json/pull/1391
  177. namespace std
  178. {
  179. #if defined(__clang__)
  180. // Fix: https://github.com/nlohmann/json/issues/1401
  181. #pragma clang diagnostic push
  182. #pragma clang diagnostic ignored "-Wmismatched-tags"
  183. #endif
  184. template<typename IteratorType>
  185. class tuple_size<::nlohmann::detail::iteration_proxy_value<IteratorType>> // NOLINT(cert-dcl58-cpp)
  186. : public std::integral_constant<std::size_t, 2> {};
  187. template<std::size_t N, typename IteratorType>
  188. class tuple_element<N, ::nlohmann::detail::iteration_proxy_value<IteratorType >> // NOLINT(cert-dcl58-cpp)
  189. {
  190. public:
  191. using type = decltype(
  192. get<N>(std::declval <
  193. ::nlohmann::detail::iteration_proxy_value<IteratorType >> ()));
  194. };
  195. #if defined(__clang__)
  196. #pragma clang diagnostic pop
  197. #endif
  198. } // namespace std
  199. #if JSON_HAS_RANGES
  200. template <typename IteratorType>
  201. inline constexpr bool ::std::ranges::enable_borrowed_range<::nlohmann::detail::iteration_proxy<IteratorType>> = true;
  202. #endif