consumer.h
Go to the documentation of this file.
#include <iterator>
#include <wibble/amorph.h>
#include <wibble/range.h>
#include <wibble/cast.h>
#ifndef WIBBLE_CONSUMER_H
#define WIBBLE_CONSUMER_H
namespace wibble {
template< typename T > struct Consumer;
template< typename T >
struct ConsumerInterface
{
typedef T InputType;
virtual void consume( const T &a ) = 0;
virtual void consume( Range< T > s ) = 0;
virtual ~ConsumerInterface() {}
};
template< typename T, typename W >
struct ConsumerMorph : Morph< ConsumerMorph< T, W >, W, ConsumerInterface< T > >
{
ConsumerMorph() {}
ConsumerMorph( const W &w ) : Morph< ConsumerMorph, W, ConsumerInterface< T > >( w ) {}
virtual void consume( const T &a ) {
return this->wrapped().consume( a );
}
virtual void consume( Range< T > s ) {
while ( !s.empty() ) {
consume( s.head() );
s = s.tail();
}
}
};
template< typename T, typename Self >
struct ConsumerMixin : mixin::Comparable< Self >
{
Self &self() { return *static_cast< Self * >( this ); }
const Self &self() const { return *static_cast< const Self * >( this ); }
typedef std::output_iterator_tag iterator_category;
typedef T ConsumedType;
bool operator<=( const Self &o ) const { return this <= &o; }
Consumer< T > &operator++() { return self(); }
Consumer< T > &operator++(int) { return self(); }
Consumer< T > &operator*() { return self(); }
Consumer< T > &operator=( const T &a ) {
self()->consume( a );
return self();
}
};
template< typename T >
struct Consumer: Amorph< Consumer< T >, ConsumerInterface< T > >,
ConsumerMixin< T, Consumer< T > >
{
typedef Amorph< Consumer< T >, ConsumerInterface< T > > Super;
typedef void value_type;
typedef void difference_type;
typedef void pointer;
typedef void reference;
Consumer( const MorphInterface< ConsumerInterface< T > > &i ) : Super( i ) {}
Consumer() {}
void consume( const T &a ) {
return this->implementation()->consume( a );
}
Consumer< T > &operator=( const T &a ) {
consume( a );
return *this;
}
// output iterator - can't read or move
};
template< typename T, typename Out >
struct ConsumerFromIterator : ConsumerMixin< T, ConsumerFromIterator< T, Out > >
{
ConsumerFromIterator( Out out ) : m_out( out ) {}
void consume( const T& a ) {
*(*m_out) = a;
++(*m_out);
}
protected:
Out m_out;
};
template< typename R >
Consumer< typename R::ConsumedType > consumerMorph( R r ) {
return ConsumerMorph< typename R::ConsumedType , R >( r );
}
// insert iterators
template< typename Out >
Consumer< typename Out::container_type::value_type > consumer( Out out ) {
return consumerMorph(
ConsumerFromIterator< typename Out::container_type::value_type, Out >( out ) );
}
// containers
template< typename T >
typename IsType< Consumer< typename T::value_type >, typename T::iterator >::T consumer( T &c ) {
return consumer( std::inserter( c, c.end() ) );
}
// consumers
template< typename T >
Consumer< T > consumer( const ConsumerInterface< T > &t ) {
return t;
}
}
#endif
amorph.h
-*- C++ -*-
cast.h
wibble
Definition: amorph.h:17
wibble::consumerMorph
Consumer< typename R::ConsumedType > consumerMorph(R r)
Definition: consumer.h:102
wibble::iterator
Iterator< typename I::value_type > iterator(I i)
Definition: iterator.h:123
wibble::consumer
Consumer< typename Out::container_type::value_type > consumer(Out out)
Definition: consumer.h:108
range.h
-*- C++ -*-
wibble::Amorph
Definition: amorph.h:272
wibble::Amorph< Consumer< T >, ConsumerInterface< T > >::implementation
const Interface * implementation() const
Definition: amorph.h:361
wibble::ConsumerFromIterator
Definition: consumer.h:91
wibble::ConsumerFromIterator::ConsumerFromIterator
ConsumerFromIterator(Out out)
Definition: consumer.h:92
wibble::ConsumerFromIterator::consume
void consume(const T &a)
Definition: consumer.h:93
wibble::ConsumerFromIterator::m_out
Out m_out
Definition: consumer.h:98
wibble::ConsumerInterface
Definition: consumer.h:21
wibble::ConsumerInterface::InputType
T InputType
Definition: consumer.h:22
wibble::ConsumerInterface::consume
virtual void consume(Range< T > s)=0
wibble::ConsumerInterface::~ConsumerInterface
virtual ~ConsumerInterface()
Definition: consumer.h:25
wibble::ConsumerInterface::consume
virtual void consume(const T &a)=0
wibble::ConsumerMixin
Definition: consumer.h:48
wibble::ConsumerMixin::operator<=
bool operator<=(const Self &o) const
Definition: consumer.h:54
wibble::ConsumerMixin::operator*
Consumer< T > & operator*()
Definition: consumer.h:57
wibble::ConsumerMixin::operator++
Consumer< T > & operator++()
Definition: consumer.h:55
wibble::ConsumerMixin::operator++
Consumer< T > & operator++(int)
Definition: consumer.h:56
wibble::ConsumerMixin::operator=
Consumer< T > & operator=(const T &a)
Definition: consumer.h:58
wibble::ConsumerMixin::ConsumedType
T ConsumedType
Definition: consumer.h:52
wibble::ConsumerMixin::iterator_category
std::output_iterator_tag iterator_category
Definition: consumer.h:51
wibble::ConsumerMorph
Definition: consumer.h:30
wibble::ConsumerMorph::ConsumerMorph
ConsumerMorph(const W &w)
Definition: consumer.h:32
wibble::ConsumerMorph::ConsumerMorph
ConsumerMorph()
Definition: consumer.h:31
wibble::ConsumerMorph::consume
virtual void consume(Range< T > s)
Definition: consumer.h:38
wibble::ConsumerMorph::consume
virtual void consume(const T &a)
Definition: consumer.h:34
wibble::Consumer
Definition: consumer.h:67
wibble::Consumer::value_type
void value_type
Definition: consumer.h:70
wibble::Consumer::Super
Amorph< Consumer< T >, ConsumerInterface< T > > Super
Definition: consumer.h:68
wibble::Consumer::operator=
Consumer< T > & operator=(const T &a)
Definition: consumer.h:82
wibble::Consumer::pointer
void pointer
Definition: consumer.h:72
wibble::Consumer::Consumer
Consumer()
Definition: consumer.h:76
wibble::Consumer::Consumer
Consumer(const MorphInterface< ConsumerInterface< T > > &i)
Definition: consumer.h:75
wibble::Consumer::difference_type
void difference_type
Definition: consumer.h:71
wibble::Consumer::reference
void reference
Definition: consumer.h:73
wibble::Consumer::consume
void consume(const T &a)
Definition: consumer.h:78
wibble::IsType
Definition: cast.h:25
wibble::MorphInterface
An interface implemented by all morph classes.
Definition: amorph.h:91
wibble::Morph
Definition: amorph.h:144
wibble::Morph< ConsumerMorph< T, W >, W, ConsumerInterface< T > >::wrapped
const Wrapped & wrapped() const
Definition: amorph.h:181
wibble::RangeMixin::empty
bool empty() const
Definition: range.h:82
wibble::RangeMixin::tail
Self tail() const
Definition: range.h:75
wibble::Range
Definition: range.h:155
wibble::Range::head
T head() const
Definition: range.h:163
wibble::mixin::Comparable
Definition: mixin.h:13