-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDefine.h
More file actions
449 lines (404 loc) · 10.3 KB
/
Define.h
File metadata and controls
449 lines (404 loc) · 10.3 KB
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
// Serialize example
// This example shows writing JSON string with writer directly.
#ifndef __DEFINE_H__
#define __DEFINE_H__
#include "rapidjson/writer.h" // for stringify JSON
#include "rapidjson/document.h"
#include <cstdio>
#include <string>
#include <vector>
#include <sstream>
#include <iostream>
#include <stdexcept>
#include <stdint.h>
#include "globe.h"
using namespace rapidjson;
using namespace std;
class CString;
class CInteger;
class CString
{
public:
CString()
{
this->_isNull = true;
}
CString(const std::string &str)
{
this->_data = str;
this->_isNull = false;
}
std::string toString()
{
if (isNull())
{
throw logic_error("CString is NULL");
}
return this->_data;
}
void assign(const std::string &str)
{
this->_data = str;
this->_isNull = false;
}
void assign(const CString &rhs)
{
this->_data = rhs._data;
this->_isNull = rhs._isNull;
}
operator std::string()const
{
if (isNull())
{
throw logic_error("CString is NULL");
}
return this->_data;
}
bool isNull()const
{
return this->_isNull;
}
private:
std::string _data;
bool _isNull;
};
class CInteger
{
public:
CInteger()
{
this->_isNull = true;
}
CInteger(int64_t value)
{
this->_data = value;
this->_isNull = false;
}
std::string toString()
{
if (isNull())
{
throw logic_error("CInteger is NULL");
}
return TC_Common::tostr<int64_t>(this->_data);
}
void assign(int64_t newValue)
{
this->_data = newValue;
this->_isNull = false;
}
void assign(const CInteger &newValue)
{
this->_data = newValue._data;
this->_isNull = newValue._isNull;
}
void assign(const string &newValue)
{
int64_t intValue = S2L(newValue);
this->_data = intValue;
this->_isNull = false;
}
operator int64_t() const
{
if (isNull())
{
throw logic_error("CInteger is NULL");
}
return this->_data;
}
bool isNull()const
{
return this->_isNull;
}
private:
int64_t _data;
bool _isNull;
};
class CDouble
{
public:
CDouble()
{
this->_isNull = true;
}
CDouble(double value)
{
this->_data = value;
this->_isNull = false;
}
std::string toString()
{
if (isNull())
{
throw logic_error("CDouble is NULL");
}
return TC_Common::tostr<double>(this->_data);
}
void assign(double newValue)
{
this->_data = newValue;
this->_isNull = false;
}
void assign(const CDouble &newValue)
{
this->_data = newValue._data;
this->_isNull = newValue._isNull;
}
void assign(const string &newValue)
{
double intValue = TC_Common::strto<double>(newValue);
this->_data = intValue;
this->_isNull = false;
}
operator double() const
{
if (isNull())
{
throw logic_error("CInteger is NULL");
}
return this->_data;
}
bool isNull()const
{
return this->_isNull;
}
private:
double _data;
bool _isNull;
};
// 管理后台请求状态码
const static int64_t RESULT_CODE_SUCCESS = 1;
const static int64_t RESULT_CODE_FAIL = 0;
#define INSERT_ASSIGN_STATEMENT(member) req.data[#member] = request._##member.toString()
#define UPDATE_ASSIGN_STATEMENT(member) if (request._##member.isNull() == false) \
{ \
req.data[#member] = request._##member.toString(); \
}
#define SERIALIZE_MEMBER(writer, member) serializeMember(writer,#member,_##member)
#define SET_DOC_MEMBER(document, member) setDocMember(document, #member, _##member);
#define SET_RESPONSE_MEMBER(member) if (key == #member) { \
response._##member.assign(value); \
}
template <typename Writer>
void serializeMember(Writer &writer, const CString &content)
{
if (content.isNull())
{
writer.Null();
}
else
{
std::string str = content;
writer.String(str.c_str(), (SizeType)str.length());
}
}
template <typename Writer>
void serializeMember(Writer &writer, const CInteger &content)
{
if (content.isNull())
{
writer.Null();
}
else
{
writer.Int64(content);
}
}
template <typename Writer>
void serializeMember(Writer &writer, const CDouble &content)
{
if (content.isNull())
{
writer.Null();
}
else
{
writer.Double(content);
}
}
template <typename Writer, typename MemberType>
void serializeMember(Writer &writer, const string &keyName, const MemberType &member)
{
writer.String(keyName.c_str());
serializeMember(writer, member);
}
static void setDocMember(const Document &d, const std::string &keyName, CString &member)
{
if (
d.HasMember(keyName.c_str()) == false ||
(d[keyName.c_str()].IsString() == false && d[keyName.c_str()].IsNull() == false)
)
{
throw logic_error("json Type error!");
}
if (d[keyName.c_str()].IsString())
{
member.assign(d[keyName.c_str()].GetString());
}
}
static void setDocMember(const Document &d, const std::string &keyName, CInteger &member)
{
if (
d.HasMember(keyName.c_str()) == false ||
(d[keyName.c_str()].IsInt64() == false && d[keyName.c_str()].IsNull() == false)
)
{
throw logic_error("json Type error!");
}
if (d[keyName.c_str()].IsInt64())
{
member.assign(d[keyName.c_str()].GetInt64());
}
}
static void setDocMember(const Document &d, const std::string &keyName, CDouble &member)
{
if (
d.HasMember(keyName.c_str()) == false ||
(d[keyName.c_str()].IsDouble() == false && d[keyName.c_str()].IsNull() == false)
)
{
throw logic_error("json Type error!");
}
if (d[keyName.c_str()].IsDouble())
{
member.assign(d[keyName.c_str()].GetDouble());
}
}
class GMRequest
{
public:
GMRequest() {}
GMRequest(const std::string &code, const std::string ¶ms, int64_t time, const std::string &sign) :
_code(code),
_params(params),
_time(time),
_sign(sign) {}
template <typename Writer>
void Serialize(Writer &writer) const
{
writer.StartObject();
SERIALIZE_MEMBER(writer, code);
SERIALIZE_MEMBER(writer, params);
SERIALIZE_MEMBER(writer, time);
SERIALIZE_MEMBER(writer, sign);
writer.EndObject();
}
void toString(std::string &json)
{
StringBuffer sb;
Writer<StringBuffer> writer(sb);
Serialize(writer);
json = sb.GetString();
}
void Deserialize(const string &json)
{
try
{
Document d;
if (d.Parse(json.c_str()).HasParseError())
{
throw logic_error("parse json error. raw data : " + json);
}
//赋值
SET_DOC_MEMBER(d, code);
SET_DOC_MEMBER(d, params);
SET_DOC_MEMBER(d, time);
SET_DOC_MEMBER(d, sign);
}
catch (const std::exception &e)
{
std::string errInfo = std::string(__FILE__) + ":" + std::to_string(__LINE__) + ":GMRequest decode error!";
throw logic_error(errInfo);
}
}
void getParams(vector<tars::Char> ¶ms)
{
std::string str;
if(this->_params.isNull())
{
str = "";
}
else
{
str = this->_params;
}
params.assign(str.begin(), str.end());
}
std::string getParams()
{
std::string str = this->_params;
return str;
}
int getCode() const
{
if (this->_code.isNull())
{
throw logic_error("code Null!");
}
std::string code = this->_code;
return S2I(code);
}
private:
CString _code;
CString _params;
CInteger _time;
CString _sign;
};
class GMResponse
{
public:
GMResponse() {}
GMResponse(int64_t result, const std::string &msg, const std::string &resultData, int64_t totalItems, int64_t totalPages) :
_result(result),
_msg(msg),
_resultData(resultData),
_totalItems(totalItems),
_totalPages(totalPages) {}
// 删除响应使用的构造函数
GMResponse(int64_t result, const std::string &msg) :
_result(result),
_msg(msg) {}
// 插入和更新响应使用的构造函数
GMResponse(int64_t result, const std::string &msg, const std::string &resultData) :
_result(result),
_msg(msg),
_resultData(resultData) {}
template <typename Writer>
void Serialize(Writer &writer) const
{
writer.StartObject();
SERIALIZE_MEMBER(writer, result);
SERIALIZE_MEMBER(writer, msg);
SERIALIZE_MEMBER(writer, resultData);
SERIALIZE_MEMBER(writer, totalItems);
SERIALIZE_MEMBER(writer, totalPages);
writer.EndObject();
}
void toString(std::string &json)
{
StringBuffer sb;
Writer<StringBuffer> writer(sb);
Serialize(writer);
json = sb.GetString();
}
void Deserialize(const string &json)
{
Document d;
if (d.Parse(json.c_str()).HasParseError())
{
throw logic_error("parse json error. raw data : " + json);
}
SET_DOC_MEMBER(d, result);
SET_DOC_MEMBER(d, msg);
SET_DOC_MEMBER(d, resultData);
SET_DOC_MEMBER(d, totalItems);
SET_DOC_MEMBER(d, totalPages);
}
private:
CInteger _result;
CString _msg;
CString _resultData;
CInteger _totalItems;
CInteger _totalPages;
};
#endif // !__DEFINE_H