SHORE API
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
filter.hpp
Go to the documentation of this file.
1 
2 /*
3  * Copyright 2008,2009,2010,2011,2012 Stephan Ossowski, Korbinian Schneeberger,
4  * Felix Ott, Joerg Hagmann, Alf Scotland, Sebastian Bender
5  *
6  * This file is part of SHORE.
7  *
8  * SHORE is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * SHORE is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with SHORE. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
26 
27 #ifndef SHORE_PROCESSING_FILTER_HPP__
28 #define SHORE_PROCESSING_FILTER_HPP__
29 
30 #include <stdexcept>
31 #include <vector>
32 
33 #include <boost/function.hpp>
34 
35 
36 #include "pipeline.hpp"
39 
40 
41 namespace shore {
42 
44 template<typename T,typename U=T>
45 class thru
46 :public pipe_facade<thru<T,U>,T,U>
47 {
48  private:
49 
50  friend class pipeline_core_access;
51 
52  void append(const T &obj)
53  {
54  this->emit(obj);
55  }
56 };
57 
59 template<typename T,typename U=T>
60 struct conversion
61 {
62  T & operator()(T & dst,const U & src) const
63  {
64  dst=src;
65  }
66 };
67 
69 template<typename T,typename U=T,typename Converter=conversion<T,U> >
71 :public pipe_facade<conversion_pipe<T,U>,T,U>,
72  public buffer_chain<U>
73 {
74  private:
75 
76  friend class pipeline_core_access;
77 
78  Converter m_conv;
79 
80  void next()
81  {
84  }
85 
86  void append(const T & obj)
87  {
89 
92  }
93 
94  void flush()
95  {
98  }
99 };
100 
102 template<typename T>
103 class filter
104 {
105  private:
106 
107  typedef boost::function<bool (const T&)> functor_t;
108 
109  const T* m_current;
110 
111  std::vector<functor_t> m_ops;
112 
113  public:
114 
115  typedef T current_type;
116  typedef T append_type;
117 
118  filter():m_current(0) {}
119 
120  void add_op(functor_t func)
121  {
122  m_ops.push_back(func);
123  }
124 
125  bool has_data() const
126  {
127  return m_current!=0;
128  }
129 
130  const T& current() const
131  {
132  return *m_current;
133  }
134 
135  void next()
136  {
137  m_current=0;
138  }
139 
140  void append(const T& obj)
141  {
142  if(m_current!=0)
143  throw std::logic_error("filter::append: filter is full");
144 
145  for(size_t i=0;i<m_ops.size();++i)
146  {
147  if(!(m_ops[i](obj)))
148  return;
149  }
150  m_current=&obj;
151  }
152 
153  void flush() {}
154 
155  bool empty() const
156  {
157  return m_ops.empty();
158  }
159 };
160 
162 template<typename T>
163 class splitter
164 :public pipe_facade<splitter<T>,T,T>
165 {
166  private:
167 
168  friend class shore::pipeline_core_access;
169 
170  typedef boost::function<bool (const T&)> functor_t;
171 
172  std::vector<functor_t> m_ops;
173 
174  feed<T> m_fail;
175 
176 
177  void append(const T &obj)
178  {
179  for(size_t i=0;i<m_ops.size();++i)
180  {
181  if(!(m_ops[i](obj)))
182  {
183  m_fail.append(obj);
184  return;
185  }
186  }
187  this->emit(obj);
188  }
189 
190  void flush()
191  {
192  m_fail.flush();
193  }
194 
195  public:
196 
197  feed<T> &failures()
198  {
199  return m_fail;
200  }
201 
202  void add_op(functor_t func)
203  {
204  m_ops.push_back(func);
205  }
206 
207  bool empty() const
208  {
209  return m_ops.empty();
210  }
211 };
212 
213 } //namespace shore
214 
215 #endif // SHORE_PROCESSING_FILTER_HPP__
216