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 
15 namespace wibble {
16 
17 template< typename T > struct Consumer;
18 
19 template< 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 
28 template< typename T, typename W >
29 struct ConsumerMorph : Morph< ConsumerMorph< T, W >, W, ConsumerInterface< T > >
30 {
32  ConsumerMorph( const W &w ) : Morph< ConsumerMorph, W, ConsumerInterface< T > >( w ) {}
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 
46 template< 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 
64 template< typename T >
65 struct 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 
75  Consumer( const MorphInterface< ConsumerInterface< T > > &i ) : Super( i ) {}
76  Consumer() {}
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 
89 template< typename T, typename Out >
90 struct 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  }
97 protected:
98  Out m_out;
99 };
100 
101 template< typename R >
104 }
105 
106 // insert iterators
107 template< typename Out >
109  return consumerMorph(
111 }
112 
113 // containers
114 template< typename T >
116  return consumer( std::inserter( c, c.end() ) );
117 }
118 
119 // consumers
120 template< typename T >
122  return t;
123 }
124 
125 }
126 
127 #endif
Iterator< typename I::value_type > iterator(I i)
Definition: iterator.h:123
void reference
Definition: consumer.h:73
ConsumerMorph(const W &w)
Definition: consumer.h:32
T InputType
Definition: consumer.h:22
Definition: cast.h:25
void consume(const T &a)
Definition: consumer.h:78
Definition: consumer.h:90
Consumer< T > & operator=(const T &a)
Definition: consumer.h:82
Consumer< typename R::ConsumedType > consumerMorph(R r)
Definition: consumer.h:102
std::output_iterator_tag iterator_category
Definition: consumer.h:51
Consumer< typename Out::container_type::value_type > consumer(Out out)
Definition: consumer.h:108
void consume(const T &a)
Definition: consumer.h:93
void difference_type
Definition: consumer.h:71
bool operator<=(const Self &o) const
Definition: consumer.h:54
Definition: consumer.h:20
-*- C++ -*-
Consumer(const MorphInterface< ConsumerInterface< T > > &i)
Definition: consumer.h:75
ConsumerFromIterator(Out out)
Definition: consumer.h:92
Consumer()
Definition: consumer.h:76
Definition: amorph.h:272
virtual void consume(const T &a)=0
An interface implemented by all morph classes.
Definition: amorph.h:91
-*- C++ -*-
virtual void consume(const T &a)
Definition: consumer.h:34
void value_type
Definition: consumer.h:70
Consumer< T > & operator*()
Definition: consumer.h:57
T head() const
Definition: range.h:163
Definition: mixin.h:13
ConsumerMorph()
Definition: consumer.h:31
Definition: consumer.h:29
void pointer
Definition: consumer.h:72
Amorph< Consumer< T >, ConsumerInterface< T > > Super
Definition: consumer.h:68
virtual void consume(Range< T > s)
Definition: consumer.h:38
Definition: amorph.h:17
Consumer< T > & operator=(const T &a)
Definition: consumer.h:58
T ConsumedType
Definition: consumer.h:52
Definition: range.h:20
Consumer< T > & operator++()
Definition: consumer.h:55
bool empty() const
Definition: range.h:82
Out m_out
Definition: consumer.h:98
Definition: consumer.h:17
virtual ~ConsumerInterface()
Definition: consumer.h:25
Definition: amorph.h:141
Definition: consumer.h:47
Consumer< T > & operator++(int)
Definition: consumer.h:56
Range< T > tail() const
Definition: range.h:75