diff --git a/cimgen/languages/cpp/lang_pack.py b/cimgen/languages/cpp/lang_pack.py index 755757df..40cf466c 100644 --- a/cimgen/languages/cpp/lang_pack.py +++ b/cimgen/languages/cpp/lang_pack.py @@ -58,6 +58,9 @@ def get_class_location(class_name, class_map, version): # NOSONAR "label": "{{#langPack.label}}{{label}}{{/langPack.label}}", "insert_assign": "{{#langPack.insert_assign_fn}}{{.}}{{/langPack.insert_assign_fn}}", "insert_class_assign": "{{#langPack.insert_class_assign_fn}}{{.}}{{/langPack.insert_class_assign_fn}}", + "insert_get": "{{#langPack.insert_get_fn}}{{.}}{{/langPack.insert_get_fn}}", + "insert_class_get": "{{#langPack.insert_class_get_fn}}{{.}}{{/langPack.insert_class_get_fn}}", + "insert_enum_get": "{{#langPack.insert_enum_get_fn}}{{.}}{{/langPack.insert_enum_get_fn}}", } @@ -160,6 +163,38 @@ def insert_class_assign_fn(text, render): ) +def insert_get_fn(text, render): + attribute_txt = render(text) + attribute_json = eval(attribute_txt) + if not _attribute_is_primitive_or_datatype(attribute_json): + return "" + label = attribute_json["label"] + class_name = attribute_json["domain"] + return ' get_map.emplace("cim:' + class_name + "." + label + '", &get_' + class_name + "_" + label + ");\n" + + +def insert_class_get_fn(text, render): + attribute_txt = render(text) + attribute_json = eval(attribute_txt) + if _attribute_is_primitive_or_datatype_or_enum(attribute_json): + return "" + if not attribute_json["is_used"]: + return "" + label = attribute_json["label"] + class_name = attribute_json["domain"] + return ' get_map.emplace("cim:' + class_name + "." + label + '", &get_' + class_name + "_" + label + ");\n" + + +def insert_enum_get_fn(text, render): + attribute_txt = render(text) + attribute_json = eval(attribute_txt) + if not attribute_json["is_enum_attribute"]: + return "" + label = attribute_json["label"] + class_name = attribute_json["domain"] + return ' get_map.emplace("cim:' + class_name + "." + label + '", &get_' + class_name + "_" + label + ");\n" + + def create_nullptr_assigns(text, render): attributes_txt = render(text) if attributes_txt.strip() == "": @@ -337,6 +372,112 @@ def create_assign(text, render): return assign +def create_class_get(text, render): + attribute_txt = render(text) + attribute_json = eval(attribute_txt) + if _attribute_is_primitive_or_datatype_or_enum(attribute_json): + return "" + if not attribute_json["is_used"]: + return "" + if attribute_json["is_list_attribute"]: + get = """ +bool get_OBJECT_CLASS_LABEL(const BaseClass* BaseClass_ptr1, std::list& BaseClass_list) +{ + if (const OBJECT_CLASS* element = dynamic_cast(BaseClass_ptr1)) + { + std::copy(element->LABEL.begin(), element->LABEL.end(), std::back_inserter(BaseClass_list)); + return !BaseClass_list.empty(); + } + return false; +}""".replace( # noqa: E101,W191 + "OBJECT_CLASS", attribute_json["domain"] + ).replace( + "LABEL", attribute_json["label"] + ) + else: + get = """ +bool get_OBJECT_CLASS_LABEL(const BaseClass* BaseClass_ptr1, std::list& BaseClass_list) +{ + if (const OBJECT_CLASS* element = dynamic_cast(BaseClass_ptr1)) + { + if (element->LABEL != 0) + { + BaseClass_list.push_back(element->LABEL); + return true; + } + } + return false; +}""".replace( # noqa: E101,W191 + "OBJECT_CLASS", attribute_json["domain"] + ).replace( + "LABEL", attribute_json["label"] + ) + + return get + + +def create_get(text, render): + attribute_txt = render(text) + attribute_json = eval(attribute_txt) + get = "" + if not _attribute_is_primitive_or_datatype(attribute_json): + return "" + label_without_keyword = attribute_json["label"] + if label_without_keyword == "switch": + label_without_keyword = "_switch" + + get = ( + """ +bool get_CLASS_LABEL(const BaseClass* BaseClass_ptr1, std::stringstream& buffer) +{ + if (const CLASS* element = dynamic_cast(BaseClass_ptr1)) + { + buffer << element->LBL_WO_KEYWORD; + if (!buffer.str().empty()) + { + return true; + } + } + buffer.setstate(std::ios::failbit); + return false; +}""".replace( # noqa: E101,W191 + "CLASS", attribute_json["domain"] + ) + .replace("LABEL", attribute_json["label"]) + .replace("LBL_WO_KEYWORD", label_without_keyword) + ) + + return get + + +def create_enum_get(text, render): + attribute_txt = render(text) + attribute_json = eval(attribute_txt) + if not attribute_json["is_enum_attribute"]: + return "" + + get = """ +bool get_CLASS_LABEL(const BaseClass* BaseClass_ptr1, std::stringstream& buffer) +{ + if (const CLASS* element = dynamic_cast(BaseClass_ptr1)) + { + buffer << element->LABEL; + if (!buffer.str().empty()) + { + return true; + } + } + buffer.setstate(std::ios::failbit); + return false; +}""".replace( # noqa: E101,W191 + "CLASS", attribute_json["domain"] + ).replace( + "LABEL", attribute_json["label"] + ) + + return get + + def attribute_decl(text, render): attribute_txt = render(text) attribute_json = eval(attribute_txt) diff --git a/cimgen/languages/cpp/static/BaseClass.cpp b/cimgen/languages/cpp/static/BaseClass.cpp index 5baf1c2c..cc93fcd5 100644 --- a/cimgen/languages/cpp/static/BaseClass.cpp +++ b/cimgen/languages/cpp/static/BaseClass.cpp @@ -16,6 +16,9 @@ const char* BaseClass::debugString() const void BaseClass::addConstructToMap(std::unordered_map& factory_map) {} void BaseClass::addPrimitiveAssignFnsToMap(std::unordered_map& assign_map) {} void BaseClass::addClassAssignFnsToMap(std::unordered_map& assign_map) {} +void BaseClass::addPrimitiveGetFnsToMap(std::map& get_map) const {} +void BaseClass::addClassGetFnsToMap(std::map& get_map) const {} +void BaseClass::addEnumGetFnsToMap(std::map& get_map) const {} const BaseClassDefiner BaseClass::declare() { diff --git a/cimgen/languages/cpp/static/BaseClass.hpp b/cimgen/languages/cpp/static/BaseClass.hpp index a5b797f3..f761249c 100644 --- a/cimgen/languages/cpp/static/BaseClass.hpp +++ b/cimgen/languages/cpp/static/BaseClass.hpp @@ -13,6 +13,10 @@ #include "BaseClassDefiner.hpp" #include "CGMESProfile.hpp" +class BaseClass; +typedef bool (*class_get_function)(const BaseClass*, std::list&); +typedef bool (*get_function)(const BaseClass*, std::stringstream&); + class BaseClass { std::string rdfid; @@ -31,6 +35,9 @@ class BaseClass static void addConstructToMap(std::unordered_map& factory_map); static void addPrimitiveAssignFnsToMap(std::unordered_map& assign_map); static void addClassAssignFnsToMap(std::unordered_map& assign_map); + virtual void addPrimitiveGetFnsToMap(std::map& get_map) const; + virtual void addClassGetFnsToMap(std::map& get_map) const; + virtual void addEnumGetFnsToMap(std::map& get_map) const; static const CIMPP::BaseClassDefiner declare(); }; #endif // BASECLASS_HPP diff --git a/cimgen/languages/cpp/templates/cpp_header_template.mustache b/cimgen/languages/cpp/templates/cpp_header_template.mustache index 58a15482..b163f9dc 100644 --- a/cimgen/languages/cpp/templates/cpp_header_template.mustache +++ b/cimgen/languages/cpp/templates/cpp_header_template.mustache @@ -41,6 +41,9 @@ namespace CIMPP static void addConstructToMap(std::unordered_map& factory_map); static void addPrimitiveAssignFnsToMap(std::unordered_map& assign_map); static void addClassAssignFnsToMap(std::unordered_map& assign_map); + void addPrimitiveGetFnsToMap(std::map& get_map) const override; + void addClassGetFnsToMap(std::map& get_map) const override; + void addEnumGetFnsToMap(std::map& get_map) const override; static const BaseClassDefiner declare(); }; diff --git a/cimgen/languages/cpp/templates/cpp_object_template.mustache b/cimgen/languages/cpp/templates/cpp_object_template.mustache index 845dd25e..d09c2ce4 100644 --- a/cimgen/languages/cpp/templates/cpp_object_template.mustache +++ b/cimgen/languages/cpp/templates/cpp_object_template.mustache @@ -4,6 +4,8 @@ Generated from the CGMES files via cimgen: https://github.com/sogno-platform/cim #include "{{class_name}}.hpp" #include +#include +#include #include {{#attributes}} @@ -52,6 +54,18 @@ std::map> {{#langPack.create_class_assign}}{{.}}{{/langPack.create_class_assign}} {{/attributes}} +{{#attributes}} +{{#langPack.create_get}}{{.}}{{/langPack.create_get}} +{{/attributes}} + +{{#attributes}} +{{#langPack.create_class_get}}{{.}}{{/langPack.create_class_get}} +{{/attributes}} + +{{#attributes}} +{{#langPack.create_enum_get}}{{.}}{{/langPack.create_enum_get}} +{{/attributes}} + const char {{class_name}}::debugName[] = "{{class_name}}"; const char* {{class_name}}::debugString() const { @@ -77,6 +91,30 @@ void {{class_name}}::addClassAssignFnsToMap(std::unordered_map& get_map) const +{ + {{sub_class_of}}::addPrimitiveGetFnsToMap(get_map); +{{#attributes}} +{{> insert_get}} +{{/attributes}} +} + +void {{class_name}}::addClassGetFnsToMap(std::map& get_map) const +{ + {{sub_class_of}}::addClassGetFnsToMap(get_map); +{{#attributes}} +{{> insert_class_get}} +{{/attributes}} +} + +void {{class_name}}::addEnumGetFnsToMap(std::map& get_map) const +{ + {{sub_class_of}}::addEnumGetFnsToMap(get_map); +{{#attributes}} +{{> insert_enum_get}} +{{/attributes}} +} + const BaseClassDefiner {{class_name}}::declare() { return BaseClassDefiner({{class_name}}::addConstructToMap, {{class_name}}::addPrimitiveAssignFnsToMap, {{class_name}}::addClassAssignFnsToMap, {{class_name}}::debugName);