mirror of
https://github.com/nlohmann/json
synced 2024-12-02 16:30:11 +00:00
move json_reverse_iterator outside of basic_json
This commit is contained in:
parent
0ba98b6fe0
commit
bb752fd30c
215
src/json.hpp
215
src/json.hpp
@ -4459,6 +4459,111 @@ template <typename IteratorType> class iteration_proxy
|
||||
return iteration_proxy_internal(container.end());
|
||||
}
|
||||
};
|
||||
|
||||
/*!
|
||||
@brief a template for a reverse iterator class
|
||||
|
||||
@tparam Base the base iterator type to reverse. Valid types are @ref
|
||||
iterator (to create @ref reverse_iterator) and @ref const_iterator (to
|
||||
create @ref const_reverse_iterator).
|
||||
|
||||
@requirement The class satisfies the following concept requirements:
|
||||
-
|
||||
[RandomAccessIterator](http://en.cppreference.com/w/cpp/concept/RandomAccessIterator):
|
||||
The iterator that can be moved to point (forward and backward) to any
|
||||
element in constant time.
|
||||
- [OutputIterator](http://en.cppreference.com/w/cpp/concept/OutputIterator):
|
||||
It is possible to write to the pointed-to element (only if @a Base is
|
||||
@ref iterator).
|
||||
|
||||
@since version 1.0.0
|
||||
*/
|
||||
template <typename Base>
|
||||
class json_reverse_iterator : public std::reverse_iterator<Base>
|
||||
{
|
||||
public:
|
||||
using difference_type = std::ptrdiff_t;
|
||||
/// shortcut to the reverse iterator adaptor
|
||||
using base_iterator = std::reverse_iterator<Base>;
|
||||
/// the reference type for the pointed-to element
|
||||
using reference = typename Base::reference;
|
||||
|
||||
/// create reverse iterator from iterator
|
||||
json_reverse_iterator(
|
||||
const typename base_iterator::iterator_type& it) noexcept
|
||||
: base_iterator(it) {}
|
||||
|
||||
/// create reverse iterator from base class
|
||||
json_reverse_iterator(const base_iterator& it) noexcept : base_iterator(it) {}
|
||||
|
||||
/// post-increment (it++)
|
||||
json_reverse_iterator operator++(int)
|
||||
{
|
||||
return static_cast<json_reverse_iterator>(base_iterator::operator++(1));
|
||||
}
|
||||
|
||||
/// pre-increment (++it)
|
||||
json_reverse_iterator& operator++()
|
||||
{
|
||||
return static_cast<json_reverse_iterator&>(base_iterator::operator++());
|
||||
}
|
||||
|
||||
/// post-decrement (it--)
|
||||
json_reverse_iterator operator--(int)
|
||||
{
|
||||
return static_cast<json_reverse_iterator>(base_iterator::operator--(1));
|
||||
}
|
||||
|
||||
/// pre-decrement (--it)
|
||||
json_reverse_iterator& operator--()
|
||||
{
|
||||
return static_cast<json_reverse_iterator&>(base_iterator::operator--());
|
||||
}
|
||||
|
||||
/// add to iterator
|
||||
json_reverse_iterator& operator+=(difference_type i)
|
||||
{
|
||||
return static_cast<json_reverse_iterator&>(base_iterator::operator+=(i));
|
||||
}
|
||||
|
||||
/// add to iterator
|
||||
json_reverse_iterator operator+(difference_type i) const
|
||||
{
|
||||
return static_cast<json_reverse_iterator>(base_iterator::operator+(i));
|
||||
}
|
||||
|
||||
/// subtract from iterator
|
||||
json_reverse_iterator operator-(difference_type i) const
|
||||
{
|
||||
return static_cast<json_reverse_iterator>(base_iterator::operator-(i));
|
||||
}
|
||||
|
||||
/// return difference
|
||||
difference_type operator-(const json_reverse_iterator& other) const
|
||||
{
|
||||
return base_iterator(*this) - base_iterator(other);
|
||||
}
|
||||
|
||||
/// access to successor
|
||||
reference operator[](difference_type n) const
|
||||
{
|
||||
return *(this->operator+(n));
|
||||
}
|
||||
|
||||
/// return the key of an object iterator
|
||||
auto key() const -> decltype(std::declval<Base>().key())
|
||||
{
|
||||
auto it = --this->base();
|
||||
return it.key();
|
||||
}
|
||||
|
||||
/// return the value of an iterator
|
||||
reference value() const
|
||||
{
|
||||
auto it = --this->base();
|
||||
return it.operator * ();
|
||||
}
|
||||
};
|
||||
} // namespace detail
|
||||
|
||||
/// namespace to hold default `to_json` / `from_json` functions
|
||||
@ -4946,11 +5051,11 @@ class basic_json
|
||||
using iter_impl = ::nlohmann::detail::iter_impl<BasicJsonType>;
|
||||
template <typename Iterator>
|
||||
using iteration_proxy = ::nlohmann::detail::iteration_proxy<Iterator>;
|
||||
template<typename Base> using json_reverse_iterator = ::nlohmann::detail::json_reverse_iterator<Base>;
|
||||
|
||||
public:
|
||||
using value_t = detail::value_t;
|
||||
// forward declarations
|
||||
template<typename Base> class json_reverse_iterator;
|
||||
using json_pointer = ::nlohmann::json_pointer;
|
||||
template<typename T, typename SFINAE>
|
||||
using json_serializer = JSONSerializer<T, SFINAE>;
|
||||
@ -11298,114 +11403,6 @@ class basic_json
|
||||
/// the value of the current element
|
||||
json_value m_value = {};
|
||||
|
||||
public:
|
||||
/*!
|
||||
@brief a template for a reverse iterator class
|
||||
|
||||
@tparam Base the base iterator type to reverse. Valid types are @ref
|
||||
iterator (to create @ref reverse_iterator) and @ref const_iterator (to
|
||||
create @ref const_reverse_iterator).
|
||||
|
||||
@requirement The class satisfies the following concept requirements:
|
||||
- [RandomAccessIterator](http://en.cppreference.com/w/cpp/concept/RandomAccessIterator):
|
||||
The iterator that can be moved to point (forward and backward) to any
|
||||
element in constant time.
|
||||
- [OutputIterator](http://en.cppreference.com/w/cpp/concept/OutputIterator):
|
||||
It is possible to write to the pointed-to element (only if @a Base is
|
||||
@ref iterator).
|
||||
|
||||
@since version 1.0.0
|
||||
*/
|
||||
template<typename Base>
|
||||
class json_reverse_iterator : public std::reverse_iterator<Base>
|
||||
{
|
||||
public:
|
||||
/// shortcut to the reverse iterator adaptor
|
||||
using base_iterator = std::reverse_iterator<Base>;
|
||||
/// the reference type for the pointed-to element
|
||||
using reference = typename Base::reference;
|
||||
|
||||
/// create reverse iterator from iterator
|
||||
json_reverse_iterator(const typename base_iterator::iterator_type& it) noexcept
|
||||
: base_iterator(it)
|
||||
{}
|
||||
|
||||
/// create reverse iterator from base class
|
||||
json_reverse_iterator(const base_iterator& it) noexcept
|
||||
: base_iterator(it)
|
||||
{}
|
||||
|
||||
/// post-increment (it++)
|
||||
json_reverse_iterator operator++(int)
|
||||
{
|
||||
return static_cast<json_reverse_iterator>(base_iterator::operator++(1));
|
||||
}
|
||||
|
||||
/// pre-increment (++it)
|
||||
json_reverse_iterator& operator++()
|
||||
{
|
||||
return static_cast<json_reverse_iterator&>(base_iterator::operator++());
|
||||
}
|
||||
|
||||
/// post-decrement (it--)
|
||||
json_reverse_iterator operator--(int)
|
||||
{
|
||||
return static_cast<json_reverse_iterator>(base_iterator::operator--(1));
|
||||
}
|
||||
|
||||
/// pre-decrement (--it)
|
||||
json_reverse_iterator& operator--()
|
||||
{
|
||||
return static_cast<json_reverse_iterator&>(base_iterator::operator--());
|
||||
}
|
||||
|
||||
/// add to iterator
|
||||
json_reverse_iterator& operator+=(difference_type i)
|
||||
{
|
||||
return static_cast<json_reverse_iterator&>(base_iterator::operator+=(i));
|
||||
}
|
||||
|
||||
/// add to iterator
|
||||
json_reverse_iterator operator+(difference_type i) const
|
||||
{
|
||||
return static_cast<json_reverse_iterator>(base_iterator::operator+(i));
|
||||
}
|
||||
|
||||
/// subtract from iterator
|
||||
json_reverse_iterator operator-(difference_type i) const
|
||||
{
|
||||
return static_cast<json_reverse_iterator>(base_iterator::operator-(i));
|
||||
}
|
||||
|
||||
/// return difference
|
||||
difference_type operator-(const json_reverse_iterator& other) const
|
||||
{
|
||||
return base_iterator(*this) - base_iterator(other);
|
||||
}
|
||||
|
||||
/// access to successor
|
||||
reference operator[](difference_type n) const
|
||||
{
|
||||
return *(this->operator+(n));
|
||||
}
|
||||
|
||||
/// return the key of an object iterator
|
||||
typename object_t::key_type key() const
|
||||
{
|
||||
auto it = --this->base();
|
||||
return it.key();
|
||||
}
|
||||
|
||||
/// return the value of an iterator
|
||||
reference value() const
|
||||
{
|
||||
auto it = --this->base();
|
||||
return it.operator * ();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
private:
|
||||
//////////////////////////////////////////
|
||||
// binary serialization/deserialization //
|
||||
//////////////////////////////////////////
|
||||
|
Loading…
Reference in New Issue
Block a user