🔀 merge pull request #395 from Bosswestfalen/develop

Replace class iterator and const_iterator by using a single template class to reduce code.
This commit is contained in:
Niels Lohmann 2016-12-14 21:56:43 +01:00 committed by GitHub
commit fe00b368ae
2 changed files with 106 additions and 378 deletions

View file

@ -261,10 +261,12 @@ class basic_json
/// the type of an element const pointer /// the type of an element const pointer
using const_pointer = typename std::allocator_traits<allocator_type>::const_pointer; using const_pointer = typename std::allocator_traits<allocator_type>::const_pointer;
// forward declaration for iterators
template <typename U> class iter_impl;
/// an iterator for a basic_json container /// an iterator for a basic_json container
class iterator; using iterator = iter_impl<basic_json>;
/// a const iterator for a basic_json container /// a const iterator for a basic_json container
class const_iterator; using const_iterator = iter_impl<const basic_json>;
/// a reverse iterator for a basic_json container /// a reverse iterator for a basic_json container
using reverse_iterator = json_reverse_iterator<typename basic_json::iterator>; using reverse_iterator = json_reverse_iterator<typename basic_json::iterator>;
/// a const reverse iterator for a basic_json container /// a const reverse iterator for a basic_json container
@ -8204,10 +8206,10 @@ class basic_json
public: public:
/*! /*!
@brief a const random access iterator for the @ref basic_json class @brief a template for a random access iterator for the @ref basic_json class
This class implements a const iterator for the @ref basic_json class. From This class implements a both iterators (iterator and const_iterator)
this class, the @ref iterator class is derived. for the @ref basic_json class.
@note An iterator is called *initialized* when a pointer to a JSON value @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 has been set (e.g., by a constructor or a copy assignment). If the
@ -8222,25 +8224,35 @@ class basic_json
@since version 1.0.0 @since version 1.0.0
*/ */
class const_iterator : public std::iterator<std::random_access_iterator_tag, const basic_json> template <typename U>
class iter_impl : public std::iterator<std::random_access_iterator_tag, U>
{ {
/// allow basic_json to access private members /// allow basic_json to access private members
friend class basic_json; friend class basic_json;
// make sure U is basic_json or const basic_json
static_assert(std::is_same<U, basic_json>::value
or std::is_same<U, const basic_json>::value,
"iter_impl only accepts (const) basic_json");
public: public:
/// the type of the values when the iterator is dereferenced /// the type of the values when the iterator is dereferenced
using value_type = typename basic_json::value_type; using value_type = typename basic_json::value_type;
/// a type to represent differences between iterators /// a type to represent differences between iterators
using difference_type = typename basic_json::difference_type; using difference_type = typename basic_json::difference_type;
/// defines a pointer to the type iterated over (value_type) /// defines a pointer to the type iterated over (value_type)
using pointer = typename basic_json::const_pointer; using pointer = typename std::conditional<std::is_const<U>::value,
typename basic_json::const_pointer,
typename basic_json::pointer>::type;
/// defines a reference to the type iterated over (value_type) /// defines a reference to the type iterated over (value_type)
using reference = typename basic_json::const_reference; using reference = typename std::conditional<std::is_const<U>::value,
typename basic_json::const_reference,
typename basic_json::reference>::type;
/// the category of the iterator /// the category of the iterator
using iterator_category = std::bidirectional_iterator_tag; using iterator_category = std::bidirectional_iterator_tag;
/// default constructor /// default constructor
const_iterator() = default; iter_impl() = default;
/*! /*!
@brief constructor for a given JSON instance @brief constructor for a given JSON instance
@ -8248,7 +8260,7 @@ class basic_json
@pre object != nullptr @pre object != nullptr
@post The iterator is initialized; i.e. `m_object != nullptr`. @post The iterator is initialized; i.e. `m_object != nullptr`.
*/ */
explicit const_iterator(pointer object) noexcept explicit iter_impl(pointer object) noexcept
: m_object(object) : m_object(object)
{ {
assert(m_object != nullptr); assert(m_object != nullptr);
@ -8275,37 +8287,25 @@ class basic_json
} }
} }
/*! /*
@brief copy constructor given a non-const iterator Use operator const_iterator instead of
@param[in] other iterator to copy from const_iterator(const iterator& other) noexcept
@note It is not checked whether @a other is initialized. to avoid two class definitions for iterator and const_iterator.
This function is only called if this class is an iterator.
If this class is a const_iterator this function is not called.
*/ */
explicit const_iterator(const iterator& other) noexcept operator const_iterator() const
: m_object(other.m_object) {
{ const_iterator ret;
if (m_object != nullptr)
{
switch (m_object->m_type)
{
case basic_json::value_t::object:
{
m_it.object_iterator = other.m_it.object_iterator;
break;
}
case basic_json::value_t::array: if (m_object)
{ {
m_it.array_iterator = other.m_it.array_iterator; ret.m_object = m_object;
break; ret.m_it = m_it;
}
default:
{
m_it.primitive_iterator = other.m_it.primitive_iterator;
break;
}
}
} }
return ret;
} }
/*! /*!
@ -8313,7 +8313,7 @@ class basic_json
@param[in] other iterator to copy from @param[in] other iterator to copy from
@note It is not checked whether @a other is initialized. @note It is not checked whether @a other is initialized.
*/ */
const_iterator(const const_iterator& other) noexcept iter_impl(const iter_impl& other) noexcept
: m_object(other.m_object), m_it(other.m_it) : m_object(other.m_object), m_it(other.m_it)
{} {}
@ -8322,7 +8322,7 @@ class basic_json
@param[in,out] other iterator to copy from @param[in,out] other iterator to copy from
@note It is not checked whether @a other is initialized. @note It is not checked whether @a other is initialized.
*/ */
const_iterator& operator=(const_iterator other) noexcept( iter_impl& operator=(iter_impl other) noexcept(
std::is_nothrow_move_constructible<pointer>::value and std::is_nothrow_move_constructible<pointer>::value and
std::is_nothrow_move_assignable<pointer>::value and std::is_nothrow_move_assignable<pointer>::value and
std::is_nothrow_move_constructible<internal_iterator>::value and std::is_nothrow_move_constructible<internal_iterator>::value and
@ -8484,7 +8484,7 @@ class basic_json
@brief post-increment (it++) @brief post-increment (it++)
@pre The iterator is initialized; i.e. `m_object != nullptr`. @pre The iterator is initialized; i.e. `m_object != nullptr`.
*/ */
const_iterator operator++(int) iter_impl operator++(int)
{ {
auto result = *this; auto result = *this;
++(*this); ++(*this);
@ -8495,7 +8495,7 @@ class basic_json
@brief pre-increment (++it) @brief pre-increment (++it)
@pre The iterator is initialized; i.e. `m_object != nullptr`. @pre The iterator is initialized; i.e. `m_object != nullptr`.
*/ */
const_iterator& operator++() iter_impl& operator++()
{ {
assert(m_object != nullptr); assert(m_object != nullptr);
@ -8527,7 +8527,7 @@ class basic_json
@brief post-decrement (it--) @brief post-decrement (it--)
@pre The iterator is initialized; i.e. `m_object != nullptr`. @pre The iterator is initialized; i.e. `m_object != nullptr`.
*/ */
const_iterator operator--(int) iter_impl operator--(int)
{ {
auto result = *this; auto result = *this;
--(*this); --(*this);
@ -8538,7 +8538,7 @@ class basic_json
@brief pre-decrement (--it) @brief pre-decrement (--it)
@pre The iterator is initialized; i.e. `m_object != nullptr`. @pre The iterator is initialized; i.e. `m_object != nullptr`.
*/ */
const_iterator& operator--() iter_impl& operator--()
{ {
assert(m_object != nullptr); assert(m_object != nullptr);
@ -8570,7 +8570,7 @@ class basic_json
@brief comparison: equal @brief comparison: equal
@pre The iterator is initialized; i.e. `m_object != nullptr`. @pre The iterator is initialized; i.e. `m_object != nullptr`.
*/ */
bool operator==(const const_iterator& other) const bool operator==(const iter_impl& other) const
{ {
// if objects are not the same, the comparison is undefined // if objects are not the same, the comparison is undefined
if (m_object != other.m_object) if (m_object != other.m_object)
@ -8603,7 +8603,7 @@ class basic_json
@brief comparison: not equal @brief comparison: not equal
@pre The iterator is initialized; i.e. `m_object != nullptr`. @pre The iterator is initialized; i.e. `m_object != nullptr`.
*/ */
bool operator!=(const const_iterator& other) const bool operator!=(const iter_impl& other) const
{ {
return not operator==(other); return not operator==(other);
} }
@ -8612,7 +8612,7 @@ class basic_json
@brief comparison: smaller @brief comparison: smaller
@pre The iterator is initialized; i.e. `m_object != nullptr`. @pre The iterator is initialized; i.e. `m_object != nullptr`.
*/ */
bool operator<(const const_iterator& other) const bool operator<(const iter_impl& other) const
{ {
// if objects are not the same, the comparison is undefined // if objects are not the same, the comparison is undefined
if (m_object != other.m_object) if (m_object != other.m_object)
@ -8645,7 +8645,7 @@ class basic_json
@brief comparison: less than or equal @brief comparison: less than or equal
@pre The iterator is initialized; i.e. `m_object != nullptr`. @pre The iterator is initialized; i.e. `m_object != nullptr`.
*/ */
bool operator<=(const const_iterator& other) const bool operator<=(const iter_impl& other) const
{ {
return not other.operator < (*this); return not other.operator < (*this);
} }
@ -8654,7 +8654,7 @@ class basic_json
@brief comparison: greater than @brief comparison: greater than
@pre The iterator is initialized; i.e. `m_object != nullptr`. @pre The iterator is initialized; i.e. `m_object != nullptr`.
*/ */
bool operator>(const const_iterator& other) const bool operator>(const iter_impl& other) const
{ {
return not operator<=(other); return not operator<=(other);
} }
@ -8663,7 +8663,7 @@ class basic_json
@brief comparison: greater than or equal @brief comparison: greater than or equal
@pre The iterator is initialized; i.e. `m_object != nullptr`. @pre The iterator is initialized; i.e. `m_object != nullptr`.
*/ */
bool operator>=(const const_iterator& other) const bool operator>=(const iter_impl& other) const
{ {
return not operator<(other); return not operator<(other);
} }
@ -8672,7 +8672,7 @@ class basic_json
@brief add to iterator @brief add to iterator
@pre The iterator is initialized; i.e. `m_object != nullptr`. @pre The iterator is initialized; i.e. `m_object != nullptr`.
*/ */
const_iterator& operator+=(difference_type i) iter_impl& operator+=(difference_type i)
{ {
assert(m_object != nullptr); assert(m_object != nullptr);
@ -8703,7 +8703,7 @@ class basic_json
@brief subtract from iterator @brief subtract from iterator
@pre The iterator is initialized; i.e. `m_object != nullptr`. @pre The iterator is initialized; i.e. `m_object != nullptr`.
*/ */
const_iterator& operator-=(difference_type i) iter_impl& operator-=(difference_type i)
{ {
return operator+=(-i); return operator+=(-i);
} }
@ -8712,7 +8712,7 @@ class basic_json
@brief add to iterator @brief add to iterator
@pre The iterator is initialized; i.e. `m_object != nullptr`. @pre The iterator is initialized; i.e. `m_object != nullptr`.
*/ */
const_iterator operator+(difference_type i) iter_impl operator+(difference_type i)
{ {
auto result = *this; auto result = *this;
result += i; result += i;
@ -8723,7 +8723,7 @@ class basic_json
@brief subtract from iterator @brief subtract from iterator
@pre The iterator is initialized; i.e. `m_object != nullptr`. @pre The iterator is initialized; i.e. `m_object != nullptr`.
*/ */
const_iterator operator-(difference_type i) iter_impl operator-(difference_type i)
{ {
auto result = *this; auto result = *this;
result -= i; result -= i;
@ -8734,7 +8734,7 @@ class basic_json
@brief return difference @brief return difference
@pre The iterator is initialized; i.e. `m_object != nullptr`. @pre The iterator is initialized; i.e. `m_object != nullptr`.
*/ */
difference_type operator-(const const_iterator& other) const difference_type operator-(const iter_impl& other) const
{ {
assert(m_object != nullptr); assert(m_object != nullptr);
@ -8830,141 +8830,6 @@ class basic_json
internal_iterator m_it = internal_iterator(); internal_iterator m_it = internal_iterator();
}; };
/*!
@brief a mutable random access iterator for the @ref basic_json class
@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.
@since version 1.0.0
*/
class iterator : public const_iterator
{
public:
using base_iterator = const_iterator;
using pointer = typename basic_json::pointer;
using reference = typename basic_json::reference;
/// default constructor
iterator() = default;
/// constructor for a given JSON instance
explicit iterator(pointer object) noexcept
: base_iterator(object)
{}
/// copy constructor
iterator(const iterator& other) noexcept
: base_iterator(other)
{}
/// copy assignment
iterator& operator=(iterator other) noexcept(
std::is_nothrow_move_constructible<pointer>::value and
std::is_nothrow_move_assignable<pointer>::value and
std::is_nothrow_move_constructible<internal_iterator>::value and
std::is_nothrow_move_assignable<internal_iterator>::value
)
{
base_iterator::operator=(other);
return *this;
}
/// return a reference to the value pointed to by the iterator
reference operator*() const
{
return const_cast<reference>(base_iterator::operator*());
}
/// dereference the iterator
pointer operator->() const
{
return const_cast<pointer>(base_iterator::operator->());
}
/// post-increment (it++)
iterator operator++(int)
{
iterator result = *this;
base_iterator::operator++();
return result;
}
/// pre-increment (++it)
iterator& operator++()
{
base_iterator::operator++();
return *this;
}
/// post-decrement (it--)
iterator operator--(int)
{
iterator result = *this;
base_iterator::operator--();
return result;
}
/// pre-decrement (--it)
iterator& operator--()
{
base_iterator::operator--();
return *this;
}
/// add to iterator
iterator& operator+=(difference_type i)
{
base_iterator::operator+=(i);
return *this;
}
/// subtract from iterator
iterator& operator-=(difference_type i)
{
base_iterator::operator-=(i);
return *this;
}
/// add to iterator
iterator operator+(difference_type i)
{
auto result = *this;
result += i;
return result;
}
/// subtract from iterator
iterator operator-(difference_type i)
{
auto result = *this;
result -= i;
return result;
}
/// return difference
difference_type operator-(const iterator& other) const
{
return base_iterator::operator-(other);
}
/// access to successor
reference operator[](difference_type n) const
{
return const_cast<reference>(base_iterator::operator[](n));
}
/// return the value of an iterator
reference value() const
{
return const_cast<reference>(base_iterator::value());
}
};
/*! /*!
@brief a template for a reverse iterator class @brief a template for a reverse iterator class

View file

@ -8204,10 +8204,10 @@ class basic_json
public: public:
/*! /*!
@brief a const random access iterator for the @ref basic_json class @brief a template for a random access iterator for the @ref basic_json class
This class implements a const iterator for the @ref basic_json class. From This class implements a both iterators (iterator and const_iterator)
this class, the @ref iterator class is derived. for the @ref basic_json class.
@note An iterator is called *initialized* when a pointer to a JSON value @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 has been set (e.g., by a constructor or a copy assignment). If the
@ -8222,25 +8222,35 @@ class basic_json
@since version 1.0.0 @since version 1.0.0
*/ */
class const_iterator : public std::iterator<std::random_access_iterator_tag, const basic_json> template <typename U>
class iter_impl : public std::iterator<std::random_access_iterator_tag, U>
{ {
/// allow basic_json to access private members /// allow basic_json to access private members
friend class basic_json; friend class basic_json;
// make sure U is basic_json or const basic_json
static_assert(std::is_same<U, basic_json>::value
or std::is_same<U, const basic_json>::value,
"iter_impl only accepts (const) basic_json");
public: public:
/// the type of the values when the iterator is dereferenced /// the type of the values when the iterator is dereferenced
using value_type = typename basic_json::value_type; using value_type = typename basic_json::value_type;
/// a type to represent differences between iterators /// a type to represent differences between iterators
using difference_type = typename basic_json::difference_type; using difference_type = typename basic_json::difference_type;
/// defines a pointer to the type iterated over (value_type) /// defines a pointer to the type iterated over (value_type)
using pointer = typename basic_json::const_pointer; using pointer = typename std::conditional<std::is_const<U>::value,
typename basic_json::const_pointer,
typename basic_json::pointer>::type;
/// defines a reference to the type iterated over (value_type) /// defines a reference to the type iterated over (value_type)
using reference = typename basic_json::const_reference; using reference = typename std::conditional<std::is_const<U>::value,
typename basic_json::const_reference,
typename basic_json::reference>::type;
/// the category of the iterator /// the category of the iterator
using iterator_category = std::bidirectional_iterator_tag; using iterator_category = std::bidirectional_iterator_tag;
/// default constructor /// default constructor
const_iterator() = default; iter_impl() = default;
/*! /*!
@brief constructor for a given JSON instance @brief constructor for a given JSON instance
@ -8248,7 +8258,7 @@ class basic_json
@pre object != nullptr @pre object != nullptr
@post The iterator is initialized; i.e. `m_object != nullptr`. @post The iterator is initialized; i.e. `m_object != nullptr`.
*/ */
explicit const_iterator(pointer object) noexcept explicit iter_impl(pointer object) noexcept
: m_object(object) : m_object(object)
{ {
assert(m_object != nullptr); assert(m_object != nullptr);
@ -8275,37 +8285,25 @@ class basic_json
} }
} }
/*! /*
@brief copy constructor given a non-const iterator Use operator const_iterator instead of
@param[in] other iterator to copy from const_iterator(const iterator& other) noexcept
@note It is not checked whether @a other is initialized. to avoid two class definitions for iterator and const_iterator.
This function is only called if this class is an iterator.
If this class is a const_iterator this function is not called.
*/ */
explicit const_iterator(const iterator& other) noexcept operator const_iterator() const
: m_object(other.m_object) {
{ const_iterator ret;
if (m_object != nullptr)
{
switch (m_object->m_type)
{
case basic_json::value_t::object:
{
m_it.object_iterator = other.m_it.object_iterator;
break;
}
case basic_json::value_t::array: if (m_object)
{ {
m_it.array_iterator = other.m_it.array_iterator; ret.m_object = m_object;
break; ret.m_it = m_it;
}
default:
{
m_it.primitive_iterator = other.m_it.primitive_iterator;
break;
}
}
} }
return ret;
} }
/*! /*!
@ -8313,7 +8311,7 @@ class basic_json
@param[in] other iterator to copy from @param[in] other iterator to copy from
@note It is not checked whether @a other is initialized. @note It is not checked whether @a other is initialized.
*/ */
const_iterator(const const_iterator& other) noexcept iter_impl(const iter_impl& other) noexcept
: m_object(other.m_object), m_it(other.m_it) : m_object(other.m_object), m_it(other.m_it)
{} {}
@ -8322,7 +8320,7 @@ class basic_json
@param[in,out] other iterator to copy from @param[in,out] other iterator to copy from
@note It is not checked whether @a other is initialized. @note It is not checked whether @a other is initialized.
*/ */
const_iterator& operator=(const_iterator other) noexcept( iter_impl& operator=(iter_impl other) noexcept(
std::is_nothrow_move_constructible<pointer>::value and std::is_nothrow_move_constructible<pointer>::value and
std::is_nothrow_move_assignable<pointer>::value and std::is_nothrow_move_assignable<pointer>::value and
std::is_nothrow_move_constructible<internal_iterator>::value and std::is_nothrow_move_constructible<internal_iterator>::value and
@ -8484,7 +8482,7 @@ class basic_json
@brief post-increment (it++) @brief post-increment (it++)
@pre The iterator is initialized; i.e. `m_object != nullptr`. @pre The iterator is initialized; i.e. `m_object != nullptr`.
*/ */
const_iterator operator++(int) iter_impl operator++(int)
{ {
auto result = *this; auto result = *this;
++(*this); ++(*this);
@ -8495,7 +8493,7 @@ class basic_json
@brief pre-increment (++it) @brief pre-increment (++it)
@pre The iterator is initialized; i.e. `m_object != nullptr`. @pre The iterator is initialized; i.e. `m_object != nullptr`.
*/ */
const_iterator& operator++() iter_impl& operator++()
{ {
assert(m_object != nullptr); assert(m_object != nullptr);
@ -8527,7 +8525,7 @@ class basic_json
@brief post-decrement (it--) @brief post-decrement (it--)
@pre The iterator is initialized; i.e. `m_object != nullptr`. @pre The iterator is initialized; i.e. `m_object != nullptr`.
*/ */
const_iterator operator--(int) iter_impl operator--(int)
{ {
auto result = *this; auto result = *this;
--(*this); --(*this);
@ -8538,7 +8536,7 @@ class basic_json
@brief pre-decrement (--it) @brief pre-decrement (--it)
@pre The iterator is initialized; i.e. `m_object != nullptr`. @pre The iterator is initialized; i.e. `m_object != nullptr`.
*/ */
const_iterator& operator--() iter_impl& operator--()
{ {
assert(m_object != nullptr); assert(m_object != nullptr);
@ -8570,7 +8568,7 @@ class basic_json
@brief comparison: equal @brief comparison: equal
@pre The iterator is initialized; i.e. `m_object != nullptr`. @pre The iterator is initialized; i.e. `m_object != nullptr`.
*/ */
bool operator==(const const_iterator& other) const bool operator==(const iter_impl& other) const
{ {
// if objects are not the same, the comparison is undefined // if objects are not the same, the comparison is undefined
if (m_object != other.m_object) if (m_object != other.m_object)
@ -8603,7 +8601,7 @@ class basic_json
@brief comparison: not equal @brief comparison: not equal
@pre The iterator is initialized; i.e. `m_object != nullptr`. @pre The iterator is initialized; i.e. `m_object != nullptr`.
*/ */
bool operator!=(const const_iterator& other) const bool operator!=(const iter_impl& other) const
{ {
return not operator==(other); return not operator==(other);
} }
@ -8612,7 +8610,7 @@ class basic_json
@brief comparison: smaller @brief comparison: smaller
@pre The iterator is initialized; i.e. `m_object != nullptr`. @pre The iterator is initialized; i.e. `m_object != nullptr`.
*/ */
bool operator<(const const_iterator& other) const bool operator<(const iter_impl& other) const
{ {
// if objects are not the same, the comparison is undefined // if objects are not the same, the comparison is undefined
if (m_object != other.m_object) if (m_object != other.m_object)
@ -8645,7 +8643,7 @@ class basic_json
@brief comparison: less than or equal @brief comparison: less than or equal
@pre The iterator is initialized; i.e. `m_object != nullptr`. @pre The iterator is initialized; i.e. `m_object != nullptr`.
*/ */
bool operator<=(const const_iterator& other) const bool operator<=(const iter_impl& other) const
{ {
return not other.operator < (*this); return not other.operator < (*this);
} }
@ -8654,7 +8652,7 @@ class basic_json
@brief comparison: greater than @brief comparison: greater than
@pre The iterator is initialized; i.e. `m_object != nullptr`. @pre The iterator is initialized; i.e. `m_object != nullptr`.
*/ */
bool operator>(const const_iterator& other) const bool operator>(const iter_impl& other) const
{ {
return not operator<=(other); return not operator<=(other);
} }
@ -8663,7 +8661,7 @@ class basic_json
@brief comparison: greater than or equal @brief comparison: greater than or equal
@pre The iterator is initialized; i.e. `m_object != nullptr`. @pre The iterator is initialized; i.e. `m_object != nullptr`.
*/ */
bool operator>=(const const_iterator& other) const bool operator>=(const iter_impl& other) const
{ {
return not operator<(other); return not operator<(other);
} }
@ -8672,7 +8670,7 @@ class basic_json
@brief add to iterator @brief add to iterator
@pre The iterator is initialized; i.e. `m_object != nullptr`. @pre The iterator is initialized; i.e. `m_object != nullptr`.
*/ */
const_iterator& operator+=(difference_type i) iter_impl& operator+=(difference_type i)
{ {
assert(m_object != nullptr); assert(m_object != nullptr);
@ -8703,7 +8701,7 @@ class basic_json
@brief subtract from iterator @brief subtract from iterator
@pre The iterator is initialized; i.e. `m_object != nullptr`. @pre The iterator is initialized; i.e. `m_object != nullptr`.
*/ */
const_iterator& operator-=(difference_type i) iter_impl& operator-=(difference_type i)
{ {
return operator+=(-i); return operator+=(-i);
} }
@ -8712,7 +8710,7 @@ class basic_json
@brief add to iterator @brief add to iterator
@pre The iterator is initialized; i.e. `m_object != nullptr`. @pre The iterator is initialized; i.e. `m_object != nullptr`.
*/ */
const_iterator operator+(difference_type i) iter_impl operator+(difference_type i)
{ {
auto result = *this; auto result = *this;
result += i; result += i;
@ -8723,7 +8721,7 @@ class basic_json
@brief subtract from iterator @brief subtract from iterator
@pre The iterator is initialized; i.e. `m_object != nullptr`. @pre The iterator is initialized; i.e. `m_object != nullptr`.
*/ */
const_iterator operator-(difference_type i) iter_impl operator-(difference_type i)
{ {
auto result = *this; auto result = *this;
result -= i; result -= i;
@ -8734,7 +8732,7 @@ class basic_json
@brief return difference @brief return difference
@pre The iterator is initialized; i.e. `m_object != nullptr`. @pre The iterator is initialized; i.e. `m_object != nullptr`.
*/ */
difference_type operator-(const const_iterator& other) const difference_type operator-(const iter_impl& other) const
{ {
assert(m_object != nullptr); assert(m_object != nullptr);
@ -8830,141 +8828,6 @@ class basic_json
internal_iterator m_it = internal_iterator(); internal_iterator m_it = internal_iterator();
}; };
/*!
@brief a mutable random access iterator for the @ref basic_json class
@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.
@since version 1.0.0
*/
class iterator : public const_iterator
{
public:
using base_iterator = const_iterator;
using pointer = typename basic_json::pointer;
using reference = typename basic_json::reference;
/// default constructor
iterator() = default;
/// constructor for a given JSON instance
explicit iterator(pointer object) noexcept
: base_iterator(object)
{}
/// copy constructor
iterator(const iterator& other) noexcept
: base_iterator(other)
{}
/// copy assignment
iterator& operator=(iterator other) noexcept(
std::is_nothrow_move_constructible<pointer>::value and
std::is_nothrow_move_assignable<pointer>::value and
std::is_nothrow_move_constructible<internal_iterator>::value and
std::is_nothrow_move_assignable<internal_iterator>::value
)
{
base_iterator::operator=(other);
return *this;
}
/// return a reference to the value pointed to by the iterator
reference operator*() const
{
return const_cast<reference>(base_iterator::operator*());
}
/// dereference the iterator
pointer operator->() const
{
return const_cast<pointer>(base_iterator::operator->());
}
/// post-increment (it++)
iterator operator++(int)
{
iterator result = *this;
base_iterator::operator++();
return result;
}
/// pre-increment (++it)
iterator& operator++()
{
base_iterator::operator++();
return *this;
}
/// post-decrement (it--)
iterator operator--(int)
{
iterator result = *this;
base_iterator::operator--();
return result;
}
/// pre-decrement (--it)
iterator& operator--()
{
base_iterator::operator--();
return *this;
}
/// add to iterator
iterator& operator+=(difference_type i)
{
base_iterator::operator+=(i);
return *this;
}
/// subtract from iterator
iterator& operator-=(difference_type i)
{
base_iterator::operator-=(i);
return *this;
}
/// add to iterator
iterator operator+(difference_type i)
{
auto result = *this;
result += i;
return result;
}
/// subtract from iterator
iterator operator-(difference_type i)
{
auto result = *this;
result -= i;
return result;
}
/// return difference
difference_type operator-(const iterator& other) const
{
return base_iterator::operator-(other);
}
/// access to successor
reference operator[](difference_type n) const
{
return const_cast<reference>(base_iterator::operator[](n));
}
/// return the value of an iterator
reference value() const
{
return const_cast<reference>(base_iterator::value());
}
};
/*! /*!
@brief a template for a reverse iterator class @brief a template for a reverse iterator class