NymphRPC Remote Procedure Call Library
nymph_types.h
1/*
2 nymph_types.h - Defines the NymphRPC data types.
3
4 Revision 1
5
6 Notes:
7 -
8
9 History:
10 2017/06/24, Maya Posch : Initial version.
11 2021/10/01, Maya Posch : New type system.
12
13 (c) Nyanko.ws
14*/
15
16
17#pragma once
18#ifndef NYMPH_TYPES_H
19#define NYMPH_TYPES_H
20
21#include <Poco/Poco.h>
22
23#include <string>
24#include <map>
25#include <vector>
26#include <cstdint>
27
28
29class NymphMessage;
30
31
32enum NymphInternalTypes {
33 NYMPH_TYPE_NULL = 0x00,
34 NYMPH_TYPE_NONE = 0x01,
35 NYMPH_TYPE_BOOLEAN_FALSE = 0x02,
36 NYMPH_TYPE_BOOLEAN_TRUE = 0x03,
37 NYMPH_TYPE_UINT8 = 0x04,
38 NYMPH_TYPE_SINT8 = 0x05,
39 NYMPH_TYPE_UINT16 = 0x06,
40 NYMPH_TYPE_SINT16 = 0x07,
41 NYMPH_TYPE_UINT32 = 0x08,
42 NYMPH_TYPE_SINT32 = 0x09,
43 NYMPH_TYPE_UINT64 = 0x0a,
44 NYMPH_TYPE_SINT64 = 0x0b,
45 NYMPH_TYPE_FLOAT = 0x0c,
46 NYMPH_TYPE_DOUBLE = 0x0d,
47 NYMPH_TYPE_ARRAY = 0x0e,
48 NYMPH_TYPE_EMPTY_STRING = 0x0f,
49 NYMPH_TYPE_STRING = 0x10,
50 NYMPH_TYPE_STRUCT = 0x11,
51 NYMPH_TYPE_VOID = 0x12,
52 NYMPH_TYPE_BLOB = 0x13
53};
54
55
56// External types.
57// I.e. those seen by an application using the library.
58enum NymphTypes {
59 NYMPH_NULL = 0,
60 NYMPH_ARRAY,
61 NYMPH_BOOL,
62 NYMPH_UINT8,
63 NYMPH_SINT8,
64 NYMPH_UINT16,
65 NYMPH_SINT16,
66 NYMPH_UINT32,
67 NYMPH_SINT32,
68 NYMPH_UINT64,
69 NYMPH_SINT64,
70 NYMPH_FLOAT,
71 NYMPH_DOUBLE,
72 NYMPH_INT,
73 NYMPH_LONG,
74 NYMPH_SHORT,
75 NYMPH_STRING,
76 NYMPH_STRUCT,
77 NYMPH_BLOB,
78 NYMPH_ANY
79};
80
81
82struct NymphPair;
83
84
85class NymphType {
86 NymphTypes type = NYMPH_NULL;
87 union DataUnion {
88 void* any;
89 bool boolean;
90 uint8_t uint8;
91 int8_t int8;
92 uint16_t uint16;
93 int16_t int16;
94 uint32_t uint32;
95 int32_t int32;
96 uint64_t uint64;
97 int64_t int64;
98 float fp32;
99 double fp64;
100 const char* chars;
101 std::vector<NymphType*>* vector;
102 std::map<std::string, NymphPair>* pairs;
103 };
104
105 DataUnion data;
106 uint64_t length; // Length of serialised value in bytes.
107 uint32_t strLength; // String length (for NYMPH_STRING).
108 bool emptyString = false; // Indicates whether a NYMPH_STRING is empty.
109 bool own = false;
110 std::string* string = 0;
111 NymphMessage* linkedMsg = 0;
112
113public:
114 NymphType() { }
115 NymphType(bool v);
116 NymphType(uint8_t v);
117 NymphType(int8_t v);
118 NymphType(uint16_t v);
119 NymphType(int16_t v);
120 NymphType(uint32_t v);
121 NymphType(int32_t v);
122 NymphType(uint64_t v);
123 NymphType(int64_t v);
124 NymphType(float v);
125 NymphType(double v);
126 NymphType(char* v, uint32_t bytes, bool own = false);
127 NymphType(std::string* v, bool own = false);
128 NymphType(std::vector<NymphType*>* v, bool own = false);
129 NymphType(std::map<std::string, NymphPair>* v, bool own = false);
130
131 ~NymphType();
132
133 bool getBool(bool* v = 0);
134 uint8_t getUint8(uint8_t* v = 0);
135 int8_t getInt8(int8_t* v = 0);
136 uint16_t getUint16(uint16_t* v = 0);
137 int16_t getInt16(int16_t* v = 0);
138 uint32_t getUint32(uint32_t* v = 0);
139 int32_t getInt32(int32_t* v = 0);
140 uint64_t getUint64(uint64_t* v = 0);
141 int64_t getInt64(int64_t* v = 0);
142 float getFloat(float* v = 0);
143 double getDouble(double* v = 0);
144 const char* getChar(const char* v = 0);
145 std::vector<NymphType*>* getArray(std::vector<NymphType*>* v = 0);
146 std::map<std::string, NymphPair>* getStruct(std::map<std::string, NymphPair>* v = 0);
147
148 std::string getString();
149 bool getStructValue(std::string key, NymphType* &value);
150
151 void setValue(bool v);
152 void setValue(uint8_t v);
153 void setValue(int8_t v);
154 void setValue(uint16_t v);
155 void setValue(int16_t v);
156 void setValue(uint32_t v);
157 void setValue(int32_t v);
158 void setValue(uint64_t v);
159 void setValue(int64_t v);
160 void setValue(float v);
161 void setValue(double v);
162 void setValue(char* v, uint32_t bytes, bool own = false);
163 void setValue(std::string* v, bool own = false);
164 void setValue(std::vector<NymphType*>* v, bool own = false);
165 void setValue(std::map<std::string, NymphPair>* v, bool own = false);
166
167 uint64_t bytes();
168 uint32_t string_length();
169 NymphTypes valuetype();
170
171 void serialize(uint8_t* &index);
172
173 void linkWithMessage(NymphMessage* msg);
174 void triggerAddRC();
175 void discard();
176};
177
178
179struct NymphPair {
180 NymphType* key;
181 NymphType* value;
182};
183
184#endif
Definition: nymph_message.h:39
Definition: nymph_types.h:85
Definition: nymph_types.h:179