Monado OpenXR Runtime
xrt::auxiliary::util::RandomAccessIteratorBase< ContainerOrHelper > Class Template Reference

Template for base class used by "random-access" iterators and const_iterators, providing all the functionality that is independent of element type and const-ness of the iterator. More...

#include <util/u_iterator_base.hpp>

Public Types

using iterator_category = std::random_access_iterator_tag
 
using difference_type = std::ptrdiff_t
 
using const_iterator_base = std::conditional_t< std::is_const< ContainerOrHelper >::value, RandomAccessIteratorBase, RandomAccessIteratorBase< std::add_const_t< ContainerOrHelper > > >
 

Public Member Functions

bool valid () const noexcept
 Is this iterator valid? More...
 
 operator bool () const noexcept
 Is this iterator valid? More...
 
size_t index () const noexcept
 What is the index stored by this iterator? More...
 
bool is_past_the_end () const noexcept
 Is this iterator pointing "past the end" of the container? More...
 
bool is_cleared () const noexcept
 True if this iterator is "irrecoverably" invalid (that is, cleared or default constructed). More...
 
std::ptrdiff_t operator- (const RandomAccessIteratorBase< ContainerOrHelper > &other) const
 Compute the difference between two iterators. More...
 
RandomAccessIteratorBaseoperator+= (std::ptrdiff_t n)
 Increment by an arbitrary value. More...
 
RandomAccessIteratorBaseoperator-= (std::ptrdiff_t n)
 Decrement by an arbitrary value. More...
 
RandomAccessIteratorBase operator+ (std::ptrdiff_t n) const
 Add some arbitrary amount to a copy of this iterator. More...
 
RandomAccessIteratorBase operator- (std::ptrdiff_t n) const
 Subtract some arbitrary amount from a copy of this iterator. More...
 
 RandomAccessIteratorBase ()=default
 Default constructor - initializes to "cleared" (that is, irrecoverably invalid - because we have no reference to a container) More...
 
 RandomAccessIteratorBase (ContainerOrHelper &container, size_t index)
 Constructor from a helper/container and index. More...
 
const_iterator_base as_const () const
 Get a const iterator base pointing to the same element as this element. More...
 

Static Public Member Functions

static RandomAccessIteratorBase begin (ContainerOrHelper &container)
 Factory function: construct the "begin" iterator. More...
 
static RandomAccessIteratorBase end (ContainerOrHelper &container)
 Factory function: construct the "past the end" iterator that can be decremented safely. More...
 

Protected Member Functions

 RandomAccessIteratorBase (ContainerOrHelper *container, size_t index)
 for internal use More...
 
void increment_n (std::size_t n)
 Increment an arbitrary amount. More...
 
void decrement_n (std::size_t n)
 Decrement an arbitrary amount. More...
 
ContainerOrHelper * container () const noexcept
 Get the associated container or helper. More...
 

Related Functions

(Note that these are not member functions.)

template<typename ContainerOrHelper >
static bool operator== (RandomAccessIteratorBase< ContainerOrHelper > const &lhs, RandomAccessIteratorBase< ContainerOrHelper > const &rhs) noexcept
 Equality comparison operator for RandomAccessIteratorBase. More...
 
template<typename ContainerOrHelper >
static bool operator!= (RandomAccessIteratorBase< ContainerOrHelper > const &lhs, RandomAccessIteratorBase< ContainerOrHelper > const &rhs) noexcept
 Inequality comparison operator for RandomAccessIteratorBase. More...
 

Detailed Description

template<typename ContainerOrHelper>
class xrt::auxiliary::util::RandomAccessIteratorBase< ContainerOrHelper >

Template for base class used by "random-access" iterators and const_iterators, providing all the functionality that is independent of element type and const-ness of the iterator.

Using inheritance instead of composition here to more easily satisfy the C++ standard's requirements for iterators (e.g. that const iterators and iterators are comparable, etc.)

All invalid instances will compare as equal, as required by the spec, but they are not all equivalent. You can freely go "past the end" (they will be invalid, so cannot dereference, but you can get them back to valid), but you can't go "past the beginning". That is, you can do *(buf.end() - 1) successfully if your buffer has at least one element, even though buf.end() is invalid.

Template Parameters
ContainerOrHelperYour container or some member thereof that provides a size() method. If it's a helper instead of the actual container, make it const.

Constructor & Destructor Documentation

◆ RandomAccessIteratorBase() [1/3]

template<typename ContainerOrHelper >
xrt::auxiliary::util::RandomAccessIteratorBase< ContainerOrHelper >::RandomAccessIteratorBase ( )
default

Default constructor - initializes to "cleared" (that is, irrecoverably invalid - because we have no reference to a container)

Referenced by xrt::auxiliary::util::RandomAccessIteratorBase< ContainerOrHelper >::begin(), and xrt::auxiliary::util::RandomAccessIteratorBase< ContainerOrHelper >::end().

◆ RandomAccessIteratorBase() [2/3]

template<typename ContainerOrHelper >
xrt::auxiliary::util::RandomAccessIteratorBase< ContainerOrHelper >::RandomAccessIteratorBase ( ContainerOrHelper &  container,
size_t  index 
)
inlineexplicit

Constructor from a helper/container and index.

Parameters
containerThe helper or container we will iterate through.
indexAn index - may be out of range.

◆ RandomAccessIteratorBase() [3/3]

template<typename ContainerOrHelper >
xrt::auxiliary::util::RandomAccessIteratorBase< ContainerOrHelper >::RandomAccessIteratorBase ( ContainerOrHelper *  container,
size_t  index 
)
inlineprotected

