NymphRPC Remote Procedure Call Library
nymph_types.h
1 /*
2  nymph_types.h - Defines the NymphRPC data types.
3 
4  Revision 0
5 
6  Notes:
7  -
8 
9  History:
10  2017/06/24, Maya Posch : Initial version.
11 
12  (c) Nyanko.ws
13 */
14 
15 
16 #pragma once
17 #ifndef NYMPH_TYPES_H
18 #define NYMPH_TYPES_H
19 
20 #include <Poco/Poco.h>
21 #include <Poco/JSON/Object.h>
22 
23 #include <string>
24 #include <map>
25 #include <vector>
26 #include <cstdint>
27 
28 
29 enum NymphInternalTypes {
30  NYMPH_TYPE_NULL = 0x00,
31  NYMPH_TYPE_NONE = 0x01,
32  NYMPH_TYPE_BOOLEAN_FALSE = 0x02,
33  NYMPH_TYPE_BOOLEAN_TRUE = 0x03,
34  NYMPH_TYPE_UINT8 = 0x04,
35  NYMPH_TYPE_SINT8 = 0x05,
36  NYMPH_TYPE_UINT16 = 0x06,
37  NYMPH_TYPE_SINT16 = 0x07,
38  NYMPH_TYPE_UINT32 = 0x08,
39  NYMPH_TYPE_SINT32 = 0x09,
40  NYMPH_TYPE_UINT64 = 0x0a,
41  NYMPH_TYPE_SINT64 = 0x0b,
42  NYMPH_TYPE_FLOAT = 0x0c,
43  NYMPH_TYPE_DOUBLE = 0x0d,
44  NYMPH_TYPE_ARRAY = 0x0e,
45  NYMPH_TYPE_EMPTY_STRING = 0x0f,
46  NYMPH_TYPE_STRING = 0x10,
47  NYMPH_TYPE_STRUCT = 0x11,
48  NYMPH_TYPE_VOID = 0x12,
49  NYMPH_TYPE_BLOB = 0x13
50 };
51 
52 
53 // External types.
54 // I.e. those seen by an application using the library.
55 enum NymphTypes {
56  NYMPH_NULL = 0,
57  NYMPH_ARRAY,
58  NYMPH_BOOL,
59  NYMPH_UINT8,
60  NYMPH_SINT8,
61  NYMPH_UINT16,
62  NYMPH_SINT16,
63  NYMPH_UINT32,
64  NYMPH_SINT32,
65  NYMPH_UINT64,
66  NYMPH_SINT64,
67  NYMPH_FLOAT,
68  NYMPH_DOUBLE,
69  NYMPH_INT,
70  NYMPH_LONG,
71  NYMPH_SHORT,
72  NYMPH_STRING,
73  NYMPH_STRUCT,
74  NYMPH_BLOB,
75  NYMPH_ANY
76 };
77 
78 
79 // >>> UTILITY METHODS <<<
80 //
81 inline uint8_t getUInt8(std::string* binary, int &index) {
82  return (uint8_t) (*binary)[index++];
83 }
84 
85 
86 inline uint16_t getUInt16(std::string* binary, int &index) {
87  uint16_t val = *((uint16_t*) &((*binary)[index]));
88  index += 2;
89  return val;
90 }
91 
92 
93 inline uint32_t getUInt32(std::string* binary, int &index) {
94  uint32_t val = *((uint32_t*) &((*binary)[index]));
95  index += 4;
96  return val;
97 }
98 
99 
100 inline uint64_t getUInt64(std::string* binary, int &index) {
101  uint64_t val = *((uint64_t*) &((*binary)[index]));
102  index += 8;
103  return val;
104 }
105 
106 
107 inline int8_t getSInt8(std::string* binary, int &index) {
108  return (int8_t) (*binary)[index++];
109 }
110 
111 
112 inline int16_t getSInt16(std::string* binary, int &index) {
113  int16_t val = *((int16_t*) &((*binary)[index]));
114  index += 2;
115  return val;
116 }
117 
118 
119 inline int32_t getSInt32(std::string* binary, int &index) {
120  int32_t val = *((int32_t*) &((*binary)[index]));
121  index += 4;
122  return val;
123 }
124 
125 
126 inline int64_t getSInt64(std::string* binary, int &index) {
127  int64_t val = *((int64_t*) &((*binary)[index]));
128  index += 8;
129  return val;
130 }
131 
132 
133 class NymphType {
134  //
135 
136 public:
137  virtual ~NymphType() = 0;
138  virtual NymphTypes type() = 0;
139  virtual std::string toString(bool quotes = false) = 0;
140  virtual std::string serialize() = 0;
141  virtual bool deserialize(std::string* binary, int &index) = 0;
142  virtual bool empty() = 0;
143  virtual uint32_t binarySize() = 0;
144 };
145 
146 inline NymphType::~NymphType() {}
147 
148 class NymphString;
149 
150 
151 struct NymphPair {
152  NymphType* key;
153  NymphType* value;
154 };
155 
156 
157 class NymphNull : public NymphType {
158 public:
159  NymphTypes type() { return NYMPH_NULL; }
160  std::string toString(bool quotes = false) { return std::string(); }
161  std::string serialize() { return std::string(); }
162  bool deserialize(std::string* binary, int &index) { return true; }
163  bool empty() { return true; }
164  uint32_t binarySize() { return 0; }
165 };
166 
167 
168 class NymphArray : public NymphType {
169  std::vector<NymphType*> values;
170  bool isEmpty;
171  uint32_t binSize; // For pre-allocating.
172 
173 public:
174  NymphArray() { isEmpty = true; binSize = 0; }
175  ~NymphArray();
176  NymphTypes type() { return NYMPH_ARRAY; }
177  std::string toString(bool quotes = false);
178  std::vector<NymphType*> getValues() { return values; }
179  void addValue(NymphType* value);
180  std::string serialize();
181  bool deserialize(std::string* binary, int &index);
182  bool empty() { return isEmpty; }
183  uint32_t binarySize() { return (10 + binSize); }
184 };
185 
186 
187 class NymphBoolean : public NymphType {
188  bool value;
189  bool isEmpty;
190 
191 public:
192  NymphBoolean(bool value) { this->value = value; }
193  NymphBoolean(std::string* value, int &index) { deserialize(value, index); }
194  NymphBoolean(std::string value);
195  NymphTypes type() { return NYMPH_BOOL; }
196  std::string toString(bool quotes = false);
197  void setValue(bool value);
198  bool getValue();
199  std::string serialize();
200  bool deserialize(std::string* binary, int &index);
201  bool empty() { return isEmpty; }
202  uint32_t binarySize() { return 1; }
203 };
204 
205 
206 class NymphUint8 : public NymphType {
207  uint8_t value;
208  bool isEmpty;
209 
210 public:
211  NymphUint8(uint8_t value) { this->value = value; }
212  NymphUint8(std::string* value, int &index) { deserialize(value, index); }
213  NymphTypes type() { return NYMPH_UINT8; }
214  std::string toString(bool quotes = false);
215  void setValue(uint8_t value) { this->value = value; isEmpty = false; }
216  uint8_t getValue() { return value; }
217  std::string serialize();
218  bool deserialize(std::string* binary, int &index);
219  bool empty() { return isEmpty; }
220  uint32_t binarySize() { return 2; }
221 };
222 
223 
224 class NymphSint8 : public NymphType {
225  int8_t value;
226  bool isEmpty;
227 
228 public:
229  NymphSint8(int8_t value) { this->value = value; }
230  NymphSint8(std::string* value, int &index) { deserialize(value, index); }
231  NymphTypes type() { return NYMPH_SINT8; }
232  std::string toString(bool quotes = false);
233  void setValue(int8_t value);
234  int8_t getValue() { return value; }
235  std::string serialize();
236  bool deserialize(std::string* binary, int &index);
237  bool empty() { return isEmpty; }
238  uint32_t binarySize() { return 2; }
239 };
240 
241 
242 class NymphUint16 : public NymphType {
243  uint16_t value;
244  bool isEmpty;
245 
246 public:
247  NymphUint16(uint16_t value) { this->value = value; }
248  NymphUint16(std::string* value, int &index) { deserialize(value, index); }
249  NymphTypes type() { return NYMPH_UINT16; }
250  std::string toString(bool quotes = false);
251  void setValue(uint16_t value) { this->value = value; isEmpty = false; }
252  uint16_t getValue() { return value; }
253  std::string serialize();
254  bool deserialize(std::string* binary, int &index);
255  bool empty() { return isEmpty; }
256  uint32_t binarySize() { return 3; }
257 };
258 
259 
260 class NymphSint16 : public NymphType {
261  int16_t value;
262  bool isEmpty;
263 
264 public:
265  NymphSint16(int16_t value) { this->value = value; }
266  NymphSint16(std::string* value, int &index) { deserialize(value, index); }
267  NymphTypes type() { return NYMPH_SINT16; }
268  std::string toString(bool quotes = false);
269  void setValue(int16_t value) { this->value = value; isEmpty = false; }
270  int16_t getValue() { return value; }
271  std::string serialize();
272  bool deserialize(std::string* binary, int &index);
273  bool empty() { return isEmpty; }
274  uint32_t binarySize() { return 3; }
275 };
276 
277 
278 class NymphUint32 : public NymphType {
279  uint32_t value;
280  bool isEmpty;
281 
282 public:
283  NymphUint32(uint32_t value) { this->value = value; }
284  NymphUint32(std::string* value, int &index) { deserialize(value, index); }
285  NymphTypes type() { return NYMPH_UINT32; }
286  std::string toString(bool quotes = false);
287  void setValue(uint32_t value) { this->value = value; isEmpty = false; }
288  uint32_t getValue() { return value; }
289  std::string serialize();
290  bool deserialize(std::string* binary, int &index);
291  bool empty() { return isEmpty; }
292  uint32_t binarySize() { return 5; }
293 };
294 
295 
296 class NymphSint32 : public NymphType {
297  int32_t value;
298  bool isEmpty;
299 
300 public:
301  NymphSint32(int32_t value) { this->value = value; }
302  NymphSint32(std::string* value, int &index) { deserialize(value, index); }
303  NymphTypes type() { return NYMPH_SINT32; }
304  std::string toString(bool quotes = false);
305  void setValue(int32_t value) { this->value = value; isEmpty = false; }
306  int32_t getValue() { return value; }
307  std::string serialize();
308  bool deserialize(std::string* binary, int &index);
309  bool empty() { return isEmpty; }
310  uint32_t binarySize() { return 5; }
311 };
312 
313 
314 class NymphUint64 : public NymphType {
315  uint64_t value;
316  bool isEmpty;
317 
318 public:
319  NymphUint64(uint64_t value) { this->value = value; }
320  NymphUint64(std::string* value, int &index) { deserialize(value, index); }
321  NymphTypes type() { return NYMPH_UINT64; }
322  std::string toString(bool quotes = false);
323  void setValue(int64_t value) { this->value = value; isEmpty = false; }
324  uint64_t getValue() { return value; }
325  std::string serialize();
326  bool deserialize(std::string* binary, int &index);
327  bool empty() { return isEmpty; }
328  uint32_t binarySize() { return 9; }
329 };
330 
331 
332 class NymphSint64 : public NymphType {
333  int64_t value;
334  bool isEmpty;
335 
336 public:
337  NymphSint64(int64_t value) { this->value = value; }
338  NymphSint64(std::string* value, int &index) { deserialize(value, index); }
339  NymphTypes type() { return NYMPH_SINT64; }
340  std::string toString(bool quotes = false);
341  void setValue(int64_t value) { this->value = value; isEmpty = false; }
342  int64_t getValue() { return value; }
343  std::string serialize();
344  bool deserialize(std::string* binary, int &index);
345  bool empty() { return isEmpty; }
346  uint32_t binarySize() { return 9; }
347 };
348 
349 
350 class NymphDouble : public NymphType {
351  double value;
352  bool isEmpty;
353 
354 public:
355  NymphDouble(double value) { this->value = value; }
356  NymphDouble(std::string* value, int &index) { deserialize(value, index); }
357  NymphTypes type() { return NYMPH_DOUBLE; }
358  std::string toString(bool quotes = false);
359  std::string serialize();
360  bool deserialize(std::string* binary, int &index);
361  bool empty() { return isEmpty; }
362  void setValue(double value) { this->value = value; }
363  double getValue() { return value; }
364  uint32_t binarySize() { return 9; }
365 };
366 
367 
368 class NymphFloat : public NymphType {
369  float value;
370  bool isEmpty;
371 
372 public:
373  NymphFloat(float value) { this->value = value; }
374  NymphFloat(std::string* value, int &index) { deserialize(value, index); }
375  NymphTypes type() { return NYMPH_FLOAT; }
376  std::string toString(bool quotes = false);
377  std::string serialize();
378  bool deserialize(std::string* binary, int &index);
379  bool empty() { return isEmpty; }
380  void setValue(float value) { this->value = value; }
381  float getValue() { return value; }
382  uint32_t binarySize() { return 5; }
383 };
384 
385 
386 class NymphString : public NymphType {
387  std::string value;
388  bool emptyString;
389  bool isEmpty;
390  uint32_t binSize;
391 
392 public:
393  NymphString() { isEmpty = true; emptyString = true; binSize = 0; }
394  NymphString(std::string value);
395  NymphString(std::string* value, int &index) { deserialize(value, index); }
396  NymphTypes type() { return NYMPH_STRING; }
397  std::string toString(bool quotes = false);
398  std::string serialize();
399  bool deserialize(std::string* binary, int &index);
400  bool empty() { return isEmpty; }
401  void setValue(std::string value);
402  std::string getValue() { return value; }
403  void setEmptyString(bool val = true) { isEmpty = false; emptyString = val; }
404  uint32_t binarySize() { return binSize; }
405 };
406 
407 
408 class NymphStruct : public NymphType {
409  std::map<std::string, NymphPair> pairs;
410  bool isEmpty;
411  uint32_t binSize; // For pre-allocating.
412 
413 public:
414  NymphStruct() { isEmpty = true; binSize = 0; }
415  ~NymphStruct();
416  NymphTypes type() { return NYMPH_STRUCT; }
417  std::string toString(bool quotes = false);
418  std::string serialize();
419  bool deserialize(std::string* binary, int &index);
420  bool empty() { return isEmpty; }
421  void addPair(std::string key, NymphType* value);
422  bool getValue(std::string key, NymphType* &value);
423  uint32_t binarySize() { return binSize; }
424 };
425 
426 
427 class NymphBlob : public NymphType {
428  std::string value;
429  bool isEmpty;
430  uint32_t binSize;
431 
432 public:
433  NymphBlob() { isEmpty = true; binSize = 0; }
434  NymphBlob(std::string value);
435  NymphBlob(std::string* value, int &index) { deserialize(value, index); }
436  NymphTypes type() { return NYMPH_BLOB; }
437  std::string toString(bool quotes = false);
438  std::string serialize();
439  bool deserialize(std::string* binary, int &index);
440  bool empty() { return isEmpty; }
441  void setValue(std::string value);
442  std::string getValue() { return value; }
443  uint32_t binarySize() { return binSize; }
444 };
445 
446 
447 /* class NymphTable : public NymphType {
448  vector<NymphType*> values;
449  bool isEmpty;
450 
451 public:
452  NymphTable() { isEmpty = true; }
453  ~NymphTable();
454  NymphTypes type() { return NYMPH_TABLE; }
455  std::string toString(bool quotes = false);
456  std::string serialize();
457  bool deserialize(std::string binary);
458  bool empty() { return isEmpty; }
459  void addValue(NymphType* value, int &index); { values.push_back(value); isEmpty = false; }
460  vector<NymphType*> getValues() { return values; }
461  void setValues(vector<NymphType*> values) { this->values = values; isEmpty = false; }
462  bool getJson(std::string &json, std::string &result);
463  std::string getJson();
464  bool setJson(std::string &json, std::string &result);
465  bool setJsonObject(JSON::Object::Ptr &json, std::string &result);
466  JSON::Object::Ptr getJsonObject(std::string &result);
467 }; */
468 
469 #endif
Definition: nymph_types.h:168
Definition: nymph_types.h:427
Definition: nymph_types.h:187
Definition: nymph_types.h:350
Definition: nymph_types.h:368
Definition: nymph_types.h:157
Definition: nymph_types.h:260
Definition: nymph_types.h:296
Definition: nymph_types.h:332
Definition: nymph_types.h:224
Definition: nymph_types.h:386
Definition: nymph_types.h:408
Definition: nymph_types.h:133
Definition: nymph_types.h:242
Definition: nymph_types.h:278
Definition: nymph_types.h:314
Definition: nymph_types.h:206
Definition: nymph_types.h:151