RSB  0.17.0
Event.cpp
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 #include "Event.h"
28 
29 #include <ostream>
30 
31 #include <boost/format.hpp>
32 
33 #include <rsc/runtime/ContainerIO.h>
34 #include <rsc/misc/IllegalStateException.h>
35 
36 #include "EventId.h"
37 #include "MetaData.h"
38 #include "Scope.h"
39 
40 using namespace std;
41 using namespace boost;
42 
43 namespace rsb {
44 
45 class Event::Impl {
46 public:
49 
51 
52  // is this a single type, a hierarchy or a set?
53  std::string type;
54 
55  std::string method;
56 
57  std::set<EventId> causes;
58 
60 };
61 
62 Event::Event() :
63  d(new Impl()) {
64  d->scope.reset(new Scope);
65 }
66 
67 Event::Event(const Event& event) :
68  d(new Impl(*event.d)) {
69 
70 }
71 
72 Event::Event(ScopePtr scope, boost::shared_ptr<void> payload,
73  const string& type, const string& method) :
74  d(new Impl()) {
75  d->scope = scope;
76  d->content = payload;
77  d->type = type;
78  d->method = method;
79 }
80 
81 Event::Event(Scope scope, boost::shared_ptr<void> payload, const string& type,
82  const string& method) :
83  d(new Impl()) {
84  d->scope.reset(new Scope(scope));
85  d->content = payload;
86  d->type = type;
87  d->method = method;
88 }
89 
91 }
92 
93 string Event::getClassName() const {
94  return "Event";
95 }
96 
97 void Event::printContents(ostream& stream) const {
98  stream << "id = ";
99  if (d->id) {
100  stream << d->id;
101  } else {
102  stream << "UNSPECIFIED";
103  }
104  stream << ", type = " << d->type << ", scope = ";
105  if (d->scope) {
106  stream << *d->scope;
107  } else {
108  stream << "UNSPECIFIED";
109  }
110  stream << ", metaData = " << d->metaData << ", method = " << d->method;
111  stream << ", causes = " << d->causes;
112 }
113 
114 boost::uint64_t Event::getSequenceNumber() const {
115  return getId().getSequenceNumber();
116 }
117 
119  if (!d->id) {
120  throw rsc::misc::IllegalStateException(
121  "The event does not contain id information.");
122  }
123  return *d->id;
124 }
125 
126 EventId Event::getEventId() const {
127  return getId();
128 }
129 
130 void Event::setId(const rsc::misc::UUID& senderId,
131  const boost::uint32_t& sequenceNumber) {
132  d->id.reset(new EventId(senderId, sequenceNumber));
133 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
134  d->metaData.setSenderId(senderId);
135 #pragma GCC diagnostic pop
136 }
137 
138 void Event::setEventId(const rsc::misc::UUID& senderId,
139  const boost::uint32_t& sequenceNumber) {
140  setId(senderId, sequenceNumber);
141 }
142 
144  d->scope = s;
145 }
146 
147 void Event::setScope(const Scope& s) {
148  d->scope = ScopePtr(new Scope(s));
149 }
150 
152  return d->scope;
153 }
154 
156  return *d->scope;
157 }
158 
160  d->content = data;
161 }
162 
164  return d->content;
165 }
166 
167 string Event::getType() const {
168  return d->type;
169 }
170 
171 void Event::setType(const string& t) {
172  d->type = t;
173 }
174 
175 bool Event::addCause(const EventId& id) {
176  return d->causes.insert(id).second;
177 }
178 
179 bool Event::removeCause(const EventId& id) {
180  return d->causes.erase(id) > 0;
181 }
182 
183 bool Event::isCause(const EventId& id) const {
184  return this->d->causes.find(id) != this->d->causes.end();
185 }
186 
187 set<EventId> Event::getCauses() const {
188  return d->causes;
189 }
190 
191 string Event::getMethod() const {
192  return d->method;
193 }
194 
195 void Event::setMethod(const string& method) {
196  d->method = method;
197 }
198 
200  return d->metaData;
201 }
202 
204  return d->metaData;
205 }
206 
207 void Event::setMetaData(const MetaData& metaData) {
208  d->metaData = metaData;
209 }
210 
211 }
std::set< EventId > causes
Definition: Event.cpp:57
boost::shared_ptr< Scope > ScopePtr
Definition: Event.h:48
boost::shared_ptr< void > VoidPtr
Definition: Event.h:51
void setType(const std::string &type)
Definition: Event.cpp:171
VoidPtr getData()
Definition: Event.cpp:163
void setData(VoidPtr d)
Definition: Event.cpp:159
std::string type
Definition: Event.cpp:53
EventIdPtr id
Definition: Event.cpp:47
boost::uint32_t getSequenceNumber() const
Definition: EventId.cpp:47
MetaData getMetaData() const
Returns a copy of the current meta-data state of this event.
Definition: Event.cpp:199
ScopePtr getScopePtr() const
Definition: Event.cpp:151
MetaData & mutableMetaData()
Returns an in-place mutable version of the event&#39;s meta-data.
Definition: Event.cpp:203
Basic message that is exchanged between informers and listeners.
Definition: Event.h:60
bool addCause(const EventId &id)
Adds the id of one event to the causes of this event.
Definition: Event.cpp:175
std::string getType() const
Definition: Event.cpp:167
void setScope(const Scope &scope)
Definition: Event.cpp:147
STL namespace.
void setScopePtr(ScopePtr scope)
Definition: Event.cpp:143
Scope getScope() const
Definition: Event.cpp:155
VoidPtr content
Definition: Event.cpp:50
boost::scoped_ptr< Impl > d
Definition: Event.h:259
std::string method
Definition: Event.cpp:55
std::string getClassName() const
Definition: Event.cpp:93
ScopePtr scope
Definition: Event.cpp:48
MetaData metaData
Definition: Event.cpp:59
std::set< EventId > getCauses() const
Returns all causing events marked so far.
Definition: Event.cpp:187
Framework-supplied meta data attached to each event that give information e.g.
Definition: MetaData.h:55
std::string getMethod() const
Returns the method associated with this event.
Definition: Event.cpp:191
void setMethod(const std::string &method)
Sets the method associated with this event.
Definition: Event.cpp:195
bool removeCause(const EventId &id)
Removes a causing event from the set of causes for this event.
Definition: Event.cpp:179
bool isCause(const EventId &id) const
Tells whether the id of one event is already marked as a cause of this event.
Definition: Event.cpp:183
void setId(const rsc::misc::UUID &senderId, const boost::uint32_t &sequenceNumber)
Sets all information necessary to generate an id for this event.
Definition: Event.cpp:130
boost::shared_ptr< EventId > EventIdPtr
Definition: EventId.h:82
void setMetaData(const MetaData &metaData)
Replaces the event&#39;s meta-data with a new instance.
Definition: Event.cpp:207
virtual ~Event()
Definition: Event.cpp:90
EventId getId() const
Returns the id of this event.
Definition: Event.cpp:118
A unique ID for events in RSB.
Definition: EventId.h:48
Scope is a descriptor for a hierarchical channel of the unified bus.
Definition: Scope.h:46
void printContents(std::ostream &stream) const
Definition: Event.cpp:97