SHORE API
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
buffer_chain.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 
25 
26 #ifndef SHORE_PROCESSING_BUFFER_CHAIN_HPP__
27 #define SHORE_PROCESSING_BUFFER_CHAIN_HPP__
28 
29 #include <deque>
30 #include <string>
31 
32 #include "shore/base/util.hpp"
34 
35 namespace shore {
36 
40 template<typename T>
42 {
43  public:
44 
45  typedef plugin<T> plugin_type;
46 
47  private:
48 
50  std::deque<T *> m_buffers;
51 
53  std::vector<T *> m_unused;
54 
56  std::vector<plugin_type *> m_plugins;
57 
59  size_t m_next_plugin;
60 
62  bool m_ready;
63 
65  bool m_flushed;
66 
67 
69  bool apply_plugins()
70  {
71  while(m_next_plugin<m_plugins.size())
72  {
73  const int result=
74  m_plugins[m_next_plugin]->apply(m_buffers,
75  m_unused,
76  m_flushed);
77 
78  if(result&plugin_type::PLUGIN_STARVED)
79  {
80  if(result&plugin_type::PLUGIN_END)
81  m_next_plugin=0;
82 
83  return false;
84  }
85 
86  if(result&plugin_type::PLUGIN_END)
87  m_next_plugin=0;
88  else
89  ++m_next_plugin;
90  }
91 
92  m_next_plugin=0;
93 
94  return !m_buffers.empty();
95  }
96 
97  protected:
98 
102  bool buffer_chain_ready() const
103  {
104  return m_ready;
105  }
106 
111  {
112  if(!buffer_chain_waiting())
113  throw_exception(std::logic_error("buffer_chain_push()"
114  " not allowed here"));
115 
116  if(m_unused.empty())
117  m_buffers.push_back(new T);
118  else
119  {
120  m_buffers.push_back(m_unused.back());
121  m_unused.pop_back();
122  }
123 
124  return *(m_buffers.back());
125  }
126 
131  {
132  if(!buffer_chain_waiting())
133  throw_exception(std::logic_error("buffer_chain_prepare()"
134  " not allowed here"));
135 
136  m_ready=apply_plugins();
137 
138  return m_ready;
139  }
140 
146  bool buffer_chain_flush(const bool undo_last_push)
147  {
148  if(!buffer_chain_waiting())
149  throw_exception(std::logic_error("buffer_chain_flush()"
150  " not allowed here"));
151 
152  if(undo_last_push&&(!m_buffers.empty()))
153  {
154  m_unused.push_back(m_buffers.back());
155  m_buffers.pop_back();
156  }
157 
158  m_flushed=true;
159 
160  m_ready=apply_plugins();
161 
162  return m_ready;
163  }
164 
168  const T & buffer_chain_front() const
169  {
170  return *m_buffers.front();
171  }
172 
176  bool buffer_chain_waiting() const
177  {
178  return !(m_ready||m_flushed);
179  }
180 
185  {
186  m_unused.push_back(m_buffers.front());
187  m_buffers.pop_front();
188 
189  m_ready=apply_plugins();
190 
191  return m_ready;
192  }
193 
198  {
199  while(!m_buffers.empty())
200  {
201  m_unused.push_back(m_buffers.back());
202  m_buffers.pop_back();
203  }
204  m_ready=m_flushed=false;
205  }
206 
209  T * buffer_chain_swap(T * buf)
210  {
211  T *const ret=m_buffers.back();
212  m_buffers.back()=buf;
213  return ret;
214  }
215 
216  public:
217 
222  :m_next_plugin(0),
223  m_ready(false),
224  m_flushed(false)
225  {}
226 
231  {
233 
234  while(!m_unused.empty())
235  {
236  delete m_unused.back();
237  m_unused.pop_back();
238  }
239  }
240 
244  void add_plugin(plugin_type *const p)
245  {
246  m_plugins.push_back(p);
247  }
248 
251  {
252  m_plugins.clear();
253  }
254 };
255 
256 } //namespace shore
257 
258 #endif // SHORE_PROCESSING_BUFFER_CHAIN_HPP__
259