2018-01-10 09:18:31 +00:00
|
|
|
#pragma once
|
2017-08-14 15:29:55 +00:00
|
|
|
|
2017-08-14 17:28:01 +00:00
|
|
|
#include <cstddef> // ptrdiff_t
|
|
|
|
#include <iterator> // reverse_iterator
|
|
|
|
#include <utility> // declval
|
2017-08-14 15:29:55 +00:00
|
|
|
|
|
|
|
namespace nlohmann
|
|
|
|
{
|
|
|
|
namespace detail
|
|
|
|
{
|
|
|
|
//////////////////////
|
|
|
|
// reverse_iterator //
|
|
|
|
//////////////////////
|
|
|
|
|
|
|
|
/*!
|
|
|
|
@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:
|
|
|
|
-
|
2018-06-24 20:30:37 +00:00
|
|
|
[BidirectionalIterator](https://en.cppreference.com/w/cpp/named_req/BidirectionalIterator):
|
2017-08-14 15:29:55 +00:00
|
|
|
The iterator that can be moved can be moved in both directions (i.e.
|
|
|
|
incremented and decremented).
|
2018-06-24 20:30:37 +00:00
|
|
|
- [OutputIterator](https://en.cppreference.com/w/cpp/named_req/OutputIterator):
|
2017-08-14 15:29:55 +00:00
|
|
|
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 adapter
|
|
|
|
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
|
2018-06-23 09:00:42 +00:00
|
|
|
explicit json_reverse_iterator(const typename base_iterator::iterator_type& it) noexcept
|
2017-08-14 15:29:55 +00:00
|
|
|
: base_iterator(it) {}
|
|
|
|
|
|
|
|
/// create reverse iterator from base class
|
2018-06-23 09:00:42 +00:00
|
|
|
explicit json_reverse_iterator(const base_iterator& it) noexcept : base_iterator(it) {}
|
2017-08-14 15:29:55 +00:00
|
|
|
|
|
|
|
/// post-increment (it++)
|
|
|
|
json_reverse_iterator const 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 const 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 * ();
|
|
|
|
}
|
|
|
|
};
|
2018-10-07 16:39:18 +00:00
|
|
|
} // namespace detail
|
|
|
|
} // namespace nlohmann
|