pstore2
Classes | Functions
pointer_based_iterator.hpp File Reference

Provides pointer_based_iterator<> an iterator wrapper for pointers. More...

#include <cstddef>
#include <iterator>
#include <type_traits>
Include dependency graph for pointer_based_iterator.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  pstore::pointer_based_iterator< T >
 

Functions

template<typename T >
pointer_based_iterator< T > pstore::operator+ (pointer_based_iterator< T > const i, typename pointer_based_iterator< T >::difference_type const n) noexcept
 Move an iterator i forwards by distance n. More...
 
template<typename T >
pointer_based_iterator< T > pstore::operator+ (typename pointer_based_iterator< T >::difference_type const n, pointer_based_iterator< T > const i) noexcept
 Move an iterator i forwards by distance n. More...
 
template<typename T >
pointer_based_iterator< T > pstore::operator- (pointer_based_iterator< T > const i, typename pointer_based_iterator< T >::difference_type const n) noexcept
 Move an iterator i backwards by distance n. More...
 
template<typename T >
pointer_based_iterator< T >::difference_type pstore::operator- (pointer_based_iterator< T > b, pointer_based_iterator< T > a) noexcept
 

Detailed Description

Provides pointer_based_iterator<> an iterator wrapper for pointers.

Pointers to an array make perfectly good random access iterators. However there are a few of minor niggles with their usage.

First, pointers sometimes take the value nullptr to indicate the end of a sequence. Consider the POSIX readdir() API or a traditional singly-linked list where the last element of the list has a 'next' pointer of nullptr.

Second, there’s no easy way to portably add debug-time checks to raw pointers. Having a class allows us to sanity-check the value of the pointer relative to the container to which it points.

Third, our style guide (and clang-tidy) promotes the use of explicit '*' when declaring an auto variable of pointer type. This is good, but if we're declarating the result type of a standard library function that returns an iterator we would prefer to avoid hard-wiring the type as a pointer and instead stick with an abstract 'iterator'.

The pointer_based_iterator<> class is intended to resolve both of these "problems" by providing a random access iterator wrapper around a pointer.

Function Documentation

◆ operator+() [1/2]

template<typename T >
pointer_based_iterator<T> pstore::operator+ ( pointer_based_iterator< T > const  i,
typename pointer_based_iterator< T >::difference_type const  n 
)
inlinenoexcept

Move an iterator i forwards by distance n.

n can be both positive or negative.

Parameters
iThe iterator to be moved.
nThe distance by which iterator i should be moved.
Returns
The new iterator.

◆ operator+() [2/2]

template<typename T >
pointer_based_iterator<T> pstore::operator+ ( typename pointer_based_iterator< T >::difference_type const  n,
pointer_based_iterator< T > const  i 
)
inlinenoexcept

Move an iterator i forwards by distance n.

n can be both positive or negative.

Parameters
iThe iterator to be moved.
nThe distance by which iterator i should be moved.
Returns
The new iterator.

◆ operator-()

template<typename T >
pointer_based_iterator<T> pstore::operator- ( pointer_based_iterator< T > const  i,
typename pointer_based_iterator< T >::difference_type const  n 
)
inlinenoexcept

Move an iterator i backwards by distance n.

n can be both positive or negative.

Parameters
iThe iterator to be moved.
nThe distance by which iterator i should be moved.
Returns
The new iterator.