A library for working with phylogenetic data.
LambdaIteratorGenerator< T > Class Template Reference

#include <genesis/utils/containers/lambda_iterator.hpp>

Detailed Description

template<class T>
class genesis::utils::LambdaIteratorGenerator< T >

Type erasure for iterators, using std::function to get rid of the underlying input type.

This class offers some abstraction to get a uniform iterator type over some underlying iterator. Expects a function (most likely, you want to use a lambda) that converts the underlying data into the desired type T, which is where the type erasure happens. The data that is iterated over is kept in a shared pointer, which at the end of the iteration (when the underlying iterator is done) simply has to be returned as nullptr by the lambda to indicate the end of the iteration.


// Convert from an iterator over VcfRecord to Variant.
auto beg = vcf_range.begin();
auto end = vcf_range.end();

// Create the conversion with type erasure via the lambda function.
generator = LambdaIteratorGenerator<Variant>(
    [beg, end]() mutable -> std::shared_ptr<Variant>{
        if( beg != end ) {
            auto res = std::make_shared<Variant>( convert_to_variant(*beg) );
            return res;
        } else {
            return nullptr;

// Iterate over generator.begin() and generator.end()
for( auto it : generator ) ...

For other types of iterators, instead of beg and end, other input can be used:

// Use a pileup iterator, which does not offer begin and end.
auto it = SimplePileupInputIterator( utils::from_file( pileup_file_.value ), reader );
generator = LambdaIteratorGenerator<Variant>(
    [it]() mutable -> std::shared_ptr<Variant>{
        if( it ) {
            auto res = std::make_shared<Variant>( convert_to_variant(*it) );
            return res;
        } else {
            return nullptr;

And accordinly for other underlying iterator types.

Definition at line 87 of file lambda_iterator.hpp.

Public Member Functions

 LambdaIteratorGenerator ()=default
 LambdaIteratorGenerator (self_type &&)=default
 LambdaIteratorGenerator (self_type const &)=default
 LambdaIteratorGenerator (std::function< std::shared_ptr< value_type >()> get_element)
 ~LambdaIteratorGenerator ()=default
LambdaIterator begin ()
LambdaIterator end ()
 operator bool () const
 Return whether a function was assigend to this generator, that is, whether it is default constructed (false) or not (true). More...
self_typeoperator= (self_type &&)=default
self_typeoperator= (self_type const &)=default

Public Types

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

Public Attributes

friend LambdaIterator


class  LambdaIterator

Constructor & Destructor Documentation

◆ LambdaIteratorGenerator() [1/4]

◆ LambdaIteratorGenerator() [2/4]

LambdaIteratorGenerator ( std::function< std::shared_ptr< value_type >()>  get_element)

Definition at line 190 of file lambda_iterator.hpp.

◆ ~LambdaIteratorGenerator()

◆ LambdaIteratorGenerator() [3/4]

LambdaIteratorGenerator ( self_type const &  )

◆ LambdaIteratorGenerator() [4/4]

Member Function Documentation

◆ begin()

LambdaIterator begin ( )

Definition at line 210 of file lambda_iterator.hpp.

◆ end()

LambdaIterator end ( )

Definition at line 215 of file lambda_iterator.hpp.

◆ operator bool()

operator bool ( ) const

Return whether a function was assigend to this generator, that is, whether it is default constructed (false) or not (true).

Definition at line 224 of file lambda_iterator.hpp.

◆ operator=() [1/2]

self_type& operator= ( self_type &&  )

◆ operator=() [2/2]

self_type& operator= ( self_type const &  )

Member Typedef Documentation

◆ difference_type

using difference_type = std::ptrdiff_t

Definition at line 99 of file lambda_iterator.hpp.

◆ iterator_category

using iterator_category = std::input_iterator_tag

Definition at line 100 of file lambda_iterator.hpp.

◆ pointer

using pointer = value_type const*

Definition at line 97 of file lambda_iterator.hpp.

◆ reference

using reference = value_type const&

Definition at line 98 of file lambda_iterator.hpp.

◆ self_type

Definition at line 95 of file lambda_iterator.hpp.

◆ value_type

using value_type = T

Definition at line 96 of file lambda_iterator.hpp.

Member Data Documentation

◆ LambdaIterator

Definition at line 204 of file lambda_iterator.hpp.

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