Skip to content

Commit

Permalink
move json_reverse_iterator outside of basic_json
Browse files Browse the repository at this point in the history
  • Loading branch information
theodelrieu committed Jul 9, 2017
1 parent af464b3 commit db73ded
Showing 1 changed file with 106 additions and 109 deletions.
215 changes: 106 additions & 109 deletions src/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4456,6 +4456,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
Expand Down Expand Up @@ -4942,11 +5047,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>;
Expand Down Expand Up @@ -11294,114 +11399,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 //
//////////////////////////////////////////
Expand Down

0 comments on commit db73ded

Please sign in to comment.