SeqAn3 3.4.0-rc.1
The Modern C++ library for sequence analysis.
Loading...
Searching...
No Matches
seqan3::detail::inherited_iterator_base< derived_t, base_t > Class Template Reference

A CRTP base template for creating iterators that inherit from other iterators. More...

#include <seqan3/core/range/detail/inherited_iterator_base.hpp>

+ Inheritance diagram for seqan3::detail::inherited_iterator_base< derived_t, base_t >:

Public Types

Associated types

All are derived from the base_t.

using difference_type = std::iter_difference_t< base_t >
 The difference type.
 
using value_type = std::iter_value_t< base_t >
 The value type.
 
using reference = std::iter_reference_t< base_t >
 The reference type.
 
using pointer = detail::iter_pointer_t< base_t >
 The pointer type.
 
using iterator_category = maybe_present
 The iterator category tag.
 
using iterator_concept = detail::iterator_concept_tag_t< base_t >
 The iterator concept tag.
 
- Public Types inherited from seqan3::detail::maybe_iterator_category< underlying_iterator_t >
using iterator_category = MAYBE_PRESENT(std::iterator_traits< underlying_iterator_t >::iterator_category)
 The iterator category tag. (not always present!)
 

Public Member Functions

constexpr base_t base () &&noexcept
 Returns an rvalue of the base.
 
constexpr base_t & base () &noexcept
 Get a reference to the base.
 
constexpr base_t const & base () const &noexcept
 Get a const reference to the base.
 
Constructors, destructor and assignment

The exception specification is explicitly "inherited" to also work for pointers as base.

constexpr inherited_iterator_base () noexcept(std::is_nothrow_default_constructible_v< base_t >)=default
 Defaulted.
 
constexpr inherited_iterator_base (inherited_iterator_base const &rhs) noexcept(std::is_nothrow_copy_constructible_v< base_t >)=default
 Defaulted.
 
constexpr inherited_iterator_base (inherited_iterator_base &&rhs) noexcept(std::is_nothrow_move_constructible_v< base_t >)=default
 Defaulted.
 
constexpr inherited_iterator_baseoperator= (inherited_iterator_base const &rhs) noexcept(std::is_nothrow_copy_assignable_v< base_t >)=default
 Defaulted.
 
constexpr inherited_iterator_baseoperator= (inherited_iterator_base &&rhs) noexcept(std::is_nothrow_move_assignable_v< base_t >)=default
 Defaulted.
 
 ~inherited_iterator_base () noexcept(std::is_nothrow_destructible_v< base_t >)=default
 Defaulted.
 
constexpr inherited_iterator_base (base_t it) noexcept(std::is_nothrow_move_constructible_v< base_t >)
 Delegate to base class if inheriting from non-pointer iterator.
 
constexpr inherited_iterator_base (base_t it) noexcept
 Initialise member if deriving from pointer.
 
Comparison operators

Unless specialised in derived_type, all operators perform base_t's operator and cast to derived_t.

constexpr bool operator== (derived_t const &rhs) const noexcept(noexcept(std::declval< base_t & >()==std::declval< base_t & >()))
 Checks whether *this is equal to rhs.
 
constexpr bool operator!= (derived_t const &rhs) const noexcept(noexcept(std::declval< base_t & >()==std::declval< base_t & >()))
 Checks whether *this is not equal to rhs.
 
constexpr bool operator< (derived_t const &rhs) const noexcept(noexcept(std::declval< base_t & >()< std::declval< base_t & >()))
 Checks whether *this is less than rhs.
 
constexpr bool operator> (derived_t const &rhs) const noexcept(noexcept(std::declval< base_t & >() > std::declval< base_t & >()))
 Checks whether *this is greater than rhs.
 
constexpr bool operator<= (derived_t const &rhs) const noexcept(noexcept(std::declval< base_t & >() > std::declval< base_t & >()))
 Checks whether *this is less than or equal to rhs.
 
constexpr bool operator>= (derived_t const &rhs) const noexcept(noexcept(std::declval< base_t & >()< std::declval< base_t & >()))
 Checks whether *this is greater than or equal to rhs.
 
Reference/Dereference operators
constexpr reference operator* () noexcept(noexcept(*std::declval< base_t & >()))
 Dereference operator returns element currently pointed at.
 
constexpr decltype(auto) operator* () const noexcept(noexcept(*std::declval< base_t const & >()))
 Dereference operator returns element currently pointed at.
 
constexpr pointer operator-> () noexcept(noexcept(*std::declval< base_t & >()))
 Return pointer to this iterator.
 
constexpr decltype(auto) operator-> () const noexcept(noexcept(*std::declval< base_t const & >()))
 Return pointer to this iterator.
 
constexpr decltype(auto) operator[] (std::make_signed_t< difference_type > const n) noexcept(noexcept(std::declval< base_t & >()[0]))
 Return underlying container value currently pointed at.
 
constexpr decltype(auto) operator[] (std::make_signed_t< difference_type > const n) const noexcept(noexcept(std::declval< base_t const & >()[0]))
 Return underlying container value currently pointed at.
 

Private Member Functions

constexpr base_t & as_base () &noexcept
 Cast this to base type.
 
constexpr base_t const & as_base () const &noexcept
 Cast this to base type.
 
constexpr derived_tthis_derived ()
 Cast this to derived type.
 
constexpr derived_t const * this_derived () const
 Cast this to derived type.
 

