summaryrefslogtreecommitdiffstats
path: root/src/uscxml/messages/Event.h
blob: b774f8aa9a441df6fad2f566825135cb86dfeab9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/**
 *  @file
 *  @author     2012-2014 Stefan Radomski (stefan.radomski@cs.tu-darmstadt.de)
 *  @copyright  Simplified BSD
 *
 *  @cond
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the FreeBSD license as published by the FreeBSD
 *  project.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 *  You should have received a copy of the FreeBSD license along with this
 *  program. If not, see <http://www.opensource.org/licenses/bsd-license>.
 *  @endcond
 */

#ifndef EVENT_H_6174D929
#define EVENT_H_6174D929

#include "uscxml/messages/Data.h"
#include "uscxml/util/UUID.h"

#define ERROR_PLATFORM_THROW(msg) \
	uscxml::ErrorEvent e; \
	e.name = "error.platform"; \
	e.data.compound["cause"] = uscxml::Data(msg, uscxml::Data::VERBATIM); \
    e.data.compound["file"] = uscxml::Data(uscxml::toStr(__FILE__), uscxml::Data::VERBATIM); \
    e.data.compound["line"] = uscxml::Data(uscxml::toStr(__LINE__), uscxml::Data::INTERPRETED); \
	throw e; \

#define ERROR_EXECUTION(identifier, cause) \
	uscxml::ErrorEvent identifier; \
	identifier.data.compound["cause"] = uscxml::Data(cause, uscxml::Data::VERBATIM); \
    identifier.data.compound["file"] = uscxml::Data(uscxml::toStr(__FILE__), uscxml::Data::VERBATIM); \
    identifier.data.compound["line"] = uscxml::Data(uscxml::toStr(__LINE__), uscxml::Data::INTERPRETED); \
	identifier.name = "error.execution"; \
	identifier.eventType = uscxml::Event::PLATFORM;

#define ERROR_EXECUTION2(identifier, cause, node) \
	uscxml::ErrorEvent identifier; \
	identifier.data.compound["cause"] = uscxml::Data(cause, uscxml::Data::VERBATIM); \
    identifier.data.compound["file"] = uscxml::Data(uscxml::toStr(__FILE__), uscxml::Data::VERBATIM); \
    identifier.data.compound["line"] = uscxml::Data(uscxml::toStr(__LINE__), uscxml::Data::INTERPRETED); \
	identifier.name = "error.execution"; \
	identifier.data.compound["xpath"] = uscxml::Data(DOMUtils::xPathForNode(node), uscxml::Data::VERBATIM); \
	identifier.eventType = uscxml::Event::PLATFORM;

#define ERROR_COMMUNICATION(identifier, cause) \
	uscxml::ErrorEvent identifier; \
	identifier.data.compound["cause"] = uscxml::Data(cause, uscxml::Data::VERBATIM); \
    identifier.data.compound["file"] = uscxml::Data(uscxml::toStr(__FILE__), uscxml::Data::VERBATIM); \
    identifier.data.compound["line"] = uscxml::Data(uscxml::toStr(__LINE__), uscxml::Data::INTERPRETED); \
	identifier.name = "error.communication"; \
	identifier.eventType = uscxml::Event::PLATFORM;

#define ERROR_COMMUNICATION2(identifier, cause, node) \
	uscxml::ErrorEvent identifier; \
	identifier.data.compound["cause"] = uscxml::Data(cause, uscxml::Data::VERBATIM); \
    identifier.data.compound["file"] = uscxml::Data(uscxml::toStr(__FILE__), uscxml::Data::VERBATIM); \
    identifier.data.compound["line"] = uscxml::Data(uscxml::toStr(__LINE__), uscxml::Data::INTERPRETED); \
	identifier.name = "error.communication"; \
	identifier.data.compound["xpath"] = uscxml::Data(DOMUtils::xPathForNode(node), uscxml::Data::VERBATIM); \
	identifier.eventType = uscxml::Event::PLATFORM;

#define ERROR_EXECUTION_THROW(cause) \
{\
	ERROR_EXECUTION(exc, cause); \
	throw exc;\
}

