libdl  0.0.1
Simple yet powerful deep learning
Loading...
Searching...
No Matches
dl::utils::GenericIterator< T > Class Template Reference

Represents a generic iterator that can be initialized to hold any other class instance that satisfies the std::input_or_output_iterator concept. More...

#include <generic_iterator.hpp>

Public Types

using iterator_category = std::input_iterator_tag
 
using difference_type = std::ptrdiff_t
 
using value_type = T
 
using pointer = T *
 
using reference = T &
 

Public Member Functions

 GenericIterator ()
 Default constructor that creates a nullptr-iterator.
 
 GenericIterator (const GenericIterator< T > &other)
 Copy-constructs a new generic iterator.
 
 GenericIterator (GenericIterator< T > &&other)
 Move-constructs a new generic iterator.
 
template<std::input_or_output_iterator TIt>
 GenericIterator (TIt iterator) noexcept
 Constructs a new GenericIterator from the provided arbitrary object that satisfies the std::input_or_output_iterator concept.
 
template<std::input_or_output_iterator TIt>
 GenericIterator (TIt iterator, std::shared_ptr< auto > userdata) noexcept
 Constructs a new GenericIterator from the provided arbitrary object that satisfies the std::input_or_output_iterator concept. The additional userdata parameter can keep the underlying data that the iterator iterates over such that it is not deleted as long as at least one iterator is still alive.
 
GenericIterator< T > & operator= (const GenericIterator< T > &other) noexcept
 Assignment opterator.
 
GenericIterator< T > & operator= (GenericIterator< T > &&other) noexcept
 Move-assignment operator.
 
operator* () const
 Dereferences the iterator. That is, returns the data it currently points at.
 
GenericIterator< T > & operator++ () noexcept
 Increments the iterator. That is, advancing it forward by one.
 
GenericIterator< T > operator++ (int) noexcept
 Late increments the iterator. That is, advances the iterator by one but returns a copy of the iterator prior to advancing.
 
bool operator== (const GenericIterator< T > &other) const
 Checks if this iterator is equal to the provided one.
 

Detailed Description

template<typename T>
class dl::utils::GenericIterator< T >

Represents a generic iterator that can be initialized to hold any other class instance that satisfies the std::input_or_output_iterator concept.

Represents a generic iterator that can be initialized to hold any other class instance that satisfies the std::input_or_output_iterator concept. For example consider the following code:

std::vector<int> myvec = {1, 2, 3, 4};
GenericIterator<int> begin = myvec.begin();
GenericIterator<int> end = myvec.end();
T begin(T... args)
Represents a generic iterator that can be initialized to hold any other class instance that satisfies...
T end(T... args)

begin and end again are valid iterators but they are not specific to the container they iterate through.

Template Parameters
Tthe type of the elements being iterated.

Definition at line 24 of file generic_iterator.hpp.

Member Typedef Documentation

◆ difference_type

template<typename T >
using dl::utils::GenericIterator< T >::difference_type = std::ptrdiff_t

Definition at line 143 of file generic_iterator.hpp.

◆ iterator_category

template<typename T >
using dl::utils::GenericIterator< T >::iterator_category = std::input_iterator_tag

Definition at line 142 of file generic_iterator.hpp.

◆ pointer

template<typename T >
using dl::utils::GenericIterator< T >::pointer = T*

Definition at line 145 of file generic_iterator.hpp.

◆ reference

template<typename T >
using dl::utils::GenericIterator< T >::reference = T&

Definition at line 146 of file generic_iterator.hpp.

◆ value_type

template<typename T >
using dl::utils::GenericIterator< T >::value_type = T

Definition at line 144 of file generic_iterator.hpp.

Constructor & Destructor Documentation

◆ GenericIterator() [1/5]

template<typename T >
dl::utils::GenericIterator< T >::GenericIterator ( )
inline

Default constructor that creates a nullptr-iterator.

Definition at line 151 of file generic_iterator.hpp.

151: impl(nullptr) {}

◆ GenericIterator() [2/5]

template<typename T >
dl::utils::GenericIterator< T >::GenericIterator ( const GenericIterator< T > &  other)
inline

Copy-constructs a new generic iterator.

Parameters
otherThe iterator to copy.

