-
Notifications
You must be signed in to change notification settings - Fork 0
/
GenericDto.h
153 lines (137 loc) · 3.76 KB
/
GenericDto.h
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
#pragma once
#include "common.h"
#include <cstdlib>
#include <cstring>
class GenericDto
{
int type;
// This is populated only when deserializing a payload
int byteLength = -1;
public:
int iValue = 0;
float fValue = 0;
std::string sValue;
int id;
GenericDto(int id = 0) : type(0), id(id) {}
static GenericDto createInt(int value, int id = 0)
{
GenericDto d(id);
d.iValue = value;
d.type = 1;
return d;
}
static GenericDto createFloat(float value, int id = 0)
{
GenericDto d(id);
d.fValue = value;
d.type = 2;
return d;
}
static GenericDto createString(const std::string &value, int id = 0)
{
GenericDto d(id);
d.sValue = value;
d.type = 3;
return d;
}
static GenericDto deserialize(const std::string &serialized)
{
GenericDto d;
const auto headerSz = 1 + 2 * sizeof(int);
if (serialized.size() >= headerSz)
{
d.type = serialized[0] - '0';
int len = 0;
memcpy(&len, serialized.substr(1).c_str(), sizeof(int));
d.byteLength = len;
memcpy(&d.id, serialized.substr(1 + sizeof(int)).c_str(), sizeof(int));
if (serialized.size() < len)
{
d.type = 0;
return d;
}
auto val = serialized.substr(headerSz, len - headerSz);
if (d.type == 1)
{
memcpy(&d.iValue, val.c_str(), sizeof(int));
}
else if (d.type == 2)
{
memcpy(&d.fValue, val.c_str(), sizeof(float));
}
else if (d.type == 3)
{
d.sValue = val.substr(0, len - headerSz);
}
else
{
d.type = 0;
}
}
return d;
}
static std::vector<GenericDto> deserializeAll(const std::string &serialized)
{
std::string s = serialized;
std::vector<GenericDto> result;
while (s.size() > 0)
{
auto dto = deserialize(s);
if (!dto.isValid() || dto.byteLength <= 0)
{
break;
}
result.push_back(dto);
s = s.substr(dto.byteLength);
}
return result;
}
static std::string serializeParameter(float value, int idx)
{
const auto saveId = getSaveIdForParam(idx);
const auto opts = getNumberOfOptions(idx);
if (opts == 0)
return createFloat(value, saveId).serialize();
else
return createInt(Util::getSelection(value, opts), saveId).serialize();
}
bool isValid() { return type >= 1 && type <= 3; }
std::string serialize()
{
if (!isValid())
{
return "";
}
std::string payload;
if (type == 1)
{
payload = std::string((char *)&iValue, sizeof(int));
}
else if (type == 2)
{
payload = std::string((char *)&fValue, sizeof(float));
}
else
{
payload = sValue;
}
char headerBuf[1 + 2 * sizeof(int)];
headerBuf[0] = (char)type + '0';
int len = sizeof(headerBuf) + payload.size();
memcpy(&headerBuf[1], &len, sizeof(int));
memcpy(&headerBuf[1 + sizeof(int)], &id, sizeof(int));
std::string header(headerBuf, sizeof(headerBuf));
return header + payload;
}
// Human readable type mapping: i = int, f = float, s = string, x = invalid
char getType()
{
if (type == 1)
return 'i';
if (type == 2)
return 'f';
if (type == 3)
return 's';
return 'x';
}
};