Event.h
Go to the documentation of this file.
1 
20 #ifndef EVENT_H_6174D929
21 #define EVENT_H_6174D929
22 
23 #include "uscxml/messages/Data.h"
24 #include "uscxml/util/UUID.h"
25 
26 #define ERROR_PLATFORM_THROW(msg) \
27  uscxml::ErrorEvent e; \
28  e.name = "error.platform"; \
29  e.data.compound["cause"] = uscxml::Data(msg, uscxml::Data::VERBATIM); \
30  e.data.compound["file"] = uscxml::Data(uscxml::toStr(__FILE__), uscxml::Data::VERBATIM); \
31  e.data.compound["line"] = uscxml::Data(uscxml::toStr(__LINE__), uscxml::Data::INTERPRETED); \
32  throw e; \
33 
34 #define ERROR_EXECUTION(identifier, cause) \
35  uscxml::ErrorEvent identifier; \
36  identifier.data.compound["cause"] = uscxml::Data(cause, uscxml::Data::VERBATIM); \
37  identifier.data.compound["file"] = uscxml::Data(uscxml::toStr(__FILE__), uscxml::Data::VERBATIM); \
38  identifier.data.compound["line"] = uscxml::Data(uscxml::toStr(__LINE__), uscxml::Data::INTERPRETED); \
39  identifier.name = "error.execution"; \
40  identifier.eventType = uscxml::Event::PLATFORM;
41 
42 #define ERROR_EXECUTION2(identifier, cause, node) \
43  uscxml::ErrorEvent identifier; \
44  identifier.data.compound["cause"] = uscxml::Data(cause, uscxml::Data::VERBATIM); \
45  identifier.data.compound["file"] = uscxml::Data(uscxml::toStr(__FILE__), uscxml::Data::VERBATIM); \
46  identifier.data.compound["line"] = uscxml::Data(uscxml::toStr(__LINE__), uscxml::Data::INTERPRETED); \
47  identifier.name = "error.execution"; \
48  identifier.data.compound["xpath"] = uscxml::Data(DOMUtils::xPathForNode(node), uscxml::Data::VERBATIM); \
49  identifier.eventType = uscxml::Event::PLATFORM;
50 
51 #define ERROR_COMMUNICATION(identifier, cause) \
52  uscxml::ErrorEvent identifier; \
53  identifier.data.compound["cause"] = uscxml::Data(cause, uscxml::Data::VERBATIM); \
54  identifier.data.compound["file"] = uscxml::Data(uscxml::toStr(__FILE__), uscxml::Data::VERBATIM); \
55  identifier.data.compound["line"] = uscxml::Data(uscxml::toStr(__LINE__), uscxml::Data::INTERPRETED); \
56  identifier.name = "error.communication"; \
57  identifier.eventType = uscxml::Event::PLATFORM;
58 
59 #define ERROR_COMMUNICATION2(identifier, cause, node) \
60  uscxml::ErrorEvent identifier; \
61  identifier.data.compound["cause"] = uscxml::Data(cause, uscxml::Data::VERBATIM); \
62  identifier.data.compound["file"] = uscxml::Data(uscxml::toStr(__FILE__), uscxml::Data::VERBATIM); \
63  identifier.data.compound["line"] = uscxml::Data(uscxml::toStr(__LINE__), uscxml::Data::INTERPRETED); \
64  identifier.name = "error.communication"; \
65  identifier.data.compound["xpath"] = uscxml::Data(DOMUtils::xPathForNode(node), uscxml::Data::VERBATIM); \
66  identifier.eventType = uscxml::Event::PLATFORM;
67 
68 #define ERROR_EXECUTION_THROW(cause) \
69 {\
70  ERROR_EXECUTION(exc, cause); \
71  throw exc;\
72 }
73 
74 #define ERROR_EXECUTION_THROW2(cause, node) \
75 {\
76  ERROR_EXECUTION2(exc, cause, node); \
77  throw exc;\
78 }
79 
80 #define ERROR_COMMUNICATION_THROW(cause) \
81 {\
82  ERROR_COMMUNICATION(exc, cause); \
83  throw exc;\
84 }
85 
86 #define ERROR_COMMUNICATION_THROW2(cause, node) \
87 {\
88  ERROR_COMMUNICATION(exc, cause, node); \
89  throw exc;\
90 }
91 
92 namespace uscxml {
93 
94 class USCXML_API Event {
95 public:
96  enum Type {
97  INTERNAL = 1,
98  EXTERNAL = 2,
99  PLATFORM = 3
100  };
101 
102  Event() : eventType(INTERNAL), hideSendId(false), uuid(UUID::getUUID()) {}
103  explicit Event(const std::string& name, Type type = INTERNAL) : name(name), eventType(type), hideSendId(false) {}
104  static Event fromData(const Data& data);
105 
106  bool operator< (const Event& other) const {
107  return this < &other;
108  }
109 
110  bool operator==(const Event& other) const {
111  return (this->name == other.name &&
112  this->sendid == other.sendid &&
113  this->invokeid == other.invokeid &&
114  this->data == other.data);
115  }
116  bool operator!=(const Event& other) const {
117  return !(*this == other);
118  }
119 
120  operator bool() {
121  return name.size() > 0;
122  }
123 
124  operator Data();
125 
126  operator std::string() {
127  std::stringstream ss;
128  ss << *this;
129  return ss.str();
130  }
131 
132  typedef std::multimap<std::string, Data> params_t;
133  typedef std::map<std::string, Data> namelist_t;
134 
135  static bool getParam(const params_t& params, const std::string& name, Data& target) {
136  if (params.find(name) != params.end()) {
137  target = params.find(name)->second;
138  return true;
139  }
140  return false;
141  }
142 
143  static bool getParam(const params_t& params, const std::string& name, std::list<Data>& target) {
144  if (params.find(name) != params.end()) {
145  std::pair<params_t::const_iterator, params_t::const_iterator> rangeIter = params.equal_range(name);
146  while(rangeIter.first != rangeIter.second) {
147  target.push_back(rangeIter.first->second);
148  rangeIter.first++;
149  }
150  return true;
151  }
152  return false;
153  }
154 
155  template <typename T> static bool getParam(const params_t& params, const std::string& name, T& target) {
156  if (params.find(name) != params.end()) {
157  target = strTo<T>(params.find(name)->second.atom);
158  return true;
159  }
160  return false;
161  }
162 
163  static bool getParam(const params_t& params, const std::string& name, bool& target) {
164  if (params.find(name) != params.end()) {
165  target = true;
166  if (iequals(params.find(name)->second.atom, "false")) {
167  target = false;
168  } else if(iequals(params.find(name)->second.atom, "off")) {
169  target = false;
170  } else if(iequals(params.find(name)->second.atom, "no")) {
171  target = false;
172  } else if(iequals(params.find(name)->second.atom, "0")) {
173  target = false;
174  }
175  return true;
176  }
177  return false;
178  }
179 
180  template <typename T> static bool getParam(const params_t& params, const std::string& name, std::list<T>& target) {
181  if (params.find(name) != params.end()) {
182  std::pair<params_t::const_iterator, params_t::const_iterator> rangeIter = params.equal_range(name);
183  while(rangeIter.first != rangeIter.second) {
184  target.push_back(strTo<T>(rangeIter.first->second.atom));
185  rangeIter.first++;
186  }
187  return true;
188  }
189  return false;
190  }
191 
192  std::string raw;
193  std::string name;
194  Type eventType;
195  std::string origin;
196  std::string origintype;
197  std::string sendid;
198  bool hideSendId; // sendid is assumed to be undef with some ecma tests
199  std::string invokeid;
200  Data data;
201  std::map<std::string, Data> namelist;
202  std::multimap<std::string, Data> params;
203  std::string uuid; // the sendid is not necessarily unique!
204 
205  friend USCXML_API std::ostream& operator<< (std::ostream& os, const Event& event);
206 };
207 
208 USCXML_API std::ostream& operator<< (std::ostream& os, const Event& event);
209 
210 
211 class USCXML_API ErrorEvent : public Event {
212 public:
213  ErrorEvent() : Event() {}
214  ErrorEvent(const std::string& msg) : Event("error.platform") {
215  data.compound["msg"] = Data(msg, Data::VERBATIM);
216  }
217 };
218 
219 }
220 
221 
222 
223 #endif /* end of include guard: EVENT_H_6174D929 */
Definition: Breakpoint.cpp:26
Definition: Event.h:211
Definition: Event.h:94
Definition: Data.h:44