2018-01-10 17:18:31 +08:00
|
|
|
#pragma once
|
2017-08-14 23:26:22 +08:00
|
|
|
|
2017-08-15 01:28:01 +08:00
|
|
|
#include <cstddef> // size_t
|
2018-04-10 14:29:07 +08:00
|
|
|
#include <iterator> // input_iterator_tag
|
2019-03-17 19:01:49 +08:00
|
|
|
#include <string> // string, to_string
|
2018-12-19 22:47:35 +08:00
|
|
|
#include <tuple> // tuple_size, get, tuple_element
|
2017-08-15 01:28:01 +08:00
|
|
|
|
2018-12-19 22:47:35 +08:00
|
|
|
#include <nlohmann/detail/meta/type_traits.hpp>
|
2019-03-17 19:01:49 +08:00
|
|
|
#include <nlohmann/detail/value_t.hpp>
|
2017-08-14 23:26:22 +08:00
|
|
|
|
|
|
|
namespace nlohmann
|
|
|
|
{
|
|
|
|
namespace detail
|
|
|
|
{
|
2020-06-16 15:35:26 +08:00
|
|
|
template<typename string_type, typename std::enable_if<
|
|
|
|
std::is_same<char, typename string_type::value_type>::value, int>::type = 0>
|
2019-10-17 02:00:05 +08:00
|
|
|
void int_to_string( string_type& target, std::size_t value )
|
2019-09-26 19:13:01 +08:00
|
|
|
{
|
2020-06-22 20:17:56 +08:00
|
|
|
// For ADL
|
|
|
|
using std::to_string;
|
|
|
|
target = to_string(value);
|
2019-09-26 19:13:01 +08:00
|
|
|
}
|
2018-12-19 22:47:35 +08:00
|
|
|
template <typename IteratorType> class iteration_proxy_value
|
2017-08-14 23:26:22 +08:00
|
|
|
{
|
2018-12-19 22:47:35 +08:00
|
|
|
public:
|
|
|
|
using difference_type = std::ptrdiff_t;
|
|
|
|
using value_type = iteration_proxy_value;
|
|
|
|
using pointer = value_type * ;
|
|
|
|
using reference = value_type & ;
|
|
|
|
using iterator_category = std::input_iterator_tag;
|
2019-09-26 19:13:01 +08:00
|
|
|
using string_type = typename std::remove_cv< typename std::remove_reference<decltype( std::declval<IteratorType>().key() ) >::type >::type;
|
2018-12-19 22:47:35 +08:00
|
|
|
|
2017-08-14 23:26:22 +08:00
|
|
|
private:
|
2018-12-19 22:47:35 +08:00
|
|
|
/// the iterator
|
|
|
|
IteratorType anchor;
|
|
|
|
/// 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 19:13:01 +08:00
|
|
|
mutable string_type array_index_str = "0";
|
2018-12-19 22:47:35 +08:00
|
|
|
/// an empty string (to return a reference for primitive values)
|
2019-09-26 19:13:01 +08:00
|
|
|
const string_type empty_str = "";
|
2018-12-19 22:47:35 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
explicit iteration_proxy_value(IteratorType it) noexcept : anchor(it) {}
|
|
|
|
|
|
|
|
/// dereference operator (needed for range-based for)
|
|
|
|
iteration_proxy_value& operator*()
|
2017-08-14 23:26:22 +08:00
|
|
|
{
|
2018-12-19 22:47:35 +08:00
|
|
|
return *this;
|
|
|
|
}
|
2017-08-14 23:26:22 +08:00
|
|
|
|
2018-12-19 22:47:35 +08:00
|
|
|
/// increment operator (needed for range-based for)
|
|
|
|
iteration_proxy_value& operator++()
|
|
|
|
{
|
|
|
|
++anchor;
|
|
|
|
++array_index;
|
2017-08-14 23:26:22 +08:00
|
|
|
|
2018-12-19 22:47:35 +08:00
|
|
|
return *this;
|
|
|
|
}
|
2017-08-14 23:26:22 +08:00
|
|
|
|
2018-12-19 22:47:35 +08:00
|
|
|
/// equality operator (needed for InputIterator)
|
2019-01-13 22:40:49 +08:00
|
|
|
bool operator==(const iteration_proxy_value& o) const
|
2018-12-19 22:47:35 +08:00
|
|
|
{
|
|
|
|
return anchor == o.anchor;
|
|
|
|
}
|
2018-04-10 14:29:07 +08:00
|
|
|
|
2018-12-19 22:47:35 +08:00
|
|
|
/// inequality operator (needed for range-based for)
|
2019-01-13 22:40:49 +08:00
|
|
|
bool operator!=(const iteration_proxy_value& o) const
|
2018-12-19 22:47:35 +08:00
|
|
|
{
|
|
|
|
return anchor != o.anchor;
|
|
|
|
}
|
2017-08-14 23:26:22 +08:00
|
|
|
|
2018-12-19 22:47:35 +08:00
|
|
|
/// return key of the iterator
|
2019-09-26 19:13:01 +08:00
|
|
|
const string_type& key() const
|
2018-12-19 22:47:35 +08:00
|
|
|
{
|
|
|
|
assert(anchor.m_object != nullptr);
|
2017-08-14 23:26:22 +08:00
|
|
|
|
2018-12-19 22:47:35 +08:00
|
|
|
switch (anchor.m_object->type())
|
|
|
|
{
|
|
|
|
// use integer array index as key
|
|
|
|
case value_t::array:
|
2017-08-14 23:26:22 +08:00
|
|
|
{
|
2018-12-19 22:47:35 +08:00
|
|
|
if (array_index != array_index_last)
|
2018-05-27 18:04:22 +08:00
|
|
|
{
|
2019-09-26 19:13:01 +08:00
|
|
|
int_to_string( array_index_str, array_index );
|
2018-12-19 22:47:35 +08:00
|
|
|
array_index_last = array_index;
|
2018-05-27 18:04:22 +08:00
|
|
|
}
|
2018-12-19 22:47:35 +08:00
|
|
|
return array_index_str;
|
|
|
|
}
|
2017-08-14 23:26:22 +08:00
|
|
|
|
2018-12-19 22:47:35 +08:00
|
|
|
// use key from the object
|
|
|
|
case value_t::object:
|
|
|
|
return anchor.key();
|
2017-08-14 23:26:22 +08:00
|
|
|
|
2018-12-19 22:47:35 +08:00
|
|
|
// use an empty key for all primitive types
|
|
|
|
default:
|
|
|
|
return empty_str;
|
2017-08-14 23:26:22 +08:00
|
|
|
}
|
2018-12-19 22:47:35 +08:00
|
|
|
}
|
2017-08-14 23:26:22 +08:00
|
|
|
|
2018-12-19 22:47:35 +08:00
|
|
|
/// return value of the iterator
|
|
|
|
typename IteratorType::reference value() const
|
|
|
|
{
|
|
|
|
return anchor.value();
|
|
|
|
}
|
|
|
|
};
|
2017-08-14 23:26:22 +08:00
|
|
|
|
2018-12-19 22:47:35 +08:00
|
|
|
/// proxy class for the items() function
|
|
|
|
template<typename IteratorType> class iteration_proxy
|
|
|
|
{
|
|
|
|
private:
|
2017-08-14 23:26:22 +08:00
|
|
|
/// the container to iterate
|
|
|
|
typename IteratorType::reference container;
|
|
|
|
|
|
|
|
public:
|
|
|
|
/// construct iteration proxy from a container
|
2018-01-17 03:41:04 +08:00
|
|
|
explicit iteration_proxy(typename IteratorType::reference cont) noexcept
|
2017-08-14 23:26:22 +08:00
|
|
|
: container(cont) {}
|
|
|
|
|
|
|
|
/// return iterator begin (needed for range-based for)
|
2018-12-19 22:47:35 +08:00
|
|
|
iteration_proxy_value<IteratorType> begin() noexcept
|
2017-08-14 23:26:22 +08:00
|
|
|
{
|
2018-12-19 22:47:35 +08:00
|
|
|
return iteration_proxy_value<IteratorType>(container.begin());
|
2017-08-14 23:26:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// return iterator end (needed for range-based for)
|
2018-12-19 22:47:35 +08:00
|
|
|
iteration_proxy_value<IteratorType> end() noexcept
|
2017-08-14 23:26:22 +08:00
|
|
|
{
|
2018-12-19 22:47:35 +08:00
|
|
|
return iteration_proxy_value<IteratorType>(container.end());
|
2017-08-14 23:26:22 +08:00
|
|
|
}
|
|
|
|
};
|
2018-12-19 22:47:35 +08:00
|
|
|
// Structured Bindings Support
|
|
|
|
// For further reference see https://blog.tartanllama.xyz/structured-bindings/
|
|
|
|
// And see https://github.com/nlohmann/json/pull/1391
|
|
|
|
template <std::size_t N, typename IteratorType, enable_if_t<N == 0, int> = 0>
|
|
|
|
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
|
|
|
|
template <std::size_t N, typename IteratorType, enable_if_t<N == 1, int> = 0>
|
|
|
|
auto get(const nlohmann::detail::iteration_proxy_value<IteratorType>& i) -> decltype(i.value())
|
|
|
|
{
|
|
|
|
return i.value();
|
|
|
|
}
|
2018-10-08 00:39:18 +08:00
|
|
|
} // namespace detail
|
|
|
|
} // namespace nlohmann
|
2018-12-19 22:47:35 +08: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-31 01:59:50 +08:00
|
|
|
#if defined(__clang__)
|
|
|
|
// Fix: https://github.com/nlohmann/json/issues/1401
|
|
|
|
#pragma clang diagnostic push
|
|
|
|
#pragma clang diagnostic ignored "-Wmismatched-tags"
|
|
|
|
#endif
|
2018-12-19 22:47:35 +08:00
|
|
|
template <typename IteratorType>
|
2018-12-21 05:41:48 +08:00
|
|
|
class tuple_size<::nlohmann::detail::iteration_proxy_value<IteratorType>>
|
|
|
|
: public std::integral_constant<std::size_t, 2> {};
|
2018-12-19 22:47:35 +08:00
|
|
|
|
|
|
|
template <std::size_t N, typename IteratorType>
|
2018-12-21 05:41:48 +08:00
|
|
|
class tuple_element<N, ::nlohmann::detail::iteration_proxy_value<IteratorType >>
|
2018-12-19 22:47:35 +08:00
|
|
|
{
|
2018-12-21 05:41:48 +08:00
|
|
|
public:
|
2018-12-19 22:47:35 +08:00
|
|
|
using type = decltype(
|
|
|
|
get<N>(std::declval <
|
|
|
|
::nlohmann::detail::iteration_proxy_value<IteratorType >> ()));
|
|
|
|
};
|
2019-01-31 01:59:50 +08:00
|
|
|
#if defined(__clang__)
|
|
|
|
#pragma clang diagnostic pop
|
|
|
|
#endif
|
2019-03-15 21:55:13 +08:00
|
|
|
} // namespace std
|