summaryrefslogtreecommitdiff
path: root/src/modules/rpc.h
blob: 75a7efd62077a0a3bf92dc5d3f9eb04922a07b78 (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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#ifndef RPC_H
#define RPC_H

#include <string>
#include <map>
#include <stdexcept>

class RPCValue;

typedef enum
{
	RPCNull,
	RPCBoolean,
	RPCInteger,
	RPCString,
	RPCArray,
	RPCObject
} RPCValueType;

typedef std::map<std::string,RPCValue*> RPCObjectContainer;
typedef std::vector<RPCValue*> RPCArrayContainer;

class RPCValue : public classbase
{
 protected:
	RPCValueType type;
	void *value;
	
	double *CastInteger()
	{
		return (double*)value;
	}
	
	std::string *CastString()
	{
		return (std::string*)value;
	}
	
	RPCObjectContainer *CastObject()
	{
		return (RPCObjectContainer*)value;
	}
	
	RPCArrayContainer *CastArray()
	{
		return (RPCArrayContainer*)value;
	}
	
	void DestroyValue()
	{
		// Some versions of GCC complain about declaration in switch statements
		RPCArrayContainer *a;
		RPCObjectContainer *o;
		switch (type)
		{
			case RPCInteger:
				delete this->CastInteger();
				break;
			case RPCString:
				delete this->CastString();
				break;
			case RPCArray:
				a = this->CastArray();
				for (RPCArrayContainer::iterator i = a->begin(); i != a->end(); i++)
					delete *i;
				delete a;
				break;
			case RPCObject:
				o = this->CastObject();
				for (RPCObjectContainer::iterator i = o->begin(); i != o->end(); i++)
					delete i->second;
				delete o;
				break;
			default:
				break;
		}
		
		value = NULL;
	}
	
	void InitValue()
	{
		switch (type)
		{
			case RPCNull:
			case RPCBoolean:
				value = NULL;
				break;
			case RPCInteger:
				value = new double;
				break;
			case RPCString:
				value = new std::string;
				break;
			case RPCArray:
				value = new RPCArrayContainer;
				break;
			case RPCObject:
				value = new RPCObjectContainer;
				break;
		}
	}
	
	RPCValue(const RPCValue &v) { }
	
 public:
	RPCValue *parent;

	RPCValue(RPCValue *rparent = NULL) : type(RPCNull), value(NULL), parent(rparent) { }
	RPCValue(RPCValueType ttype, RPCValue *rparent = NULL) : type(ttype), value(NULL), parent(rparent) { InitValue(); }
	RPCValue(bool nvalue, RPCValue *rparent = NULL) : type(RPCBoolean), value((void*)nvalue), parent(rparent) { }
	RPCValue(double nvalue, RPCValue *rparent = NULL) : type(RPCInteger), parent(rparent) { value = new double(nvalue); }
	RPCValue(const std::string &nvalue, RPCValue *rparent = NULL) : type(RPCString), parent(rparent) { value = new std::string(nvalue); }
	
	virtual ~RPCValue()
	{
		DestroyValue();
	}
	
	RPCValueType GetType()
	{
		return type;
	}
	
	void SetNull()
	{
		DestroyValue();
		type = RPCNull;
	}
	
	void SetBoolean(bool nvalue)
	{
		DestroyValue();
		value = (void*)nvalue;
		type = RPCBoolean;
	}
	
	void SetInteger(double nvalue)
	{
		if (type == RPCInteger)
		{
			*this->CastInteger() = nvalue;
		}
		else
		{
			DestroyValue();
			value = new double(nvalue);
			type = RPCInteger;
		}
	}
	
	void SetString(const std::string &nvalue)
	{
		if (type == RPCString)
		{
			this->CastString()->assign(nvalue);
		}
		else
		{
			DestroyValue();
			value = new std::string(nvalue);
			type = RPCString;
		}
	}
	
	void SetArray()
	{
		if (type == RPCArray)
		{
			this->CastArray()->clear();
		}
		else
		{
			DestroyValue();
			type = RPCArray;
			InitValue();
		}
	}
	
	void SetObject()
	{
		if (type == RPCObject)
		{
			this->CastObject()->clear();
		}
		else
		{
			DestroyValue();
			type = RPCObject;
			InitValue();
		}
	}
	
	void ArrayAdd(RPCValue *nvalue)
	{
		if (type != RPCArray)
			return;
		RPCArrayContainer *a = this->CastArray();
		a->push_back(nvalue);
		nvalue->parent = this;
	}
	
	void ObjectAdd(const std::string &key, RPCValue *nvalue)
	{
		if (type != RPCObject)
			return;
		RPCObjectContainer *o = this->CastObject();
		o->insert(std::make_pair(key, nvalue));
		nvalue->parent = this;
	}
	
	RPCValue *GetArray(int i)
	{
		if (type != RPCArray)
			return NULL;
		RPCArrayContainer *a = this->CastArray();
		if ((i < 0) || (i >= (signed)a->size()))
			return NULL;
		return a->at(i);
	}
	
	int ArraySize()
	{
		if (type != RPCArray)
			return 0;
		RPCArrayContainer *a = this->CastArray();
		return a->size();
	}
	
	RPCValue *GetObject(const std::string &key)
	{
		if (type != RPCObject)
			return NULL;
		RPCObjectContainer *o = this->CastObject();
		RPCObjectContainer::iterator it = o->find(key);
		if (it == o->end())
			return NULL;
		return it->second;
	}
	
	std::pair<RPCObjectContainer::iterator,RPCObjectContainer::iterator> GetObjectIterator()
	{
		if (type != RPCObject)
			throw std::runtime_error("Cannot get iterator for a non-object RPC value");
		RPCObjectContainer *o = this->CastObject();
		return std::make_pair(o->begin(), o->end());
	}
	
	std::string GetString()
	{
		if (type != RPCString)
			return std::string();
		return *this->CastString();
	}
	
	double GetInt()
	{
		if (type != RPCInteger)
			return 0;
		return *this->CastInteger();
	}
	
	bool GetBool()
	{
		if (type != RPCBoolean)
			return 0;
		return (value != NULL);
	}
};

class RPCRequest : public classbase
{
 protected:
	
 public:
	std::string method;
	RPCValue *parameters;
	RPCValue *result;
	std::string provider;
	bool claimed;
	std::string error;
	
	RPCRequest(const std::string &sprovider, const std::string &smethod, RPCValue *rparameters)
		: method(smethod), parameters(rparameters), provider(sprovider), claimed(false)
	{
		result = new RPCValue();
	}
	
	~RPCRequest()
	{
		if (result)
			delete result;
	}
};

#endif