RSB  0.7.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Handler.h
Go to the documentation of this file.
1 /* ============================================================
2  *
3  * This file is a part of the RSB project
4  *
5  * Copyright (C) 2010 by Sebastian Wrede <swrede at techfak dot uni-bielefeld dot de>
6  *
7  * This file may be licensed under the terms of the
8  * GNU Lesser General Public License Version 3 (the ``LGPL''),
9  * or (at your option) any later version.
10  *
11  * Software distributed under the License is distributed
12  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
13  * express or implied. See the LGPL for the specific language
14  * governing rights and limitations.
15  *
16  * You should have received a copy of the LGPL along with this
17  * program. If not, go to http://www.gnu.org/licenses/lgpl.html
18  * or write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  * The development of this software was supported by:
22  * CoR-Lab, Research Institute for Cognition and Robotics
23  * Bielefeld University
24  *
25  * ============================================================ */
26 
27 #pragma once
28 
29 #include <set>
30 
31 #include <boost/shared_ptr.hpp>
32 #include <boost/function.hpp>
33 
34 #include <rsc/runtime/TypeStringTools.h>
35 
36 #include "Event.h"
37 
39 
40 #include "filter/Filter.h"
41 
42 #include "rsb/rsbexports.h"
43 
44 namespace rsb {
45 
53 class RSB_EXPORT Handler: public eventprocessing::Handler {
54 public:
55 
62  std::set<std::string> getMethods() const;
63  bool acceptsMethod(const std::string& method) const;
64 
65 protected:
66 
73  explicit Handler(const std::string& method = "");
74 
81  explicit Handler(const std::set<std::string>& methods);
82 
83  virtual ~Handler();
84 
85  std::string getClassName() const {
86  return rsc::runtime::typeName(typeid(*this));
87  }
88 private:
89  std::set<std::string> methods;
90 };
91 
92 typedef boost::shared_ptr<Handler> HandlerPtr;
93 
94 typedef boost::function<void(EventPtr)> EventFunction;
95 
101 class RSB_EXPORT EventFunctionHandler: public Handler {
102 public:
103  EventFunctionHandler(const EventFunction& function,
104  const std::string& method = "");
105 
106  std::string getClassName() const;
107  void printContents(std::ostream& stream) const;
108 
109  void handle(EventPtr event);
110 protected:
111  EventFunction function;
112 };
113 
123 template<typename T>
125 public:
126  typedef boost::shared_ptr<T> DataPtr;
127  typedef boost::function<void(DataPtr)> DataFunction;
128 
129  std::string getClassName() const {
130  return "DataFunctionHandler";
131  }
132 
133  void printContents(std::ostream& stream) const {
134  stream << "DataType = " << rsc::runtime::typeName<T>()
135  << ", function = " << function;
136  }
137 
139  function(function) {
140  }
141 
142  void handle(EventPtr event) {
143  this->function(boost::static_pointer_cast<T>(event->getData()));
144  }
145 protected:
146  DataFunction function;
147 };
148 
156 template<class DataType>
157 class DataHandler: public Handler {
158 public:
159  virtual ~DataHandler() {
160  }
161 
162  void handle(EventPtr event) {
163  notify(boost::static_pointer_cast<DataType>(event->getData()));
164  }
165 
166  virtual void notify(boost::shared_ptr<DataType> data) = 0;
167 
168 };
169 
176 class RSB_EXPORT FilteringHandler : public Handler {
177 public:
179  HandlerPtr next)
180  : filter(filter), next(next) {
181  }
182 
183  void handle(EventPtr event) {
184  if (this->filter->match(event)) {
185  this->next->handle(event);
186  }
187  }
188 protected:
191 };
192 
193 }