RSB  0.7.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Informer.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 <string>
30 
31 #include <boost/cstdint.hpp>
32 #include <boost/shared_ptr.hpp>
33 
34 #include <rsc/runtime/TypeStringTools.h>
35 #include <rsc/logging/Logger.h>
36 
37 #include "rsb/rsbexports.h"
38 
39 #include "Event.h"
40 #include "QualityOfServiceSpec.h"
41 #include "Participant.h"
42 
44 
45 #include "transport/Connector.h"
46 
47 namespace rsb {
48 
58 class AnyType {
59 };
60 
61 namespace detail {
62 
63 template<typename T>
64 struct TypeName {
65  std::string operator()() const {
66  return rsc::runtime::typeName<T>();
67  }
68 };
69 
70 template<>
71 struct TypeName<AnyType> {
72  std::string operator()() const {
73  return "";
74  }
75 };
76 
77 }
78 
94 class RSB_EXPORT InformerBase: public Participant {
95 public:
96  template<typename T>
97  struct DataPtr {
98  typedef boost::shared_ptr<T> type;
99  };
100 
101  InformerBase(const std::vector<transport::OutConnectorPtr>& connectors,
102  const Scope& scope, const ParticipantConfig& config,
103  const std::string& defaultType);
104 
105  virtual ~InformerBase();
106 
107  void printContents(std::ostream& stream) const;
108 
115  std::string getType() const;
116 
123  void setQualityOfSerivceSpecs(const QualityOfServiceSpec& specs);
124 
144  template<class T1>
145  EventPtr publish(boost::shared_ptr<T1> data,
146  std::string type = rsc::runtime::typeName<T1>()) {
147  VoidPtr p = boost::static_pointer_cast<void>(data);
148  return publish(p, type);
149  }
150 
151  template<class T1>
152  EventPtr uncheckedPublish(boost::shared_ptr<T1> data,
153  const std::string& type = rsc::runtime::typeName<T1>()) {
154  VoidPtr p = boost::static_pointer_cast<void>(data);
155  return uncheckedPublish(p, type);
156  }
157 
163  virtual EventPtr createEvent() const;
164 
177  EventPtr publish(VoidPtr data, const std::string& type);
178  EventPtr uncheckedPublish(VoidPtr data, const std::string& type);
179 
193  EventPtr publish(EventPtr event);
194 
195 protected:
196  void checkedPublish(EventPtr event);
197  void uncheckedPublish(EventPtr event);
198 
199  boost::uint32_t nextSequenceNumber();
200 
201  std::string defaultType;
203  boost::uint32_t currentSequenceNumber;
204 private:
205  rsc::logging::LoggerPtr logger;
206 };
207 
208 typedef boost::shared_ptr<InformerBase> InformerBasePtr;
209 
226 template<class T>
227 class Informer: public InformerBase {
228 public:
229 
233  typedef boost::shared_ptr<Informer<T> > Ptr;
234 
238  typedef boost::shared_ptr<T> DataPtr;
239 
254  Informer(const std::vector<transport::OutConnectorPtr>& connectors,
255  const Scope& scope, const ParticipantConfig& config,
256  const std::string& type = detail::TypeName<T>()()) :
257  InformerBase(connectors, scope, config, type),
258  logger(rsc::logging::Logger::getLogger(getClassName())) {
259  }
260 
261  virtual ~Informer() {
262  }
263 
264  std::string getClassName() const {
265  return rsc::runtime::typeName<Informer<T> >();
266  }
267 
278  event->setType(getType());
279  return event;
280  }
281 
289  EventPtr publish(boost::shared_ptr<T> data) {
290  VoidPtr p = boost::static_pointer_cast<void>(data);
291  return InformerBase::publish(p, this->getType());
292  }
293 
294  template<class T1>
295  EventPtr publish(boost::shared_ptr<T1> data,
296  std::string type = rsc::runtime::typeName(typeid(T1))) {
297  return InformerBase::publish(data, type);
298  }
299 
301  return InformerBase::publish(event);
302  }
303 private:
304  rsc::logging::LoggerPtr logger;
305 };
306 
307 }