RSB  0.10.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 #include <boost/thread/mutex.hpp>
34 
35 #include <rsc/runtime/TypeStringTools.h>
36 #include <rsc/logging/Logger.h>
37 
38 #include "rsb/rsbexports.h"
39 
40 #include "Event.h"
41 #include "QualityOfServiceSpec.h"
42 #include "Participant.h"
43 
45 
46 #include "transport/Connector.h"
47 
48 namespace rsb {
49 
59 class AnyType {
60 };
61 
62 namespace detail {
63 
64 template<typename T>
65 struct TypeName {
66  std::string operator()() const {
67  return rsc::runtime::typeName<T>();
68  }
69 };
70 
71 template<>
72 struct TypeName<AnyType> {
73  std::string operator()() const {
74  return "";
75  }
76 };
77 
78 }
79 
95 class RSB_EXPORT InformerBase: public Participant {
96 public:
97  template<typename T>
98  struct DataPtr {
99  typedef boost::shared_ptr<T> type;
100  };
101 
102  InformerBase(const std::vector<transport::OutConnectorPtr>& connectors,
103  const Scope& scope, const ParticipantConfig& config,
104  const std::string& defaultType);
105 
106  virtual ~InformerBase();
107 
108  void printContents(std::ostream& stream) const;
109 
116  std::string getType() const;
117 
124  void setQualityOfSerivceSpecs(const QualityOfServiceSpec& specs);
125 
145  template<class T1>
146  EventPtr publish(boost::shared_ptr<T1> data,
147  std::string type = rsc::runtime::typeName<T1>()) {
148  VoidPtr p = boost::static_pointer_cast<void>(data);
149  return publish(p, type);
150  }
151 
152  template<class T1>
153  EventPtr uncheckedPublish(boost::shared_ptr<T1> data,
154  const std::string& type = rsc::runtime::typeName<T1>()) {
155  VoidPtr p = boost::static_pointer_cast<void>(data);
156  return uncheckedPublish(p, type);
157  }
158 
164  virtual EventPtr createEvent() const;
165 
178  EventPtr publish(VoidPtr data, const std::string& type);
179  EventPtr uncheckedPublish(VoidPtr data, const std::string& type);
180 
194  EventPtr publish(EventPtr event);
195 
196 protected:
197  void checkedPublish(EventPtr event);
198  void uncheckedPublish(EventPtr event);
199 
200  boost::uint32_t nextSequenceNumber();
201 
202  std::string defaultType;
204 
205 private:
206  rsc::logging::LoggerPtr logger;
207  boost::uint32_t currentSequenceNumber;
208  boost::mutex sequenceNumberMutex;
209 };
210 
211 typedef boost::shared_ptr<InformerBase> InformerBasePtr;
212 
229 template<class T>
230 class Informer: public InformerBase {
231 public:
232 
236  typedef boost::shared_ptr<Informer<T> > Ptr;
237 
241  typedef boost::shared_ptr<T> DataPtr;
242 
257  Informer(const std::vector<transport::OutConnectorPtr>& connectors,
258  const Scope& scope, const ParticipantConfig& config,
259  const std::string& type = detail::TypeName<T>()()) :
260  InformerBase(connectors, scope, config, type),
261  logger(rsc::logging::Logger::getLogger(getClassName())) {
262  }
263 
264  virtual ~Informer() {
265  }
266 
267  std::string getClassName() const {
268  return rsc::runtime::typeName<Informer<T> >();
269  }
270 
281  event->setType(getType());
282  return event;
283  }
284 
292  EventPtr publish(boost::shared_ptr<T> data) {
293  VoidPtr p = boost::static_pointer_cast<void>(data);
294  return InformerBase::publish(p, this->getType());
295  }
296 
297  template<class T1>
298  EventPtr publish(boost::shared_ptr<T1> data,
299  std::string type = rsc::runtime::typeName(typeid(T1))) {
300  return InformerBase::publish(data, type);
301  }
302 
304  return InformerBase::publish(event);
305  }
306 private:
307  rsc::logging::LoggerPtr logger;
308 };
309 
310 }