RSB  0.9.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 getEventId().getSequenceNumber();
116 }
117 
118 rsc::misc::UUID Event::getId() const {
119  return getEventId().getAsUUID();
120 }
121 
123  if (!d->id) {
124  throw rsc::misc::IllegalStateException(
125  "The event does not contain id information.");
126  }
127  return *d->id;
128 }
129 
130 void Event::setEventId(const rsc::misc::UUID& senderId,
131  const boost::uint32_t& sequenceNumber) {
132  d->id.reset(new EventId(senderId, sequenceNumber));
133  d->metaData.setSenderId(senderId);
134 }
135 
137  d->scope = s;
138 }
139 
140 void Event::setScope(const Scope& s) {
141  d->scope = ScopePtr(new Scope(s));
142 }
143 
145  return d->scope;
146 }
147 
149  return *d->scope;
150 }
151 
153  d->content = data;
154 }
155 
157  return d->content;
158 }
159 
160 string Event::getType() const {
161  return d->type;
162 }
163 
164 void Event::setType(const string& t) {
165  d->type = t;
166 }
167 
168 bool Event::addCause(const EventId& id) {
169  return d->causes.insert(id).second;
170 }
171 
172 bool Event::removeCause(const EventId& id) {
173  return d->causes.erase(id) > 0;
174 }
175 
176 bool Event::isCause(const EventId& id) const {
177  return d->causes.count(id) > 0;
178 }
179 
180 set<EventId> Event::getCauses() const {
181  return d->causes;
182 }
183 
184 string Event::getMethod() const {
185  return d->method;
186 }
187 
188 void Event::setMethod(const string& method) {
189  d->method = method;
190 }
191 
193  return d->metaData;
194 }
195 
197  return d->metaData;
198 }
199 
200 void Event::setMetaData(const MetaData& metaData) {
201  d->metaData = metaData;
202 }
203 
204 }
std::set< EventId > causes
Definition: Event.cpp:57
boost::shared_ptr< Scope > ScopePtr
Definition: Event.h:49
boost::shared_ptr< void > VoidPtr
Definition: Event.h:52
void setType(const std::string &type)
Definition: Event.cpp:164
VoidPtr getData()
Definition: Event.cpp:156
void setEventId(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
void setData(VoidPtr d)
Definition: Event.cpp:152
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:192
ScopePtr getScopePtr() const
Definition: Event.cpp:144
MetaData & mutableMetaData()
Returns an in-place mutable version of the event's meta-data.
Definition: Event.cpp:196
Basic message that is exchanged between informers and listeners.
Definition: Event.h:61
EventId getEventId() const
Returns the id of this event.
Definition: Event.cpp:122
bool addCause(const EventId &id)
Adds the id of one event to the causes of this event.
Definition: Event.cpp:168
std::string getType() const
Definition: Event.cpp:160
void setScope(const Scope &scope)
Definition: Event.cpp:140
boost::shared_ptr< EventId > EventIdPtr
Definition: Event.h:46
void setScopePtr(ScopePtr scope)
Definition: Event.cpp:136
Scope getScope() const
Definition: Event.cpp:148
VoidPtr content
Definition: Event.cpp:50
rsc::misc::UUID getAsUUID() const
Definition: EventId.cpp:51
boost::scoped_ptr< Impl > d
Definition: Event.h:246
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:180
Framework-supplied meta data attached to each event that give information e.g.
Definition: MetaData.h:54
std::string getMethod() const
Returns the method associated with this event.
Definition: Event.cpp:184
void setMethod(const std::string &method)
Sets the method associated with this event.
Definition: Event.cpp:188
bool removeCause(const EventId &id)
Removes a causing event from the set of causes for this event.
Definition: Event.cpp:172
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:176
void setMetaData(const MetaData &metaData)
Replaces the event's meta-data with a new instance.
Definition: Event.cpp:200
virtual ~Event()
Definition: Event.cpp:90
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