#define ERROR_EXECUTION_THROW2(cause, node) \
{\
	ERROR_EXECUTION2(exc, cause, node); \
	throw exc;\
}

#define ERROR_COMMUNICATION_THROW(cause) \
{\
	ERROR_COMMUNICATION(exc, cause); \
	throw exc;\
}

#define ERROR_COMMUNICATION_THROW2(cause, node) \
{\
	ERROR_COMMUNICATION(exc, cause, node); \
	throw exc;\
}

namespace uscxml {

class USCXML_API Event {
public:
	enum Type {
		INTERNAL = 1,
		EXTERNAL = 2,
		PLATFORM = 3
	};

	Event() : eventType(INTERNAL), hideSendId(false), uuid(UUID::getUUID()) {}
	explicit Event(const std::string& name, Type type = INTERNAL) : name(name), eventType(type), hideSendId(false) {}
	static Event fromData(const Data& data);

	bool operator< (const Event& other) const     {
		return this < &other;
	}

	bool operator==(const Event& other) const     {
		return (this->name == other.name &&
		        this->sendid == other.sendid &&
		        this->invokeid == other.invokeid &&
		        this->data == other.data);
	}
	bool operator!=(const Event& other) const     {
		return !(*this == other);
	}

	operator bool() {
		return name.size() > 0;
	}

	operator Data();

	operator std::string() {
		std::stringstream ss;
		ss << *this;
		return ss.str();
	}

	typedef std::multimap<std::string, Data> params_t;
	typedef std::map<std::string, Data> namelist_t;

	static bool getParam(const params_t& params, const std::string& name, Data& target) {
		if (params.find(name) != params.end()) {
			target = params.find(name)->second;
			return true;
		}
		return false;
	}

	static bool getParam(const params_t& params, const std::string& name, std::list<Data>& target) {
		if (params.find(name) != params.end()) {
			std::pair<params_t::const_iterator, params_t::const_iterator> rangeIter = params.equal_range(name);
			while(rangeIter.first != rangeIter.second) {
				target.push_back(rangeIter.first->second);
				rangeIter.first++;
			}
			return true;
		}
		return false;
	}

	template <typename T> static bool getParam(const params_t& params, const std::string& name, T& target) {
		if (params.find(name) != params.end()) {
			target = strTo<T>(params.find(name)->second.atom);
			return true;
		}
		return false;
	}

	static bool getParam(const params_t& params, const std::string& name, bool& target) {
		if (params.find(name) != params.end()) {
			target = true;
			if (iequals(params.find(name)->second.atom, "false")) {
				target = false;
			} else if(iequals(params.find(name)->second.atom, "off")) {
				target = false;
			} else if(iequals(params.find(name)->second.atom, "no")) {
				target = false;
			} else if(iequals(params.find(name)->second.atom, "0")) {
				target = false;
			}
			return true;
		}
		return false;
	}

	template <typename T> static bool getParam(const params_t& params, const std::string& name, std::list<T>& target) {
		if (params.find(name) != params.end()) {
			std::pair<params_t::const_iterator, params_t::const_iterator> rangeIter = params.equal_range(name);
			while(rangeIter.first != rangeIter.second) {
				target.push_back(strTo<T>(rangeIter.first->second.atom));
				rangeIter.first++;
			}
			return true;
		}
		return false;
	}

	std::string raw;
	std::string name;
	Type eventType;
	std::string origin;
	std::string origintype;
	std::string sendid;
	bool hideSendId; // sendid is assumed to be undef with some ecma tests
	std::string invokeid;
	Data data;
	std::map<std::string, Data> namelist;
	std::multimap<std::string, Data> params;
	std::string uuid; // the sendid is not necessarily unique!

	friend USCXML_API std::ostream& operator<< (std::ostream& os, const Event& event);
};

USCXML_API std::ostream& operator<< (std::ostream& os, const Event& event);


class USCXML_API ErrorEvent : public Event {
public:
	ErrorEvent() : Event() {}
	ErrorEvent(const std::string& msg) : Event("error.platform") {
		data.compound["msg"] = Data(msg, Data::VERBATIM);
	}
};

}



#endif /* end of include guard: EVENT_H_6174D929 */