libfly  6.2.2
C++20 utility library for Linux, macOS, and Windows
fly::detail::JsonIterator< JsonType > Class Template Reference

#include <json_iterator.hpp>

Public Types

enum class  Position : std::uint8_t { Begin , End }
 
using iterator_category = std::bidirectional_iterator_tag
 
using value_type = JsonType
 
using difference_type = typename JsonType::difference_type
 
using reference = std::conditional_t< is_const_iterator, typename JsonType::const_reference, typename JsonType::reference >
 
using pointer = std::conditional_t< is_const_iterator, typename JsonType::const_pointer, typename JsonType::pointer >
 

Public Member Functions

 JsonIterator ()=default
 
 JsonIterator (pointer json, Position position) noexcept(false)
 
 JsonIterator (const NonConstJsonIterator &iterator) noexcept
 
JsonIteratoroperator= (const NonConstJsonIterator &iterator) noexcept
 
reference operator* () const
 
pointer operator-> () const
 
reference operator[] (difference_type offset) const
 
bool operator== (const JsonIterator &iterator) const
 
bool operator!= (const JsonIterator &iterator) const
 
bool operator< (const JsonIterator &iterator) const
 
bool operator<= (const JsonIterator &iterator) const
 
bool operator> (const JsonIterator &iterator) const
 
bool operator>= (const JsonIterator &iterator) const
 
JsonIterator operator++ (int)
 
JsonIteratoroperator++ ()
 
JsonIterator operator-- (int)
 
JsonIteratoroperator-- ()
 
JsonIteratoroperator+= (difference_type offset)
 
JsonIteratoroperator-= (difference_type offset)
 
JsonIterator operator+ (difference_type offset) const
 
JsonIterator operator- (difference_type offset) const
 
difference_type operator- (const JsonIterator &iterator) const
 
const json_object_type::key_type & key () const
 
reference value () const
 

Friends

template<typename J >
JsonIterator< J > operator+ (typename JsonIterator< J >::difference_type offset, const JsonIterator< J > &iterator)
 

Detailed Description

template<fly::SameAs< Json > JsonType>
class fly::detail::JsonIterator< JsonType >

Class to provide iterator access to a Json instance. Both const and non-const iterators are supported.

For Json object instances, this class satisfies the requirements of a BidirectionalIterator. For Json array instances, this class satisfies the requirements of a RandomAccessIterator. All other Json types are not supported.

Iterators may be default constructed, copy constructed, or constructed from a Json instance. A requirement of iterators is to allow constructing a const iterator from a non-const iterator (and to forbid the other direction). To achieve this, the standard copy constructor and assignment operator are left implicitly defined. Overloads are explictly defined which accept non-const iterators. These allow constructing const iterators from const iterators, non-const iterators from non-const iterators, and const iterators from non- const iterators.

Iterators are protected against some classes of undefined behavior. If any of the below conditions are met, an exception will be raised:

1. Dereferencing an empty or past-the-end iterator.
2. Creating an iterator which escapes the range [begin, end] of the Json instance.
3. Performing RandomAccessIterator operations on a BidirectionalIterator.

There is not yet protection against an iterator-invalidating operation on the Json instance. For example, the following will not raise an exception:

fly::Json json {1, 2, 3};
auto it = json.begin();
json = {4, 5, 6};
bool b = it->empty(); // Undefined behavior
Author
Timothy Flynn (trfly.nosp@m.nn89.nosp@m.@pm.m.nosp@m.e)
Version
May 17, 2020

Member Typedef Documentation

◆ iterator_category

template<fly::SameAs< Json > JsonType>
using fly::detail::JsonIterator< JsonType >::iterator_category = std::bidirectional_iterator_tag

Aliases for canonical STL iterator member types.

Member Enumeration Documentation

◆ Position

template<fly::SameAs< Json > JsonType>
enum fly::detail::JsonIterator::Position : std::uint8_t
strong

Enumeration to indicate the initial position of the iterator.

Constructor & Destructor Documentation

◆ JsonIterator() [1/3]

