forked from xR3b0rn/dbcppp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDBCAST2Network.cpp
More file actions
695 lines (683 loc) · 25.1 KB
/
DBCAST2Network.cpp
File metadata and controls
695 lines (683 loc) · 25.1 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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
#include <iterator>
#include <regex>
#include <fstream>
#include <variant>
#include <sstream>
#include <boost/variant.hpp>
#include "../../include/dbcppp/Network.h"
#include "../../include/dbcppp/CApi.h"
#include "DBCX3.h"
using namespace dbcppp;
using namespace dbcppp::DBCX3::AST;
static auto getVersion(const G_Network& gnet)
{
return gnet.version.version;
}
static auto getNewSymbols(const G_Network& gnet)
{
std::vector<std::string> nodes;
for (const auto& ns : gnet.new_symbols)
{
nodes.push_back(ns);
}
return nodes;
}
static auto getSignalType(const G_Network& gnet, const G_ValueTable& vt)
{
std::optional<std::unique_ptr<ISignalType>> signal_type;
auto iter = std::find_if(gnet.signal_types.begin(), gnet.signal_types.end(),
[&](const auto& st)
{
return st.value_table_name == vt.name;
});
if (iter != gnet.signal_types.end())
{
auto& st = *iter;
signal_type = ISignalType::Create(
std::string(st.name)
, st.size
, st.byte_order == '0' ? ISignal::EByteOrder::BigEndian : ISignal::EByteOrder::LittleEndian
, st.value_type == '+' ? ISignal::EValueType::Unsigned : ISignal::EValueType::Signed
, st.factor
, st.offset
, st.minimum
, st.maximum
, std::string(st.unit)
, st.default_value
, std::string(st.value_table_name));
}
return signal_type;
}
static auto getValueTables(const G_Network& gnet)
{
std::vector<std::unique_ptr<IValueTable>> value_tables;
for (const auto& vt : gnet.value_tables)
{
auto sig_type = getSignalType(gnet, vt);
std::vector<std::unique_ptr<IValueEncodingDescription>> copy_ved;
for (const auto& ved : vt.value_encoding_descriptions)
{
auto desc = ved.description;
auto pved = IValueEncodingDescription::Create(ved.value, std::move(desc));
copy_ved.push_back(std::move(pved));
}
auto nvt = IValueTable::Create(std::string(vt.name), std::move(sig_type), std::move(copy_ved));
value_tables.push_back(std::move(nvt));
}
return value_tables;
}
static auto getBitTiming(const G_Network& gnet)
{
std::unique_ptr<IBitTiming> bit_timing;
if (gnet.bit_timing)
{
bit_timing = IBitTiming::Create(gnet.bit_timing->baudrate, gnet.bit_timing->BTR1, gnet.bit_timing->BTR2);
}
else
{
bit_timing = IBitTiming::Create(0, 0, 0);
}
return bit_timing;
}
template <class Variant>
class Visitor
: public boost::static_visitor<void>
{
public:
Visitor(Variant& var)
: _var(var)
{}
template <class T>
void operator()(const T& v)
{
_var = v;
}
private:
Variant& _var;
};
template <class... Args>
auto boost_variant_to_std_variant(const boost::variant<Args...>& old)
{
using var_t = std::variant<Args...>;
var_t new_;
Visitor<var_t> visitor(new_);
old.apply_visitor(visitor);
return new_;
}
static auto getAttributeValues(const G_Network& gnet, const G_Node& n)
{
std::vector<std::unique_ptr<IAttribute>> attribute_values;
for (const variant_attribute_t& av : gnet.attribute_values)
{
auto av_ = boost_variant_to_std_variant(av);
if (auto pav = std::get_if<G_AttributeNode>(&av_); pav && pav->node_name == n.name)
{
auto name = pav->attribute_name;
auto value = boost_variant_to_std_variant(pav->value);
auto attribute = IAttribute::Create(std::move(name), IAttributeDefinition::EObjectType::Node, std::move(value));
attribute_values.push_back(std::move(attribute));
}
}
return attribute_values;
}
static auto getComment(const G_Network& gnet, const G_Node& n)
{
std::string comment;
auto iter_comment = std::find_if(gnet.comments.begin(), gnet.comments.end(),
[&](const auto& c)
{
auto c_ = boost_variant_to_std_variant(c.comment);
auto pcn = std::get_if<G_CommentNode>(&c_);
return pcn && pcn->node_name == n.name;
});
if (iter_comment != gnet.comments.end())
{
auto comment_ = boost_variant_to_std_variant(iter_comment->comment);
comment = std::get<G_CommentNode>(comment_).comment;
}
return comment;
}
static auto getNodes(const G_Network& gnet)
{
std::vector<std::unique_ptr<INode>> nodes;
for (const auto& n : gnet.nodes)
{
auto comment = getComment(gnet, n);
auto attribute_values = getAttributeValues(gnet, n);
auto nn = INode::Create(std::string(n.name), std::move(comment), std::move(attribute_values));
nodes.push_back(std::move(nn));
}
return nodes;
}
static auto getAttributeValues(const G_Network& gnet, const G_Message& m, const G_Signal& s)
{
std::vector<std::unique_ptr<IAttribute>> attribute_values;
for (const auto& vav : gnet.attribute_values)
{
auto vav_ = boost_variant_to_std_variant(vav);
if (auto pas = std::get_if<G_AttributeSignal>(&vav_))
{
const auto& av = *pas;
if (av.message_id == m.id && av.signal_name == s.name)
{
auto value = boost_variant_to_std_variant(av.value);
auto attribute = IAttribute::Create(std::string(av.attribute_name), IAttributeDefinition::EObjectType::Signal, std::move(value));
attribute_values.push_back(std::move(attribute));
}
}
}
return attribute_values;
}
static auto getValueDescriptions(const G_Network& gnet, const G_Message& m, const G_Signal& s)
{
std::vector<std::unique_ptr<IValueEncodingDescription>> value_descriptions;
for (const auto& vds : gnet.value_descriptions_sig_env_var)
{
auto vds_description_ = boost_variant_to_std_variant(vds.description);
if (auto pvds = std::get_if<G_ValueDescriptionSignal>(&vds_description_);
pvds && pvds->message_id == m.id && pvds->signal_name == s.name)
{
auto vds = std::get<G_ValueDescriptionSignal>(vds_description_).value_descriptions;
for (const auto& vd : vds)
{
auto desc = vd.description;
auto pvd = IValueEncodingDescription::Create(vd.value, std::move(desc));
value_descriptions.push_back(std::move(pvd));
}
break;
}
}
return value_descriptions;
}
static auto getComment(const G_Network& gnet, const G_Message& m, const G_Signal& s)
{
std::string comment;
for (const auto& c : gnet.comments)
{
auto c_ = boost_variant_to_std_variant(c.comment);
if (auto pcs = std::get_if<G_CommentSignal>(&c_);
pcs && pcs->message_id == m.id && pcs->signal_name == s.name)
{
comment = std::get<G_CommentSignal>(c_).comment;
break;
}
}
return comment;
}
static auto getSignalExtendedValueType(const G_Network& gnet, const G_Message& m, const G_Signal& s)
{
ISignal::EExtendedValueType extended_value_type = ISignal::EExtendedValueType::Integer;
auto iter = std::find_if(gnet.signal_extended_value_types.begin(), gnet.signal_extended_value_types.end(),
[&](const G_SignalExtendedValueType& sev)
{
return sev.message_id == m.id && sev.signal_name == s.name;
});
if (iter != gnet.signal_extended_value_types.end())
{
switch (iter->value)
{
case 1: extended_value_type = ISignal::EExtendedValueType::Float; break;
case 2: extended_value_type = ISignal::EExtendedValueType::Double; break;
}
}
return extended_value_type;
}
static auto getSignalMultiplexerValues(const G_Network& gnet, const std::string& s, const uint64_t m)
{
std::vector<std::unique_ptr<ISignalMultiplexerValue>> signal_multiplexer_values;
for (const auto& gsmv : gnet.signal_multiplexer_values)
{
if (gsmv.signal_name == s && gsmv.message_id == m)
{
auto switch_name = gsmv.switch_name;
std::vector<ISignalMultiplexerValue::Range> value_ranges;
for (const auto& r : gsmv.value_ranges)
{
value_ranges.push_back({r.from, r.to});
}
auto signal_multiplexer_value = ISignalMultiplexerValue::Create(
std::move(switch_name)
, std::move(value_ranges));
signal_multiplexer_values.push_back(std::move(signal_multiplexer_value));
}
}
return signal_multiplexer_values;
}
static auto getSignals(const G_Network& gnet, const G_Message& m)
{
std::vector<std::unique_ptr<ISignal>> signals;
for (const G_Signal& s : m.signals)
{
std::vector<std::string> receivers;
auto attribute_values = getAttributeValues(gnet, m, s);
auto value_descriptions = getValueDescriptions(gnet, m, s);
auto extended_value_type = getSignalExtendedValueType(gnet, m, s);
auto multiplexer_indicator = ISignal::EMultiplexer::NoMux;
auto comment = getComment(gnet, m, s);
auto signal_multiplexer_values = getSignalMultiplexerValues(gnet, s.name, m.id);
uint64_t multiplexer_switch_value = 0;
if (s.multiplexer_indicator)
{
auto m = *s.multiplexer_indicator;
if (m.substr(0, 1) == "M")
{
multiplexer_indicator = ISignal::EMultiplexer::MuxSwitch;
}
else
{
multiplexer_indicator = ISignal::EMultiplexer::MuxValue;
std::string value = m.substr(1, m.size());
multiplexer_switch_value = std::atoi(value.c_str());
}
}
for (const auto& n : s.receivers)
{
receivers.push_back(n);
}
auto ns = ISignal::Create(
m.size
, std::string(s.name)
, multiplexer_indicator
, multiplexer_switch_value
, s.start_bit
, s.signal_size
, s.byte_order == '0' ? ISignal::EByteOrder::BigEndian : ISignal::EByteOrder::LittleEndian
, s.value_type == '+' ? ISignal::EValueType::Unsigned : ISignal::EValueType::Signed
, s.factor
, s.offset
, s.minimum
, s.maximum
, std::string(s.unit)
, std::move(receivers)
, std::move(attribute_values)
, std::move(value_descriptions)
, std::move(comment)
, extended_value_type
, std::move(signal_multiplexer_values));
if (ns->Error(ISignal::EErrorCode::SignalExceedsMessageSize))
{
std::cout << "Warning: The signals '" << m.name << "::" << s.name << "'"
<< " start_bit + bit_size exceeds the byte size of the message! Ignoring this error will lead to garbage data when using the decode function of this signal." << std::endl;
}
if (ns->Error(ISignal::EErrorCode::WrongBitSizeForExtendedDataType))
{
std::cout << "Warning: The signals '" << m.name << "::" << s.name << "'"
<< " bit_size does not fit the bit size of the specified ExtendedValueType." << std::endl;
}
if (ns->Error(ISignal::EErrorCode::MaschinesFloatEncodingNotSupported))
{
std::cout << "Warning: Signal '" << m.name << "::" << s.name << "'"
<< " This warning appears when a signal uses type float but the system this programm is running on does not uses IEEE 754 encoding for floats." << std::endl;
}
if (ns->Error(ISignal::EErrorCode::MaschinesDoubleEncodingNotSupported))
{
std::cout << "Warning: Signal '" << m.name << "::" << s.name << "'"
<< " This warning appears when a signal uses type double but the system this programm is running on does not uses IEEE 754 encoding for doubles." << std::endl;
}
signals.push_back(std::move(ns));
}
return signals;
}
static auto getMessageTransmitters(const G_Network& gnet, const G_Message& m)
{
std::vector<std::string> message_transmitters;
auto iter_mt = std::find_if(gnet.message_transmitters.begin(), gnet.message_transmitters.end(),
[&](const G_MessageTransmitter& mt)
{
return mt.id == m.id;
});
if (iter_mt != gnet.message_transmitters.end())
{
for (const auto& t : iter_mt->transmitters)
{
message_transmitters.push_back(t);
}
}
return message_transmitters;
}
static auto getAttributeValues(const G_Network& gnet, const G_Message& m)
{
std::vector<std::unique_ptr<IAttribute>> attribute_values;
for (const auto& vav : gnet.attribute_values)
{
auto vav_ = boost_variant_to_std_variant(vav);
if (auto pam = std::get_if<G_AttributeMessage>(&vav_))
{
const auto& av = *pam;
if (av.message_id == m.id)
{
auto value = boost_variant_to_std_variant(av.value);
auto attribute = IAttribute::Create(std::string(av.attribute_name), IAttributeDefinition::EObjectType::Message, std::move(value));
attribute_values.push_back(std::move(attribute));
}
}
}
return attribute_values;
}
static auto getComment(const G_Network& gnet, const G_Message& m)
{
std::string comment;
for (const auto& c : gnet.comments)
{
auto c_ = boost_variant_to_std_variant(c.comment);
if (auto pcm = std::get_if<G_CommentMessage>(&c_);
pcm && pcm->message_id == m.id)
{
comment = std::get<G_CommentMessage>(c_).comment;
break;
}
}
return comment;
}
static auto getSignalGroups(const G_Network& gnet, const G_Message& m)
{
std::vector<std::unique_ptr<ISignalGroup>> signal_groups;
for (const auto& sg : gnet.signal_groups)
{
if (sg.message_id == m.id)
{
auto name = sg.signal_group_name;
auto signal_names = sg.signal_names;
auto signal_group = ISignalGroup::Create(
sg.message_id
, std::move(name)
, sg.repetitions
, std::move(signal_names));
signal_groups.push_back(std::move(signal_group));
}
}
return signal_groups;
}
static auto getMessages(const G_Network& gnet)
{
std::vector<std::unique_ptr<IMessage>> messages;
for (const auto& m : gnet.messages)
{
auto message_transmitters = getMessageTransmitters(gnet, m);
auto signals = getSignals(gnet, m);
auto attribute_values = getAttributeValues(gnet, m);
auto comment = getComment(gnet, m);
auto signal_groups = getSignalGroups(gnet, m);
auto msg = IMessage::Create(
m.id
, std::string(m.name)
, m.size
, std::string(m.transmitter)
, std::move(message_transmitters)
, std::move(signals)
, std::move(attribute_values)
, std::move(comment)
, std::move(signal_groups));
if (msg->Error() == IMessage::EErrorCode::MuxValeWithoutMuxSignal)
{
std::cout << "Warning: Message " << msg->Name() << " does have mux value but no mux signal!" << std::endl;
}
messages.push_back(std::move(msg));
}
return messages;
}
static auto getValueDescriptions(const G_Network& gnet, const G_EnvironmentVariable& ev)
{
std::vector<std::unique_ptr<IValueEncodingDescription>> value_descriptions;
for (const auto& vds : gnet.value_descriptions_sig_env_var)
{
auto vds_description = boost_variant_to_std_variant(vds.description);
if (auto pvde = std::get_if<G_ValueDescriptionEnvVar>(&vds_description);
pvde && pvde->env_var_name == ev.name)
{
auto vds = std::get<G_ValueDescriptionEnvVar>(vds_description).value_descriptions;
for (const auto& vd : vds)
{
auto desc = vd.description;
auto pvd = IValueEncodingDescription::Create(vd.value, std::move(desc));
value_descriptions.push_back(std::move(pvd));
}
break;
}
}
return value_descriptions;
}
static auto getAttributeValues(const G_Network& gnet, const G_EnvironmentVariable& ev)
{
std::vector<std::unique_ptr<IAttribute>> attribute_values;
for (const auto& vav : gnet.attribute_values)
{
auto vav_ = boost_variant_to_std_variant(vav);
if (auto paev = std::get_if<G_AttributeEnvVar>(&vav_))
{
const auto& av = *paev;
if (av.env_var_name == ev.name)
{
auto value = boost_variant_to_std_variant(av.value);
auto attribute = IAttribute::Create(std::string(av.attribute_name), IAttributeDefinition::EObjectType::EnvironmentVariable, std::move(value));
attribute_values.push_back(std::move(attribute));
}
}
}
return attribute_values;
}
static auto getComment(const G_Network& gnet, const G_EnvironmentVariable& ev)
{
std::string comment;
for (const auto& c : gnet.comments)
{
auto c_ = boost_variant_to_std_variant(c.comment);
if (auto pce = std::get_if<G_CommentEnvVar>(&c_);
pce && pce->env_var_name == ev.name)
{
comment = std::get<G_CommentEnvVar>(c_).comment;
break;
}
}
return comment;
}
static auto getEnvironmentVariables(const G_Network& gnet)
{
std::vector<std::unique_ptr<IEnvironmentVariable>> environment_variables;
for (const auto& ev : gnet.environment_variables)
{
IEnvironmentVariable::EVarType var_type;
IEnvironmentVariable::EAccessType access_type;
std::vector<std::string> access_nodes = ev.access_nodes;
auto value_descriptions = getValueDescriptions(gnet, ev);
auto attribute_values = getAttributeValues(gnet, ev);
auto comment = getComment(gnet, ev);
uint64_t data_size = 0;
switch (ev.var_type)
{
case 0: var_type = IEnvironmentVariable::EVarType::Integer; break;
case 1: var_type = IEnvironmentVariable::EVarType::Float; break;
case 2: var_type = IEnvironmentVariable::EVarType::String; break;
}
access_type = IEnvironmentVariable::EAccessType::Unrestricted;
if (ev.access_type == "DUMMY_NODE_VECTOR0") access_type = IEnvironmentVariable::EAccessType::Unrestricted;
else if (ev.access_type == "DUMMY_NODE_VECTOR1") access_type = IEnvironmentVariable::EAccessType::Read;
else if (ev.access_type == "DUMMY_NODE_VECTOR2") access_type = IEnvironmentVariable::EAccessType::Write;
else if (ev.access_type == "DUMMY_NODE_VECTOR3") access_type = IEnvironmentVariable::EAccessType::ReadWrite;
else if (ev.access_type == "DUMMY_NODE_VECTOR8000") access_type = IEnvironmentVariable::EAccessType::Unrestricted_;
else if (ev.access_type == "DUMMY_NODE_VECTOR8001") access_type = IEnvironmentVariable::EAccessType::Read_;
else if (ev.access_type == "DUMMY_NODE_VECTOR8002") access_type = IEnvironmentVariable::EAccessType::Write_;
else if (ev.access_type == "DUMMY_NODE_VECTOR8003") access_type = IEnvironmentVariable::EAccessType::ReadWrite_;
for (auto& evd : gnet.environment_variable_datas)
{
if (evd.name == ev.name)
{
var_type = IEnvironmentVariable::EVarType::Data;
data_size = evd.size;
break;
}
}
auto env_var = IEnvironmentVariable::Create(
std::string(ev.name)
, var_type
, ev.minimum
, ev.maximum
, std::string(ev.unit)
, ev.initial_value
, ev.id
, access_type
, std::move(access_nodes)
, std::move(value_descriptions)
, data_size
, std::move(attribute_values)
, std::move(comment));
environment_variables.push_back(std::move(env_var));
}
return environment_variables;
}
static auto getAttributeDefinitions(const G_Network& gnet)
{
std::vector<std::unique_ptr<IAttributeDefinition>> attribute_definitions;
struct VisitorValueType
{
IAttributeDefinition::value_type_t operator()(const G_AttributeValueTypeInt& cn)
{
IAttributeDefinition::ValueTypeInt vt;
vt.minimum = cn.minimum;
vt.maximum = cn.maximum;
return vt;
}
IAttributeDefinition::value_type_t operator()(const G_AttributeValueTypeHex& cn)
{
IAttributeDefinition::ValueTypeHex vt;
vt.minimum = cn.minimum;
vt.maximum = cn.maximum;
return vt;
}
IAttributeDefinition::value_type_t operator()(const G_AttributeValueTypeFloat& cn)
{
IAttributeDefinition::ValueTypeFloat vt;
vt.minimum = cn.minimum;
vt.maximum = cn.maximum;
return vt;
}
IAttributeDefinition::value_type_t operator()(const G_AttributeValueTypeString& cn)
{
return IAttributeDefinition::ValueTypeString();
}
IAttributeDefinition::value_type_t operator()(const G_AttributeValueTypeEnum& cn)
{
IAttributeDefinition::ValueTypeEnum vt;
for (auto& e : cn.values)
{
vt.values.emplace_back(e);
}
return vt;
}
};
for (const auto& ad : gnet.attribute_definitions)
{
IAttributeDefinition::EObjectType object_type;
auto cvt = ad.value_type;
if (!ad.object_type)
{
object_type = IAttributeDefinition::EObjectType::Network;
}
else if (*ad.object_type == "BU_")
{
object_type = IAttributeDefinition::EObjectType::Node;
}
else if (*ad.object_type == "BO_")
{
object_type = IAttributeDefinition::EObjectType::Message;
}
else if (*ad.object_type == "SG_")
{
object_type = IAttributeDefinition::EObjectType::Signal;
}
else
{
object_type = IAttributeDefinition::EObjectType::EnvironmentVariable;
}
VisitorValueType vvt;
auto value = boost_variant_to_std_variant(cvt.value);
std::visit(vvt, value);
auto nad = IAttributeDefinition::Create(std::move(std::string(ad.name)), object_type, std::visit(vvt, value));
attribute_definitions.push_back(std::move(nad));
}
return attribute_definitions;
}
static auto getAttributeDefaults(const G_Network& gnet)
{
std::vector<std::unique_ptr<IAttribute>> attribute_defaults;
for (auto& ad : gnet.attribute_defaults)
{
auto value = boost_variant_to_std_variant(ad.value);
auto nad = IAttribute::Create(std::string(ad.name), IAttributeDefinition::EObjectType::Network, value);
attribute_defaults.push_back(std::move(nad));
}
return attribute_defaults;
}
static auto getAttributeValues(const G_Network& gnet)
{
std::vector<std::unique_ptr<IAttribute>> attribute_values;
for (const auto& av : gnet.attribute_values)
{
auto av_ = boost_variant_to_std_variant(av);
if (auto pan = std::get_if<G_AttributeNetwork>(&av_))
{
auto av_ = *pan;
auto value = boost_variant_to_std_variant(av_.value);
auto attribute = IAttribute::Create(
std::string(av_.attribute_name)
, IAttributeDefinition::EObjectType::Network
, std::move(value));
attribute_values.push_back(std::move(attribute));
}
}
return attribute_values;
}
static auto getComment(const G_Network& gnet)
{
std::string comment;
for (const auto& c : gnet.comments)
{
auto c_ = boost_variant_to_std_variant(c.comment);
if (auto pcn = std::get_if<G_CommentNetwork>(&c_))
{
comment = pcn->comment;
break;
}
}
return comment;
}
std::unique_ptr<INetwork> DBCAST2Network(const G_Network& gnet)
{
return INetwork::Create(
getVersion(gnet)
, getNewSymbols(gnet)
, getBitTiming(gnet)
, getNodes(gnet)
, getValueTables(gnet)
, getMessages(gnet)
, getEnvironmentVariables(gnet)
, getAttributeDefinitions(gnet)
, getAttributeDefaults(gnet)
, getAttributeValues(gnet)
, getComment(gnet));
}
std::unique_ptr<INetwork> INetwork::LoadDBCFromIs(std::istream& is)
{
std::string str((std::istreambuf_iterator<char>(is)), std::istreambuf_iterator<char>());
std::unique_ptr<dbcppp::INetwork> network;
if (auto gnet = dbcppp::DBCX3::ParseFromMemory(str.c_str(), str.c_str() + str.size()))
{
network = DBCAST2Network(*gnet);
}
return network;
}
extern "C"
{
DBCPPP_API const dbcppp_Network* dbcppp_NetworkLoadDBCFromFile(const char* filename)
{
std::ifstream is(filename);
auto net = INetwork::LoadDBCFromIs(is);
return reinterpret_cast<const dbcppp_Network*>(net.release());
}
DBCPPP_API const dbcppp_Network* dbcppp_NetworkLoadDBCFromMemory(const char* data)
{
std::istringstream iss(data);
auto net = INetwork::LoadDBCFromIs(iss);
return reinterpret_cast<const dbcppp_Network*>(net.release());
}
}