Data.h
Go to the documentation of this file.
1 
20 #ifndef DATA_H_09E4D8E5
21 #define DATA_H_09E4D8E5
22 
23 #include <list>
24 #include <map>
25 #include <memory>
26 
27 #include "uscxml/config.h"
28 #include "uscxml/Common.h"
30 #include "uscxml/messages/Blob.h"
31 
32 //#include <xercesc/dom/DOMDocument.hpp>
33 
34 // forward declare
35 namespace XERCESC_NS {
36  class DOMDocument;
37  class DOMNode;
38 }
39 
40 namespace uscxml {
41 
42 static int _dataIndentation = 1;
43 
44 class USCXML_API Data {
45 public:
46  enum Type {
47  VERBATIM,
48  INTERPRETED,
49  };
50 
51  Data() : node(NULL), type(INTERPRETED) {}
52 
53  Data(const char* data, size_t size, const std::string& mimeType, bool adopt = false);
54 
55  // convenience constructors
56  Data(bool atom) : node(NULL), type(VERBATIM) {
57  if (atom) {
58  this->atom = "true";
59  } else {
60  this->atom = "false";
61  }
62  }
63 
64  // template <typename T> Data(T value, Type type = INTERPRETED) : atom(toStr(value)), type(type) {}
65 
66  // we will have to drop this constructor as it interferes with operator Data() and requires C++11
67  template <typename T>
68  Data(T value, typename std::enable_if<! std::is_base_of<Data, T>::value>::type* = nullptr)
69  : node(NULL), atom(toStr(value)), type(VERBATIM) {}
70  template <typename T>
71  Data(T value, Type type, typename std::enable_if<! std::is_base_of<Data, T>::value>::type* = nullptr)
72  : node(NULL), atom(toStr(value)), type(type) {}
73 
74  ~Data() {}
75 
76  bool empty() const {
77  bool hasContent = (atom.length() > 0 || !compound.empty() || !array.empty() || binary || node);
78  return !hasContent;
79  }
80 
81  bool operator<(const Data& other) const {
82  if (other.atom != atom)
83  return other.atom < atom;
84  if (other.array != array)
85  return other.array < array;
86  if (other.compound != compound)
87  return other.compound < compound;
88  if (other.node != node)
89  return other.node < node;
90  if (other.binary != binary)
91  return other.binary < binary;
92  if (other.type != type)
93  return other.type < type;
94 
95  return false;
96  }
97 
98  void merge(const Data& other);
99 
100  bool hasKey(const std::string& key) const {
101  return (!compound.empty() && compound.find(key) != compound.end());
102  }
103 
104  Data& operator[](const std::string& key) {
105  return operator[](key.c_str());
106  }
107 
108  const Data& operator[](const std::string& key) const {
109  return operator[](key.c_str());
110  }
111 
112  Data& operator[](const char* key) {
113  return compound[key];
114  }
115 
116  const Data& operator[](const char* key) const {
117  return compound.at(key);
118  }
119 
120  Data& operator[](const size_t index) {
121  while(array.size() < index) {
122  array.push_back(Data("", Data::VERBATIM));
123  }
124  std::list<Data>::iterator arrayIter = array.begin();
125  for (size_t i = 0; i < index; i++, arrayIter++) {}
126  return *arrayIter;
127  }
128 
129  const Data at(const std::string& key) const {
130  return at(key.c_str());
131  }
132 
133  const Data at(const char* key) const {
134  if (hasKey(key))
135  return compound.at(key);
136  Data data;
137  return data;
138  }
139 
140  const Data item(const size_t index) const {
141  if (array.size() > index) {
142  std::list<Data>::const_iterator arrayIter = array.begin();
143  for (size_t i = 0; i < index; i++, arrayIter++) {}
144  return *arrayIter;
145  }
146  Data data;
147  return data;
148  }
149 
150  void put(std::string key, const Data& data) {
151  compound[key] = data;
152  }
153 
154  void put(size_t index, const Data& data) {
155  this[index] = data;
156  }
157 
158  bool operator==(const Data &other) const {
159  return (*this < other || other < *this);
160  }
161 
162  bool operator!=(const Data &other) const {
163  return !(*this == other);
164  }
165 
166  operator std::string() const {
167  return atom;
168  }
169 
170  operator std::map<std::string, Data>() {
171  return compound;
172  }
173 
174  operator std::list<Data>() {
175  return array;
176  }
177 
178  static Data fromJSON(const std::string& jsonString);
179  static std::string toJSON(const Data& data);
180  std::string asJSON() const;
181 
182 
183  std::map<std::string, Data> getCompound() {
184  return compound;
185  }
186  void setCompound(const std::map<std::string, Data>& compound) {
187  this->compound = compound;
188  }
189 
190  std::list<Data> getArray() {
191  return array;
192  }
193  void setArray(const std::list<Data>& array) {
194  this->array = array;
195  }
196 
197  std::string getAtom() const {
198  return atom;
199  }
200  void setAtom(const std::string& atom) {
201  this->atom = atom;
202  }
203 
204  Blob getBinary() {
205  return this->binary;
206  }
207  void setBinary(const Blob& binary) {
208  this->binary = binary;
209  }
210 
211  Type getType() {
212  return type;
213  }
214  void setType(const Type type) {
215  this->type = type;
216  }
217 
218 #ifdef SWIGIMPORTED
219 protected:
220 #endif
221 
222  XERCESC_NS::DOMNode* node;
223  std::shared_ptr<XERCESC_NS::DOMDocument*> adoptedDoc;
224  std::map<std::string, Data> compound;
225  std::list<Data> array;
226  std::string atom;
227  Blob binary;
228  Type type;
229 
230 protected:
231  friend USCXML_API std::ostream& operator<< (std::ostream& os, const Data& data);
232 };
233 
234 USCXML_API std::ostream& operator<< (std::ostream& os, const Data& data);
235 
236 }
237 
238 #endif /* end of include guard: DATA_H_09E4D8E5 */
Definition: InterpreterIssue.cpp:33
Definition: InterpreterIssue.h:31
Definition: Blob.h:65
Definition: Data.h:44