ADTF
Loading...
Searching...
No Matches
xml_ddtoxml_factory.h
Go to the documentation of this file.
1
14
15#ifndef DD_DD_TO_XML_FACTORY_H_INCLUDED
16#define DD_DD_TO_XML_FACTORY_H_INCLUDED
17
23
24#include <stdexcept>
25#include <string>
26
27namespace ddl {
28namespace dd {
29
38template <typename DOM_NODE_TYPE>
50 template <typename T>
51 static bool setOptionalAttribute(DOM_NODE_TYPE& dom_node,
52 const std::string& attribute_name,
53 const utility::Optional<T>& value)
54 {
55 if (value) {
56 dom_node.setAttribute(attribute_name, std::to_string(*value));
57 return true;
58 }
59 return false;
60 }
61
72 static bool setOptionalAttribute(DOM_NODE_TYPE& dom_node,
73 const std::string& attribute_name,
74 const std::string& value)
75 {
76 if (!value.empty()) {
77 dom_node.setAttribute(attribute_name, value);
78 return true;
79 }
80 return false;
81 }
82
90 static void setData(DOM_NODE_TYPE& dom_node,
91 const std::string& sub_node_name,
92 const std::string& value)
93 {
94 DOM_NODE_TYPE sub_node = dom_node.createChild(sub_node_name);
95 sub_node.setData(value);
96 }
97
104 static void createNode(DOM_NODE_TYPE& parent_node, const datamodel::Header& header)
105 {
106 const auto& literals = DDXmlLiterals::getInstance();
107 DOM_NODE_TYPE sub_node = parent_node.createChild(literals.header);
108 setData(sub_node,
109 literals.language_version,
111 setData(sub_node, literals.author, header.getAuthor());
112 setData(sub_node, literals.date_creation, header.getDateCreation());
113 setData(sub_node, literals.date_change, header.getDateChange());
114 setData(sub_node, literals.description, header.getDescription());
115 const auto& declarations = header.getExtDeclarations();
116 for (auto ext = declarations.cbegin(); ext != declarations.cend(); ++ext) {
117 DOM_NODE_TYPE sub_ext_node = sub_node.createChild(literals.ext_declaration);
118 sub_ext_node.setAttribute(literals.key, ext->second->getKey());
119 sub_ext_node.setAttribute(literals.value, ext->second->getValue());
120 }
121 }
122
129 static void createNode(DOM_NODE_TYPE& parent_node, const datamodel::BaseUnit& base_unit)
130 {
131 const auto& literals = DDXmlLiterals::getInstance();
132 DOM_NODE_TYPE sub_node = parent_node.createChild(literals.baseunit);
133 sub_node.setAttribute(literals.name, base_unit.getName());
134 sub_node.setAttribute(literals.symbol, base_unit.getSymbol());
135 sub_node.setAttribute(literals.description, base_unit.getDescription());
136 }
137
144 static void createNode(DOM_NODE_TYPE& parent_node, const datamodel::UnitPrefix& unit_prefix)
145 {
146 const auto& literals = DDXmlLiterals::getInstance();
147 DOM_NODE_TYPE sub_node = parent_node.createChild(literals.prefixes);
148 sub_node.setAttribute(literals.name, unit_prefix.getName());
149 auto power = std::to_string(unit_prefix.getPower());
150 sub_node.setAttribute(literals.power, power);
151 sub_node.setAttribute(literals.symbol, unit_prefix.getSymbol());
152 }
153
160 static void createNode(DOM_NODE_TYPE& parent_node, const datamodel::Unit& unit)
161 {
162 const auto& literals = DDXmlLiterals::getInstance();
163 DOM_NODE_TYPE sub_node = parent_node.createChild(literals.unit);
164 sub_node.setAttribute(literals.name, unit.getName());
165 setData(sub_node, literals.numerator, unit.getNumerator());
166 setData(sub_node, literals.denominator, unit.getDenominator());
167 setData(sub_node, literals.offset, unit.getOffset());
168 const auto& ref_units = unit.getRefUnits();
169 for (auto ref_unit = ref_units.cbegin(); ref_unit != ref_units.cend(); ++ref_unit) {
170 DOM_NODE_TYPE sub_node_elem = sub_node.createChild(literals.refUnit);
171 sub_node_elem.setAttribute(literals.name, (*ref_unit).getUnitName());
172 sub_node_elem.setAttribute(literals.power, std::to_string((*ref_unit).getPower()));
173 sub_node_elem.setAttribute(literals.prefix, (*ref_unit).getPrefixName());
174 }
175 }
176
184 static void createNode(DOM_NODE_TYPE& parent_node,
186 const Version& file_ddl_version)
187 {
188 const auto& literals = DDXmlLiterals::getInstance();
189 DOM_NODE_TYPE sub_node = parent_node.createChild(literals.datatype);
190 // changes between 2.0 and 3.0
191 // attribute name changed to "name" from "type"
192 // mandatory
193 if (file_ddl_version >= Version(3, 0)) {
194 sub_node.setAttribute(literals.name, data_type.getName());
195 }
196 else {
197 sub_node.setAttribute(literals.type__, data_type.getName());
198 }
199 sub_node.setAttribute(literals.size, std::to_string(data_type.getBitSize()));
200 // optional
201 setOptionalAttribute(sub_node, literals.description, data_type.getDescription());
202 setOptionalAttribute<size_t>(sub_node, literals.arraysize, data_type.getArraySize());
203 setOptionalAttribute(sub_node, literals.unit, data_type.getUnitName());
204 // min / max introduced in DataDefinition 3.0
205 if (file_ddl_version >= Version(3, 0)) {
206 setOptionalAttribute(sub_node, literals.min, data_type.getMin());
207 setOptionalAttribute(sub_node, literals.max, data_type.getMax());
208 }
209 }
210
217 static void createNode(DOM_NODE_TYPE& parent_node, const datamodel::EnumType& enum_type)
218 {
219 const auto& literals = DDXmlLiterals::getInstance();
220 DOM_NODE_TYPE sub_node = parent_node.createChild(literals.enum__);
221 sub_node.setAttribute(literals.name, enum_type.getName());
222 sub_node.setAttribute(literals.type__, enum_type.getDataTypeName());
223 const auto& elems = enum_type.getElements();
224 for (auto elem = elems.cbegin(); elem != elems.cend(); ++elem) {
225 DOM_NODE_TYPE sub_node_elem = sub_node.createChild(literals.element);
226 sub_node_elem.setAttribute(literals.name, elem->second->getName());
227 sub_node_elem.setAttribute(literals.value, elem->second->getValue());
228 }
229 }
230
238 static void createNode(DOM_NODE_TYPE& parent_node,
239 const datamodel::StructType::Element& element,
240 const Version& file_ddl_version)
241 {
242 const auto& literals = DDXmlLiterals::getInstance();
243 DOM_NODE_TYPE sub_node = parent_node.createChild(literals.element);
244 sub_node.setAttribute(literals.name, element.getName());
245 sub_node.setAttribute(literals.type__, element.getTypeName());
246
247 // From version 4.0 on this information is specified within the <deserialized> tag.
248 DOM_NODE_TYPE* serialized_node = &sub_node;
249 DOM_NODE_TYPE serialized_node_version_40;
250 if (file_ddl_version >= Version(4, 0)) {
251 serialized_node_version_40 = sub_node.createChild(literals.serialized);
252 serialized_node = &serialized_node_version_40;
253 }
254 serialized_node->setAttribute(literals.bytepos, std::to_string(*element.getBytePos()));
255 serialized_node->setAttribute(literals.byteorder,
257 setOptionalAttribute<size_t>(*serialized_node, literals.bitpos, element.getBitPos());
258 setOptionalAttribute<size_t>(*serialized_node, literals.numbits, element.getNumBits());
259 // From version 4.0 on this information is specified within the <serialized> tag.
260 DOM_NODE_TYPE* deserialized_node = &sub_node;
261 DOM_NODE_TYPE deserialized_node_version_40;
262 if (file_ddl_version >= Version(4, 0)) {
263 deserialized_node_version_40 = sub_node.createChild(literals.deserialized);
264 deserialized_node = &deserialized_node_version_40;
265 }
266 deserialized_node->setAttribute(literals.alignment, std::to_string(element.getAlignment()));
267 // optional
268 setOptionalAttribute(sub_node, literals.description, element.getDescription());
269 setOptionalAttribute(sub_node, literals.unit, element.getUnitName());
270 setOptionalAttribute(sub_node, literals.comment, element.getComment());
271
272 // in DataDefinition 2.0 dynamic arrays are introduced
273 if (file_ddl_version >= Version(2, 0) && element.getArraySize().isDynamicArraySize()) {
275 sub_node, literals.arraysize, element.getArraySize().getArraySizeElementName());
276 }
277 else {
278 auto array_size = element.getArraySize().getArraySizeValue();
279 if (array_size == 0) {
280 array_size = 1;
281 }
282 sub_node.setAttribute(literals.arraysize, std::to_string(array_size));
283 }
284 // introduced in DataDefinition 2.0
285 if (file_ddl_version >= Version(2, 0)) {
286 setOptionalAttribute(sub_node, literals.value, element.getValue());
287 }
288 // introduced in DataDefinition 3.0
289 if (file_ddl_version >= Version(3, 0)) {
290 setOptionalAttribute(sub_node, literals.min, element.getMin());
291 setOptionalAttribute(sub_node, literals.max, element.getMax());
292 setOptionalAttribute(sub_node, literals.default__, element.getDefault());
293 setOptionalAttribute(sub_node, literals.scale, element.getScale());
294 setOptionalAttribute(sub_node, literals.offset, element.getOffset());
295 }
296 if (file_ddl_version >= Version(4, 2)) {
298 sub_node, literals.valid_element_count, element.getValidElementCount());
299 }
300 }
301
308 static void createNode(DOM_NODE_TYPE& parent_node,
310 const Version& file_ddl_version)
311 {
312 const auto& literals = DDXmlLiterals::getInstance();
313 DOM_NODE_TYPE sub_node = parent_node.createChild(literals.struct__);
314 sub_node.setAttribute(literals.name, struct_type.getName());
315 sub_node.setAttribute(literals.version, struct_type.getVersion());
316
317 setOptionalAttribute<size_t>(sub_node, literals.alignment, struct_type.getAlignment());
318 setOptionalAttribute(sub_node, literals.comment, struct_type.getComment());
319 if (struct_type.getLanguageVersion() != Version(0, 0)) {
320 sub_node.setAttribute(literals.ddlversion,
321 VersionConversion::toString(struct_type.getLanguageVersion()));
322 }
323 const auto& elements = struct_type.getElements();
324 for (auto elem = elements.cbegin(); elem != elements.cend(); ++elem) {
325 auto current_elem = *elem;
326 createNode(sub_node, *current_elem, file_ddl_version);
327 }
328 }
329
335 static void createNode(DOM_NODE_TYPE& parent_node,
337 {
338 const auto& literals = DDXmlLiterals::getInstance();
339 DOM_NODE_TYPE sub_node = parent_node.createChild(literals.streammetatype);
340 sub_node.setAttribute(literals.name, stream_meta_type.getName());
341 sub_node.setAttribute(literals.version, stream_meta_type.getVersion());
342 setOptionalAttribute(sub_node, literals.parent, stream_meta_type.getParent());
343
344 const auto& props = stream_meta_type.getProperties();
345 for (auto prop = props.cbegin(); prop != props.cend(); ++prop) {
346 DOM_NODE_TYPE sub_prop_node = sub_node.createChild(literals.property);
347 sub_prop_node.setAttribute(literals.name, prop->second->getName());
348 sub_prop_node.setAttribute(literals.type__, prop->second->getType());
349 }
350 }
351
358 static void createNode(DOM_NODE_TYPE& parent_node, const datamodel::Stream& stream)
359 {
360 const auto& literals = DDXmlLiterals::getInstance();
361 DOM_NODE_TYPE sub_node = parent_node.createChild(literals.stream);
362 sub_node.setAttribute(literals.name, stream.getName());
363 sub_node.setAttribute(literals.type__, stream.getStreamTypeName());
364 setOptionalAttribute(sub_node, literals.description, stream.getDescription());
365
366 const auto& stream_structs = stream.getStructs();
367 for (auto stream_struct = stream_structs.cbegin(); stream_struct != stream_structs.cend();
368 ++stream_struct) {
369 DOM_NODE_TYPE sub_struct_node = sub_node.createChild(literals.struct__);
370 setOptionalAttribute(sub_struct_node, literals.name, (*stream_struct)->getName());
371 sub_struct_node.setAttribute(literals.type__, (*stream_struct)->getTypeName());
372 auto byte_pos = (*stream_struct)->getBytePos();
373 if (!byte_pos) {
374 byte_pos = 0;
375 }
376 sub_struct_node.setAttribute(literals.bytepos, std::to_string(*byte_pos));
377 }
378 }
379
386 static void createNode(DOM_NODE_TYPE& parent_node, const datamodel::DataDefinition& dd)
387 {
388 const auto& literals = DDXmlLiterals::getInstance();
389
390 createNode(parent_node, *(dd.getHeader()));
391
392 DOM_NODE_TYPE units_node = parent_node.createChild(literals.units);
393 // write base unit types
394 const auto& base_units = dd.getBaseUnits();
395 for (auto base_unit = base_units.cbegin(); base_unit != base_units.cend(); ++base_unit) {
396 createNode(units_node, *(base_unit->second));
397 }
398 // write unit prefix types
399 const auto& unit_prefixes = dd.getUnitPrefixes();
400 for (auto unit_prefix = unit_prefixes.cbegin(); unit_prefix != unit_prefixes.cend();
401 ++unit_prefix) {
402 createNode(units_node, *(unit_prefix->second));
403 }
404 // write unit types
405 const auto& units = dd.getUnits();
406 for (auto unit = units.cbegin(); unit != units.cend(); ++unit) {
407 createNode(units_node, *(unit->second));
408 }
409
410 // write data types
411 DOM_NODE_TYPE data_types_node = parent_node.createChild(literals.datatypes);
412 const auto& data_types = dd.getDataTypes();
413 for (auto data_type = data_types.cbegin(); data_type != data_types.cend(); ++data_type) {
414 createNode(data_types_node, *(data_type->second), dd.getVersion());
415 }
416 // write enum types
417 DOM_NODE_TYPE enum_types_node = parent_node.createChild(literals.enums);
418 const auto& enum_types = dd.getEnumTypes();
419 for (auto enum_type = enum_types.cbegin(); enum_type != enum_types.cend(); ++enum_type) {
420 createNode(enum_types_node, *(enum_type->second));
421 }
422 // write struct types
423 DOM_NODE_TYPE struct_types_node = parent_node.createChild(literals.structs);
424 const auto& struct_types = dd.getStructTypes();
425 for (auto struct_type = struct_types.cbegin(); struct_type != struct_types.cend();
426 ++struct_type) {
427 createNode(struct_types_node, *(struct_type->second), dd.getVersion());
428 }
429
430 // write stream meta types types
431 if (dd.getVersion() >= dd::Version(4, 0)) {
432 DOM_NODE_TYPE stream_meta_types_node =
433 parent_node.createChild(literals.streammetatypes);
434 const auto& stream_meta_types_types = dd.getStreamMetaTypes();
435 for (auto stream_meta_type = stream_meta_types_types.cbegin();
436 stream_meta_type != stream_meta_types_types.cend();
438 createNode(stream_meta_types_node, *(stream_meta_type->second));
439 }
440 }
441
442 DOM_NODE_TYPE streams_node = parent_node.createChild(literals.streams);
443 const auto& streams = dd.getStreams();
444 for (auto stream = streams.cbegin(); stream != streams.cend(); ++stream) {
445 createNode(streams_node, *(stream->second));
446 }
447 }
448};
449} // namespace dd
450} // namespace ddl
451
452#endif // DD_DD_TO_XML_FACTORY_H_INCLUDED
size_t getArraySizeValue() const
const std::string & getArraySizeElementName() const
bool isDynamicArraySize() const
static std::string toString(ByteOrder byte_order)
convert byte_order to a string.
static std::string toString(const Version &version)
Version to string conversion.
DDL Version.
Definition dd_common_types.h:203
BaseUnit.
Definition datamodel_units.h:108
DataDefinition Datamodel This datamodel is observable for any change of:
Definition datamodel_datadefinition.h:64
observable DataDefinition object class to describe (POD) DataType.
Definition datamodel_types.h:113
observable DataDefinition object class to describe EnumType.
Definition datamodel_types.h:320
Data Definition datamodel for the Header.
Definition datamodel_header.h:37
Version getLanguageVersion() const
Get the Language Version.
const std::string & getAuthor() const
Get the Author.
const std::string & getDescription() const
Get the Description.
const std::string & getDateChange() const
Get the Date of last Change.
const ExtDeclarations & getExtDeclarations() const
Get the Ext Declarations object.
const std::string & getDateCreation() const
Get the Date of Creation.
observable DataDefinition object class to describe StreamMetaType.
Definition datamodel_types.h:1224
observable Stream DataDefinition object.
Definition datamodel_streams.h:41
const std::string & getDescription() const
Get the Description.
const Structs & getStructs() const
Get the Structs object.
const std::string & getName() const
Get the Name.
const std::string & getStreamTypeName() const
Get the Stream Type Name.
virtual size_t getAlignment() const
Get the Alignment.
observable DataDefinition object class for a Element of a StructType.
Definition datamodel_types.h:736
const std::string & getDefault() const
Get the Default object.
const std::string & getScale() const
Get the Scale (for information only)
const std::string & getOffset() const
Get the Offset (for information only)
const std::string & getDescription() const
Get the Description.
const std::string & getComment() const
Get the Comment.
const std::string & getUnitName() const
Get the Unit Name.
const std::string & getName() const
Get the Name.
const std::string & getValue() const
Get the value.
const std::string & getMin() const
Get the Min (for information only). This was introduced in DDL 3.0.
const std::string & getTypeName() const
Get the Type Name.
const std::string & getMax() const
Get the Max (for information only). This was introduced in DDL 3.0.
const std::string & getValidElementCount() const
Get the Valid Element Count element name.
const ArraySize & getArraySize() const
Get the Array Size.
OptionalSize getNumBits() const
Get the Num Bits (if set)
OptionalSize getBitPos() const
Get the Bit Pos (if set)
OptionalSize getBytePos() const
Get the Byte Pos.
ByteOrder getByteOrder() const
Get the Byte Order.
observable DataDefinition object class to describe StructType.
Definition datamodel_types.h:453
Unit Prefix - datamodel pefixes.
Definition datamodel_units.h:221
BaseUnit.
Definition datamodel_units.h:338
definition of the dd namespace
Definition datamodel_base.h:26
@ base_unit
the unit is a base unit (BaseUnit)
Definition dd_common_types.h:91
@ unit
the unit is a unit (Unit)
Definition dd_common_types.h:86
@ struct_type
the type is a struct type (StructType)
Definition dd_common_types.h:64
@ data_type
the type is a data type (DataType)
Definition dd_common_types.h:54
@ stream_meta_type
the type is an stream meta type (StreamMetaType)
Definition dd_common_types.h:69
@ enum_type
the type is a enum type (EnumType)
Definition dd_common_types.h:59
definiton of the unit prefixes namespace
Definition dd_predefined_units.h:61
definition of the ddl namespace
Definition codec.h:21
Template class to create DD DOM nodes with the help of the type DOM_NODE_TYPE.
Definition xml_ddtoxml_factory.h:39
static void createNode(DOM_NODE_TYPE &parent_node, const datamodel::DataDefinition &dd)
Create a Node for the DD.
Definition xml_ddtoxml_factory.h:386
static void setData(DOM_NODE_TYPE &dom_node, const std::string &sub_node_name, const std::string &value)
Set the data of the new created tag with the name sub_node_name.
Definition xml_ddtoxml_factory.h:90
static bool setOptionalAttribute(DOM_NODE_TYPE &dom_node, const std::string &attribute_name, const utility::Optional< T > &value)
Set the Optional Attribute. This will only set the attribute if the value is valid.
Definition xml_ddtoxml_factory.h:51
static void createNode(DOM_NODE_TYPE &parent_node, const datamodel::Header &header)
Create a Node for the header.
Definition xml_ddtoxml_factory.h:104
static void createNode(DOM_NODE_TYPE &parent_node, const datamodel::BaseUnit &base_unit)
Create a Node for the base_unit.
Definition xml_ddtoxml_factory.h:129
static void createNode(DOM_NODE_TYPE &parent_node, const datamodel::StreamMetaType &stream_meta_type)
Create a Node for the stream_meta_type.
Definition xml_ddtoxml_factory.h:335
static void createNode(DOM_NODE_TYPE &parent_node, const datamodel::StructType &struct_type, const Version &file_ddl_version)
Create a Node for the struct_type.
Definition xml_ddtoxml_factory.h:308
static void createNode(DOM_NODE_TYPE &parent_node, const datamodel::StructType::Element &element, const Version &file_ddl_version)
Create a Node for the struct_type element.
Definition xml_ddtoxml_factory.h:238
static void createNode(DOM_NODE_TYPE &parent_node, const datamodel::Unit &unit)
Create a Node for the unit.
Definition xml_ddtoxml_factory.h:160
static void createNode(DOM_NODE_TYPE &parent_node, const datamodel::EnumType &enum_type)
Create a Node for the enum_type.
Definition xml_ddtoxml_factory.h:217
static void createNode(DOM_NODE_TYPE &parent_node, const datamodel::Stream &stream)
Create a Node for the stream.
Definition xml_ddtoxml_factory.h:358
static void createNode(DOM_NODE_TYPE &parent_node, const datamodel::UnitPrefix &unit_prefix)
Create a Node for the unit_prefix.
Definition xml_ddtoxml_factory.h:144
static bool setOptionalAttribute(DOM_NODE_TYPE &dom_node, const std::string &attribute_name, const std::string &value)
Set the Optional Attribute. This will only set the attribute if the value is not empty.
Definition xml_ddtoxml_factory.h:72
static void createNode(DOM_NODE_TYPE &parent_node, const datamodel::DataType &data_type, const Version &file_ddl_version)
Create a Node for the data_type.
Definition xml_ddtoxml_factory.h:184
An optional template as long as the std::optional is not available here.
Definition access_optional.h:31