template<fly::SameAs< Json > JsonType>
fly::detail::JsonIterator< JsonType >::JsonIterator ( )
default

Default constructor. Initializes the iterator to an empty value.

◆ JsonIterator() [2/3]

template<fly::SameAs< Json > JsonType>
fly::detail::JsonIterator< JsonType >::JsonIterator ( pointer  json,
Position  position 
)
noexcept

Constructor to initialize the iterator to be pointed at the beginning or end of a Json instance.

Parameters
jsonA pointer to the Json instance.
positionThe initial position of the iterator.
Exceptions
JsonIteratorExceptionIf the Json instance is not iterable.

◆ JsonIterator() [3/3]

template<fly::SameAs< Json > JsonType>
fly::detail::JsonIterator< JsonType >::JsonIterator ( const NonConstJsonIterator< JsonType > &  iterator)
noexcept

Conversion copy constructor. Allows constructing a const or non-const iterator from a non-const iterator.

Parameters
iteratorThe iterator instance to copy.

Member Function Documentation

◆ key()

template<fly::SameAs< Json > JsonType>
const json_object_type::key_type & fly::detail::JsonIterator< JsonType >::key

Retrieve a reference to the key of the Json instance pointed to by this iterator. Only valid for Json object types.

Returns
A reference to the Json object's key.
Exceptions
JsonIteratorExceptionIf the Json instance is not an object.
NullJsonExceptionIf the iterator is empty or past-the-end.

◆ operator!=()

template<fly::SameAs< Json > JsonType>
bool fly::detail::JsonIterator< JsonType >::operator!= ( const JsonIterator< JsonType > &  iterator) const

Unequality comparison operator.

Parameters
iteratorThe iterator instance to compare.
Returns
True if the two iterators are unequivalent.
Exceptions
BadJsonComparisonExceptionIf the two iterators are not for the same Json instance.
NullJsonExceptionIf either iterator is empty.

◆ operator*()

template<fly::SameAs< Json > JsonType>
auto fly::detail::JsonIterator< JsonType >::operator*

Retrieve a reference to the Json instance pointed to by this iterator.

Returns
A reference to the Json instance.
Exceptions
NullJsonExceptionIf the iterator is empty or past-the-end.

◆ operator+()

template<fly::SameAs< Json > JsonType>
auto fly::detail::JsonIterator< JsonType >::operator+ ( difference_type  offset) const

Addition operator. Retrieve an iterator pointed at the Json instance some offset earlier or later in the sequence. Invalid for Json object types.

Parameters
offsetThe offset to retrieve.
Returns
An iterator pointed at the Json instance.
Exceptions
JsonIteratorExceptionIf the Json instance is an object.
NullJsonExceptionIf the iterator is empty.
OutOfRangeJsonExceptionIf the iterator at the offset escapes the Json instance's valid range.

◆ operator++() [1/2]

template<fly::SameAs< Json > JsonType>
auto fly::detail::JsonIterator< JsonType >::operator++

Pre-increment operator. Sets the instance pointed to by this iterator to the next instance in the sequence.

Returns
A reference to this iterator instance.
Exceptions
NullJsonExceptionIf the iterator is empty.
OutOfRangeJsonExceptionIf the next iterator escapes the Json instance's valid range.

◆ operator++() [2/2]

template<fly::SameAs< Json > JsonType>
auto fly::detail::JsonIterator< JsonType >::operator++ ( int  )

Post-increment operator. Sets the instance pointed to by this iterator to the next instance in the sequence.

Returns
A copy of the iterator before the increment.
Exceptions
NullJsonExceptionIf the iterator is empty.
OutOfRangeJsonExceptionIf the next iterator escapes the Json instance's valid range.

◆ operator+=()

template<fly::SameAs< Json > JsonType>
auto fly::detail::JsonIterator< JsonType >::operator+= ( difference_type  offset)

Addition operator. Sets the Json instance pointed to by this iterator to some offset earlier or later in the sequence. Invalid for Json object types.

