2018-01-10 09:18:31 +00:00
|
|
|
#pragma once
|
2017-08-14 15:26:22 +00:00
|
|
|
|
2017-08-14 17:28:01 +00:00
|
|
|
#include <cstddef> // size_t
|
2018-04-10 06:29:07 +00:00
|
|
|
#include <iterator> // input_iterator_tag
|
2019-03-17 11:01:49 +00:00
|
|
|
#include <string> // string, to_string
|
2018-12-19 14:47:35 +00:00
|
|
|
#include <tuple> // tuple_size, get, tuple_element
|
2021-03-24 06:15:18 +00:00
|
|
|
#include <utility> // move
|
2017-08-14 17:28:01 +00:00
|
|
|
|
2022-05-29 11:08:06 +00:00
|
|
|
#if JSON_HAS_RANGES
|
|
|
|
#include <ranges> // enable_borrowed_range
|
|
|
|
#endif
|
|
|
|
|
2018-12-19 14:47:35 +00:00
|
|
|
#include <nlohmann/detail/meta/type_traits.hpp>
|
2019-03-17 11:01:49 +00:00
|
|
|
#include <nlohmann/detail/value_t.hpp>
|
2017-08-14 15:26:22 +00:00
|
|
|
|
|
|
|
namespace nlohmann
|
|
|
|
{
|
|
|
|
namespace detail
|
|
|
|
{
|
2019-10-01 08:34:21 +00:00
|
|
|
template<typename string_type>
|
2019-10-16 18:00:05 +00:00
|
|
|
void int_to_string( string_type& target, std::size_t value )
|
2019-09-26 11:13:01 +00:00
|
|
|
{
|
2020-06-22 12:17:56 +00:00
|
|
|
// For ADL
|
|
|
|
using std::to_string;
|
|
|
|
target = to_string(value);
|
2019-09-26 11:13:01 +00:00
|
|
|
}
|
2020-06-03 12:20:36 +00:00
|
|
|
template<typename IteratorType> class iteration_proxy_value
|
2017-08-14 15:26:22 +00:00
|
|
|
{
|
2018-12-19 14:47:35 +00:00
|
|
|
public:
|
|
|
|
using difference_type = std::ptrdiff_t;
|
|
|
|
using value_type = iteration_proxy_value;
|
2022-05-29 11:08:06 +00:00
|
|
|
using pointer = value_type *;
|
|
|
|
using reference = value_type &;
|
2018-12-19 14:47:35 +00:00
|
|
|
using iterator_category = std::input_iterator_tag;
|
2019-09-26 11:13:01 +00:00
|
|
|
using string_type = typename std::remove_cv< typename std::remove_reference<decltype( std::declval<IteratorType>().key() ) >::type >::type;
|
2018-12-19 14:47:35 +00:00
|
|
|
|
2017-08-14 15:26:22 +00:00
|
|
|
private:
|
2018-12-19 14:47:35 +00:00
|
|
|
/// the iterator
|
2022-05-29 11:08:06 +00:00
|
|
|
IteratorType anchor{};
|
2018-12-19 14:47:35 +00:00
|
|
|
/// an index for arrays (used to create key names)
|
|
|
|
std::size_t array_index = 0;
|
|
|
|
/// last stringified array index
|
|
|
|
mutable std::size_t array_index_last = 0;
|
|
|
|
/// a string representation of the array index
|
2019-09-26 11:13:01 +00:00
|
|
|
mutable string_type array_index_str = "0";
|
2018-12-19 14:47:35 +00:00
|
|
|
/// an empty string (to return a reference for primitive values)
|
2022-05-29 11:08:06 +00:00
|
|
|
string_type empty_str{};
|
2018-12-19 14:47:35 +00:00
|
|
|
|
|
|
|
public:
|
2022-05-29 11:08:06 +00:00
|
|
|
explicit iteration_proxy_value() = default;
|
|
|
|
explicit iteration_proxy_value(IteratorType it, std::size_t array_index_ = 0)
|
|
|
|
noexcept(std::is_nothrow_move_constructible<IteratorType>::value
|
|
|
|
&& std::is_nothrow_default_constructible<string_type>::value)
|
2021-03-24 06:15:18 +00:00
|
|
|
: anchor(std::move(it))
|
2022-05-29 11:08:06 +00:00
|
|
|
, array_index(array_index_)
|
2021-03-24 06:15:18 +00:00
|
|
|
{}
|
2018-12-19 14:47:35 +00:00
|
|
|
|
2022-05-29 11:08:06 +00:00
|
|
|
iteration_proxy_value(iteration_proxy_value const&) = default;
|
|
|
|
iteration_proxy_value& operator=(iteration_proxy_value const&) = default;
|
|
|
|
// older GCCs are a bit fussy and require explicit noexcept specifiers on defaulted functions
|
|
|
|
iteration_proxy_value(iteration_proxy_value&&)
|
|
|
|
noexcept(std::is_nothrow_move_constructible<IteratorType>::value
|
|
|
|
&& std::is_nothrow_move_constructible<string_type>::value) = default;
|
|
|
|
iteration_proxy_value& operator=(iteration_proxy_value&&)
|
|
|
|
noexcept(std::is_nothrow_move_assignable<IteratorType>::value
|
|
|
|
&& std::is_nothrow_move_assignable<string_type>::value) = default;
|
|
|
|
~iteration_proxy_value() = default;
|
|
|
|
|
2018-12-19 14:47:35 +00:00
|
|
|
/// dereference operator (needed for range-based for)
|
2022-05-29 11:08:06 +00:00
|
|
|
const iteration_proxy_value& operator*() const
|
2017-08-14 15:26:22 +00:00
|
|
|
{
|
2018-12-19 14:47:35 +00:00
|
|
|
return *this;
|
|
|
|
}
|
2017-08-14 15:26:22 +00:00
|
|
|
|
2018-12-19 14:47:35 +00:00
|
|
|
/// increment operator (needed for range-based for)
|
|
|
|
iteration_proxy_value& operator++()
|
|
|
|
{
|
|
|
|
++anchor;
|
|
|
|
++array_index;
|
2017-08-14 15:26:22 +00:00
|
|
|
|
2018-12-19 14:47:35 +00:00
|
|
|
return *this;
|
|
|
|
}
|
2017-08-14 15:26:22 +00:00
|
|
|
|
2022-05-29 11:08:06 +00:00
|
|
|
iteration_proxy_value operator++(int)& // NOLINT(cert-dcl21-cpp)
|
|
|
|
{
|
|
|
|
auto tmp = iteration_proxy_value(anchor, array_index);
|
|
|
|
++anchor;
|
|
|
|
++array_index;
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
2018-12-19 14:47:35 +00:00
|
|
|
/// equality operator (needed for InputIterator)
|
2019-01-13 14:40:49 +00:00
|
|
|
bool operator==(const iteration_proxy_value& o) const
|
2018-12-19 14:47:35 +00:00
|
|
|
{
|
|
|
|
return anchor == o.anchor;
|
|
|
|
}
|
2018-04-10 06:29:07 +00:00
|
|
|
|
2018-12-19 14:47:35 +00:00
|
|
|
/// inequality operator (needed for range-based for)
|
2019-01-13 14:40:49 +00:00
|
|
|
bool operator!=(const iteration_proxy_value& o) const
|
2018-12-19 14:47:35 +00:00
|
|
|
{
|
|
|
|
return anchor != o.anchor;
|
|
|
|
}
|
2017-08-14 15:26:22 +00:00
|
|
|
|
2018-12-19 14:47:35 +00:00
|
|
|
/// return key of the iterator
|
2019-09-26 11:13:01 +00:00
|
|
|
const string_type& key() const
|
2018-12-19 14:47:35 +00:00
|
|
|
{
|
2020-07-06 10:22:31 +00:00
|
|
|
JSON_ASSERT(anchor.m_object != nullptr);
|
2017-08-14 15:26:22 +00:00
|
|
|
|
2018-12-19 14:47:35 +00:00
|
|
|
switch (anchor.m_object->type())
|
|
|
|
{
|
|
|
|
// use integer array index as key
|
|
|
|
case value_t::array:
|
2017-08-14 15:26:22 +00:00
|
|
|
{
|
2018-12-19 14:47:35 +00:00
|
|
|
if (array_index != array_index_last)
|
2018-05-27 10:04:22 +00:00
|
|
|
{
|
2019-09-26 11:13:01 +00:00
|
|
|
int_to_string( array_index_str, array_index );
|
2018-12-19 14:47:35 +00:00
|
|
|
array_index_last = array_index;
|
2018-05-27 10:04:22 +00:00
|
|
|
}
|
2018-12-19 14:47:35 +00:00
|
|
|
return array_index_str;
|
|
|
|
}
|
2017-08-14 15:26:22 +00:00
|
|
|
|
2018-12-19 14:47:35 +00:00
|
|
|
// use key from the object
|
|
|
|
case value_t::object:
|
|
|
|
return anchor.key();
|
2017-08-14 15:26:22 +00:00
|
|
|
|
2018-12-19 14:47:35 +00:00
|
|
|
// use an empty key for all primitive types
|
2021-08-12 14:33:41 +00:00
|
|
|
case value_t::null:
|
|
|
|
case value_t::string:
|
|
|
|
case value_t::boolean:
|
|
|
|
case value_t::number_integer:
|
|
|
|
case value_t::number_unsigned:
|
|
|
|
case value_t::number_float:
|
|
|
|
case value_t::binary:
|
|
|
|
case value_t::discarded:
|
2018-12-19 14:47:35 +00:00
|
|
|
default:
|
|
|
|
return empty_str;
|
2017-08-14 15:26:22 +00:00
|
|
|
}
|
2018-12-19 14:47:35 +00:00
|
|
|
}
|
2017-08-14 15:26:22 +00:00
|
|
|
|
2018-12-19 14:47:35 +00:00
|
|
|
/// return value of the iterator
|
|
|
|
typename IteratorType::reference value() const
|
|
|
|
{
|
|
|
|
return anchor.value();
|
|
|
|
}
|
|
|
|
};
|
2017-08-14 15:26:22 +00:00
|
|
|
|
2018-12-19 14:47:35 +00:00
|
|
|
/// proxy class for the items() function
|
|
|
|
template<typename IteratorType> class iteration_proxy
|
|
|
|
{
|
|
|
|
private:
|
2017-08-14 15:26:22 +00:00
|
|
|
/// the container to iterate
|
2022-05-29 11:08:06 +00:00
|
|
|
typename IteratorType::pointer container = nullptr;
|
2017-08-14 15:26:22 +00:00
|
|
|
|
|
|
|
public:
|
2022-05-29 11:08:06 +00:00
|
|
|
explicit iteration_proxy() = default;
|
|
|
|
|
2017-08-14 15:26:22 +00:00
|
|
|
/// construct iteration proxy from a container
|
2018-01-16 19:41:04 +00:00
|
|
|
explicit iteration_proxy(typename IteratorType::reference cont) noexcept
|
2022-05-29 11:08:06 +00:00
|
|
|
: container(&cont) {}
|
|
|
|
|
|
|
|
iteration_proxy(iteration_proxy const&) = default;
|
|
|
|
iteration_proxy& operator=(iteration_proxy const&) = default;
|
|
|
|
iteration_proxy(iteration_proxy&&) noexcept = default;
|
|
|
|
iteration_proxy& operator=(iteration_proxy&&) noexcept = default;
|
|
|
|
~iteration_proxy() = default;
|
2017-08-14 15:26:22 +00:00
|
|
|
|
|
|
|
/// return iterator begin (needed for range-based for)
|
2022-05-29 11:08:06 +00:00
|
|
|
iteration_proxy_value<IteratorType> begin() const noexcept
|
2017-08-14 15:26:22 +00:00
|
|
|
{
|
2022-05-29 11:08:06 +00:00
|
|
|
return iteration_proxy_value<IteratorType>(container->begin());
|
2017-08-14 15:26:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// return iterator end (needed for range-based for)
|
2022-05-29 11:08:06 +00:00
|
|
|
iteration_proxy_value<IteratorType> end() const noexcept
|
2017-08-14 15:26:22 +00:00
|
|
|
{
|
2022-05-29 11:08:06 +00:00
|
|
|
return iteration_proxy_value<IteratorType>(container->end());
|
2017-08-14 15:26:22 +00:00
|
|
|
}
|
|
|
|
};
|
2022-05-29 11:08:06 +00:00
|
|
|
|
2018-12-19 14:47:35 +00:00
|
|
|
// Structured Bindings Support
|
|
|
|
// For further reference see https://blog.tartanllama.xyz/structured-bindings/
|
|
|
|
// And see https://github.com/nlohmann/json/pull/1391
|
2020-06-03 12:20:36 +00:00
|
|
|
template<std::size_t N, typename IteratorType, enable_if_t<N == 0, int> = 0>
|
2018-12-19 14:47:35 +00:00
|
|
|
auto get(const nlohmann::detail::iteration_proxy_value<IteratorType>& i) -> decltype(i.key())
|
|
|
|
{
|
|
|
|
return i.key();
|
|
|
|
}
|
|
|
|
// Structured Bindings Support
|
|
|
|
// For further reference see https://blog.tartanllama.xyz/structured-bindings/
|
|
|
|
// And see https://github.com/nlohmann/json/pull/1391
|
2020-06-03 12:20:36 +00:00
|
|
|
template<std::size_t N, typename IteratorType, enable_if_t<N == 1, int> = 0>
|
2018-12-19 14:47:35 +00:00
|
|
|
auto get(const nlohmann::detail::iteration_proxy_value<IteratorType>& i) -> decltype(i.value())
|
|
|
|
{
|
|
|
|
return i.value();
|
|
|
|
}
|
2018-10-07 16:39:18 +00:00
|
|
|
} // namespace detail
|
|
|
|
} // namespace nlohmann
|
2018-12-19 14:47:35 +00:00
|
|
|
|
|
|
|
// The Addition to the STD Namespace is required to add
|
|
|
|
// Structured Bindings Support to the iteration_proxy_value class
|
|
|
|
// For further reference see https://blog.tartanllama.xyz/structured-bindings/
|
|
|
|
// And see https://github.com/nlohmann/json/pull/1391
|
|
|
|
namespace std
|
|
|
|
{
|
2019-01-30 17:59:50 +00:00
|
|
|
#if defined(__clang__)
|
|
|
|
// Fix: https://github.com/nlohmann/json/issues/1401
|
|
|
|
#pragma clang diagnostic push
|
|
|
|
#pragma clang diagnostic ignored "-Wmismatched-tags"
|
|
|
|
#endif
|
2020-06-03 12:20:36 +00:00
|
|
|
template<typename IteratorType>
|
2018-12-20 21:41:48 +00:00
|
|
|
class tuple_size<::nlohmann::detail::iteration_proxy_value<IteratorType>>
|
|
|
|
: public std::integral_constant<std::size_t, 2> {};
|
2018-12-19 14:47:35 +00:00
|
|
|
|
2020-06-03 12:20:36 +00:00
|
|
|
template<std::size_t N, typename IteratorType>
|
2018-12-20 21:41:48 +00:00
|
|
|
class tuple_element<N, ::nlohmann::detail::iteration_proxy_value<IteratorType >>
|
2018-12-19 14:47:35 +00:00
|
|
|
{
|
2018-12-20 21:41:48 +00:00
|
|
|
public:
|
2018-12-19 14:47:35 +00:00
|
|
|
using type = decltype(
|
|
|
|
get<N>(std::declval <
|
|
|
|
::nlohmann::detail::iteration_proxy_value<IteratorType >> ()));
|
|
|
|
};
|
2019-01-30 17:59:50 +00:00
|
|
|
#if defined(__clang__)
|
|
|
|
#pragma clang diagnostic pop
|
|
|
|
#endif
|
2019-03-15 13:55:13 +00:00
|
|
|
} // namespace std
|
2022-05-29 11:08:06 +00:00
|
|
|
|
|
|
|
#if JSON_HAS_RANGES
|
|
|
|
template <typename IteratorType>
|
|
|
|
inline constexpr bool ::std::ranges::enable_borrowed_range<::nlohmann::detail::iteration_proxy<IteratorType>> = true;
|
|
|
|
#endif
|