Definition at line 158 of file generic_iterator.hpp.

158: impl(other.impl->clone()) {}

◆ GenericIterator() [3/5]

template<typename T >
dl::utils::GenericIterator< T >::GenericIterator ( GenericIterator< T > &&  other)
inline

Move-constructs a new generic iterator.

Parameters
otherThe iterator to move.

Definition at line 165 of file generic_iterator.hpp.

165: impl(std::move(other.impl)) {}
T move(T... args)

◆ GenericIterator() [4/5]

template<typename T >
template<std::input_or_output_iterator TIt>
dl::utils::GenericIterator< T >::GenericIterator ( TIt  iterator)
inlineexplicitnoexcept

Constructs a new GenericIterator from the provided arbitrary object that satisfies the std::input_or_output_iterator concept.

Template Parameters
TItThe datatype of the underlying iterator.
Parameters
iteratorThe underlying iterator.
See also
GenericIterator(TIt, std::shared_ptr<auto>)

Definition at line 176 of file generic_iterator.hpp.

176: impl(std::make_unique<Iter<TIt>>(iterator)) {}

◆ GenericIterator() [5/5]

template<typename T >
template<std::input_or_output_iterator TIt>
dl::utils::GenericIterator< T >::GenericIterator ( TIt  iterator,
std::shared_ptr< auto >  userdata 
)
inlinenoexcept

Constructs a new GenericIterator from the provided arbitrary object that satisfies the std::input_or_output_iterator concept. The additional userdata parameter can keep the underlying data that the iterator iterates over such that it is not deleted as long as at least one iterator is still alive.

Template Parameters
TItThe datatype of the underlying iterator.
Parameters
iteratorThe underlying iterator.
userdata(Optional) additional userdata holding the data the iterator iterates over.
See also
GenericIterator(TIt)

Definition at line 189 of file generic_iterator.hpp.

190 : impl(std::make_unique<Iter<TIt>>(iterator, userdata)) {}

Member Function Documentation

◆ operator*()

template<typename T >
T dl::utils::GenericIterator< T >::operator* ( ) const
inline

Dereferences the iterator. That is, returns the data it currently points at.

Returns
The data the iterator currently points at.

Definition at line 219 of file generic_iterator.hpp.

219{ return impl->deref(); }

◆ operator++() [1/2]

template<typename T >
GenericIterator< T > & dl::utils::GenericIterator< T >::operator++ ( )
inlinenoexcept

Increments the iterator. That is, advancing it forward by one.

Returns
A reference to this iterator.

Definition at line 225 of file generic_iterator.hpp.

225 {
226 impl->advance();
227 return *this;
228 }

◆ operator++() [2/2]

template<typename T >
GenericIterator< T > dl::utils::GenericIterator< T >::operator++ ( int  )
inlinenoexcept

Late increments the iterator. That is, advances the iterator by one but returns a copy of the iterator prior to advancing.

Returns
The iterator before it got advanced.

Definition at line 235 of file generic_iterator.hpp.

235 {
236 auto copy = *this;
237 (*this)++;
238 return copy;
239 }
T copy(T... args)

◆ operator=() [1/2]

template<typename T >
GenericIterator< T > & dl::utils::GenericIterator< T >::operator= ( const GenericIterator< T > &  other)
inlinenoexcept

Assignment opterator.

Parameters
other
Returns
A reference to this.

Definition at line 198 of file generic_iterator.hpp.

198 {
199 impl = other.impl;
200 return *this;
201 }

◆ operator=() [2/2]

template<typename T >
GenericIterator< T > & dl::utils::GenericIterator< T >::operator= ( GenericIterator< T > &&  other)
inlinenoexcept

Move-assignment operator.

Parameters
other
Returns
A reference to this.

Definition at line 209 of file generic_iterator.hpp.

209 {
210 impl = std::move(other.impl);
211 return *this;
212 }

◆ operator==()

template<typename T >
bool dl::utils::GenericIterator< T >::operator== ( const GenericIterator< T > &  other) const
inline

Checks if this iterator is equal to the provided one.

Parameters
otherThe iterator to check equality against.
Returns
true if and only if this iterator is equal to other.

Definition at line 246 of file generic_iterator.hpp.

246{ return impl->equals(*other.impl); }

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