for internal use

Member Function Documentation

◆ as_const()

template<typename ContainerOrHelper >
const_iterator_base xrt::auxiliary::util::RandomAccessIteratorBase< ContainerOrHelper >::as_const ( ) const
inline

Get a const iterator base pointing to the same element as this element.

Returns
const_iterator_base

References xrt::auxiliary::util::RandomAccessIteratorBase< ContainerOrHelper >::is_cleared().

◆ begin()

template<typename ContainerOrHelper >
static RandomAccessIteratorBase xrt::auxiliary::util::RandomAccessIteratorBase< ContainerOrHelper >::begin ( ContainerOrHelper &  container)
inlinestatic

◆ container()

template<typename ContainerOrHelper >
ContainerOrHelper * xrt::auxiliary::util::RandomAccessIteratorBase< ContainerOrHelper >::container ( ) const
inlineprotectednoexcept

◆ decrement_n()

template<typename ContainerOrHelper >
void xrt::auxiliary::util::RandomAccessIteratorBase< ContainerOrHelper >::decrement_n ( std::size_t  n)
inlineprotected

Decrement an arbitrary amount.

◆ end()

template<typename ContainerOrHelper >
static RandomAccessIteratorBase xrt::auxiliary::util::RandomAccessIteratorBase< ContainerOrHelper >::end ( ContainerOrHelper &  container)
inlinestatic

◆ increment_n()

template<typename ContainerOrHelper >
void xrt::auxiliary::util::RandomAccessIteratorBase< ContainerOrHelper >::increment_n ( std::size_t  n)
inlineprotected

Increment an arbitrary amount.

◆ index()

template<typename ContainerOrHelper >
size_t xrt::auxiliary::util::RandomAccessIteratorBase< ContainerOrHelper >::index ( ) const
inlinenoexcept

What is the index stored by this iterator?

◆ is_cleared()

template<typename ContainerOrHelper >
bool xrt::auxiliary::util::RandomAccessIteratorBase< ContainerOrHelper >::is_cleared ( ) const
inlinenoexcept

True if this iterator is "irrecoverably" invalid (that is, cleared or default constructed).

Implies !valid() but is stronger. buf.end().is_cleared() is false.

Referenced by xrt::auxiliary::util::RandomAccessIteratorBase< ContainerOrHelper >::as_const(), and xrt::auxiliary::util::RandomAccessIteratorBase< ContainerOrHelper >::operator-().

◆ is_past_the_end()

template<typename ContainerOrHelper >
bool xrt::auxiliary::util::RandomAccessIteratorBase< ContainerOrHelper >::is_past_the_end ( ) const
inlinenoexcept

Is this iterator pointing "past the end" of the container?

◆ operator bool()

template<typename ContainerOrHelper >
xrt::auxiliary::util::RandomAccessIteratorBase< ContainerOrHelper >::operator bool ( ) const
inlineexplicitnoexcept

◆ operator+()

template<typename ContainerOrHelper >
RandomAccessIteratorBase xrt::auxiliary::util::RandomAccessIteratorBase< ContainerOrHelper >::operator+ ( std::ptrdiff_t  n) const

Add some arbitrary amount to a copy of this iterator.

◆ operator+=()

template<typename ContainerOrHelper >
RandomAccessIteratorBase< ContainerOrHelper > & xrt::auxiliary::util::RandomAccessIteratorBase< ContainerOrHelper >::operator+= ( std::ptrdiff_t  n)
inline

Increment by an arbitrary value.

◆ operator-() [1/2]

template<typename ContainerOrHelper >
std::ptrdiff_t xrt::auxiliary::util::RandomAccessIteratorBase< ContainerOrHelper >::operator- ( const RandomAccessIteratorBase< ContainerOrHelper > &  other) const
inline

Compute the difference between two iterators.

  • If both are cleared, the result is 0.
  • Otherwise the result is the difference in index.
Exceptions
std::logic_errorif exactly one of the iterators is cleared
std::out_of_rangeif at least one of the iterators has an index larger than the maximum value of std::ptrdiff_t.

References xrt::auxiliary::util::RandomAccessIteratorBase< ContainerOrHelper >::is_cleared().

◆ operator-() [2/2]

template<typename ContainerOrHelper >
RandomAccessIteratorBase xrt::auxiliary::util::RandomAccessIteratorBase< ContainerOrHelper >::operator- ( std::ptrdiff_t  n) const

Subtract some arbitrary amount from a copy of this iterator.

◆ operator-=()

template<typename ContainerOrHelper >
RandomAccessIteratorBase< ContainerOrHelper > & xrt::auxiliary::util::RandomAccessIteratorBase< ContainerOrHelper >::operator-= ( std::ptrdiff_t  n)
inline

Decrement by an arbitrary value.

◆ valid()

template<typename ContainerOrHelper >
bool xrt::auxiliary::util::RandomAccessIteratorBase< ContainerOrHelper >::valid
inlinenoexcept

Friends And Related Function Documentation

◆ operator!=()

template<typename ContainerOrHelper >
static bool operator!= ( RandomAccessIteratorBase< ContainerOrHelper > const &  lhs,
RandomAccessIteratorBase< ContainerOrHelper > const &  rhs 
)
related

Inequality comparison operator for RandomAccessIteratorBase.

◆ operator==()

template<typename ContainerOrHelper >
static bool operator== ( RandomAccessIteratorBase< ContainerOrHelper > const &  lhs,
RandomAccessIteratorBase< ContainerOrHelper > const &  rhs 
)
related

Equality comparison operator for RandomAccessIteratorBase.


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