SHORE API
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
plugin.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_PLUGIN_HPP__
27 #define SHORE_PROCESSING_PLUGIN_HPP__
28 
29 #include <deque>
30 #include <stdexcept>
31 #include <string>
32 
33 namespace shore {
34 
40 template<typename T>
41 class plugin
42 {
43  public:
44 
45  virtual ~plugin() {}
46 
54  {
55  PLUGIN_SUCCESS=0,
56  PLUGIN_STARVED=1,
57  PLUGIN_END=2
58  };
59 
65  virtual int apply(std::deque<T *> & buffers,
66  std::vector<T *> & unused,
67  const bool flush)=0;
68 };
69 
74 template<typename Cmp>
75 bool is_valid_comparator(const Cmp &cmp,
76  typename boost::disable_if<boost::is_pointer<Cmp> >::type * =0)
77 {
78  return true;
79 }
80 
85 template<typename Cmp>
86 bool is_valid_comparator(const Cmp &cmp,
87  typename boost::enable_if<boost::is_pointer<Cmp> >::type * =0)
88 {
89  return (cmp!=0);
90 }
91 
95 template<typename T,
96  typename Cmp=bool (*)(const T &,const T &)>
98 :public plugin<T>
99 {
100  public:
101 
102  typedef plugin<T> plugin_type;
103 
107  struct error
108  :public std::runtime_error
109  {
110  error()
111  :std::runtime_error("sorting_check: not sorted appropriately")
112  {}
113 
114  error(const std::string &name)
115  :std::runtime_error("sorting_check: \""+name+"\" is"
116  " not sorted appropriately")
117  {}
118  };
119 
120  private:
121 
123  Cmp m_cmp;
124 
126  error m_error;
127 
128  public:
129 
133  sorting_check(Cmp cmp=Cmp())
134  :m_cmp(cmp)
135  {}
136 
141  sorting_check(const std::string &name,Cmp cmp=Cmp())
142  :m_cmp(cmp),
143  m_error(name)
144  {}
145 
150  virtual int apply(std::deque<T *> & buffers,
151  std::vector<T *> & unused,
152  const bool flush)
153  {
154  if(!is_valid_comparator(m_cmp))
155  return plugin_type::PLUGIN_SUCCESS;
156 
157  if(buffers.size()<2)
158  return flush?(plugin_type::PLUGIN_SUCCESS)
159  :(plugin_type::PLUGIN_STARVED);
160 
161  if(m_cmp(*(buffers[1]),*(buffers.front())))
162  throw m_error;
163 
164  return plugin_type::PLUGIN_SUCCESS;
165  }
166 
170  Cmp get_comparator() const
171  {
172  return m_cmp;
173  }
174 };
175 
176 } //namespace shore
177 
178 #endif // SHORE_PROCESSING_PLUGIN_HPP__
179