2018-01-10 10:18:31 +01:00
|
|
|
#pragma once
|
2017-08-14 17:23:15 +02:00
|
|
|
|
2017-08-14 19:28:01 +02:00
|
|
|
#include <iterator> // iterator, random_access_iterator_tag, bidirectional_iterator_tag, advance, next
|
|
|
|
#include <type_traits> // conditional, is_const, remove_const
|
2017-08-14 17:23:15 +02:00
|
|
|
|
2018-01-29 11:21:11 +01:00
|
|
|
#include <nlohmann/detail/exceptions.hpp>
|
|
|
|
#include <nlohmann/detail/iterators/internal_iterator.hpp>
|
|
|
|
#include <nlohmann/detail/iterators/primitive_iterator.hpp>
|
|
|
|
#include <nlohmann/detail/macro_scope.hpp>
|
2018-07-24 13:22:44 +02:00
|
|
|
#include <nlohmann/detail/meta/cpp_future.hpp>
|
2019-01-13 15:31:22 +01:00
|
|
|
#include <nlohmann/detail/meta/type_traits.hpp>
|
2018-01-29 11:21:11 +01:00
|
|
|
#include <nlohmann/detail/value_t.hpp>
|
2017-08-14 17:23:15 +02:00
|
|
|
|
|
|
|
namespace nlohmann
|
|
|
|
{
|
|
|
|
namespace detail
|
|
|
|
{
|
|
|
|
// forward declare, to be able to friend it later on
|
|
|
|
template<typename IteratorType> class iteration_proxy;
|
2018-12-19 15:47:35 +01:00
|
|
|
template<typename IteratorType> class iteration_proxy_value;
|
2017-08-14 17:23:15 +02:00
|
|
|
|
|
|
|
/*!
|
|
|
|
@brief a template for a bidirectional iterator for the @ref basic_json class
|
|
|
|
This class implements a both iterators (iterator and const_iterator) for the
|
|
|
|
@ref basic_json class.
|
|
|
|
@note An iterator is called *initialized* when a pointer to a JSON value has
|
|
|
|
been set (e.g., by a constructor or a copy assignment). If the iterator is
|
|
|
|
default-constructed, it is *uninitialized* and most methods are undefined.
|
|
|
|
**The library uses assertions to detect calls on uninitialized iterators.**
|
|
|
|
@requirement The class satisfies the following concept requirements:
|
|
|
|
-
|
2018-06-24 22:30:37 +02:00
|
|
|
[BidirectionalIterator](https://en.cppreference.com/w/cpp/named_req/BidirectionalIterator):
|
2017-08-14 17:23:15 +02:00
|
|
|
The iterator that can be moved can be moved in both directions (i.e.
|
|
|
|
incremented and decremented).
|
|
|
|
@since version 1.0.0, simplified in version 2.0.9, change to bidirectional
|
|
|
|
iterators in version 3.0.0 (see https://github.com/nlohmann/json/issues/593)
|
|
|
|
*/
|
|
|
|
template<typename BasicJsonType>
|
|
|
|
class iter_impl
|
|
|
|
{
|
2020-12-29 21:36:30 +01:00
|
|
|
/// the iterator with BasicJsonType of different const-ness
|
|
|
|
using other_iter_impl = iter_impl<typename std::conditional<std::is_const<BasicJsonType>::value, typename std::remove_const<BasicJsonType>::type, const BasicJsonType>::type>;
|
2017-08-14 17:23:15 +02:00
|
|
|
/// allow basic_json to access private members
|
2020-12-29 21:36:30 +01:00
|
|
|
friend other_iter_impl;
|
2017-08-14 17:23:15 +02:00
|
|
|
friend BasicJsonType;
|
|
|
|
friend iteration_proxy<iter_impl>;
|
2018-12-19 15:47:35 +01:00
|
|
|
friend iteration_proxy_value<iter_impl>;
|
2017-08-14 17:23:15 +02:00
|
|
|
|
|
|
|
using object_t = typename BasicJsonType::object_t;
|
|
|
|
using array_t = typename BasicJsonType::array_t;
|
|
|
|
// make sure BasicJsonType is basic_json or const basic_json
|
|
|
|
static_assert(is_basic_json<typename std::remove_const<BasicJsonType>::type>::value,
|
|
|
|
"iter_impl only accepts (const) basic_json");
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
/// The std::iterator class template (used as a base class to provide typedefs) is deprecated in C++17.
|
|
|
|
/// The C++ Standard has never required user-defined iterators to derive from std::iterator.
|
|
|
|
/// A user-defined iterator should provide publicly accessible typedefs named
|
|
|
|
/// iterator_category, value_type, difference_type, pointer, and reference.
|
|
|
|
/// Note that value_type is required to be non-const, even for constant iterators.
|
|
|
|
using iterator_category = std::bidirectional_iterator_tag;
|
|
|
|
|
|
|
|
/// the type of the values when the iterator is dereferenced
|
|
|
|
using value_type = typename BasicJsonType::value_type;
|
|
|
|
/// a type to represent differences between iterators
|
|
|
|
using difference_type = typename BasicJsonType::difference_type;
|
|
|
|
/// defines a pointer to the type iterated over (value_type)
|
|
|
|
using pointer = typename std::conditional<std::is_const<BasicJsonType>::value,
|
|
|
|
typename BasicJsonType::const_pointer,
|
|
|
|
typename BasicJsonType::pointer>::type;
|
|
|
|
/// defines a reference to the type iterated over (value_type)
|
|
|
|
using reference =
|
|
|
|
typename std::conditional<std::is_const<BasicJsonType>::value,
|
|
|
|
typename BasicJsonType::const_reference,
|
|
|
|
typename BasicJsonType::reference>::type;
|
|
|
|
|
|
|
|
/// default constructor
|
|
|
|
iter_impl() = default;
|
|
|
|
|
|
|
|
/*!
|
|
|
|
@brief constructor for a given JSON instance
|
|
|
|
@param[in] object pointer to a JSON object for this iterator
|
|
|
|
@pre object != nullptr
|
|
|
|
@post The iterator is initialized; i.e. `m_object != nullptr`.
|
|
|
|
*/
|
|
|
|
explicit iter_impl(pointer object) noexcept : m_object(object)
|
|
|
|
{
|
2020-07-06 12:22:31 +02:00
|
|
|
JSON_ASSERT(m_object != nullptr);
|
2017-08-14 17:23:15 +02:00
|
|
|
|
|
|
|
switch (m_object->m_type)
|
|
|
|
{
|
|
|
|
case value_t::object:
|
|
|
|
{
|
|
|
|
m_it.object_iterator = typename object_t::iterator();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case value_t::array:
|
|
|
|
{
|
|
|
|
m_it.array_iterator = typename array_t::iterator();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
m_it.primitive_iterator = primitive_iterator_t();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
@note The conventional copy constructor and copy assignment are implicitly
|
|
|
|
defined. Combined with the following converting constructor and
|
|
|
|
assignment, they support: (1) copy from iterator to iterator, (2)
|
|
|
|
copy from const iterator to const iterator, and (3) conversion from
|
|
|
|
iterator to const iterator. However conversion from const iterator
|
|
|
|
to iterator is not defined.
|
|
|
|
*/
|
|
|
|
|
2019-06-24 13:10:01 +02:00
|
|
|
/*!
|
|
|
|
@brief const copy constructor
|
|
|
|
@param[in] other const iterator to copy from
|
2019-10-19 11:59:46 +02:00
|
|
|
@note This copy constructor had to be defined explicitly to circumvent a bug
|
|
|
|
occurring on msvc v19.0 compiler (VS 2015) debug build. For more
|
2019-06-24 13:10:01 +02:00
|
|
|
information refer to: https://github.com/nlohmann/json/issues/1608
|
|
|
|
*/
|
2019-06-21 20:46:42 +02:00
|
|
|
iter_impl(const iter_impl<const BasicJsonType>& other) noexcept
|
2019-06-30 22:14:02 +02:00
|
|
|
: m_object(other.m_object), m_it(other.m_it)
|
|
|
|
{}
|
2019-06-21 20:46:42 +02:00
|
|
|
|
2019-07-12 21:05:16 +02:00
|
|
|
/*!
|
|
|
|
@brief converting assignment
|
|
|
|
@param[in] other const iterator to copy from
|
|
|
|
@return const/non-const iterator
|
|
|
|
@note It is not checked whether @a other is initialized.
|
|
|
|
*/
|
|
|
|
iter_impl& operator=(const iter_impl<const BasicJsonType>& other) noexcept
|
|
|
|
{
|
|
|
|
m_object = other.m_object;
|
|
|
|
m_it = other.m_it;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-08-14 17:23:15 +02:00
|
|
|
/*!
|
|
|
|
@brief converting constructor
|
|
|
|
@param[in] other non-const iterator to copy from
|
|
|
|
@note It is not checked whether @a other is initialized.
|
|
|
|
*/
|
|
|
|
iter_impl(const iter_impl<typename std::remove_const<BasicJsonType>::type>& other) noexcept
|
2019-06-30 22:14:02 +02:00
|
|
|
: m_object(other.m_object), m_it(other.m_it)
|
|
|
|
{}
|
2017-08-14 17:23:15 +02:00
|
|
|
|
|
|
|
/*!
|
|
|
|
@brief converting assignment
|
2019-07-12 21:05:16 +02:00
|
|
|
@param[in] other non-const iterator to copy from
|
2017-08-14 17:23:15 +02:00
|
|
|
@return const/non-const iterator
|
|
|
|
@note It is not checked whether @a other is initialized.
|
|
|
|
*/
|
|
|
|
iter_impl& operator=(const iter_impl<typename std::remove_const<BasicJsonType>::type>& other) noexcept
|
|
|
|
{
|
|
|
|
m_object = other.m_object;
|
|
|
|
m_it = other.m_it;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-08-12 13:30:06 +02:00
|
|
|
JSON_PRIVATE_UNLESS_TESTED:
|
2017-08-14 17:23:15 +02:00
|
|
|
/*!
|
|
|
|
@brief set the iterator to the first value
|
|
|
|
@pre The iterator is initialized; i.e. `m_object != nullptr`.
|
|
|
|
*/
|
|
|
|
void set_begin() noexcept
|
|
|
|
{
|
2020-07-06 12:22:31 +02:00
|
|
|
JSON_ASSERT(m_object != nullptr);
|
2017-08-14 17:23:15 +02:00
|
|
|
|
|
|
|
switch (m_object->m_type)
|
|
|
|
{
|
|
|
|
case value_t::object:
|
|
|
|
{
|
|
|
|
m_it.object_iterator = m_object->m_value.object->begin();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case value_t::array:
|
|
|
|
{
|
|
|
|
m_it.array_iterator = m_object->m_value.array->begin();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case value_t::null:
|
|
|
|
{
|
|
|
|
// set to end so begin()==end() is true: null is empty
|
|
|
|
m_it.primitive_iterator.set_end();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
m_it.primitive_iterator.set_begin();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
@brief set the iterator past the last value
|
|
|
|
@pre The iterator is initialized; i.e. `m_object != nullptr`.
|
|
|
|
*/
|
|
|
|
void set_end() noexcept
|
|
|
|
{
|
2020-07-06 12:22:31 +02:00
|
|
|
JSON_ASSERT(m_object != nullptr);
|
2017-08-14 17:23:15 +02:00
|
|
|
|
|
|
|
switch (m_object->m_type)
|
|
|
|
{
|
|
|
|
case value_t::object:
|
|
|
|
{
|
|
|
|
m_it.object_iterator = m_object->m_value.object->end();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case value_t::array:
|
|
|
|
{
|
|
|
|
m_it.array_iterator = m_object->m_value.array->end();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
m_it.primitive_iterator.set_end();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
/*!
|
|
|
|
@brief return a reference to the value pointed to by the iterator
|
|
|
|
@pre The iterator is initialized; i.e. `m_object != nullptr`.
|
|
|
|
*/
|
|
|
|
reference operator*() const
|
|
|
|
{
|
2020-07-06 12:22:31 +02:00
|
|
|
JSON_ASSERT(m_object != nullptr);
|
2017-08-14 17:23:15 +02:00
|
|
|
|
|
|
|
switch (m_object->m_type)
|
|
|
|
{
|
|
|
|
case value_t::object:
|
|
|
|
{
|
2020-07-06 12:22:31 +02:00
|
|
|
JSON_ASSERT(m_it.object_iterator != m_object->m_value.object->end());
|
2017-08-14 17:23:15 +02:00
|
|
|
return m_it.object_iterator->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
case value_t::array:
|
|
|
|
{
|
2020-07-06 12:22:31 +02:00
|
|
|
JSON_ASSERT(m_it.array_iterator != m_object->m_value.array->end());
|
2017-08-14 17:23:15 +02:00
|
|
|
return *m_it.array_iterator;
|
|
|
|
}
|
|
|
|
|
|
|
|
case value_t::null:
|
2021-01-25 13:47:50 +01:00
|
|
|
JSON_THROW(invalid_iterator::create(214, "cannot get value", *m_object));
|
2017-08-14 17:23:15 +02:00
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
2019-07-01 22:37:30 +02:00
|
|
|
if (JSON_HEDLEY_LIKELY(m_it.primitive_iterator.is_begin()))
|
2017-08-14 17:23:15 +02:00
|
|
|
{
|
|
|
|
return *m_object;
|
|
|
|
}
|
|
|
|
|
2021-01-25 13:47:50 +01:00
|
|
|
JSON_THROW(invalid_iterator::create(214, "cannot get value", *m_object));
|
2017-08-14 17:23:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
@brief dereference the iterator
|
|
|
|
@pre The iterator is initialized; i.e. `m_object != nullptr`.
|
|
|
|
*/
|
|
|
|
pointer operator->() const
|
|
|
|
{
|
2020-07-06 12:22:31 +02:00
|
|
|
JSON_ASSERT(m_object != nullptr);
|
2017-08-14 17:23:15 +02:00
|
|
|
|
|
|
|
switch (m_object->m_type)
|
|
|
|
{
|
|
|
|
case value_t::object:
|
|
|
|
{
|
2020-07-06 12:22:31 +02:00
|
|
|
JSON_ASSERT(m_it.object_iterator != m_object->m_value.object->end());
|
2017-08-14 17:23:15 +02:00
|
|
|
return &(m_it.object_iterator->second);
|
|
|
|
}
|
|
|
|
|
|
|
|
case value_t::array:
|
|
|
|
{
|
2020-07-06 12:22:31 +02:00
|
|
|
JSON_ASSERT(m_it.array_iterator != m_object->m_value.array->end());
|
2017-08-14 17:23:15 +02:00
|
|
|
return &*m_it.array_iterator;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
2019-07-01 22:37:30 +02:00
|
|
|
if (JSON_HEDLEY_LIKELY(m_it.primitive_iterator.is_begin()))
|
2017-08-14 17:23:15 +02:00
|
|
|
{
|
|
|
|
return m_object;
|
|
|
|
}
|
|
|
|
|
2021-01-25 13:47:50 +01:00
|
|
|
JSON_THROW(invalid_iterator::create(214, "cannot get value", *m_object));
|
2017-08-14 17:23:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
@brief post-increment (it++)
|
|
|
|
@pre The iterator is initialized; i.e. `m_object != nullptr`.
|
|
|
|
*/
|
|
|
|
iter_impl const operator++(int)
|
|
|
|
{
|
|
|
|
auto result = *this;
|
|
|
|
++(*this);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
@brief pre-increment (++it)
|
|
|
|
@pre The iterator is initialized; i.e. `m_object != nullptr`.
|
|
|
|
*/
|
|
|
|
iter_impl& operator++()
|
|
|
|
{
|
2020-07-06 12:22:31 +02:00
|
|
|
JSON_ASSERT(m_object != nullptr);
|
2017-08-14 17:23:15 +02:00
|
|
|
|
|
|
|
switch (m_object->m_type)
|
|
|
|
{
|
|
|
|
case value_t::object:
|
|
|
|
{
|
|
|
|
std::advance(m_it.object_iterator, 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case value_t::array:
|
|
|
|
{
|
|
|
|
std::advance(m_it.array_iterator, 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
++m_it.primitive_iterator;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
@brief post-decrement (it--)
|
|
|
|
@pre The iterator is initialized; i.e. `m_object != nullptr`.
|
|
|
|
*/
|
|
|
|
iter_impl const operator--(int)
|
|
|
|
{
|
|
|
|
auto result = *this;
|
|
|
|
--(*this);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
@brief pre-decrement (--it)
|
|
|
|
@pre The iterator is initialized; i.e. `m_object != nullptr`.
|
|
|
|
*/
|
|
|
|
iter_impl& operator--()
|
|
|
|
{
|
2020-07-06 12:22:31 +02:00
|
|
|
JSON_ASSERT(m_object != nullptr);
|
2017-08-14 17:23:15 +02:00
|
|
|
|
|
|
|
switch (m_object->m_type)
|
|
|
|
{
|
|
|
|
case value_t::object:
|
|
|
|
{
|
|
|
|
std::advance(m_it.object_iterator, -1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case value_t::array:
|
|
|
|
{
|
|
|
|
std::advance(m_it.array_iterator, -1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
--m_it.primitive_iterator;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
2020-12-29 21:36:30 +01:00
|
|
|
@brief comparison: equal
|
2017-08-14 17:23:15 +02:00
|
|
|
@pre The iterator is initialized; i.e. `m_object != nullptr`.
|
|
|
|
*/
|
2020-12-29 21:36:30 +01:00
|
|
|
template < typename IterImpl, detail::enable_if_t < (std::is_same<IterImpl, iter_impl>::value || std::is_same<IterImpl, other_iter_impl>::value), std::nullptr_t > = nullptr >
|
|
|
|
bool operator==(const IterImpl& other) const
|
2017-08-14 17:23:15 +02:00
|
|
|
{
|
|
|
|
// if objects are not the same, the comparison is undefined
|
2019-07-01 22:37:30 +02:00
|
|
|
if (JSON_HEDLEY_UNLIKELY(m_object != other.m_object))
|
2017-08-14 17:23:15 +02:00
|
|
|
{
|
2021-01-25 13:47:50 +01:00
|
|
|
JSON_THROW(invalid_iterator::create(212, "cannot compare iterators of different containers", *m_object));
|
2017-08-14 17:23:15 +02:00
|
|
|
}
|
|
|
|
|
2020-07-06 12:22:31 +02:00
|
|
|
JSON_ASSERT(m_object != nullptr);
|
2017-08-14 17:23:15 +02:00
|
|
|
|
|
|
|
switch (m_object->m_type)
|
|
|
|
{
|
|
|
|
case value_t::object:
|
|
|
|
return (m_it.object_iterator == other.m_it.object_iterator);
|
|
|
|
|
|
|
|
case value_t::array:
|
|
|
|
return (m_it.array_iterator == other.m_it.array_iterator);
|
|
|
|
|
|
|
|
default:
|
|
|
|
return (m_it.primitive_iterator == other.m_it.primitive_iterator);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
2020-12-29 21:36:30 +01:00
|
|
|
@brief comparison: not equal
|
2017-08-14 17:23:15 +02:00
|
|
|
@pre The iterator is initialized; i.e. `m_object != nullptr`.
|
|
|
|
*/
|
2020-12-29 21:36:30 +01:00
|
|
|
template < typename IterImpl, detail::enable_if_t < (std::is_same<IterImpl, iter_impl>::value || std::is_same<IterImpl, other_iter_impl>::value), std::nullptr_t > = nullptr >
|
|
|
|
bool operator!=(const IterImpl& other) const
|
2017-08-14 17:23:15 +02:00
|
|
|
{
|
2020-06-03 14:20:36 +02:00
|
|
|
return !operator==(other);
|
2017-08-14 17:23:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
2020-12-29 21:36:30 +01:00
|
|
|
@brief comparison: smaller
|
2017-08-14 17:23:15 +02:00
|
|
|
@pre The iterator is initialized; i.e. `m_object != nullptr`.
|
|
|
|
*/
|
|
|
|
bool operator<(const iter_impl& other) const
|
|
|
|
{
|
|
|
|
// if objects are not the same, the comparison is undefined
|
2019-07-01 22:37:30 +02:00
|
|
|
if (JSON_HEDLEY_UNLIKELY(m_object != other.m_object))
|
2017-08-14 17:23:15 +02:00
|
|
|
{
|
2021-01-25 13:47:50 +01:00
|
|
|
JSON_THROW(invalid_iterator::create(212, "cannot compare iterators of different containers", *m_object));
|
2017-08-14 17:23:15 +02:00
|
|
|
}
|
|
|
|
|
2020-07-06 12:22:31 +02:00
|
|
|
JSON_ASSERT(m_object != nullptr);
|
2017-08-14 17:23:15 +02:00
|
|
|
|
|
|
|
switch (m_object->m_type)
|
|
|
|
{
|
|
|
|
case value_t::object:
|
2021-01-25 13:47:50 +01:00
|
|
|
JSON_THROW(invalid_iterator::create(213, "cannot compare order of object iterators", *m_object));
|
2017-08-14 17:23:15 +02:00
|
|
|
|
|
|
|
case value_t::array:
|
|
|
|
return (m_it.array_iterator < other.m_it.array_iterator);
|
|
|
|
|
|
|
|
default:
|
|
|
|
return (m_it.primitive_iterator < other.m_it.primitive_iterator);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
2020-12-29 21:36:30 +01:00
|
|
|
@brief comparison: less than or equal
|
2017-08-14 17:23:15 +02:00
|
|
|
@pre The iterator is initialized; i.e. `m_object != nullptr`.
|
|
|
|
*/
|
|
|
|
bool operator<=(const iter_impl& other) const
|
|
|
|
{
|
2020-06-03 14:20:36 +02:00
|
|
|
return !other.operator < (*this);
|
2017-08-14 17:23:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
2020-12-29 21:36:30 +01:00
|
|
|
@brief comparison: greater than
|
2017-08-14 17:23:15 +02:00
|
|
|
@pre The iterator is initialized; i.e. `m_object != nullptr`.
|
|
|
|
*/
|
|
|
|
bool operator>(const iter_impl& other) const
|
|
|
|
{
|
2020-06-03 14:20:36 +02:00
|
|
|
return !operator<=(other);
|
2017-08-14 17:23:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
2020-12-29 21:36:30 +01:00
|
|
|
@brief comparison: greater than or equal
|
2017-08-14 17:23:15 +02:00
|
|
|
@pre The iterator is initialized; i.e. `m_object != nullptr`.
|
|
|
|
*/
|
|
|
|
bool operator>=(const iter_impl& other) const
|
|
|
|
{
|
2020-06-03 14:20:36 +02:00
|
|
|
return !operator<(other);
|
2017-08-14 17:23:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
2020-12-29 21:36:30 +01:00
|
|
|
@brief add to iterator
|
2017-08-14 17:23:15 +02:00
|
|
|
@pre The iterator is initialized; i.e. `m_object != nullptr`.
|
|
|
|
*/
|
|
|
|
iter_impl& operator+=(difference_type i)
|
|
|
|
{
|
2020-07-06 12:22:31 +02:00
|
|
|
JSON_ASSERT(m_object != nullptr);
|
2017-08-14 17:23:15 +02:00
|
|
|
|
|
|
|
switch (m_object->m_type)
|
|
|
|
{
|
|
|
|
case value_t::object:
|
2021-01-25 13:47:50 +01:00
|
|
|
JSON_THROW(invalid_iterator::create(209, "cannot use offsets with object iterators", *m_object));
|
2017-08-14 17:23:15 +02:00
|
|
|
|
|
|
|
case value_t::array:
|
|
|
|
{
|
|
|
|
std::advance(m_it.array_iterator, i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
m_it.primitive_iterator += i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
2020-12-29 21:36:30 +01:00
|
|
|
@brief subtract from iterator
|
2017-08-14 17:23:15 +02:00
|
|
|
@pre The iterator is initialized; i.e. `m_object != nullptr`.
|
|
|
|
*/
|
|
|
|
iter_impl& operator-=(difference_type i)
|
|
|
|
{
|
|
|
|
return operator+=(-i);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
2020-12-29 21:36:30 +01:00
|
|
|
@brief add to iterator
|
2017-08-14 17:23:15 +02:00
|
|
|
@pre The iterator is initialized; i.e. `m_object != nullptr`.
|
|
|
|
*/
|
|
|
|
iter_impl operator+(difference_type i) const
|
|
|
|
{
|
|
|
|
auto result = *this;
|
|
|
|
result += i;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
2020-12-29 21:36:30 +01:00
|
|
|
@brief addition of distance and iterator
|
2017-08-14 17:23:15 +02:00
|
|
|
@pre The iterator is initialized; i.e. `m_object != nullptr`.
|
|
|
|
*/
|
|
|
|
friend iter_impl operator+(difference_type i, const iter_impl& it)
|
|
|
|
{
|
|
|
|
auto result = it;
|
|
|
|
result += i;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
2020-12-29 21:36:30 +01:00
|
|
|
@brief subtract from iterator
|
2017-08-14 17:23:15 +02:00
|
|
|
@pre The iterator is initialized; i.e. `m_object != nullptr`.
|
|
|
|
*/
|
|
|
|
iter_impl operator-(difference_type i) const
|
|
|
|
{
|
|
|
|
auto result = *this;
|
|
|
|
result -= i;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
2020-12-29 21:36:30 +01:00
|
|
|
@brief return difference
|
2017-08-14 17:23:15 +02:00
|
|
|
@pre The iterator is initialized; i.e. `m_object != nullptr`.
|
|
|
|
*/
|
|
|
|
difference_type operator-(const iter_impl& other) const
|
|
|
|
{
|
2020-07-06 12:22:31 +02:00
|
|
|
JSON_ASSERT(m_object != nullptr);
|
2017-08-14 17:23:15 +02:00
|
|
|
|
|
|
|
switch (m_object->m_type)
|
|
|
|
{
|
|
|
|
case value_t::object:
|
2021-01-25 13:47:50 +01:00
|
|
|
JSON_THROW(invalid_iterator::create(209, "cannot use offsets with object iterators", *m_object));
|
2017-08-14 17:23:15 +02:00
|
|
|
|
|
|
|
case value_t::array:
|
|
|
|
return m_it.array_iterator - other.m_it.array_iterator;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return m_it.primitive_iterator - other.m_it.primitive_iterator;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
2020-12-29 21:36:30 +01:00
|
|
|
@brief access to successor
|
2017-08-14 17:23:15 +02:00
|
|
|
@pre The iterator is initialized; i.e. `m_object != nullptr`.
|
|
|
|
*/
|
|
|
|
reference operator[](difference_type n) const
|
|
|
|
{
|
2020-07-06 12:22:31 +02:00
|
|
|
JSON_ASSERT(m_object != nullptr);
|
2017-08-14 17:23:15 +02:00
|
|
|
|
|
|
|
switch (m_object->m_type)
|
|
|
|
{
|
|
|
|
case value_t::object:
|
2021-01-25 13:47:50 +01:00
|
|
|
JSON_THROW(invalid_iterator::create(208, "cannot use operator[] for object iterators", *m_object));
|
2017-08-14 17:23:15 +02:00
|
|
|
|
|
|
|
case value_t::array:
|
|
|
|
return *std::next(m_it.array_iterator, n);
|
|
|
|
|
|
|
|
case value_t::null:
|
2021-01-25 13:47:50 +01:00
|
|
|
JSON_THROW(invalid_iterator::create(214, "cannot get value", *m_object));
|
2017-08-14 17:23:15 +02:00
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
2019-07-01 22:37:30 +02:00
|
|
|
if (JSON_HEDLEY_LIKELY(m_it.primitive_iterator.get_value() == -n))
|
2017-08-14 17:23:15 +02:00
|
|
|
{
|
|
|
|
return *m_object;
|
|
|
|
}
|
|
|
|
|
2021-01-25 13:47:50 +01:00
|
|
|
JSON_THROW(invalid_iterator::create(214, "cannot get value", *m_object));
|
2017-08-14 17:23:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
2020-12-29 21:36:30 +01:00
|
|
|
@brief return the key of an object iterator
|
2017-08-14 17:23:15 +02:00
|
|
|
@pre The iterator is initialized; i.e. `m_object != nullptr`.
|
|
|
|
*/
|
2018-05-26 13:26:40 +02:00
|
|
|
const typename object_t::key_type& key() const
|
2017-08-14 17:23:15 +02:00
|
|
|
{
|
2020-07-06 12:22:31 +02:00
|
|
|
JSON_ASSERT(m_object != nullptr);
|
2017-08-14 17:23:15 +02:00
|
|
|
|
2019-07-01 22:37:30 +02:00
|
|
|
if (JSON_HEDLEY_LIKELY(m_object->is_object()))
|
2017-08-14 17:23:15 +02:00
|
|
|
{
|
|
|
|
return m_it.object_iterator->first;
|
|
|
|
}
|
|
|
|
|
2021-01-25 13:47:50 +01:00
|
|
|
JSON_THROW(invalid_iterator::create(207, "cannot use key() for non-object iterators", *m_object));
|
2017-08-14 17:23:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
2020-12-29 21:36:30 +01:00
|
|
|
@brief return the value of an iterator
|
2017-08-14 17:23:15 +02:00
|
|
|
@pre The iterator is initialized; i.e. `m_object != nullptr`.
|
|
|
|
*/
|
|
|
|
reference value() const
|
|
|
|
{
|
|
|
|
return operator*();
|
|
|
|
}
|
|
|
|
|
2020-08-12 13:30:06 +02:00
|
|
|
JSON_PRIVATE_UNLESS_TESTED:
|
2017-08-14 17:23:15 +02:00
|
|
|
/// associated JSON instance
|
|
|
|
pointer m_object = nullptr;
|
|
|
|
/// the actual iterator of the associated instance
|
2019-03-17 12:01:49 +01:00
|
|
|
internal_iterator<typename std::remove_const<BasicJsonType>::type> m_it {};
|
2017-08-14 17:23:15 +02:00
|
|
|
};
|
2019-06-30 22:14:02 +02:00
|
|
|
} // namespace detail
|
2019-03-17 12:01:49 +01:00
|
|
|
} // namespace nlohmann
|