wibble 1.1
consumer.h
Go to the documentation of this file.
1
6#include <iterator>
7
8#include <wibble/amorph.h>
9#include <wibble/range.h>
10#include <wibble/cast.h>
11
12#ifndef WIBBLE_CONSUMER_H
13#define WIBBLE_CONSUMER_H
14
15namespace wibble {
16
17template< typename T > struct Consumer;
18
19template< typename T >
21{
22 typedef T InputType;
23 virtual void consume( const T &a ) = 0;
24 virtual void consume( Range< T > s ) = 0;
25 virtual ~ConsumerInterface() {}
26};
27
28template< typename T, typename W >
29struct ConsumerMorph : Morph< ConsumerMorph< T, W >, W, ConsumerInterface< T > >
30{
33
34 virtual void consume( const T &a ) {
35 return this->wrapped().consume( a );
36 }
37
38 virtual void consume( Range< T > s ) {
39 while ( !s.empty() ) {
40 consume( s.head() );
41 s = s.tail();
42 }
43 }
44};
45
46template< typename T, typename Self >
48{
49 Self &self() { return *static_cast< Self * >( this ); }
50 const Self &self() const { return *static_cast< const Self * >( this ); }
51 typedef std::output_iterator_tag iterator_category;
52 typedef T ConsumedType;
53
54 bool operator<=( const Self &o ) const { return this <= &o; }
55 Consumer< T > &operator++() { return self(); }
56 Consumer< T > &operator++(int) { return self(); }
57 Consumer< T > &operator*() { return self(); }
58 Consumer< T > &operator=( const T &a ) {
59 self()->consume( a );
60 return self();
61 }
62};
63
64template< typename T >
65struct Consumer: Amorph< Consumer< T >, ConsumerInterface< T > >,
66 ConsumerMixin< T, Consumer< T > >
67{
69
70 typedef void value_type;
71 typedef void difference_type;
72 typedef void pointer;
73 typedef void reference;
74
77
78 void consume( const T &a ) {
79 return this->implementation()->consume( a );
80 }
81
82 Consumer< T > &operator=( const T &a ) {
83 consume( a );
84 return *this;
85 }
86 // output iterator - can't read or move
87};
88
89template< typename T, typename Out >
90struct ConsumerFromIterator : ConsumerMixin< T, ConsumerFromIterator< T, Out > >
91{
92 ConsumerFromIterator( Out out ) : m_out( out ) {}
93 void consume( const T& a ) {
94 *(*m_out) = a;
95 ++(*m_out);
96 }
97protected:
99};
100
101template< typename R >
105
106// insert iterators
107template< typename Out >
112
113// containers
114template< typename T >
115typename IsType< Consumer< typename T::value_type >, typename T::iterator >::T consumer( T &c ) {
116 return consumer( std::inserter( c, c.end() ) );
117}
118
119// consumers
120template< typename T >
122 return t;
123}
124
125}
126
127#endif
-*- C++ -*-
Definition amorph.h:17
Consumer< typename R::ConsumedType > consumerMorph(R r)
Definition consumer.h:102
Consumer< typename Out::container_type::value_type > consumer(Out out)
Definition consumer.h:108
-*- C++ -*-
Definition amorph.h:272
const Interface * implementation() const
Definition amorph.h:361
Definition consumer.h:91
ConsumerFromIterator(Out out)
Definition consumer.h:92
void consume(const T &a)
Definition consumer.h:93
Out m_out
Definition consumer.h:98
Definition consumer.h:21
T InputType
Definition consumer.h:22
virtual void consume(Range< T > s)=0
virtual ~ConsumerInterface()
Definition consumer.h:25
virtual void consume(const T &a)=0
Definition consumer.h:48
bool operator<=(const Self &o) const
Definition consumer.h:54
const Self & self() const
Definition consumer.h:50
Consumer< T > & operator++(int)
Definition consumer.h:56
Consumer< T > & operator*()
Definition consumer.h:57
Self & self()
Definition consumer.h:49
Consumer< T > & operator=(const T &a)
Definition consumer.h:58
T ConsumedType
Definition consumer.h:52
Consumer< T > & operator++()
Definition consumer.h:55
std::output_iterator_tag iterator_category
Definition consumer.h:51
Definition consumer.h:30
ConsumerMorph(const W &w)
Definition consumer.h:32
ConsumerMorph()
Definition consumer.h:31
virtual void consume(Range< T > s)
Definition consumer.h:38
virtual void consume(const T &a)
Definition consumer.h:34
Definition consumer.h:67
void value_type
Definition consumer.h:70
Amorph< Consumer< T >, ConsumerInterface< T > > Super
Definition consumer.h:68
void pointer
Definition consumer.h:72
Consumer()
Definition consumer.h:76
Consumer< T > & operator=(const T &a)
Definition consumer.h:82
Consumer(const MorphInterface< ConsumerInterface< T > > &i)
Definition consumer.h:75
void difference_type
Definition consumer.h:71
void reference
Definition consumer.h:73
void consume(const T &a)
Definition consumer.h:78
An interface implemented by all morph classes.
Definition amorph.h:91
Definition amorph.h:144
const Wrapped & wrapped() const
Definition amorph.h:181
Definition amorph.h:30
Definition mixin.h:13