-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdatatype.h
387 lines (338 loc) · 12.4 KB
/
datatype.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
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
#ifndef PLC_EMULATOR__DATATYPE_H_
#define PLC_EMULATOR__DATATYPE_H_
#include <string>
#include <cstdint>
#include <unordered_map>
#include "elementary_datatypes.h"
#include "config.pb.h"
namespace plc_emulator {
class DataType;
class Config;
class DataTypeField {
private:
std::string name_;
std::string type_;
int if_type_;
config::DataTypeCategory category_;
uint64_t range_min_;
uint64_t range_max_;
std::string initial_value_;
DataType *type_ptr_;
int qualifier_;
std::string associated_resource_name_;
int storage_mem_type_;
int storage_byte_offset_;
int storage_bit_offset_;
std::string full_storage_spec_;
std::string holding_pou_type_;
int n_dimensions_;
int dim1_;
int dim2_;
public:
DataTypeField()
: name_(""),
type_(""),
range_min_(0),
range_max_(0),
initial_value_(""),
category_(static_cast<config::DataTypeCategory>(0)),
if_type_(config::FieldInterfaceType::NA),
type_ptr_(nullptr),
qualifier_(config::FieldQualifiers::NONE),
associated_resource_name_("NONE"),
storage_mem_type_(-1),
storage_byte_offset_(-1),
storage_bit_offset_(-1),
n_dimensions_(-1),
dim1_(-1),
dim2_(-1),
full_storage_spec_("NONE"),
holding_pou_type_("__NONE__") {};
DataTypeField(std::string name,
std::string type,
config::DataTypeCategory category,
int qualifier,
uint64_t range_min,
uint64_t range_max,
std::string initial_value,
int if_type,
DataType *type_ptr,
std::string resource_name = "NONE",
std::string full_storage_spec = "NONE")
: name_(std::move(name)),
type_(std::move(type)),
range_min_(range_min),
range_max_(range_max),
initial_value_(std::move(initial_value)),
category_(category),
if_type_(if_type),
qualifier_(qualifier),
type_ptr_(type_ptr),
associated_resource_name_(std::move(resource_name)),
storage_mem_type_(-1),
storage_byte_offset_(-1),
storage_bit_offset_(-1),
n_dimensions_(-1),
dim1_(-1),
dim2_(-1),
full_storage_spec_(std::move(full_storage_spec)),
holding_pou_type_("__NONE__") {};
std::string GetName() {
return name_;
}
std::string GetType() {
return type_;
}
int GetRangeMin() {
return range_min_;
}
int GetRangeMax() {
return range_max_;
}
std::string GetInitialValue() {
return initial_value_;
}
config::DataTypeCategory GetCategory() {
return category_;
}
int GetInterfaceType() {
return if_type_;
}
int GetQualifier() {
return qualifier_;
}
DataType *GetTypePtr() {
return type_ptr_;
}
std::string GetAssociatedResourceName() {
return associated_resource_name_;
}
int GetStorageMemType() {
return storage_mem_type_;
}
int GetStorageByteOffset() {
return storage_byte_offset_;
}
int GetStorageBitOffset() {
return storage_bit_offset_;
}
std::string GetFullStorageSpec() {
return full_storage_spec_;
}
int GetDimension() {
return n_dimensions_;
}
int GetDimension(int dim) {
return dim == 1 ? dim1_ : dim2_;
}
std::string GetHoldingPouType() {
return holding_pou_type_;
}
void SetExplicitStorageConstraints(int mem_type,
int byte_offset,
int bit_offset);
void Copy(DataTypeField &other) {
name_ = other.GetName();
type_ = other.GetType();
range_min_ = other.GetRangeMin();
range_max_ = other.GetRangeMax();
initial_value_ = other.GetInitialValue();
category_ = other.GetCategory();
if_type_ = other.GetInterfaceType();
qualifier_ = other.GetQualifier();
type_ptr_ = other.GetTypePtr();
storage_mem_type_ = other.GetStorageMemType();
storage_byte_offset_ = other.GetStorageByteOffset();
storage_bit_offset_ = other.GetStorageBitOffset();
n_dimensions_ = other.GetDimension();
dim1_ = other.GetDimension(1);
dim2_ = other.GetDimension(2);
}
};
class DataType {
private:
std::string alias_;
std::string name_;
Config *config_;
std::unordered_map<int, std::vector<DataTypeField>> fields_by_if_type_;
config::DataTypeCategory category_;
int pou_type_;
int size_in_bits_;
int n_fields_;
uint64_t range_min_;
uint64_t range_max_;
std::string initial_value_;
std::vector<int> dim_size_;
bool CheckRemFields(std::vector<std::string> &nested_fields,
int start_pos,
DataType *current);
bool CheckRemFields(std::vector<std::string> &nested_fields,
int start_pos,
DataType *current,
DataTypeField &result);
void SetElementaryDataTypeAttributes(std::string initial_value,
uint64_t range_min,
uint64_t range_max);
bool GetDataFieldOfArrayElement(DataTypeField &defined_field,
DataTypeField &result,
int i,
int j = -1);
public:
DataType(Config *config,
std::string alias,
std::string type,
config::DataTypeCategory category = config::DataTypeCategory::NOT_ASSIGNED,
std::string initial_value = "",
uint64_t range_min = LLONG_MIN,
uint64_t range_max = LLONG_MAX);
DataType(Config *config,
std::string alias,
std::string type,
int dim_size,
config::DataTypeCategory category,
std::string initial_value = "",
uint64_t range_min = LLONG_MIN,
uint64_t range_max = LLONG_MAX);
DataType(Config *config,
std::string alias,
std::string type,
int dim1,
int dim2,
config::DataTypeCategory category,
std::string initial_value = "",
uint64_t range_min = LLONG_MIN,
uint64_t range_max = LLONG_MAX);
std::string GetName() {
return name_;
}
config::DataTypeCategory GetCategory() {
return category_;
}
bool IsFieldPresent(std::string nested_field_name);
DataType *LookupDataType(std::string name);
void AddDataTypeField(std::string name,
std::string type,
int if_type,
int qualifier,
uint64_t range_min,
uint64_t range_max,
std::string full_storage_spec = "NONE");
void AddDataTypeField(std::string name,
std::string type,
config::DataTypeCategory category,
std::string initial_value,
int if_type,
int qualifier,
uint64_t range_min,
uint64_t range_max,
std::string full_storage_spec = "NONE");
void AddArrayDataTypeField(std::string name,
std::string type,
int dim_size,
std::string initial_value,
int if_type,
int qualifier,
uint64_t range_min,
uint64_t range_max,
std::string full_storage_spec = "NONE");
void AddArrayDataTypeField(std::string name,
std::string type,
int dim1,
int dim2,
std::string initial_value,
int if_type,
int qualifier,
uint64_t range_min,
uint64_t range_max,
std::string full_storage_spec = "NONE");
void AddDataTypeFieldAt(std::string name,
std::string type,
std::string initial_value,
int qualifier,
uint64_t range_min,
uint64_t range_max,
int mem_type,
int byte_offset,
int bit_offset,
std::string resource_name = "NONE",
std::string full_storage_spec = "NONE");
void AddDataTypeFieldAt(std::string name,
std::string type,
config::DataTypeCategory category,
std::string initial_value,
int qualifier,
uint64_t range_min,
uint64_t range_max,
int mem_type,
int byte_offset,
int bit_offset,
std::string resource_name = "NONE",
std::string full_storage_spec = "NONE");
void AddDataTypeFieldAt(std::string name,
std::string type,
int dim_size,
std::string initial_value,
int qualifier,
uint64_t range_min,
uint64_t range_max,
int mem_type,
int byte_offset,
int bit_offset,
std::string resource_name = "NONE",
std::string full_storage_spec = "NONE");
void AddDataTypeFieldAt(std::string name,
std::string type,
int dim1,
int dim2,
std::string initial_value,
int qualifier,
uint64_t range_min,
uint64_t range_max,
int mem_type,
int byte_offset,
int bit_offset,
std::string resource_name = "NONE",
std::string full_storage_spec = "NONE");
bool GetDataTypeField(std::string nested_field_name, DataTypeField &result);
void Cleanup();
};
class DataTypeUtils {
public:
static bool ValueToBool(std::string value, bool &r);
static bool ValueToByte(std::string value, uint8_t &r);
static bool ValueToWord(std::string value, uint16_t &r);
static bool ValueToDWord(std::string value, uint32_t &r);
static bool ValueToQWord(std::string value, uint64_t &r);
static bool ValueToChar(std::string value, char &r);
static bool ValueToShort(std::string value, int16_t &r);
static bool ValueToInt(std::string value, int32_t &r);
static bool ValueToLongInt(std::string value, int64_t &r);
static bool ValueToFloat(std::string value, float &r);
static bool ValueToDouble(std::string value, double &r);
static bool ValueToDate(std::string value, DateType &r);
static bool ValueToTime(std::string value, TimeType &r);
static bool ValueToDateTime(std::string value, DateTime &r);
static bool BoolToAny(bool Value, int category, std::string &r);
static bool ByteToAny(uint8_t Value, int category, std::string &r);
static bool WordToAny(uint16_t Value, int category, std::string &r);
static bool DWordToAny(uint32_t Value, int category, std::string &r);
static bool QWordToAny(uint64_t Value, int category, std::string &r);
static bool CharToAny(char Value, int category, std::string &r);
static bool ShortToAny(int16_t Value, int category, std::string &r);
static bool IntToAny(int32_t Value, int category, std::string &r);
static bool LongIntToAny(int64_t Value, int category, std::string &r);
static bool FloatToAny(float Value, int category, std::string &r);
static bool DoubleToAny(double Value, int category, std::string &r);
static bool TimeToAny(TimeType time_value, int category, std::string &r);
static std::string DateTimeToString(DateTime &dt);
static std::string DateToString(Date &date1);
static void AddToDT(DateTime &Dt, TimeType &Time);
static void AddToTOD(Time &tod, TimeType &Time);
static void SubFromDT(DateTime &Dt, TimeType &Time);
static void SubFromTOD(Time &tod, TimeType &Time);
static TimeType SubDTs(DateTime &Dt1, DateTime &Dt2);
static TimeType SubTODs(Time &tod1, Time &tod2);
static TimeType SubDATEs(Date &date1, Date &date2);
};
}
#endif //PLC_EMULATOR__DATATYPE_H_