summaryrefslogtreecommitdiffstats
path: root/src/uscxml/messages/Data.h
blob: 6ab3bec974b4abd5f5d4ab2c3477b3b204b82e55 (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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/**
 *  @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 DATA_H_09E4D8E5
#define DATA_H_09E4D8E5

#include <list>
#include <map>
#include <memory>

#include "uscxml/config.h"
#include "uscxml/Common.h"
#include "uscxml/util/Convenience.h"
#include "uscxml/messages/Blob.h"

//#include <xercesc/dom/DOMDocument.hpp>

// forward declare
namespace XERCESC_NS {
	class DOMDocument;
	class DOMNode;
}

namespace uscxml {

static int _dataIndentation = 1;

class USCXML_API Data {
public:
	enum Type {
		VERBATIM,
		INTERPRETED,
	};

	Data() : node(NULL), type(INTERPRETED) {}

	Data(const char* data, size_t size, const std::string& mimeType, bool adopt = false);

	// convenience constructors
	Data(bool atom) : node(NULL), type(VERBATIM) {
		if (atom) {
			this->atom = "true";
		} else {
			this->atom = "false";
		}
	}

	//    template <typename T> Data(T value, Type type = INTERPRETED) : atom(toStr(value)), type(type) {}

	// we will have to drop this constructor as it interferes with operator Data() and requires C++11
	template <typename T>
	Data(T value, typename std::enable_if<! std::is_base_of<Data, T>::value>::type* = nullptr)
		: node(NULL), atom(toStr(value)), type(VERBATIM) {}
	template <typename T>
	Data(T value, Type type, typename std::enable_if<! std::is_base_of<Data, T>::value>::type* = nullptr)
		: node(NULL), atom(toStr(value)), type(type) {}

	~Data() {}

	bool empty() const {
		bool hasContent = (atom.length() > 0 || !compound.empty() || !array.empty() || binary || node);
		return !hasContent;
	}

	bool operator<(const Data& other) const {
		if (other.atom != atom)
			return other.atom < atom;
		if (other.array != array)
			return other.array < array;
		if (other.compound != compound)
			return other.compound < compound;
		if (other.node != node)
			return other.node < node;
		if (other.binary != binary)
			return other.binary < binary;
		if (other.type != type)
			return other.type < type;

		return false;
	}

	void merge(const Data& other);

	bool hasKey(const std::string& key) const {
		return (!compound.empty() && compound.find(key) != compound.end());
	}

	Data& operator[](const std::string& key) {
		return operator[](key.c_str());
	}

	const Data& operator[](const std::string& key) const {
		return operator[](key.c_str());
	}

	Data& operator[](const char* key) {
		return compound[key];
	}

	const Data& operator[](const char* key) const {
		return compound.at(key);
	}

	Data& operator[](const size_t index) {
		while(array.size() < index) {
			array.push_back(Data("", Data::VERBATIM));
		}
		std::list<Data>::iterator arrayIter = array.begin();
		for (size_t i = 0; i < index; i++, arrayIter++) {}
		return *arrayIter;
	}

	const Data at(const std::string& key) const {
		return at(key.c_str());
	}

	const Data at(const char* key) const {
		if (hasKey(key))
			return compound.at(key);
		Data data;
		return data;
	}

	const Data item(const size_t index) const {
		if (array.size() > index) {
			std::list<Data>::const_iterator arrayIter = array.begin();
			for (size_t i = 0; i < index; i++, arrayIter++) {}
			return *arrayIter;
		}
		Data data;
		return data;
	}

	void put(std::string key, const Data& data) {
		compound[key] = data;
	}

	void put(size_t index, const Data& data) {
		this[index] = data;
	}

	bool operator==(const Data &other) const {
		return (*this < other || other < *this);
	}

	bool operator!=(const Data &other) const {
		return !(*this == other);
	}

	operator std::string() const {
		return atom;
	}

	operator std::map<std::string, Data>() {
		return compound;
	}

	operator std::list<Data>() {
		return array;
	}

	static Data fromJSON(const std::string& jsonString);
	static std::string toJSON(const Data& data);
	std::string asJSON() const;


	std::map<std::string, Data> getCompound() {
		return compound;
	}
	void setCompound(const std::map<std::string, Data>& compound) {
		this->compound = compound;
	}

	std::list<Data> getArray() {
		return array;
	}
	void setArray(const std::list<Data>& array) {
		this->array = array;
	}

	std::string getAtom() const {
		return atom;
	}
	void setAtom(const std::string& atom) {
		this->atom = atom;
	}

	Blob getBinary() {
		return this->binary;
	}
	void setBinary(const Blob& binary) {
		this->binary = binary;
	}

	Type getType() {
		return type;
	}
	void setType(const Type type) {
		this->type = type;
	}

#ifdef SWIGIMPORTED
protected:
#endif

	XERCESC_NS::DOMNode* node;
	std::shared_ptr<XERCESC_NS::DOMDocument*> adoptedDoc;
	std::map<std::string, Data> compound;
	std::list<Data> array;
	std::string atom;
	Blob binary;
	Type type;

protected:
	friend USCXML_API std::ostream& operator<< (std::ostream& os, const Data& data);
};

USCXML_API std::ostream& operator<< (std::ostream& os, const Data& data);

}

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