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
|
2017-08-14 17:28:01 +00:00
|
|
|
|
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
|
|
|
|
{
|
2018-12-19 14:47:35 +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;
|
|
|
|
using pointer = value_type * ;
|
|
|
|
using reference = value_type & ;
|
|
|
|
using iterator_category = std::input_iterator_tag;
|
|
|
|
|
2017-08-14 15:26:22 +00:00
|
|
|
private:
|
2018-12-19 14:47:35 +00: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
|
|
|
|
mutable std::string array_index_str = "0";
|
|
|
|
/// an empty string (to return a reference for primitive values)
|
|
|
|
const std::string empty_str = "";
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit iteration_proxy_value(IteratorType it) noexcept : anchor(it) {}
|
|
|
|
|
|
|
|
/// dereference operator (needed for range-based for)
|
|
|
|
iteration_proxy_value& operator*()
|
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
|
|
|
|
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
|
|
|
|
const std::string& key() const
|
|
|
|
{
|
|
|
|
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
|
|
|
{
|
2018-12-19 14:47:35 +00:00
|
|
|
array_index_str = std::to_string(array_index);
|
|
|
|
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
|
|
|
|
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
|
|
|
|
typename IteratorType::reference container;
|
|
|
|
|
|
|
|
public:
|
|
|
|
/// construct iteration proxy from a container
|
2018-01-16 19:41:04 +00:00
|
|
|
explicit iteration_proxy(typename IteratorType::reference cont) noexcept
|
2017-08-14 15:26:22 +00:00
|
|
|
: container(cont) {}
|
|
|
|
|
|
|
|
/// return iterator begin (needed for range-based for)
|
2018-12-19 14:47:35 +00:00
|
|
|
iteration_proxy_value<IteratorType> begin() noexcept
|
2017-08-14 15:26:22 +00:00
|
|
|
{
|
2018-12-19 14:47:35 +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)
|
2018-12-19 14:47:35 +00:00
|
|
|
iteration_proxy_value<IteratorType> end() noexcept
|
2017-08-14 15:26:22 +00:00
|
|
|
{
|
2018-12-19 14:47:35 +00:00
|
|
|
return iteration_proxy_value<IteratorType>(container.end());
|
2017-08-14 15:26:22 +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
|
|
|
|
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-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
|
2018-12-19 14:47:35 +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
|
|
|
|
|
|
|
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
|