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
|
|
|
|
#include <string> // string, to_string
|
|
|
|
|
2018-01-29 10:21:11 +00:00
|
|
|
#include <nlohmann/detail/value_t.hpp>
|
2017-08-14 15:26:22 +00:00
|
|
|
|
|
|
|
namespace nlohmann
|
|
|
|
{
|
|
|
|
namespace detail
|
|
|
|
{
|
2018-01-23 15:33:08 +00:00
|
|
|
/// proxy class for the items() function
|
2017-08-14 15:26:22 +00:00
|
|
|
template<typename IteratorType> class iteration_proxy
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
/// helper class for iteration
|
|
|
|
class iteration_proxy_internal
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
/// the iterator
|
|
|
|
IteratorType anchor;
|
|
|
|
/// an index for arrays (used to create key names)
|
|
|
|
std::size_t array_index = 0;
|
2018-05-27 10:04:22 +00:00
|
|
|
/// last stringified array index
|
|
|
|
mutable std::size_t array_index_last = 0;
|
2018-05-26 11:26:40 +00:00
|
|
|
/// a string representation of the array index
|
2018-05-27 10:04:22 +00:00
|
|
|
mutable std::string array_index_str = "0";
|
2018-05-26 11:26:40 +00:00
|
|
|
/// an empty string (to return a reference for primitive values)
|
|
|
|
const std::string empty_str = "";
|
2017-08-14 15:26:22 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
explicit iteration_proxy_internal(IteratorType it) noexcept : anchor(it) {}
|
|
|
|
|
|
|
|
/// dereference operator (needed for range-based for)
|
|
|
|
iteration_proxy_internal& operator*()
|
|
|
|
{
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// increment operator (needed for range-based for)
|
|
|
|
iteration_proxy_internal& operator++()
|
|
|
|
{
|
|
|
|
++anchor;
|
2018-05-27 10:04:22 +00:00
|
|
|
++array_index;
|
2017-08-14 15:26:22 +00:00
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// inequality operator (needed for range-based for)
|
|
|
|
bool operator!=(const iteration_proxy_internal& o) const noexcept
|
|
|
|
{
|
|
|
|
return anchor != o.anchor;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// return key of the iterator
|
2018-05-26 11:26:40 +00:00
|
|
|
const std::string& key() const
|
2017-08-14 15:26:22 +00:00
|
|
|
{
|
|
|
|
assert(anchor.m_object != nullptr);
|
|
|
|
|
|
|
|
switch (anchor.m_object->type())
|
|
|
|
{
|
|
|
|
// use integer array index as key
|
|
|
|
case value_t::array:
|
2018-05-27 10:04:22 +00:00
|
|
|
{
|
|
|
|
if (array_index != array_index_last)
|
|
|
|
{
|
|
|
|
array_index_str = std::to_string(array_index);
|
|
|
|
array_index_last = array_index;
|
|
|
|
}
|
2018-05-26 11:26:40 +00:00
|
|
|
return array_index_str;
|
2018-05-27 10:04:22 +00:00
|
|
|
}
|
2017-08-14 15:26:22 +00:00
|
|
|
|
|
|
|
// use key from the object
|
|
|
|
case value_t::object:
|
|
|
|
return anchor.key();
|
|
|
|
|
|
|
|
// use an empty key for all primitive types
|
|
|
|
default:
|
2018-05-26 11:26:40 +00:00
|
|
|
return empty_str;
|
2017-08-14 15:26:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// return value of the iterator
|
|
|
|
typename IteratorType::reference value() const
|
|
|
|
{
|
|
|
|
return anchor.value();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// 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)
|
|
|
|
iteration_proxy_internal begin() noexcept
|
|
|
|
{
|
|
|
|
return iteration_proxy_internal(container.begin());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// return iterator end (needed for range-based for)
|
|
|
|
iteration_proxy_internal end() noexcept
|
|
|
|
{
|
|
|
|
return iteration_proxy_internal(container.end());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|