forked from lichanglin688/secs_gem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecs_gem.h
306 lines (269 loc) · 6.29 KB
/
secs_gem.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
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
#ifndef SECS_GEM_H
#define SECS_GEM_H
#include <variant>
#include <vector>
#include <memory>
#include <QByteArray>
#include <initializer_list>
#include <QDebug>
class QDataStream;
namespace Secs{
using std::variant;
using std::vector;
using std::string;
using std::initializer_list;
class Item;
using ItemPtr = std::shared_ptr<Item>;
namespace ValueType
{
using ASCII = char;
using Binary = int8_t;
using Bool = bool;
using I1 = int8_t;
using I2 = int16_t;
using I4 = int32_t;
using I8 = int64_t;
using U1 = uint8_t;
using U2 = uint16_t;
using U4 = uint32_t;
using U8 = uint64_t;
using F4 = float;
using F8 = double;
using Item = ItemPtr;
};
class Item;
#define ValueTypeList ValueType::ASCII, ValueType::Bool, ValueType::U1, ValueType::U2, ValueType::U4, ValueType::U8, ValueType::I1, ValueType::I2, ValueType::I4, ValueType::I8, ValueType::F4, ValueType::F8, ValueType::Item
using Value = variant<ValueTypeList>;
//LCS和JIS8未实现,不可使用
enum class Format
{
List = 0, // 00000000
Binary = 0x20, // 00100000
Bool = 0x24, // 00100100
ASCII = 0x40, // 01000000
JIS8 = 0x44, // 01000100
LCS = 0x48, // 01001000
I8 = 0x60, // 01100000
I1 = 0x64, // 01100100
I2 = 0x68, // 01101000
I4 = 0x70, // 01110000
F8 = 0x80, // 10000000
F4 = 0x90, // 10010000
U8 = 0xA0, // 10100000
U1 = 0xA4, // 10100100
U2 = 0xA8, // 10101000
U4 = 0xB0, // 10110000
NONE = 0xFF
};
#define SECS_CREATE_ITEM(type) \
static ItemPtr type(initializer_list<ValueType::type> values = {}) \
{\
ItemPtr item = std::make_shared<Item>(Format::type); \
for (auto&& value : values)\
{\
item->append(value);\
}\
return item;\
}
#define SECS_GET_VALUE(type) \
ValueType::type get##type(size_t index = 0)\
{\
return getValue<ValueType::type>(index);\
}
class Item
{
public:
Item(Format format = Format::NONE);
~Item();
SECS_CREATE_ITEM(ASCII)
SECS_CREATE_ITEM(Bool)
SECS_CREATE_ITEM(Binary)
SECS_CREATE_ITEM(I1)
SECS_CREATE_ITEM(I2)
SECS_CREATE_ITEM(I4)
SECS_CREATE_ITEM(I8)
SECS_CREATE_ITEM(U1)
SECS_CREATE_ITEM(U2)
SECS_CREATE_ITEM(U4)
SECS_CREATE_ITEM(U8)
SECS_CREATE_ITEM(F4)
SECS_CREATE_ITEM(F8)
static ItemPtr List(initializer_list<ValueType::Item> values = {});
static ItemPtr String(const string &str);
SECS_GET_VALUE(ASCII)
SECS_GET_VALUE(Binary)
SECS_GET_VALUE(Bool)
SECS_GET_VALUE(I1)
SECS_GET_VALUE(I2)
SECS_GET_VALUE(I4)
SECS_GET_VALUE(I8)
SECS_GET_VALUE(U1)
SECS_GET_VALUE(U2)
SECS_GET_VALUE(U4)
SECS_GET_VALUE(U8)
SECS_GET_VALUE(F4)
SECS_GET_VALUE(F8)
SECS_GET_VALUE(Item)
string getString();
void append(const Value &value);
size_t getCount();
template<typename Type>
Type getValue(size_t index)
{
static_assert(std::_Is_any_of_v<Type, ValueTypeList>, "Type is not Secs type");
return std::get<Type>(values[index]);
}
Format getFormat() const;
int formatSize();
size_t dataLength();
private:
bool checkValueFormat(const Value &value);
private:
std::vector<Value> values;
Format format;
};
class Message
{
public:
QByteArray encode();
void decode(QByteArray data);
ItemPtr getItem(size_t index);
void appendItem(ItemPtr item);
uint16_t deviceID = 0;
uint8_t s = 0;
uint8_t f = 0;
uint8_t pType = 0;
uint8_t sType = 0;
uint32_t systemBytes = 0;
bool replyExpected = false;
private:
std::vector<ItemPtr> items;
private:
void inputMessageHeader(QDataStream &in);
uint8_t getStreamByte();
void inputItem(QDataStream &in, const ItemPtr &item);
void inputItemInfo(QDataStream &in, const ItemPtr &item);
void outMessageHeader(QDataStream &out);
ItemPtr outItem(QDataStream &out);
void outItemInfo(QDataStream &out, Format &format, int &count);
Value outItemValue(QDataStream &out, const Format &format);
};
class Visitor
{
public:
void operator()(ValueType::ASCII value)
{
qDebug() << value;
}
void operator()(ValueType::Bool value)
{
qDebug() << value;
}
void operator()(ValueType::I1 value)
{
qDebug() << value;
}
void operator()(ValueType::I2 value)
{
qDebug() << value;
}
void operator()(ValueType::I4 value)
{
qDebug() << value;
}
void operator()(ValueType::I8 value)
{
qDebug() << value;
}
void operator()(ValueType::U1 value)
{
qDebug() << value;
}
void operator()(ValueType::U2 value)
{
qDebug() << value;
}
void operator()(ValueType::U4 value)
{
qDebug() << value;
}
void operator()(ValueType::U8 value)
{
qDebug() << value;
}
void operator()(ValueType::Item value)
{
qDebug() << "Item count => " << value->getCount();
}
void operator()(ValueType::F4 value)
{
qDebug() << value;
}
void operator()(ValueType::F8 value)
{
qDebug() << value;
}
};
class CheckVisitor
{
public:
CheckVisitor()
{
}
void operator()(ValueType::ASCII value)
{
qDebug() << value;
}
void operator()(ValueType::Bool value)
{
qDebug() << value;
}
void operator()(ValueType::I1 value)
{
qDebug() << value;
}
void operator()(ValueType::I2 value)
{
qDebug() << value;
}
void operator()(ValueType::I4 value)
{
qDebug() << value;
}
void operator()(ValueType::I8 value)
{
qDebug() << value;
}
void operator()(ValueType::U1 value)
{
qDebug() << value;
}
void operator()(ValueType::U2 value)
{
qDebug() << value;
}
void operator()(ValueType::U4 value)
{
qDebug() << value;
}
void operator()(ValueType::U8 value)
{
qDebug() << value;
}
void operator()(ValueType::Item value)
{
qDebug() << "Item count => " << value->getCount();
}
void operator()(ValueType::F4 value)
{
qDebug() << value;
}
void operator()(ValueType::F8 value)
{
qDebug() << value;
}
private:
Format format;
};
}
#endif // SECS_GEM_H