iteration_proxy.hpp 8.0 KB

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