15 #ifndef DD_DD_FROM_XML_FACTORY_H_INCLUDED
16 #define DD_DD_FROM_XML_FACTORY_H_INCLUDED
44 template <
typename DOM_NODE_TYPE>
51 static bool isInteger(
const std::string& string_to_check)
67 int i_converted = std::stoi(from_string);
70 catch (
const std::invalid_argument&) {
73 catch (
const std::exception&) {
89 int size_converted = std::stoi(from_string);
90 if (size_converted < 0) {
95 return size_converted;
98 catch (
const std::invalid_argument&) {
101 catch (
const std::exception&) {
102 return default_value;
117 if (from_string.empty()) {
118 return default_value;
141 template <
typename T>
143 const char* attribute_name,
145 bool is_mandatory =
false,
146 const std::string& parse_error_message = {})
148 auto value = dom_node.getAttribute(attribute_name);
152 "DDFromXMLFactory::getAttribute", {
"...", attribute_name}, parse_error_message);
154 return default_value;
157 return toType(value, default_value);
177 template <
typename T>
179 const std::string& sub_node_name,
181 bool is_mandatory =
false,
182 const char* parse_error_message =
"")
184 DOM_NODE_TYPE sub_node;
185 std::string value = {};
186 if (dom_node.findNode(sub_node_name, sub_node)) {
187 value = sub_node.getData();
192 "DDFromXMLFactory::getData", {
"...", sub_node_name}, parse_error_message);
194 return default_value;
197 return toType(value, default_value);
209 const DOM_NODE_TYPE& dom_node)
211 auto key_attrib = getAttribute<std::string>(
216 "header/ext_declaration tag has no mandatory 'key' attribute");
217 auto value_attrib = getAttribute<std::string>(
222 "header/ext_declaration tag has no mandatory 'value' attribute");
235 std::vector<datamodel::Header::ExtDeclaration> exts;
236 std::list<DOM_NODE_TYPE> header_ext_nodes;
237 if (dom_node.findNodes(
"ext_declaration", header_ext_nodes)) {
238 for (
const auto& current_header_ext_node: header_ext_nodes) {
244 auto file_ddl_version = getData<std::string>(dom_node,
"language_version", {});
245 if (file_ddl_version) {
250 auto author = getData<std::string>(dom_node,
"author", {});
254 auto date_crea = getData<std::string>(dom_node,
"date_creation", {});
256 created_header.setDateCreation(date_crea);
258 auto date_cha = getData<std::string>(dom_node,
"date_change", {});
260 created_header.setDateChange(date_cha);
262 auto description = getData<std::string>(dom_node,
"description", {});
264 created_header.setDescription(description);
266 return created_header;
277 auto name = getAttribute<std::string>(
278 dom_node,
"name", {},
true,
"baseunit tag has no mandatory 'name' attribute");
279 auto symbol = getAttribute<std::string>(dom_node,
"symbol", std::string(
""));
280 auto description = getAttribute<std::string>(dom_node,
"description", std::string(
""));
292 auto name = getAttribute<std::string>(
293 dom_node,
"name", {},
true,
"prefixes tag has no mandatory 'name' attribute");
294 auto symbol = getAttribute<std::string>(
295 dom_node,
"symbol", {},
true,
"prefixes tag has no mandatory 'symbol' attribute");
296 auto power = getAttribute<int32_t>(
297 dom_node,
"power", {},
true,
"prefixes tag has no mandatory 'power' attribute");
309 auto unit_name = getAttribute<std::string>(
310 dom_node,
"name", {},
true,
"unit/refUnit tag has no mandatory 'name' attribute");
311 auto power = getAttribute<int32_t>(
312 dom_node,
"power", {},
true,
"unit/refUnit tag has no mandatory 'power' attribute");
313 auto prefix_name = getAttribute<std::string>(
314 dom_node,
"prefix", {},
true,
"unit/refUnit tag has no mandatory 'prefix' attribute");
327 std::vector<datamodel::Unit::RefUnit> ref_units;
328 std::list<DOM_NODE_TYPE> ref_unit_nodes;
329 if (dom_node.findNodes(
"refUnit", ref_unit_nodes)) {
330 for (
const auto& current_ref_unit_node: ref_unit_nodes) {
331 ref_units.emplace_back(
createRefUnit(current_ref_unit_node));
334 auto name = getAttribute<std::string>(
335 dom_node,
"name", {},
true,
"unit tag has no mandatory 'name' attribute");
336 auto numerator = getData<std::string>(
337 dom_node,
"numerator", {},
true,
"unit tag has no mandatory 'numerator' tag");
338 auto denominator = getData<std::string>(
339 dom_node,
"denominator", {},
true,
"unit tag has no mandatory 'denominator' tag");
340 auto offset = getData<std::string>(
341 dom_node,
"offset", {},
true,
"unit tag has no mandatory 'offset' tag");
342 return datamodel::Unit(*name, *numerator, *denominator, *offset, ref_units);
355 const Version& file_ddl_version,
362 if (strict && (file_ddl_version >=
Version(3, 0))) {
363 auto name_attrib = getAttribute<std::string>(
364 dom_node,
"name", {},
true,
"datatype tag has no mandatory 'name' attribute");
370 auto name_attrib = getAttribute<std::string>(dom_node,
"name", {});
371 auto type_attrib = getAttribute<std::string>(dom_node,
"type", {});
372 if (!type_attrib && !name_attrib) {
373 type_attrib = getAttribute<std::string>(
374 dom_node,
"type", {},
true,
"datatype tag has no mandatory 'type' attribute");
385 auto type_size = getAttribute<size_t>(
386 dom_node,
"size", {},
true,
"datatype tag has no mandatory 'size' attribute");
399 auto description = getAttribute<std::string>(dom_node,
"description");
401 created_type.setDescription(*description);
403 auto array_size = getAttribute<size_t>(dom_node,
"arraysize");
405 created_type.setArraySize(*array_size);
407 auto unit = getAttribute<std::string>(dom_node,
"unit");
409 created_type.setUnitName(
unit);
412 if (file_ddl_version >=
Version(3, 0) || file_ddl_version ==
Version(0, 0)) {
413 auto min_val = getAttribute<std::string>(dom_node,
"min");
415 created_type.setMin(*min_val);
417 auto max_val = getAttribute<std::string>(dom_node,
"max");
419 created_type.setMax(*max_val);
433 auto name_attrib = getAttribute<std::string>(
434 dom_node,
"name", {},
true,
"enum/element tag has no mandatory 'name' attribute");
435 auto value_attrib = getAttribute<std::string>(
436 dom_node,
"value", {},
true,
"enum/element tag has no mandatory 'value' attribute");
448 auto name_attrib = getAttribute<std::string>(
449 dom_node,
"name", {},
true,
"enum tag has no mandatory 'name' attribute");
450 auto type_attrib = getAttribute<std::string>(
451 dom_node,
"type", {},
true,
"type tag has no mandatory 'type' attribute");
454 std::list<DOM_NODE_TYPE> enum_element_nodes;
455 if (dom_node.findNodes(
"element", enum_element_nodes)) {
456 for (
const auto& current_enum_element_node: enum_element_nodes) {
457 created_enum.getElements().emplace(
475 const Version& file_ddl_version,
478 auto name_attrib = getAttribute<std::string>(
479 dom_node,
"name", {},
true,
"struct/element tag has no mandatory 'name' attribute");
480 auto type_attrib = getAttribute<std::string>(dom_node,
484 "struct/element tag '" + *name_attrib +
485 "' has no mandatory 'type' attribute");
493 Version used_file_version = file_ddl_version;
494 if (file_ddl_version ==
Version(0, 0) || !strict) {
497 DOM_NODE_TYPE serialized_node;
498 if (dom_node.findNode(
"serialized", serialized_node)) {
499 used_file_version =
Version(4, 0);
503 used_file_version =
Version(3, 0);
509 if (used_file_version >=
Version(4, 0)) {
510 DOM_NODE_TYPE serialized_node;
511 if (dom_node.findNode(
"serialized", serialized_node)) {
513 auto byte_pos_int = getAttribute<int>(serialized_node,
517 "struct/element tag '" + *name_attrib +
518 "' has no mandatory 'bytepos' attribute");
519 if (*byte_pos_int == -1) {
523 byte_pos =
static_cast<size_t>(*byte_pos_int);
525 auto byte_order_attrib =
526 getAttribute<std::string>(serialized_node,
530 "struct/element/serialized tag '" + *name_attrib +
531 "' no mandatory 'byte_order' attribute");
534 bit_pos = getAttribute<size_t>(serialized_node,
"bitpos", {});
535 num_bits = getAttribute<size_t>(serialized_node,
"numbits", {});
538 throw Error(
"DDFromXMLFactory::getAttribute",
539 "struct/element/deserialized tag was not found");
542 DOM_NODE_TYPE deserialized_node;
543 if (dom_node.findNode(
"deserialized", deserialized_node)) {
544 auto alignment_attrib =
545 getAttribute<std::string>(deserialized_node,
549 "struct/element/deserialized tag '" + *name_attrib +
550 "' has no mandatory 'alignment' attribute");
555 throw Error(
"DDFromXMLFactory::getAttribute",
556 "struct/element/serialized tag was not found");
561 auto byte_pos_int = getAttribute<int>(dom_node,
565 "struct/element tag '" + *name_attrib +
566 "' has no mandatory 'bytepos' attribute");
567 if (*byte_pos_int == -1) {
571 byte_pos =
static_cast<size_t>(*byte_pos_int);
573 auto byte_order_attrib =
574 getAttribute<std::string>(dom_node,
578 "struct/element tag '" + *name_attrib +
579 "' has no mandatory 'byte_order' attribute");
582 bit_pos = getAttribute<size_t>(dom_node,
"bitpos", {});
583 num_bits = getAttribute<size_t>(dom_node,
"numbits", {});
586 auto alignment_attrib = getAttribute<std::string>(
591 "struct/element tag '" + *name_attrib +
"' has no mandatory 'alignment' attribute");
602 auto description = getAttribute<std::string>(dom_node,
"description");
606 auto unit_name = getAttribute<std::string>(dom_node,
"unit");
610 auto comment = getAttribute<std::string>(dom_node,
"comment");
614 auto arraysize_attrib =
615 getAttribute<std::string>(dom_node,
"arraysize");
618 if (arraysize_attrib &&
isInteger(*arraysize_attrib)) {
619 arraysize_value = getAttribute<size_t>(dom_node,
"arraysize");
625 else if (arraysize_attrib)
628 if (file_ddl_version >=
Version(2, 0) || file_ddl_version ==
Version(0, 0) || !strict) {
633 if (used_file_version >=
Version(2, 0) || file_ddl_version ==
Version(0, 0) || !strict) {
634 auto value_attrib = getAttribute<std::string>(dom_node,
"value");
636 created_element.
setValue(*value_attrib);
640 if (used_file_version >=
Version(3, 0) || file_ddl_version ==
Version(0, 0) || !strict) {
641 auto min_attrib = getAttribute<std::string>(dom_node,
"min");
643 created_element.
setMin(*min_attrib);
645 auto max_attrib = getAttribute<std::string>(dom_node,
"max");
647 created_element.
setMax(*max_attrib);
649 auto default_attrib = getAttribute<std::string>(dom_node,
"default");
650 if (default_attrib) {
653 auto scale_attrib = getAttribute<std::string>(dom_node,
"scale");
655 created_element.
setScale(*scale_attrib);
657 auto offset_attrib = getAttribute<std::string>(dom_node,
"offset");
659 created_element.
setOffset(*offset_attrib);
663 return created_element;
676 const Version& file_ddl_version,
679 auto name_attrib = getAttribute<std::string>(dom_node,
"name", {});
682 auto type_attrib = getAttribute<std::string>(dom_node,
"type", {});
684 name_attrib = type_attrib;
688 name_attrib = getAttribute<std::string>(
689 dom_node,
"name", {},
true,
"struct tag has no mandatory 'name' attribute");
694 auto version_attrib = getAttribute<std::string>(dom_node,
"version", {});
695 if (!version_attrib) {
696 version_attrib =
"1";
699 auto alignment_attrib = getAttribute<std::string>(dom_node,
"alignment", {});
701 if (alignment_attrib) {
704 auto comment_attrib = getAttribute<std::string>(dom_node,
"comment", {});
705 std::string comment_to_set =
"";
706 if (comment_attrib) {
707 comment_to_set = *comment_attrib;
709 auto ddl_version_attrib = getAttribute<std::string>(dom_node,
"ddlversion", {});
710 auto ddl_version_to_set =
Version(0, 0);
711 if (ddl_version_attrib) {
716 *name_attrib, *version_attrib, alignment_to_set, comment_to_set, ddl_version_to_set);
718 std::list<DOM_NODE_TYPE> struct_element_nodes;
719 if (dom_node.findNodes(
"element", struct_element_nodes)) {
720 for (
const auto& current_struct_element_node: struct_element_nodes) {
736 const DOM_NODE_TYPE& dom_node)
738 auto name_attrib = getAttribute<std::string>(
743 "streammetatypename/property tag has no mandatory 'name' attribute");
744 auto type_attrib = getAttribute<std::string>(
749 "streammetatypename/property tag has no mandatory 'type' attribute");
761 auto name_attrib = getAttribute<std::string>(
762 dom_node,
"name", {},
true,
"streammetatype tag has no mandatory 'name' attribute");
763 auto version_attrib =
764 getAttribute<std::string>(dom_node,
768 "streammetatype tag has no mandatory 'version' attribute");
773 auto parent_attrib = getAttribute<std::string>(dom_node,
"parent", {});
778 std::list<DOM_NODE_TYPE> smt_prop_nodes;
779 if (dom_node.findNodes(
"property", smt_prop_nodes)) {
780 for (
const auto& current_smt_prop_node: smt_prop_nodes) {
781 created_type.getProperties().emplace(
796 auto name_attrib = getAttribute<std::string>(dom_node,
"name", {});
797 auto type_attrib = getAttribute<std::string>(
798 dom_node,
"type", {},
true,
"stream/struct tag has no mandatory 'type' attribute");
799 auto byte_pos = getAttribute<size_t>(dom_node,
"bytepos", {});
811 auto name_attrib = getAttribute<std::string>(dom_node,
"name", {});
812 auto type_attrib = getAttribute<std::string>(
813 dom_node,
"type", {},
true,
"stream tag has no mandatory 'type' attribute");
818 name_attrib = *type_attrib;
823 auto desc_attrib = getAttribute<std::string>(dom_node,
"description", {});
828 std::list<DOM_NODE_TYPE> ss_nodes;
829 if (dom_node.findNodes(
"struct", ss_nodes)) {
830 for (
const auto& current_stream_struct_node: ss_nodes) {
834 return created_stream;
853 DOM_NODE_TYPE header_element;
854 if (dom_node.findNode(
"header", header_element)) {
859 std::list<DOM_NODE_TYPE> baseunit_nodes;
860 if (dom_node.findNodes(
"//units/baseunit", baseunit_nodes)) {
861 for (
const auto& current_base_unit_node: baseunit_nodes) {
866 std::list<DOM_NODE_TYPE> prefix_nodes;
867 if (dom_node.findNodes(
"//units/prefixes", prefix_nodes)) {
868 for (
const auto& current_prefix_node: prefix_nodes) {
873 std::list<DOM_NODE_TYPE> unit_nodes;
874 if (dom_node.findNodes(
"//units/unit", unit_nodes)) {
875 for (
const auto& current_unit_node: unit_nodes) {
881 std::list<DOM_NODE_TYPE> datatype_nodes;
882 if (dom_node.findNodes(
"//datatypes/datatype", datatype_nodes)) {
883 for (
const auto& current_datatype_node: datatype_nodes) {
889 std::list<DOM_NODE_TYPE> enum_nodes;
890 if (dom_node.findNodes(
"//enums/enum", enum_nodes)) {
891 for (
const auto& current_enum_node: enum_nodes) {
897 std::list<DOM_NODE_TYPE> struct_nodes;
898 if (dom_node.findNodes(
"//structs/struct", struct_nodes)) {
899 for (
const auto& current_struct_node: struct_nodes) {
905 if (dom_node.getName() ==
"struct") {
911 std::list<DOM_NODE_TYPE> stream_meta_type_nodes;
912 if (dom_node.findNodes(
"//streammetatypes/streammetatype", stream_meta_type_nodes)) {
913 for (
const auto& current_smt_node: stream_meta_type_nodes) {
919 std::list<DOM_NODE_TYPE> stream_nodes;
920 if (dom_node.findNodes(
"//streams/stream", stream_nodes)) {
921 for (
const auto& current_stream_node: stream_nodes) {
dd::OptionalSize getDefaultAlignment(const std::string &name) const
Get the default alignment of a Predefined Type by name.
static const PredefinedDataTypes & getInstance()
Get the Instance object.
static Alignment fromString(const std::string &alignment, const Alignment &default_val=Alignment::e_invalid)
convert a string to an alignment.
static ByteOrder fromString(const std::string &byte_order, const ByteOrder &default_val=ByteOrder::platform_not_supported)
convert a string to a byte_order.
static Version fromString(const std::string &version, const Version &default_val=Version(0, 0))
Creates the Version from a string.
DataDefinition Datamodel This datamodel is observable for any change of:
void setHeader(const Header &header)
Set the Header object.
const EnumTypes & getEnumTypes() const
Get the Enum Types object.
const BaseUnits & getBaseUnits() const
Get the Base Units object.
const StructTypes & getStructTypes() const
Get the Struct Types object.
const StreamMetaTypes & getStreamMetaTypes() const
Get the Stream Meta Types object.
const DataTypes & getDataTypes() const
Get the Data Types object.
const Streams & getStreams() const
Get the Streams object.
const Units & getUnits() const
Get the Units object.
Version getVersion() const
Get the DDL Version.
const UnitPrefixes & getUnitPrefixes() const
Get the Unit Prefixes object.
observable DataDefinition object class to describe (POD) DataType.
observable DataDefinition object class to describe EnumType.
KeyValuePair Element
Enum type element defintion as Name Value pair.
DataDefinition object class of datamodel to hold a streamstruct.
observable Stream DataDefinition object.
void setDescription(const std::string &description)
Set the Description.
const Structs & getStructs() const
Get the Structs object.
class to describe the deserialized information of a StructType::Element.
observable DataDefinition object class for a Element of a StructType.
void setMax(const std::string &maximum_value)
Set the Max (for information only).
void setUnitName(const std::string &unit_name)
Set the Unit Name.
void setDefault(const std::string &default_value)
Set the Default Value.
void setDescription(const std::string &description)
Set the Description.
void setMin(const std::string &minimum_value)
Set the Min (for information only).
void setScale(const std::string &scale)
Set the Scale (for information only)
void setValue(const std::string &value)
Set the Value This is only valid if the data type is set to an enum type where this constant value is...
void setComment(const std::string &comment)
Set the Comment object.
void setOffset(const std::string &offset)
Set the Offset (for information only)
void setArraySize(const ArraySize &array_size)
Set the Array Size.
class to describe the serialized information of a StructType::Element.
observable DataDefinition object class to describe StructType.
datamodel for the refUnit
Unit Prefix - datamodel pefixes.
void emplace(DDL_TYPE_TO_ACCESS &&type_to_add)
emplace the given item
void emplace(DDL_TYPE_TO_ACCESS &&type_to_add)
emplaces the given item
OO DataDefinition Redesign.
OO DataDefinition Optional Implementation.
OO DataDefinition Redesign.
OO DataDefinition DataTypes header for template based DataType usages and the predefined types.
Error
Enumerates possible error values for filesystem interaction.
bool isInteger(const std::string &string_to_check)
Checks if the given string is an integer value.
datamodel::StreamMetaType StreamMetaType
Reuse of datamodel streammetatype class - ddl::dd::datamodel::StreamMetaType.
datamodel::BaseUnit BaseUnit
Reuse of datamodel base unit class - ddl::dd::datamodel::BaseUnit.
datamodel::UnitPrefix UnitPrefix
Reuse of datamodel unit prefix class - ddl::dd::datamodel::UnitPrefix.
datamodel::Unit Unit
Reuse of datamodel unit class - ddl::dd::datamodel::Unit.
@ struct_type
the type is a struct type (StructType)
@ unit
the unit is a unit (Unit)
ByteOrder
Representation of the byteorder enumeration.
datamodel::Stream Stream
Reuse of datamodel stream class - ddl::dd::datamodel::Stream.
Alignment
Alignment defintion.
@ e0
for backward compatibility
definition of the ddl namespace
Utility for the Neutrino gcc5 compiler which has really no std::to_string implementation!
Utility for string checkking within the DDFromXML header only factory.
Factory to create a DataDefinition out of a XML based description.
static datamodel::Unit::RefUnit createRefUnit(const DOM_NODE_TYPE &dom_node)
Create a Ref Unit object.
static utility::Optional< std::string > toType(const std::string &from_string, const utility::Optional< std::string > &default_value={})
specialized function to convert a string to an optional string which is only valid if set.
static datamodel::Stream createStream(const DOM_NODE_TYPE &dom_node)
Create a Stream object.
static datamodel::EnumType::Element createEnumTypeElement(const DOM_NODE_TYPE &dom_node)
Create a Enum Type Element object.
static bool isInteger(const std::string &string_to_check)
Checks if the given string is an integer value.
static datamodel::Header::ExtDeclaration createHeaderExtDeclaration(const DOM_NODE_TYPE &dom_node)
Create a Header Ext Declaration object.
static datamodel::BaseUnit createBaseUnit(const DOM_NODE_TYPE &dom_node)
Create a Base Unit object.
static datamodel::UnitPrefix createUnitPrefix(const DOM_NODE_TYPE &dom_node)
Create a Unit Prefix object.
static utility::Optional< size_t > toType(const std::string &from_string, const utility::Optional< size_t > &default_value={})
specialized conversion to the Optional<size_t>
static utility::Optional< T > getData(const DOM_NODE_TYPE &dom_node, const std::string &sub_node_name, const utility::Optional< T > &default_value={}, bool is_mandatory=false, const char *parse_error_message="")
Get the data content of a subnode of dom_node.
static datamodel::StructType::Element createStructTypeElement(const DOM_NODE_TYPE &dom_node, const Version &file_ddl_version, bool strict)
Create a Struct Type Element object.
static datamodel::StructType createStructType(const DOM_NODE_TYPE &dom_node, const Version &file_ddl_version, bool strict)
Create a Struct Type object.
static datamodel::EnumType createEnumType(const DOM_NODE_TYPE &dom_node)
Create a Enum Type object.
static datamodel::Unit createUnit(const DOM_NODE_TYPE &dom_node)
Create a Unit object.
static datamodel::Stream::Struct createStreamStruct(const DOM_NODE_TYPE &dom_node)
Create a Stream Struct object.
static datamodel::StreamMetaType createStreamMetaType(const DOM_NODE_TYPE &dom_node)
Create a Stream Meta Type object.
static datamodel::DataDefinition createDD(const DOM_NODE_TYPE &dom_node, const dd::Version &language_version, bool strict)
Creates a whole DataDefinition and read all sub node.
static datamodel::Header createHeader(const DOM_NODE_TYPE &dom_node)
Create a Header object.
static datamodel::StreamMetaType::Property createStreamMetaTypeProperty(const DOM_NODE_TYPE &dom_node)
Create a Stream Meta Type Property object.
static datamodel::DataType createDataType(const DOM_NODE_TYPE &dom_node, const Version &file_ddl_version, bool strict)
Create a Data Type object.
static utility::Optional< int > toType(const std::string &from_string, const utility::Optional< int > &default_value={})
specialized conversion to the Optional<int32_t>
static utility::Optional< T > getAttribute(const DOM_NODE_TYPE &dom_node, const char *attribute_name, const utility::Optional< T > &default_value={}, bool is_mandatory=false, const std::string &parse_error_message={})
Get the Attribute from the dom_node.
An optional template as long as the std::optional is not available here.