Private Attributes

friend derived_t
 Befriend the derived type so it can access the private members.
 
std::conditional_t< wrap_base, base_t, empty_typemember
 If the base is a pointer, we wrap it instead of inheriting.
 

Static Private Attributes

static constexpr bool wrap_base = std::is_pointer_v<base_t> || !std::semiregular<base_t>
 Whether this iterator inherits or wraps.
 

Arithmetic operators

Unless specialised in derived_type, all operators perform base_t's operator and cast to derived_t.

constexpr derived_toperator++ () noexcept(noexcept(++std::declval< base_t & >()))
 Pre-increment, return updated iterator.
 
constexpr auto operator++ (int) noexcept(noexcept(std::declval< base_t & >()++))
 Post-increment of input iterators that do not return a copy of themselves but void or a proxy type.
 
constexpr derived_t operator++ (int) noexcept(noexcept(std::declval< base_t & >()++) &&noexcept(derived_t(std::declval< base_t & >())))
 Post-increment, return previous iterator state.
 
constexpr derived_toperator-- () noexcept(noexcept(--std::declval< base_t & >()))
 Pre-decrement, return updated iterator.
 
constexpr derived_t operator-- (int) noexcept(noexcept(std::declval< base_t & >() --) &&noexcept(derived_t{std::declval< base_t & >()}))
 Post-decrement, return previous iterator state.
 
constexpr derived_toperator+= (difference_type const skip) noexcept(noexcept(std::declval< base_t & >()+=skip))
 Move iterator to the right.
 
constexpr derived_t operator+ (difference_type const skip) const noexcept(noexcept(std::declval< base_t & >()+skip) &&noexcept(derived_t{std::declval< base_t & >()}))
 Returns an iterator which is advanced by skip positions.
 
constexpr derived_toperator-= (difference_type const skip) noexcept(noexcept(std::declval< base_t & >() -=skip))
 Decrement iterator by skip.
 
constexpr derived_t operator- (difference_type const skip) const noexcept(noexcept(std::declval< base_t const & >() - skip) &&noexcept(derived_t(std::declval< base_t & >())))
 Return decremented copy of this iterator.
 
constexpr difference_type operator- (derived_t const &rhs) const noexcept(noexcept(std::declval< base_t & >() - std::declval< base_t & >()))
 Return offset between this and remote iterator's position.
 
constexpr friend derived_t operator+ (difference_type const skip, derived_t const &it) noexcept(noexcept(skip+std::declval< base_t const & >()))
 Non-member operator+ delegates to non-friend operator+.
 

Detailed Description

template<typename derived_t, std::input_or_output_iterator base_t>
class seqan3::detail::inherited_iterator_base< derived_t, base_t >

A CRTP base template for creating iterators that inherit from other iterators.

Template Parameters
derived_tThe CRTP specialisation.
base_tThe type to inherit from; must satisfy std::input_or_output_iterator.

This template enables you to inherit from another iterator and just overload those functions that you wish to change.

Note that many of this class's members assume that the derived type is constructible from the base type.

Example

You may want to have an iterator that skips those elements in an int-vector that are not divisable by two:

std::vector<int> vec{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
skip_odd_numbers_it it = begin(vec);
EXPECT_EQ(*it, 0);
++it;
EXPECT_EQ(*it, 2);
++it;
EXPECT_EQ(*it, 4);
++it;
EXPECT_EQ(*it, 6);
++it;
EXPECT_EQ(*it, 8);

You could define it like this:

class skip_odd_numbers_it :
public seqan3::detail::inherited_iterator_base<skip_odd_numbers_it, std::vector<int>::iterator>
{
private:
using base_base_t = std::vector<int>::iterator;
public:
skip_odd_numbers_it() = default;
skip_odd_numbers_it(skip_odd_numbers_it const & rhs) = default;
skip_odd_numbers_it(skip_odd_numbers_it && rhs) = default;
skip_odd_numbers_it & operator=(skip_odd_numbers_it const & rhs) = default;
skip_odd_numbers_it & operator=(skip_odd_numbers_it && rhs) = default;
~skip_odd_numbers_it() = default;
skip_odd_numbers_it(base_base_t it) : base_t{it}
{}
using difference_type = typename std::iterator_traits<base_base_t>::difference_type;
using value_type = typename std::iterator_traits<base_base_t>::value_type;
using iterator_category = typename std::iterator_traits<base_base_t>::iterator_category;
skip_odd_numbers_it & operator++()
{
*this = ++static_cast<base_base_t>(*this);
if ((**this) % 2 != 0)
*this = ++static_cast<base_base_t>(*this);
return *this;
}
skip_odd_numbers_it operator++(int)
{
skip_odd_numbers_it cpy{*this};
++(*this);
return cpy;
}
};
A CRTP base template for creating iterators that inherit from other iterators.
Definition inherited_iterator_base.hpp:49

Member Function Documentation

◆ as_base()

template<typename derived_t , std::input_or_output_iterator base_t>
constexpr base_t const & seqan3::detail::inherited_iterator_base< derived_t, base_t >::as_base ( ) const &
inlineconstexprprivatenoexcept

Cast this to base type.

◆ this_derived()

template<typename derived_t , std::input_or_output_iterator base_t>
constexpr derived_t const * seqan3::detail::inherited_iterator_base< derived_t, base_t >::this_derived ( ) const
inlineconstexprprivate

Cast this to derived type.


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