Parameters
offsetThe offset by which to increment the iterator.
Returns
A reference to this iterator instance.
Exceptions
JsonIteratorExceptionIf the Json instance is an object.
NullJsonExceptionIf the iterator is empty.
OutOfRangeJsonExceptionIf the iterator at the offset escapes the Json instance's valid range.

◆ operator-() [1/2]

template<fly::SameAs< Json > JsonType>
auto fly::detail::JsonIterator< JsonType >::operator- ( const JsonIterator< JsonType > &  iterator) const

Difference operator. Compute the distance between this iterator and another. Invalid for Json object types.

Parameters
iteratorThe iterator instance to compare.
Returns
The distance between the two iterators.
Exceptions
JsonIteratorExceptionIf the Json instance is an object.
BadJsonComparisonExceptionIf the two iterators are not for the same Json instance.
NullJsonExceptionIf either iterator is empty.

◆ operator-() [2/2]

template<fly::SameAs< Json > JsonType>
auto fly::detail::JsonIterator< JsonType >::operator- ( difference_type  offset) const

Subtraction operator. Retrieve an iterator pointed at the Json instance some offset earlier or later in the sequence. Invalid for Json object types.

Parameters
offsetThe offset to retrieve.
Returns
An iterator pointed at the Json instance.
Exceptions
JsonIteratorExceptionIf the Json instance is an object.
NullJsonExceptionIf the iterator is empty.
OutOfRangeJsonExceptionIf the iterator at the offset escapes the Json instance's valid range.

◆ operator--() [1/2]

template<fly::SameAs< Json > JsonType>
auto fly::detail::JsonIterator< JsonType >::operator--

Pre-decrement operator. Sets the instance pointed to by this iterator to the previous instance in the sequence.

Returns
A reference to this iterator instance.
Exceptions
NullJsonExceptionIf the iterator is empty.
OutOfRangeJsonExceptionIf the previous iterator escapes the Json instance's valid range.

◆ operator--() [2/2]

template<fly::SameAs< Json > JsonType>
auto fly::detail::JsonIterator< JsonType >::operator-- ( int  )

Post-decrement operator. Sets the instance pointed to by this iterator to the previous instance in the sequence.

Returns
A copy of the iterator before the decrement.
Exceptions
NullJsonExceptionIf the iterator is empty.
OutOfRangeJsonExceptionIf the previous iterator escapes the Json instance's valid range.

◆ operator-=()

template<fly::SameAs< Json > JsonType>
auto fly::detail::JsonIterator< JsonType >::operator-= ( difference_type  offset)

Subtraction operator. Sets the Json instance pointed to by this iterator to some offset earlier or later in the sequence. Invalid for Json object types.

Parameters
offsetThe offset by which to decrement the iterator.
Returns
A reference to this iterator instance.
Exceptions
JsonIteratorExceptionIf the Json instance is an object.
NullJsonExceptionIf the iterator is empty.
OutOfRangeJsonExceptionIf the iterator at the offset escapes the Json instance's valid range.

◆ operator->()

template<fly::SameAs< Json > JsonType>
auto fly::detail::JsonIterator< JsonType >::operator->

Retrieve a pointer to the Json instance pointed to by this iterator.

Returns
A pointer to the Json instance.
Exceptions
NullJsonExceptionIf the iterator is empty or past-the-end.

◆ operator<()

template<fly::SameAs< Json > JsonType>
bool fly::detail::JsonIterator< JsonType >::operator< ( const JsonIterator< JsonType > &  iterator) const

Less-than comparison operator. Invalid for Json object types.

Parameters
iteratorThe iterator instance to compare.
Returns
True if this iterator is less than the given iterator.
Exceptions
JsonIteratorExceptionIf the Json instance is an object.
BadJsonComparisonExceptionIf the two iterators are not for the same Json instance.
NullJsonExceptionIf either iterator is empty.

◆ operator<=()

template<fly::SameAs< Json > JsonType>
bool fly::detail::JsonIterator< JsonType >::operator<= ( const JsonIterator< JsonType > &  iterator) const

Less-than-or-equal-to comparison operator. Invalid for Json object types.

Parameters
iteratorThe iterator instance to compare.
Returns
True if this iterator is less than or equal to the given iterator.
Exceptions
JsonIteratorExceptionIf the Json instance is an object.
BadJsonComparisonExceptionIf the two iterators are not for the same Json instance.
NullJsonExceptionIf either iterator is empty.

◆ operator=()

template<fly::SameAs< Json > JsonType>
JsonIterator< JsonType > & fly::detail::JsonIterator< JsonType >::operator= ( const NonConstJsonIterator< JsonType > &  iterator)
noexcept

Conversion assignment operator. Allows initializing a const or non-const iterator from a non-const iterator.

Parameters
iteratorThe iterator instance to copy.
Returns
A reference to this iterator instance.

◆ operator==()

template<fly::SameAs< Json > JsonType>
bool fly::detail::JsonIterator< JsonType >::operator== ( const JsonIterator< JsonType > &  iterator) const

Equality comparison operator.

Parameters
iteratorThe iterator instance to compare.
Returns
True if the two iterators are equivalent.
Exceptions
BadJsonComparisonExceptionIf the two iterators are not for the same Json instance.
NullJsonExceptionIf either iterator is empty.

◆ operator>()

template<fly::SameAs< Json > JsonType>
bool fly::detail::JsonIterator< JsonType >::operator> ( const JsonIterator< JsonType > &  iterator) const

Greater-than comparison operator. Invalid for Json object types.

Parameters
iteratorThe iterator instance to compare.
Returns
True if this iterator is greater than the given iterator.
Exceptions
JsonIteratorExceptionIf the Json instance is an object.
BadJsonComparisonExceptionIf the two iterators are not for the same Json instance.
NullJsonExceptionIf either iterator is empty.

◆ operator>=()

template<fly::SameAs< Json > JsonType>
bool fly::detail::JsonIterator< JsonType >::operator>= ( const JsonIterator< JsonType > &  iterator) const

Greater-than-or-equal-to comparison operator. Invalid for Json object types.

Parameters
iteratorThe iterator instance to compare.
Returns
True if this iterator is greater than or equal to the given iterator.
Exceptions
JsonIteratorExceptionIf the Json instance is an object.
BadJsonComparisonExceptionIf the two iterators are not for the same Json instance.
NullJsonExceptionIf either iterator is empty.

◆ operator[]()

template<fly::SameAs< Json > JsonType>
auto fly::detail::JsonIterator< JsonType >::operator[] ( difference_type  offset) const

Retrieve a reference to the Json instance at some offset earlier or later than the instance pointed to by this iterator. Invoking operator[0] is equivalent to invoking operator*. Invalid for Json object types.

Parameters
offsetThe offset to retrieve.
Returns
A reference to the Json instance.
Exceptions
JsonIteratorExceptionIf the Json instance is an object.
NullJsonExceptionIf the iterator at the offset is empty or past-the-end.
OutOfRangeJsonExceptionIf the iterator at the offset escapes the Json instance's valid range.

◆ value()

template<fly::SameAs< Json > JsonType>
auto fly::detail::JsonIterator< JsonType >::value

Retrieve a reference to the Json instance pointed to by this iterator.

Returns
A reference to the Json instance.
Exceptions
NullJsonExceptionIf the iterator is empty or past-the-end.

Friends And Related Function Documentation

◆ operator+

template<fly::SameAs< Json > JsonType>
template<typename J >
JsonIterator<J> operator+ ( typename JsonIterator< J >::difference_type  offset,
const JsonIterator< J > &  iterator 
)
friend

Addition operator. Retrieve an iterator pointed at the Json instance some offset earlier or later in the sequence. Invalid for Json object types.

Parameters
offsetThe offset to retrieve.
Returns
An iterator pointed at the Json instance.
Exceptions
JsonIteratorExceptionIf the Json instance is an object.
NullJsonExceptionIf the iterator is empty.
OutOfRangeJsonExceptionIf the iterator at the offset escapes the Json instance's valid range.

The documentation for this class was generated from the following file: