From 4fa88cbd42b8dea4f3de1e74b88c47dd01aee9f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=BCnther?= Date: Sat, 19 Oct 2024 12:12:42 +0200 Subject: [PATCH 01/13] Add generation of string classes for cpp MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Thomas Günther --- cimgen/languages/cpp/lang_pack.py | 9 ++- .../cpp_string_header_template.mustache | 38 ++++++++++++ .../cpp_string_object_template.mustache | 62 +++++++++++++++++++ 3 files changed, 107 insertions(+), 2 deletions(-) create mode 100644 cimgen/languages/cpp/templates/cpp_string_header_template.mustache create mode 100644 cimgen/languages/cpp/templates/cpp_string_object_template.mustache diff --git a/cimgen/languages/cpp/lang_pack.py b/cimgen/languages/cpp/lang_pack.py index 6c914b3e..4268787b 100644 --- a/cimgen/languages/cpp/lang_pack.py +++ b/cimgen/languages/cpp/lang_pack.py @@ -39,6 +39,10 @@ def setup(output_path: str, cgmes_profile_details: list, cim_namespace: str): # {"filename": "cpp_enum_header_template.mustache", "ext": ".hpp"}, {"filename": "cpp_enum_object_template.mustache", "ext": ".cpp"}, ] +string_template_files = [ + {"filename": "cpp_string_header_template.mustache", "ext": ".hpp"}, + {"filename": "cpp_string_object_template.mustache", "ext": ".cpp"}, +] def get_class_location(class_name, class_map, version): # NOSONAR @@ -60,10 +64,12 @@ def run_template(output_path, class_details): templates = float_template_files elif class_details["is_an_enum_class"]: templates = enum_template_files + elif class_details["is_a_primitive_class"]: + templates = string_template_files else: templates = template_files - if class_details["class_name"] in ("String", "Integer", "Boolean", "Date"): + if class_details["class_name"] in ("Integer", "Boolean"): # These classes are defined already # We have to implement operators for them return @@ -372,7 +378,6 @@ def _attribute_is_primitive_or_datatype(attribute: dict) -> bool: "Factory", "Folders", "IEC61970", - "String", "Task", "UnknownType", ] diff --git a/cimgen/languages/cpp/templates/cpp_string_header_template.mustache b/cimgen/languages/cpp/templates/cpp_string_header_template.mustache new file mode 100644 index 00000000..9a0de1ae --- /dev/null +++ b/cimgen/languages/cpp/templates/cpp_string_header_template.mustache @@ -0,0 +1,38 @@ +#ifndef {{class_name}}_H +#define {{class_name}}_H +/* +Generated from the CGMES files via cimgen: https://github.com/sogno-platform/cimgen +*/ + +#include +#include + +#include "BaseClass.hpp" + +namespace CIMPP +{ + class {{class_name}} : public BaseClass + { + public: + {{class_name}}(); + {{class_name}}(std::string value); + ~{{class_name}}() override; + {{class_name}}& operator=(std::string &rop); + operator std::string(); + + std::string value; + bool initialized = false; + + static const char debugName[]; + const char* debugString() const override; + + static void addConstructToMap(std::unordered_map& factory_map); + static void addPrimitiveAssignFnsToMap(std::unordered_map&); + static void addClassAssignFnsToMap(std::unordered_map&); + static const BaseClassDefiner declare(); + + friend std::istream& operator>>(std::istream& lop, {{class_name}}& rop); + friend std::ostream& operator<<(std::ostream& os, const {{class_name}}& obj); + }; +} +#endif diff --git a/cimgen/languages/cpp/templates/cpp_string_object_template.mustache b/cimgen/languages/cpp/templates/cpp_string_object_template.mustache new file mode 100644 index 00000000..865a2717 --- /dev/null +++ b/cimgen/languages/cpp/templates/cpp_string_object_template.mustache @@ -0,0 +1,62 @@ +/* +Generated from the CGMES files via cimgen: https://github.com/sogno-platform/cimgen +*/ +#include "{{class_name}}.hpp" + +#include "CIMExceptions.hpp" + +using namespace CIMPP; + +{{class_name}}::{{class_name}}() {} +{{class_name}}::{{class_name}}(std::string value) : value(value), initialized(true) {} +{{class_name}}::~{{class_name}}() {} + +{{class_name}}& {{class_name}}::operator=(std::string &rop) +{ + value = rop; + initialized = true; + return *this; +} + +{{class_name}}::operator std::string() +{ + if (!initialized) + { + throw new ReadingUninitializedField(); + } + return value; +} + +const char {{class_name}}::debugName[] = "{{class_name}}"; +const char* {{class_name}}::debugString() const +{ + return {{class_name}}::debugName; +} + +void {{class_name}}::addConstructToMap(std::unordered_map& factory_map) {} +void {{class_name}}::addPrimitiveAssignFnsToMap(std::unordered_map& assign_map) {} +void {{class_name}}::addClassAssignFnsToMap(std::unordered_map& assign_map) {} + +const BaseClassDefiner {{class_name}}::declare() +{ + return BaseClassDefiner({{class_name}}::addConstructToMap, {{class_name}}::addPrimitiveAssignFnsToMap, {{class_name}}::addClassAssignFnsToMap, {{class_name}}::debugName); +} + +namespace CIMPP +{ + std::istream& operator>>(std::istream& lop, {{class_name}}& rop) + { + lop >> rop.value; + rop.initialized = true; + return lop; + } + + std::ostream& operator<<(std::ostream& os, const {{class_name}}& obj) + { + if (obj.initialized) + { + os << obj.value; + } + return os; + } +} From 251cc727721bf6653bb612eb26cd2b7b501147fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=BCnther?= Date: Sat, 19 Oct 2024 13:41:20 +0200 Subject: [PATCH 02/13] Improve cpp templates: make debugstring() const MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Thomas Günther --- .../languages/cpp/templates/cpp_float_header_template.mustache | 2 +- .../languages/cpp/templates/cpp_float_object_template.mustache | 3 ++- cimgen/languages/cpp/templates/cpp_header_template.mustache | 2 +- cimgen/languages/cpp/templates/cpp_object_template.mustache | 2 +- 4 files changed, 5 insertions(+), 4 deletions(-) diff --git a/cimgen/languages/cpp/templates/cpp_float_header_template.mustache b/cimgen/languages/cpp/templates/cpp_float_header_template.mustache index 7517148a..002c198c 100644 --- a/cimgen/languages/cpp/templates/cpp_float_header_template.mustache +++ b/cimgen/languages/cpp/templates/cpp_float_header_template.mustache @@ -28,7 +28,7 @@ namespace CIMPP bool initialized = false; static const char debugName[]; - virtual const char* debugString(); + const char* debugString() const override; static void addConstructToMap(std::unordered_map& factory_map); static void addPrimitiveAssignFnsToMap(std::unordered_map&); diff --git a/cimgen/languages/cpp/templates/cpp_float_object_template.mustache b/cimgen/languages/cpp/templates/cpp_float_object_template.mustache index 1d7116a4..721837ed 100644 --- a/cimgen/languages/cpp/templates/cpp_float_object_template.mustache +++ b/cimgen/languages/cpp/templates/cpp_float_object_template.mustache @@ -16,7 +16,8 @@ void {{class_name}}::addPrimitiveAssignFnsToMap(std::unordered_map& assign_map) {} const char {{class_name}}::debugName[] = "{{class_name}}"; -const char* {{class_name}}::debugString() { +const char* {{class_name}}::debugString() const +{ return {{class_name}}::debugName; } diff --git a/cimgen/languages/cpp/templates/cpp_header_template.mustache b/cimgen/languages/cpp/templates/cpp_header_template.mustache index 6205a4c2..fb423708 100644 --- a/cimgen/languages/cpp/templates/cpp_header_template.mustache +++ b/cimgen/languages/cpp/templates/cpp_header_template.mustache @@ -23,7 +23,7 @@ namespace CIMPP { {{/attributes}} static const char debugName[]; - virtual const char* debugString(); + const char* debugString() const override; /* constructor initialising all attributes to null */ {{class_name}}(); diff --git a/cimgen/languages/cpp/templates/cpp_object_template.mustache b/cimgen/languages/cpp/templates/cpp_object_template.mustache index ae60165d..1d36a740 100644 --- a/cimgen/languages/cpp/templates/cpp_object_template.mustache +++ b/cimgen/languages/cpp/templates/cpp_object_template.mustache @@ -43,7 +43,7 @@ void {{class_name}}::addClassAssignFnsToMap(std::unordered_map Date: Sat, 19 Oct 2024 17:55:02 +0200 Subject: [PATCH 03/13] Add generated-via-cimgen comment to the header of all generated files for cpp MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Thomas Günther --- cimgen/languages/cpp/lang_pack.py | 18 ++++++++++++++++-- .../cpp_enum_header_template.mustache | 3 +++ .../cpp_enum_object_template.mustache | 6 +++++- .../cpp_float_header_template.mustache | 3 +++ .../cpp_float_object_template.mustache | 4 ++++ .../cpp/templates/cpp_header_template.mustache | 3 +++ .../cpp/templates/cpp_object_template.mustache | 7 ++++++- 7 files changed, 40 insertions(+), 4 deletions(-) diff --git a/cimgen/languages/cpp/lang_pack.py b/cimgen/languages/cpp/lang_pack.py index 4268787b..1b2bdc29 100644 --- a/cimgen/languages/cpp/lang_pack.py +++ b/cimgen/languages/cpp/lang_pack.py @@ -419,6 +419,10 @@ def resolve_headers(path: str, version: str): # NOSONAR class_list_header = [ "#ifndef CIMCLASSLIST_H\n", "#define CIMCLASSLIST_H\n", + "/*\n", + "Generated from the CGMES files via cimgen: https://github.com/sogno-platform/cimgen\n", + "*/\n", + "\n", "using namespace CIMPP;\n", "#include \n", "static std::list CIMClassList = {\n", @@ -438,8 +442,18 @@ def resolve_headers(path: str, version: str): # NOSONAR class_blacklist, ) - iec61970_header = ["#ifndef IEC61970_H\n", "#define IEC61970_H\n"] - iec61970_footer = ['#include "UnknownType.hpp"\n', "#endif"] + iec61970_header = [ + "#ifndef IEC61970_H\n", + "#define IEC61970_H\n", + "/*\n", + "Generated from the CGMES files via cimgen: https://github.com/sogno-platform/cimgen\n", + "*/\n", + "\n", + ] + iec61970_footer = [ + '#include "UnknownType.hpp"\n', + "#endif", + ] _create_header_include_file( path, diff --git a/cimgen/languages/cpp/templates/cpp_enum_header_template.mustache b/cimgen/languages/cpp/templates/cpp_enum_header_template.mustache index 888ac3c6..75e30a07 100644 --- a/cimgen/languages/cpp/templates/cpp_enum_header_template.mustache +++ b/cimgen/languages/cpp/templates/cpp_enum_header_template.mustache @@ -1,5 +1,8 @@ #ifndef {{class_name}}_H #define {{class_name}}_H +/* +Generated from the CGMES files via cimgen: https://github.com/sogno-platform/cimgen +*/ namespace CIMPP { /* diff --git a/cimgen/languages/cpp/templates/cpp_enum_object_template.mustache b/cimgen/languages/cpp/templates/cpp_enum_object_template.mustache index 63eecaed..14b9f71d 100644 --- a/cimgen/languages/cpp/templates/cpp_enum_object_template.mustache +++ b/cimgen/languages/cpp/templates/cpp_enum_object_template.mustache @@ -1,6 +1,10 @@ +/* +Generated from the CGMES files via cimgen: https://github.com/sogno-platform/cimgen +*/ +#include "{{class_name}}.hpp" + #include #include -#include "{{class_name}}.hpp" using namespace CIMPP; diff --git a/cimgen/languages/cpp/templates/cpp_float_header_template.mustache b/cimgen/languages/cpp/templates/cpp_float_header_template.mustache index 002c198c..a8432193 100644 --- a/cimgen/languages/cpp/templates/cpp_float_header_template.mustache +++ b/cimgen/languages/cpp/templates/cpp_float_header_template.mustache @@ -1,5 +1,8 @@ #ifndef {{class_name}}_H #define {{class_name}}_H +/* +Generated from the CGMES files via cimgen: https://github.com/sogno-platform/cimgen +*/ #include #include diff --git a/cimgen/languages/cpp/templates/cpp_float_object_template.mustache b/cimgen/languages/cpp/templates/cpp_float_object_template.mustache index 721837ed..3f6f2f96 100644 --- a/cimgen/languages/cpp/templates/cpp_float_object_template.mustache +++ b/cimgen/languages/cpp/templates/cpp_float_object_template.mustache @@ -1,4 +1,8 @@ +/* +Generated from the CGMES files via cimgen: https://github.com/sogno-platform/cimgen +*/ #include "{{class_name}}.hpp" + #include "../src/CIMExceptions.hpp" using namespace CIMPP; diff --git a/cimgen/languages/cpp/templates/cpp_header_template.mustache b/cimgen/languages/cpp/templates/cpp_header_template.mustache index fb423708..b3af88e9 100644 --- a/cimgen/languages/cpp/templates/cpp_header_template.mustache +++ b/cimgen/languages/cpp/templates/cpp_header_template.mustache @@ -1,5 +1,8 @@ #ifndef {{class_name}}_H #define {{class_name}}_H +/* +Generated from the CGMES files via cimgen: https://github.com/sogno-platform/cimgen +*/ #include "{{sub_class_of}}.hpp" #include diff --git a/cimgen/languages/cpp/templates/cpp_object_template.mustache b/cimgen/languages/cpp/templates/cpp_object_template.mustache index 1d36a740..78e3283a 100644 --- a/cimgen/languages/cpp/templates/cpp_object_template.mustache +++ b/cimgen/languages/cpp/templates/cpp_object_template.mustache @@ -1,6 +1,11 @@ +/* +Generated from the CGMES files via cimgen: https://github.com/sogno-platform/cimgen +*/ +#include "{{class_name}}.hpp" + #include + #include "{{sub_class_of}}.hpp" -#include "{{class_name}}.hpp" {{#attributes}} #include "{{attribute_class}}.hpp" From f3d295ff7989cccafad79e583cafe4ae6b4eec32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=BCnther?= Date: Sat, 19 Oct 2024 18:27:49 +0200 Subject: [PATCH 04/13] Improve cpp templates (skip empty comments, improve indentations) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Thomas Günther --- .../cpp/templates/cpp_enum_header_template.mustache | 4 ++++ .../languages/cpp/templates/cpp_header_template.mustache | 8 +++++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/cimgen/languages/cpp/templates/cpp_enum_header_template.mustache b/cimgen/languages/cpp/templates/cpp_enum_header_template.mustache index 75e30a07..6de035a2 100644 --- a/cimgen/languages/cpp/templates/cpp_enum_header_template.mustache +++ b/cimgen/languages/cpp/templates/cpp_enum_header_template.mustache @@ -5,15 +5,19 @@ Generated from the CGMES files via cimgen: https://github.com/sogno-platform/cim */ namespace CIMPP { +{{#class_comment}} /* {{{class_comment}}} */ +{{/class_comment}} enum class {{class_name}} { {{#enum_instances}} +{{#comment}} /** * {{comment}} */ +{{/comment}} {{label}}, {{/enum_instances}} }; diff --git a/cimgen/languages/cpp/templates/cpp_header_template.mustache b/cimgen/languages/cpp/templates/cpp_header_template.mustache index b3af88e9..21fd927d 100644 --- a/cimgen/languages/cpp/templates/cpp_header_template.mustache +++ b/cimgen/languages/cpp/templates/cpp_header_template.mustache @@ -14,16 +14,18 @@ Generated from the CGMES files via cimgen: https://github.com/sogno-platform/cim namespace CIMPP { {{#langPack._create_attribute_class_declarations}}{{attributes}}{{/langPack._create_attribute_class_declarations}} +{{#class_comment}} /* {{{class_comment}}} */ +{{/class_comment}} class {{class_name}}: public {{sub_class_of}} { public: - {{#attributes}} - {{> attribute}} {{> label}}; /* {{comment}} Default: {{#langPack._set_default}}{{dataType}}{{/langPack._set_default}} */ - {{/attributes}} +{{#attributes}} + {{> attribute}} {{> label}}; /* {{comment}} Default: {{#langPack._set_default}}{{dataType}}{{/langPack._set_default}} */ +{{/attributes}} static const char debugName[]; const char* debugString() const override; From 250214a9de94ca01e33e21939b18c47b0f40746a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=BCnther?= Date: Sat, 19 Oct 2024 20:07:29 +0200 Subject: [PATCH 05/13] Fix cpp create_assign functions for string attribute types (Date, DateTime, MonthDay) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Thomas Günther --- cimgen/languages/cpp/lang_pack.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/cimgen/languages/cpp/lang_pack.py b/cimgen/languages/cpp/lang_pack.py index 1b2bdc29..5941223c 100644 --- a/cimgen/languages/cpp/lang_pack.py +++ b/cimgen/languages/cpp/lang_pack.py @@ -241,7 +241,11 @@ def create_assign(text, render): if label_without_keyword == "switch": label_without_keyword = "_switch" - if _class != "String": + if ( + attribute_json["is_enum_attribute"] + or attribute_json["is_datatype_attribute"] + or _class in ("Float", "Decimal", "Integer", "Boolean") + ): assign = ( """ bool assign_CLASS_LABEL(std::stringstream &buffer, BaseClass* BaseClass_ptr1) { @@ -260,7 +264,7 @@ def create_assign(text, render): .replace("LABEL", attribute_json["label"]) .replace("LBL_WO_KEYWORD", label_without_keyword) ) - else: + else: # is_primitive_string_attribute assign = """ bool assign_CLASS_LABEL(std::stringstream &buffer, BaseClass* BaseClass_ptr1) { if(CLASS* element = dynamic_cast(BaseClass_ptr1)) { From 2d1af012bef16ea60810ae53e974fe72f506da95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=BCnther?= Date: Sun, 20 Oct 2024 11:45:05 +0200 Subject: [PATCH 06/13] Fix cpp create_class_assign functions to prevent duplicate entries in attributes with attribute_type list MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Thomas Günther --- cimgen/languages/cpp/lang_pack.py | 60 ++++++++++++++++++++++++------- 1 file changed, 47 insertions(+), 13 deletions(-) diff --git a/cimgen/languages/cpp/lang_pack.py b/cimgen/languages/cpp/lang_pack.py index 5941223c..f73a45e3 100644 --- a/cimgen/languages/cpp/lang_pack.py +++ b/cimgen/languages/cpp/lang_pack.py @@ -174,8 +174,36 @@ def create_class_assign(text, render): if _attribute_is_primitive_or_datatype_or_enum(attribute_json): return "" if attribute_json["is_list_attribute"]: - assign = ( - """ + if "inverseRole" in attribute_json: + inverse = attribute_json["inverseRole"].split(".") + assign = ( + """ +bool assign_INVERSEC_INVERSEL(BaseClass*, BaseClass*); +bool assign_OBJECT_CLASS_LABEL(BaseClass* BaseClass_ptr1, BaseClass* BaseClass_ptr2) +{ + OBJECT_CLASS* element = dynamic_cast(BaseClass_ptr1); + ATTRIBUTE_CLASS* element2 = dynamic_cast(BaseClass_ptr2); + if (element != nullptr && element2 != nullptr) + { + if (std::find(element->LABEL.begin(), element->LABEL.end(), element2) == element->LABEL.end()) + { + element->LABEL.push_back(element2); + return assign_INVERSEC_INVERSEL(BaseClass_ptr2, BaseClass_ptr1); + } + return true; + } + return false; +}""".replace( # noqa: E101,W191 + "OBJECT_CLASS", attribute_json["domain"] + ) + .replace("ATTRIBUTE_CLASS", attribute_class) + .replace("LABEL", attribute_json["label"]) + .replace("INVERSEC", inverse[0]) + .replace("INVERSEL", inverse[1]) + ) + else: + assign = ( + """ bool assign_OBJECT_CLASS_LABEL(BaseClass* BaseClass_ptr1, BaseClass* BaseClass_ptr2) { if(OBJECT_CLASS* element = dynamic_cast(BaseClass_ptr1)) { if(dynamic_cast(BaseClass_ptr2) != nullptr) { @@ -185,23 +213,29 @@ def create_class_assign(text, render): } return false; }""".replace( # noqa: E101,W191 - "OBJECT_CLASS", attribute_json["domain"] + "OBJECT_CLASS", attribute_json["domain"] + ) + .replace("ATTRIBUTE_CLASS", attribute_class) + .replace("LABEL", attribute_json["label"]) ) - .replace("ATTRIBUTE_CLASS", attribute_class) - .replace("LABEL", attribute_json["label"]) - ) - elif "inverseRole" in attribute_json and attribute_json["is_used"]: + elif "inverseRole" in attribute_json: inverse = attribute_json["inverseRole"].split(".") assign = ( """ bool assign_INVERSEC_INVERSEL(BaseClass*, BaseClass*); bool assign_OBJECT_CLASS_LABEL(BaseClass* BaseClass_ptr1, BaseClass* BaseClass_ptr2) { - if(OBJECT_CLASS* element = dynamic_cast(BaseClass_ptr1)) { - element->LABEL = dynamic_cast(BaseClass_ptr2); - if(element->LABEL != nullptr) - return assign_INVERSEC_INVERSEL(BaseClass_ptr2, BaseClass_ptr1); - } - return false; + OBJECT_CLASS* element = dynamic_cast(BaseClass_ptr1); + ATTRIBUTE_CLASS* element2 = dynamic_cast(BaseClass_ptr2); + if (element != nullptr && element2 != nullptr) + { + if (element->LABEL != element2) + { + element->LABEL = element2; + return assign_INVERSEC_INVERSEL(BaseClass_ptr2, BaseClass_ptr1); + } + return true; + } + return false; }""".replace( # noqa: E101,W191 "OBJECT_CLASS", attribute_json["domain"] ) From e4bdab4ceabd233149619a4fcd57b3d31a8ef59c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=BCnther?= Date: Sun, 20 Oct 2024 11:56:22 +0200 Subject: [PATCH 07/13] Add class comment to cpp templates for float and string classes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Thomas Günther --- .../cpp/templates/cpp_float_header_template.mustache | 5 +++++ .../cpp/templates/cpp_string_header_template.mustache | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/cimgen/languages/cpp/templates/cpp_float_header_template.mustache b/cimgen/languages/cpp/templates/cpp_float_header_template.mustache index a8432193..fa2f22c1 100644 --- a/cimgen/languages/cpp/templates/cpp_float_header_template.mustache +++ b/cimgen/languages/cpp/templates/cpp_float_header_template.mustache @@ -11,6 +11,11 @@ Generated from the CGMES files via cimgen: https://github.com/sogno-platform/cim namespace CIMPP { +{{#class_comment}} + /* + {{{class_comment}}} + */ +{{/class_comment}} class {{class_name}} : public BaseClass { diff --git a/cimgen/languages/cpp/templates/cpp_string_header_template.mustache b/cimgen/languages/cpp/templates/cpp_string_header_template.mustache index 9a0de1ae..a8fb2c40 100644 --- a/cimgen/languages/cpp/templates/cpp_string_header_template.mustache +++ b/cimgen/languages/cpp/templates/cpp_string_header_template.mustache @@ -11,6 +11,11 @@ Generated from the CGMES files via cimgen: https://github.com/sogno-platform/cim namespace CIMPP { +{{#class_comment}} + /* + {{{class_comment}}} + */ +{{/class_comment}} class {{class_name}} : public BaseClass { public: From b038de62c32bd16a7036eabff9dd85f10aeff539 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=BCnther?= Date: Sun, 27 Oct 2024 19:19:44 +0100 Subject: [PATCH 08/13] Simplify and unify cpp templates for enum, float and string classes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Thomas Günther --- .../cpp_enum_header_template.mustache | 36 +++++-- .../cpp_enum_object_template.mustache | 56 ++++++++++- .../cpp_float_header_template.mustache | 37 ++++---- .../cpp_float_object_template.mustache | 94 ++++++++++--------- .../cpp_string_header_template.mustache | 24 ++--- .../cpp_string_object_template.mustache | 19 +--- 6 files changed, 157 insertions(+), 109 deletions(-) diff --git a/cimgen/languages/cpp/templates/cpp_enum_header_template.mustache b/cimgen/languages/cpp/templates/cpp_enum_header_template.mustache index 6de035a2..3076101c 100644 --- a/cimgen/languages/cpp/templates/cpp_enum_header_template.mustache +++ b/cimgen/languages/cpp/templates/cpp_enum_header_template.mustache @@ -4,23 +4,45 @@ Generated from the CGMES files via cimgen: https://github.com/sogno-platform/cimgen */ -namespace CIMPP { +#include +#include + +namespace CIMPP +{ {{#class_comment}} /* {{{class_comment}}} */ {{/class_comment}} - enum class {{class_name}} + class {{class_name}} { + public: + enum {{class_name}}_ENUM + { {{#enum_instances}} {{#comment}} - /** - * {{comment}} - */ + /** + * {{comment}} + */ {{/comment}} - {{label}}, + {{label}}, {{/enum_instances}} + }; + + {{class_name}}() : value(), initialized(false) {} + {{class_name}}({{class_name}}_ENUM value) : value(value), initialized(true) {} + + {{class_name}}& operator=({{class_name}}_ENUM rop); + operator {{class_name}}_ENUM() const; + + {{class_name}}_ENUM value; + bool initialized; + + static const char debugName[]; + const char* debugString() const; + + friend std::istream& operator>>(std::istream& lop, {{class_name}}& rop); + friend std::ostream& operator<<(std::ostream& os, const {{class_name}}& obj); }; - std::istream& operator>>(std::istream& lop, CIMPP::{{class_name}}& rop); } #endif diff --git a/cimgen/languages/cpp/templates/cpp_enum_object_template.mustache b/cimgen/languages/cpp/templates/cpp_enum_object_template.mustache index 14b9f71d..cb2998b5 100644 --- a/cimgen/languages/cpp/templates/cpp_enum_object_template.mustache +++ b/cimgen/languages/cpp/templates/cpp_enum_object_template.mustache @@ -3,14 +3,41 @@ Generated from the CGMES files via cimgen: https://github.com/sogno-platform/cim */ #include "{{class_name}}.hpp" -#include -#include +#include +#include + +#include "../src/CIMExceptions.hpp" using namespace CIMPP; -namespace CIMPP { - std::istream& operator>>(std::istream& lop, CIMPP::{{class_name}}& rop) +{{class_name}}& {{class_name}}::operator=({{class_name}}_ENUM rop) +{ + value = rop; + initialized = true; + return *this; +} + +{{class_name}}::operator {{class_name}}_ENUM() const +{ + if (!initialized) + { + throw new ReadingUninitializedField(); + } + return value; +} + +const char {{class_name}}::debugName[] = "{{class_name}}"; +const char* {{class_name}}::debugString() const +{ + return {{class_name}}::debugName; +} + +namespace CIMPP +{ + std::istream& operator>>(std::istream& lop, {{class_name}}& rop) { + rop.initialized = false; + std::string EnumSymbol; lop >> EnumSymbol; @@ -34,4 +61,25 @@ namespace CIMPP { lop.setstate(std::ios::failbit); return lop; } + + std::ostream& operator<<(std::ostream& os, const {{class_name}}& obj) + { + if (obj.initialized) + { + std::string EnumSymbol; + +{{#enum_instances}} + if (obj.value == {{class_name}}::{{label}}) + { + EnumSymbol = "{{label}}"; + } +{{/enum_instances}} + + if (!EnumSymbol.empty()) + { + os << "{{class_name}}." << EnumSymbol; + } + } + return os; + } } diff --git a/cimgen/languages/cpp/templates/cpp_float_header_template.mustache b/cimgen/languages/cpp/templates/cpp_float_header_template.mustache index fa2f22c1..e558d709 100644 --- a/cimgen/languages/cpp/templates/cpp_float_header_template.mustache +++ b/cimgen/languages/cpp/templates/cpp_float_header_template.mustache @@ -4,10 +4,8 @@ Generated from the CGMES files via cimgen: https://github.com/sogno-platform/cimgen */ -#include #include - -#include "BaseClass.hpp" +#include namespace CIMPP { @@ -16,31 +14,28 @@ namespace CIMPP {{{class_comment}}} */ {{/class_comment}} - class {{class_name}} : public BaseClass + class {{class_name}} { - public: - {{class_name}}(); - virtual ~{{class_name}}(); - {{class_name}}(long double value); - static const BaseClassDefiner declare(); - {{class_name}}& operator=(long double &rop); + {{class_name}}() : value(0.0), initialized(false) {} + {{class_name}}(long double value) : value(value), initialized(true) {} + + {{class_name}}& operator=(long double rop); + operator long double() const; + + long double value; + bool initialized; + + static const char debugName[]; + const char* debugString() const; + {{class_name}}& operator+=(const {{class_name}}& rhs); {{class_name}}& operator-=(const {{class_name}}& rhs); {{class_name}}& operator*=(const {{class_name}}& rhs); {{class_name}}& operator/=(const {{class_name}}& rhs); - friend std::istream& operator>>(std::istream& lop, {{class_name}}& rop); - operator long double(); - - long double value = 0.0; - bool initialized = false; - static const char debugName[]; - const char* debugString() const override; - - static void addConstructToMap(std::unordered_map& factory_map); - static void addPrimitiveAssignFnsToMap(std::unordered_map&); - static void addClassAssignFnsToMap(std::unordered_map&); + friend std::istream& operator>>(std::istream& lop, {{class_name}}& rop); + friend std::ostream& operator<<(std::ostream& os, const {{class_name}}& obj); }; } #endif diff --git a/cimgen/languages/cpp/templates/cpp_float_object_template.mustache b/cimgen/languages/cpp/templates/cpp_float_object_template.mustache index 3f6f2f96..ca84b71e 100644 --- a/cimgen/languages/cpp/templates/cpp_float_object_template.mustache +++ b/cimgen/languages/cpp/templates/cpp_float_object_template.mustache @@ -3,21 +3,27 @@ Generated from the CGMES files via cimgen: https://github.com/sogno-platform/cim */ #include "{{class_name}}.hpp" +#include + #include "../src/CIMExceptions.hpp" using namespace CIMPP; -{{class_name}}::{{class_name}}() {} - -{{class_name}}::~{{class_name}}(){} - -{{class_name}}::{{class_name}}(long double value) : value(value), initialized(true) {} - -void {{class_name}}::addConstructToMap(std::unordered_map& factory_map) {} - -void {{class_name}}::addPrimitiveAssignFnsToMap(std::unordered_map& assign_map) {} +{{class_name}}& {{class_name}}::operator=(long double rop) +{ + value = rop; + initialized = true; + return *this; +} -void {{class_name}}::addClassAssignFnsToMap(std::unordered_map& assign_map) {} +{{class_name}}::operator long double() const +{ + if (!initialized) + { + throw new ReadingUninitializedField(); + } + return value; +} const char {{class_name}}::debugName[] = "{{class_name}}"; const char* {{class_name}}::debugString() const @@ -25,51 +31,47 @@ const char* {{class_name}}::debugString() const return {{class_name}}::debugName; } - -const BaseClassDefiner {{class_name}}::declare() { - return BaseClassDefiner({{class_name}}::addConstructToMap, {{class_name}}::addPrimitiveAssignFnsToMap, {{class_name}}::addClassAssignFnsToMap, {{class_name}}::debugName); +{{class_name}}& {{class_name}}::operator+=(const {{class_name}}& rhs) +{ + value += rhs.value; + return *this; } -namespace CIMPP { - {{class_name}}& {{class_name}}::operator=(long double &rop) { - value = rop; - initialized = true; - return *this; - } - - {{class_name}}& {{class_name}}::operator-=(const {{class_name}}& rhs) { - value -= rhs.value; - return *this; - } - - {{class_name}}& {{class_name}}::operator*=(const {{class_name}}& rhs) { - value *= rhs.value; - return *this; - } - - {{class_name}}& {{class_name}}::operator/=(const {{class_name}}& rhs) { - value /= rhs.value; - return *this; - } +{{class_name}}& {{class_name}}::operator-=(const {{class_name}}& rhs) +{ + value -= rhs.value; + return *this; +} - {{class_name}}& {{class_name}}::operator+=(const {{class_name}}& rhs) { - value += rhs.value; - return *this; - } +{{class_name}}& {{class_name}}::operator*=(const {{class_name}}& rhs) +{ + value *= rhs.value; + return *this; +} - {{class_name}}::operator long double() { - if(!initialized) - { - throw new ReadingUninitializedField(); - } - return value; - } +{{class_name}}& {{class_name}}::operator/=(const {{class_name}}& rhs) +{ + value /= rhs.value; + return *this; +} - std::istream& operator>>(std::istream& lop, {{class_name}}& rop) { +namespace CIMPP +{ + std::istream& operator>>(std::istream& lop, {{class_name}}& rop) + { std::string tmp; lop >> tmp; rop.value = stold(tmp); rop.initialized = true; return lop; } + + std::ostream& operator<<(std::ostream& os, const {{class_name}}& obj) + { + if (obj.initialized) + { + os << obj.value; + } + return os; + } } diff --git a/cimgen/languages/cpp/templates/cpp_string_header_template.mustache b/cimgen/languages/cpp/templates/cpp_string_header_template.mustache index a8fb2c40..acec4bae 100644 --- a/cimgen/languages/cpp/templates/cpp_string_header_template.mustache +++ b/cimgen/languages/cpp/templates/cpp_string_header_template.mustache @@ -6,8 +6,7 @@ Generated from the CGMES files via cimgen: https://github.com/sogno-platform/cim #include #include - -#include "BaseClass.hpp" +#include namespace CIMPP { @@ -16,25 +15,20 @@ namespace CIMPP {{{class_comment}}} */ {{/class_comment}} - class {{class_name}} : public BaseClass + class {{class_name}} { public: - {{class_name}}(); - {{class_name}}(std::string value); - ~{{class_name}}() override; - {{class_name}}& operator=(std::string &rop); - operator std::string(); + {{class_name}}() : initialized(false) {} + {{class_name}}(const std::string& value) : value(value), initialized(true) {} + + {{class_name}}& operator=(const std::string &rop); + operator std::string() const; std::string value; - bool initialized = false; + bool initialized; static const char debugName[]; - const char* debugString() const override; - - static void addConstructToMap(std::unordered_map& factory_map); - static void addPrimitiveAssignFnsToMap(std::unordered_map&); - static void addClassAssignFnsToMap(std::unordered_map&); - static const BaseClassDefiner declare(); + const char* debugString() const; friend std::istream& operator>>(std::istream& lop, {{class_name}}& rop); friend std::ostream& operator<<(std::ostream& os, const {{class_name}}& obj); diff --git a/cimgen/languages/cpp/templates/cpp_string_object_template.mustache b/cimgen/languages/cpp/templates/cpp_string_object_template.mustache index 865a2717..07434496 100644 --- a/cimgen/languages/cpp/templates/cpp_string_object_template.mustache +++ b/cimgen/languages/cpp/templates/cpp_string_object_template.mustache @@ -3,22 +3,18 @@ Generated from the CGMES files via cimgen: https://github.com/sogno-platform/cim */ #include "{{class_name}}.hpp" -#include "CIMExceptions.hpp" +#include "../src/CIMExceptions.hpp" using namespace CIMPP; -{{class_name}}::{{class_name}}() {} -{{class_name}}::{{class_name}}(std::string value) : value(value), initialized(true) {} -{{class_name}}::~{{class_name}}() {} - -{{class_name}}& {{class_name}}::operator=(std::string &rop) +{{class_name}}& {{class_name}}::operator=(const std::string& rop) { value = rop; initialized = true; return *this; } -{{class_name}}::operator std::string() +{{class_name}}::operator std::string() const { if (!initialized) { @@ -33,15 +29,6 @@ const char* {{class_name}}::debugString() const return {{class_name}}::debugName; } -void {{class_name}}::addConstructToMap(std::unordered_map& factory_map) {} -void {{class_name}}::addPrimitiveAssignFnsToMap(std::unordered_map& assign_map) {} -void {{class_name}}::addClassAssignFnsToMap(std::unordered_map& assign_map) {} - -const BaseClassDefiner {{class_name}}::declare() -{ - return BaseClassDefiner({{class_name}}::addConstructToMap, {{class_name}}::addPrimitiveAssignFnsToMap, {{class_name}}::addClassAssignFnsToMap, {{class_name}}::debugName); -} - namespace CIMPP { std::istream& operator>>(std::istream& lop, {{class_name}}& rop) From 68029c977840f12ad1d1b81612fe97bfddcb445a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=BCnther?= Date: Sun, 27 Oct 2024 20:21:30 +0100 Subject: [PATCH 09/13] Unify formatting of the generated cpp classes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Thomas Günther --- cimgen/languages/cpp/lang_pack.py | 102 ++++++++++-------- .../templates/cpp_header_template.mustache | 28 +++-- .../templates/cpp_object_template.mustache | 37 ++++--- 3 files changed, 89 insertions(+), 78 deletions(-) diff --git a/cimgen/languages/cpp/lang_pack.py b/cimgen/languages/cpp/lang_pack.py index f73a45e3..8e32123d 100644 --- a/cimgen/languages/cpp/lang_pack.py +++ b/cimgen/languages/cpp/lang_pack.py @@ -81,7 +81,6 @@ def run_template(output_path, class_details): def _write_templated_file(class_file, class_details, template_filename): with open(class_file, "w", encoding="utf-8") as file: - class_details["setDefault"] = _set_default templates = files("cimgen.languages.cpp.templates") with templates.joinpath(template_filename).open(encoding="utf-8") as f: args = { @@ -115,7 +114,7 @@ def insert_assign_fn(text, render): label = attribute_json["label"] class_name = attribute_json["domain"] return ( - 'assign_map.insert(std::make_pair(std::string("cim:' + ' assign_map.insert(std::make_pair(std::string("cim:' + class_name + "." + label @@ -135,7 +134,7 @@ def insert_class_assign_fn(text, render): label = attribute_json["label"] class_name = attribute_json["domain"] return ( - 'assign_map.insert(std::make_pair(std::string("cim:' + ' assign_map.insert(std::make_pair(std::string("cim:' + class_name + "." + label @@ -153,13 +152,13 @@ def create_nullptr_assigns(text, render): return "" else: attributes_json = eval(attributes_txt) - nullptr_init_string = ": " + nullptr_init_string = "" for attribute in attributes_json: if attribute["is_class_attribute"]: nullptr_init_string += "LABEL(nullptr), ".replace("LABEL", attribute["label"]) if len(nullptr_init_string) > 2: - return nullptr_init_string[:-2] + return " : " + nullptr_init_string[:-2] else: return "" @@ -204,10 +203,13 @@ def create_class_assign(text, render): else: assign = ( """ -bool assign_OBJECT_CLASS_LABEL(BaseClass* BaseClass_ptr1, BaseClass* BaseClass_ptr2) { - if(OBJECT_CLASS* element = dynamic_cast(BaseClass_ptr1)) { - if(dynamic_cast(BaseClass_ptr2) != nullptr) { - element->LABEL.push_back(dynamic_cast(BaseClass_ptr2)); +bool assign_OBJECT_CLASS_LABEL(BaseClass* BaseClass_ptr1, BaseClass* BaseClass_ptr2) +{ + if (OBJECT_CLASS* element = dynamic_cast(BaseClass_ptr1)) + { + if (dynamic_cast(BaseClass_ptr2) != nullptr) + { + element->LABEL.push_back(dynamic_cast(BaseClass_ptr2)); return true; } } @@ -223,7 +225,8 @@ def create_class_assign(text, render): assign = ( """ bool assign_INVERSEC_INVERSEL(BaseClass*, BaseClass*); -bool assign_OBJECT_CLASS_LABEL(BaseClass* BaseClass_ptr1, BaseClass* BaseClass_ptr2) { +bool assign_OBJECT_CLASS_LABEL(BaseClass* BaseClass_ptr1, BaseClass* BaseClass_ptr2) +{ OBJECT_CLASS* element = dynamic_cast(BaseClass_ptr1); ATTRIBUTE_CLASS* element2 = dynamic_cast(BaseClass_ptr2); if (element != nullptr && element2 != nullptr) @@ -247,13 +250,17 @@ def create_class_assign(text, render): else: assign = ( """ -bool assign_OBJECT_CLASS_LABEL(BaseClass* BaseClass_ptr1, BaseClass* BaseClass_ptr2) { - if(OBJECT_CLASS* element = dynamic_cast(BaseClass_ptr1)) { - element->LABEL = dynamic_cast(BaseClass_ptr2); - if(element->LABEL != nullptr) - return true; - } - return false; +bool assign_OBJECT_CLASS_LABEL(BaseClass* BaseClass_ptr1, BaseClass* BaseClass_ptr2) +{ + if(OBJECT_CLASS* element = dynamic_cast(BaseClass_ptr1)) + { + element->LABEL = dynamic_cast(BaseClass_ptr2); + if (element->LABEL != nullptr) + { + return true; + } + } + return false; }""".replace( # noqa: E101,W191 "OBJECT_CLASS", attribute_json["domain"] ) @@ -282,16 +289,17 @@ def create_assign(text, render): ): assign = ( """ -bool assign_CLASS_LABEL(std::stringstream &buffer, BaseClass* BaseClass_ptr1) { - if(CLASS* element = dynamic_cast(BaseClass_ptr1)) { - buffer >> element->LBL_WO_KEYWORD; - if(buffer.fail()) - return false; - else - return true; - } - else - return false; +bool assign_CLASS_LABEL(std::stringstream &buffer, BaseClass* BaseClass_ptr1) +{ + if (CLASS* element = dynamic_cast(BaseClass_ptr1)) + { + buffer >> element->LBL_WO_KEYWORD; + if (buffer.fail()) + return false; + else + return true; + } + return false; }""".replace( # noqa: E101,W191 "CLASS", attribute_json["domain"] ) @@ -300,10 +308,12 @@ def create_assign(text, render): ) else: # is_primitive_string_attribute assign = """ -bool assign_CLASS_LABEL(std::stringstream &buffer, BaseClass* BaseClass_ptr1) { - if(CLASS* element = dynamic_cast(BaseClass_ptr1)) { +bool assign_CLASS_LABEL(std::stringstream &buffer, BaseClass* BaseClass_ptr1) +{ + if (CLASS* element = dynamic_cast(BaseClass_ptr1)) + { element->LABEL = buffer.str(); - if(buffer.fail()) + if (buffer.fail()) return false; else return true; @@ -345,8 +355,8 @@ def _create_attribute_includes(text, render): for attribute in attributes: if _attribute_is_primitive_or_datatype_or_enum(attribute): unique[attribute["attribute_class"]] = True - for clarse in unique: - include_string += '\n#include "' + clarse + '.hpp"' + for clarse in sorted(unique): + include_string += '#include "' + clarse + '.hpp"\n' return include_string @@ -361,8 +371,8 @@ def _create_attribute_class_declarations(text, render): for attribute in attributes: if attribute["is_class_attribute"] or attribute["is_list_attribute"]: unique[attribute["attribute_class"]] = True - for clarse in unique: - include_string += "\nclass " + clarse + ";" + for clarse in sorted(unique): + include_string += " class " + clarse + ";\n" return include_string @@ -423,26 +433,24 @@ def _attribute_is_primitive_or_datatype(attribute: dict) -> bool: iec61970_blacklist = ["CIMClassList", "CIMNamespaces", "Folders", "Task", "IEC61970"] -def _is_enum_class(filepath): +def _is_primitive_or_enum_class(filepath): with open(filepath, encoding="utf-8") as f: try: for line in f: - if "enum class" in line: - return True + if "static const BaseClassDefiner declare();" in line: + return False except UnicodeDecodeError as error: print("Warning: UnicodeDecodeError parsing {0}: {1}".format(filepath, error)) - return False + return True def _create_header_include_file(directory, header_include_filename, header, footer, before, after, blacklist): - lines = [] - for filename in sorted(os.listdir(directory)): filepath = os.path.join(directory, filename) basepath, ext = os.path.splitext(filepath) basename = os.path.basename(basepath) - if ext == ".hpp" and not _is_enum_class(filepath) and basename not in blacklist: + if ext == ".hpp" and not _is_primitive_or_enum_class(filepath) and basename not in blacklist: lines.append(before + basename + after) for line in lines: header.append(line) @@ -460,13 +468,15 @@ def resolve_headers(path: str, version: str): # NOSONAR "/*\n", "Generated from the CGMES files via cimgen: https://github.com/sogno-platform/cimgen\n", "*/\n", - "\n", - "using namespace CIMPP;\n", "#include \n", - "static std::list CIMClassList = {\n", + '#include "IEC61970.hpp"\n', + "using namespace CIMPP;\n", + "static std::list CIMClassList =\n", + "{\n", ] class_list_footer = [ - " UnknownType::declare() };\n", + " UnknownType::declare(),\n", + "};\n", "#endif // CIMCLASSLIST_H\n", ] @@ -475,7 +485,7 @@ def resolve_headers(path: str, version: str): # NOSONAR "CIMClassList.hpp", class_list_header, class_list_footer, - " ", + " ", "::declare(),\n", class_blacklist, ) diff --git a/cimgen/languages/cpp/templates/cpp_header_template.mustache b/cimgen/languages/cpp/templates/cpp_header_template.mustache index 21fd927d..2d8322de 100644 --- a/cimgen/languages/cpp/templates/cpp_header_template.mustache +++ b/cimgen/languages/cpp/templates/cpp_header_template.mustache @@ -4,25 +4,28 @@ Generated from the CGMES files via cimgen: https://github.com/sogno-platform/cimgen */ -#include "{{sub_class_of}}.hpp" #include -#include "Boolean.hpp" -#include "Float.hpp" +#include +#include +#include "{{sub_class_of}}.hpp" +#include "BaseClassDefiner.hpp" {{#langPack._create_attribute_includes}}{{attributes}}{{/langPack._create_attribute_includes}} - -namespace CIMPP { - +namespace CIMPP +{ {{#langPack._create_attribute_class_declarations}}{{attributes}}{{/langPack._create_attribute_class_declarations}} {{#class_comment}} /* {{{class_comment}}} */ {{/class_comment}} - class {{class_name}}: public {{sub_class_of}} + class {{class_name}} : public {{sub_class_of}} { - public: + /* constructor initialising all attributes to null */ + {{class_name}}(); + ~{{class_name}}() override; + {{#attributes}} {{> attribute}} {{> label}}; /* {{comment}} Default: {{#langPack._set_default}}{{dataType}}{{/langPack._set_default}} */ {{/attributes}} @@ -30,15 +33,10 @@ namespace CIMPP { static const char debugName[]; const char* debugString() const override; - /* constructor initialising all attributes to null */ - {{class_name}}(); - virtual ~{{class_name}}(); - static void addConstructToMap(std::unordered_map& factory_map); - static void addPrimitiveAssignFnsToMap(std::unordered_map&); - static void addClassAssignFnsToMap(std::unordered_map&); + static void addPrimitiveAssignFnsToMap(std::unordered_map& assign_map); + static void addClassAssignFnsToMap(std::unordered_map& assign_map); static const BaseClassDefiner declare(); - }; BaseClass* {{class_name}}_factory(); diff --git a/cimgen/languages/cpp/templates/cpp_object_template.mustache b/cimgen/languages/cpp/templates/cpp_object_template.mustache index 78e3283a..eec1a96b 100644 --- a/cimgen/languages/cpp/templates/cpp_object_template.mustache +++ b/cimgen/languages/cpp/templates/cpp_object_template.mustache @@ -3,10 +3,9 @@ Generated from the CGMES files via cimgen: https://github.com/sogno-platform/cim */ #include "{{class_name}}.hpp" +#include #include -#include "{{sub_class_of}}.hpp" - {{#attributes}} #include "{{attribute_class}}.hpp" {{/attributes}} @@ -14,7 +13,6 @@ Generated from the CGMES files via cimgen: https://github.com/sogno-platform/cim using namespace CIMPP; {{class_name}}::{{class_name}}(){{#langPack.create_nullptr_assigns}} {{attributes}} {{/langPack.create_nullptr_assigns}} {}; - {{class_name}}::~{{class_name}}() {}; {{#attributes}} @@ -25,35 +23,40 @@ using namespace CIMPP; {{#langPack.create_class_assign}}{{.}}{{/langPack.create_class_assign}} {{/attributes}} -namespace CIMPP { - BaseClass* {{class_name}}_factory() { - return new {{class_name}}; - } +const char {{class_name}}::debugName[] = "{{class_name}}"; +const char* {{class_name}}::debugString() const +{ + return {{class_name}}::debugName; } -void {{class_name}}::addConstructToMap(std::unordered_map& factory_map) { +void {{class_name}}::addConstructToMap(std::unordered_map& factory_map) +{ factory_map.insert(std::make_pair(std::string("cim:{{class_name}}"), &{{class_name}}_factory)); } -void {{class_name}}::addPrimitiveAssignFnsToMap(std::unordered_map& assign_map) { +void {{class_name}}::addPrimitiveAssignFnsToMap(std::unordered_map& assign_map) +{ {{#attributes}} - {{> insert_assign}} +{{> insert_assign}} {{/attributes}} } -void {{class_name}}::addClassAssignFnsToMap(std::unordered_map& assign_map) { +void {{class_name}}::addClassAssignFnsToMap(std::unordered_map& assign_map) +{ {{#attributes}} - {{> insert_class_assign}} +{{> insert_class_assign}} {{/attributes}} } -const char {{class_name}}::debugName[] = "{{class_name}}"; -const char* {{class_name}}::debugString() const +const BaseClassDefiner {{class_name}}::declare() { - return {{class_name}}::debugName; + return BaseClassDefiner({{class_name}}::addConstructToMap, {{class_name}}::addPrimitiveAssignFnsToMap, {{class_name}}::addClassAssignFnsToMap, {{class_name}}::debugName); } -const BaseClassDefiner {{class_name}}::declare() +namespace CIMPP { - return BaseClassDefiner({{class_name}}::addConstructToMap, {{class_name}}::addPrimitiveAssignFnsToMap, {{class_name}}::addClassAssignFnsToMap, {{class_name}}::debugName); + BaseClass* {{class_name}}_factory() + { + return new {{class_name}}; + } } From 2cdadfbb2591ca69e6788a7c402debce1946767c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=BCnther?= Date: Sun, 27 Oct 2024 20:47:07 +0100 Subject: [PATCH 10/13] Add BaseClass and fixed implementation for primitive classes (copied from libcimpp) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Thomas Günther --- cimgen/languages/cpp/static/BaseClass.cpp | 22 +++++ cimgen/languages/cpp/static/BaseClass.h | 1 + cimgen/languages/cpp/static/BaseClass.hpp | 22 +++++ .../languages/cpp/static/BaseClassDefiner.cpp | 14 ++++ .../languages/cpp/static/BaseClassDefiner.hpp | 24 ++++++ cimgen/languages/cpp/static/Boolean.cpp | 80 +++++++++++++++++++ cimgen/languages/cpp/static/Boolean.hpp | 38 +++++++++ cimgen/languages/cpp/static/Date.cpp | 20 +++++ cimgen/languages/cpp/static/Date.hpp | 26 ++++++ cimgen/languages/cpp/static/Float.cpp | 67 ++++++++++++++++ cimgen/languages/cpp/static/Float.hpp | 42 ++++++++++ cimgen/languages/cpp/static/Integer.cpp | 72 +++++++++++++++++ cimgen/languages/cpp/static/Integer.hpp | 41 ++++++++++ cimgen/languages/cpp/static/String.hpp | 14 ++++ 14 files changed, 483 insertions(+) create mode 100644 cimgen/languages/cpp/static/BaseClass.cpp create mode 100644 cimgen/languages/cpp/static/BaseClass.h create mode 100644 cimgen/languages/cpp/static/BaseClass.hpp create mode 100644 cimgen/languages/cpp/static/BaseClassDefiner.cpp create mode 100644 cimgen/languages/cpp/static/BaseClassDefiner.hpp create mode 100644 cimgen/languages/cpp/static/Boolean.cpp create mode 100644 cimgen/languages/cpp/static/Boolean.hpp create mode 100644 cimgen/languages/cpp/static/Date.cpp create mode 100644 cimgen/languages/cpp/static/Date.hpp create mode 100644 cimgen/languages/cpp/static/Float.cpp create mode 100644 cimgen/languages/cpp/static/Float.hpp create mode 100644 cimgen/languages/cpp/static/Integer.cpp create mode 100644 cimgen/languages/cpp/static/Integer.hpp create mode 100644 cimgen/languages/cpp/static/String.hpp diff --git a/cimgen/languages/cpp/static/BaseClass.cpp b/cimgen/languages/cpp/static/BaseClass.cpp new file mode 100644 index 00000000..20dc2088 --- /dev/null +++ b/cimgen/languages/cpp/static/BaseClass.cpp @@ -0,0 +1,22 @@ + +#include "BaseClass.hpp" + +using namespace CIMPP; + +BaseClass::~BaseClass() {} + +void BaseClass::addConstructToMap(std::unordered_map& factory_map) {} + +void BaseClass::addPrimitiveAssignFnsToMap(std::unordered_map& assign_map) {} + +void BaseClass::addClassAssignFnsToMap(std::unordered_map& assign_map) {} + +const char BaseClass::debugName[] = "BaseClass"; +const char* BaseClass::debugString() +{ + return BaseClass::debugName; +} + +CIMPP::BaseClassDefiner BaseClass::declare() { + return BaseClassDefiner(BaseClass::addConstructToMap, BaseClass::addPrimitiveAssignFnsToMap, BaseClass::addClassAssignFnsToMap, BaseClass::debugName); +} diff --git a/cimgen/languages/cpp/static/BaseClass.h b/cimgen/languages/cpp/static/BaseClass.h new file mode 100644 index 00000000..d1ebb818 --- /dev/null +++ b/cimgen/languages/cpp/static/BaseClass.h @@ -0,0 +1 @@ +#include "BaseClass.hpp" diff --git a/cimgen/languages/cpp/static/BaseClass.hpp b/cimgen/languages/cpp/static/BaseClass.hpp new file mode 100644 index 00000000..a7cd6129 --- /dev/null +++ b/cimgen/languages/cpp/static/BaseClass.hpp @@ -0,0 +1,22 @@ +#ifndef BASECLASS_HPP +#define BASECLASS_HPP + +#ifndef CGMES_BUILD +#define CGMES_BUILD +#endif + +#include +#include "BaseClassDefiner.hpp" + +class BaseClass { +public: + enum cgmesProfile {EQ = 0, SSH = 1, TP = 2, SV = 3, DY = 4, GL = 5, DI = 6}; + virtual ~BaseClass(); + static CIMPP::BaseClassDefiner declare(); + static void addConstructToMap(std::unordered_map&); + static void addPrimitiveAssignFnsToMap(std::unordered_map&); + static void addClassAssignFnsToMap(std::unordered_map&); + const static char debugName[]; + virtual const char* debugString(); +}; +#endif // BASECLASS_HPP diff --git a/cimgen/languages/cpp/static/BaseClassDefiner.cpp b/cimgen/languages/cpp/static/BaseClassDefiner.cpp new file mode 100644 index 00000000..95bcb95d --- /dev/null +++ b/cimgen/languages/cpp/static/BaseClassDefiner.cpp @@ -0,0 +1,14 @@ + +#include "BaseClassDefiner.hpp" + +using namespace CIMPP; + +BaseClassDefiner::BaseClassDefiner(void(*addConstruct)(std::unordered_map&), + void(*addPrimitiveAssignFns)(std::unordered_map&), + void(*addClassAssignFns)(std::unordered_map&), + const char *debugStr) { + addConstructToMap = addConstruct; + addPrimitiveAssignFnsToMap = addPrimitiveAssignFns; + addClassAssignFnsToMap = addClassAssignFns; + debugString = debugStr; +} diff --git a/cimgen/languages/cpp/static/BaseClassDefiner.hpp b/cimgen/languages/cpp/static/BaseClassDefiner.hpp new file mode 100644 index 00000000..a8735384 --- /dev/null +++ b/cimgen/languages/cpp/static/BaseClassDefiner.hpp @@ -0,0 +1,24 @@ +#ifndef BASECLASSDEFINER_HPP +#define BASECLASSDEFINER_HPP + +#include +#include + +class BaseClass; +typedef bool (*class_assign_function)(BaseClass*, BaseClass*); +typedef bool (*assign_function)(std::stringstream&, BaseClass*); +namespace CIMPP { + + class BaseClassDefiner { + public: + BaseClassDefiner(void(*addConstruct)(std::unordered_map&), + void(*addPrimitiveAssignFns)(std::unordered_map&), + void(*addClassAssignFns)(std::unordered_map&), + const char *debugStr); + void (*addConstructToMap)(std::unordered_map&); + void (*addPrimitiveAssignFnsToMap)(std::unordered_map&); + void (*addClassAssignFnsToMap)(std::unordered_map&); + const char* debugString; + }; +} +#endif // BASECLASSDEFINER diff --git a/cimgen/languages/cpp/static/Boolean.cpp b/cimgen/languages/cpp/static/Boolean.cpp new file mode 100644 index 00000000..35f7fa9a --- /dev/null +++ b/cimgen/languages/cpp/static/Boolean.cpp @@ -0,0 +1,80 @@ +#include "Boolean.hpp" +#include "CIMExceptions.hpp" + +using namespace CIMPP; + +Boolean::Boolean(){} + +Boolean::~Boolean(){} + +Boolean::Boolean(bool value) + : value(value), initialized(true) {} + +const BaseClassDefiner Boolean::declare() +{ + return BaseClassDefiner(Boolean::addConstructToMap, Boolean::addPrimitiveAssignFnsToMap, Boolean::addClassAssignFnsToMap, Boolean::debugName); +} + +Boolean& Boolean::operator=(bool &rop) +{ + value = rop; + initialized = true; + return *this; +} + +Boolean::operator bool() +{ + if(!initialized) + { + throw new ReadingUninitializedField(); + } + return value; +} + +void Boolean::addConstructToMap(std::unordered_map& factory_map) {} + +void Boolean::addPrimitiveAssignFnsToMap(std::unordered_map& assign_map) {} + +void Boolean::addClassAssignFnsToMap(std::unordered_map& assign_map) {} + +const char Boolean::debugName[] = "Boolean"; +const char* Boolean::debugString() +{ + return Boolean::debugName; +} + +namespace CIMPP { + std::istream& operator>>(std::istream& lop, Boolean& rop) + { + std::string tmp; + lop >> tmp; + if(tmp == "true" || tmp == "True" || tmp == "TRUE") + { + rop.value = true; + rop.initialized = true; + return lop; + } + if(tmp == "false" || tmp == "False" || tmp == "FALSE") + { + rop.value = false; + rop.initialized = true; + return lop; + } + else + { + lop.setstate(std::ios::failbit); + return lop; + } + } + + std::ostream& operator<<(std::ostream& os, Boolean& rop) + { + if (rop) { + os << "true"; + } + else { + os << "false"; + } + return os; + } +} diff --git a/cimgen/languages/cpp/static/Boolean.hpp b/cimgen/languages/cpp/static/Boolean.hpp new file mode 100644 index 00000000..34e30d18 --- /dev/null +++ b/cimgen/languages/cpp/static/Boolean.hpp @@ -0,0 +1,38 @@ +#ifndef BOOLEAN_H +#define BOOLEAN_H + +#include +#include +#include + +#include "BaseClass.hpp" + +namespace CIMPP { + /** + * A type with the value space "true" and "false". + */ + class Boolean + { + public: + Boolean(); + virtual ~Boolean(); + static const BaseClassDefiner declare(); + + Boolean(bool value); + Boolean& operator=(bool &rop); + friend std::istream& operator>>(std::istream& lop, Boolean& rop); + friend std::ostream& operator<<(std::ostream& os, Boolean& rop); + operator bool(); + + bool value = false; + bool initialized = false; + + static const char debugName[]; + virtual const char* debugString(); + + static void addConstructToMap(std::unordered_map& factory_map); + static void addPrimitiveAssignFnsToMap(std::unordered_map&); + static void addClassAssignFnsToMap(std::unordered_map&); + }; +} +#endif diff --git a/cimgen/languages/cpp/static/Date.cpp b/cimgen/languages/cpp/static/Date.cpp new file mode 100644 index 00000000..79a800c8 --- /dev/null +++ b/cimgen/languages/cpp/static/Date.cpp @@ -0,0 +1,20 @@ + +#include "BaseClass.hpp" +#include "Date.hpp" +#include "String.hpp" + +using namespace CIMPP; + +Date::Date() {} + +Date::Date(String s) +{ + value=s; +} + +Date::~Date() {} + +BaseClass* Date_factory() +{ + return new Date; +} diff --git a/cimgen/languages/cpp/static/Date.hpp b/cimgen/languages/cpp/static/Date.hpp new file mode 100644 index 00000000..6027d6e0 --- /dev/null +++ b/cimgen/languages/cpp/static/Date.hpp @@ -0,0 +1,26 @@ +#ifndef Date_H +#define Date_H + +#include "BaseClass.hpp" +#include "String.hpp" + + +/* +Date as "yyyy-mm-dd", which conforms with ISO 8601. UTC time zone is specified as "yyyy-mm-ddZ". A local timezone relative UTC is specified as "yyyy-mm-dd(+/-)hh:mm". +*/ +namespace CIMPP { + + class Date: public BaseClass + { + public: + Date(); + Date(String); + virtual ~Date(); + + private: + String value; + }; + + BaseClass* Date_factory(); +} +#endif diff --git a/cimgen/languages/cpp/static/Float.cpp b/cimgen/languages/cpp/static/Float.cpp new file mode 100644 index 00000000..4daa7662 --- /dev/null +++ b/cimgen/languages/cpp/static/Float.cpp @@ -0,0 +1,67 @@ +#include "Float.hpp" +#include "CIMExceptions.hpp" + +using namespace CIMPP; + +Float::Float(){} + +Float::~Float(){} + +Float::Float(long double value) : value(value), initialized(true) {} + +void Float::addConstructToMap(std::unordered_map& factory_map) {} + +void Float::addPrimitiveAssignFnsToMap(std::unordered_map& assign_map) {} + +void Float::addClassAssignFnsToMap(std::unordered_map& assign_map) {} + +const char Float::debugName[] = "Float"; +const char* Float::debugString() { + return Float::debugName; +} + +const BaseClassDefiner Float::declare() { + return BaseClassDefiner(Float::addConstructToMap, Float::addPrimitiveAssignFnsToMap, Float::addClassAssignFnsToMap, Float::debugName); +} + +Float& Float::operator=(long double &rop) { + value = rop; + initialized = true; + return *this; +} + +Float& Float::operator-=(const Float& rhs) { + value -= rhs.value; + return *this; +} + +Float& Float::operator*=(const Float& rhs) { + value *= rhs.value; + return *this; +} + +Float& Float::operator/=(const Float& rhs) { + value /= rhs.value; + return *this; +} + +Float& Float::operator+=(const Float& rhs) { + value += rhs.value; + return *this; +} + +Float::operator long double() { + if(!initialized) + { + throw new ReadingUninitializedField(); + } + return value; +} + +std::istream& operator>>(std::istream& lop, Float& rop) { + std::string tmp; + lop >> tmp; + rop.value = stold(tmp); + rop.initialized = true; + return lop; +} diff --git a/cimgen/languages/cpp/static/Float.hpp b/cimgen/languages/cpp/static/Float.hpp new file mode 100644 index 00000000..ab30f43d --- /dev/null +++ b/cimgen/languages/cpp/static/Float.hpp @@ -0,0 +1,42 @@ +#ifndef FLOAT_H +#define FLOAT_H + +#include +#include +#include + +#include "BaseClass.hpp" + +namespace CIMPP { + + /** + * A floating point number. The range is unspecified and not limited. + */ + class Float : public BaseClass + { + + public: + Float(); + virtual ~Float(); + Float(long double value); + static const BaseClassDefiner declare(); + Float& operator=(long double &rop); + Float& operator+=(const Float& rhs); + Float& operator-=(const Float& rhs); + Float& operator*=(const Float& rhs); + Float& operator/=(const Float& rhs); + friend std::istream& operator>>(std::istream& lop, Float& rop); + operator long double(); + + long double value = 0.0; + bool initialized = false; + + static const char debugName[]; + virtual const char* debugString(); + + static void addConstructToMap(std::unordered_map& factory_map); + static void addPrimitiveAssignFnsToMap(std::unordered_map&); + static void addClassAssignFnsToMap(std::unordered_map&); + }; +} +#endif // FLOAT_H diff --git a/cimgen/languages/cpp/static/Integer.cpp b/cimgen/languages/cpp/static/Integer.cpp new file mode 100644 index 00000000..638dd417 --- /dev/null +++ b/cimgen/languages/cpp/static/Integer.cpp @@ -0,0 +1,72 @@ +#include "Integer.hpp" +#include "CIMExceptions.hpp" + +using namespace CIMPP; + +Integer::Integer(){} + +Integer::~Integer(){} + +Integer::Integer(long int value) + : value(value), initialized(true) {} + +const BaseClassDefiner Integer::declare() { + return BaseClassDefiner(Integer::addConstructToMap, Integer::addPrimitiveAssignFnsToMap, Integer::addClassAssignFnsToMap, Integer::debugName); +} + +const char Integer::debugName[] = "Integer"; +const char* Integer::debugString() { + return Integer::debugName; +} + +void Integer::addConstructToMap(std::unordered_map& factory_map) {} + +void Integer::addPrimitiveAssignFnsToMap(std::unordered_map& assign_map) {} + +void Integer::addClassAssignFnsToMap(std::unordered_map& assign_map) {} + +namespace CIMPP { + Integer& Integer::operator=(long int &rop){ + value = rop; + initialized = true; + return *this; + } + + Integer& Integer::operator-=(const Integer& rhs){ + value -= rhs.value; + return *this; + } + + Integer& Integer::operator*=(const Integer& rhs){ + value *= rhs.value; + return *this; + } + + Integer& Integer::operator/=(const Integer& rhs){ + value /= rhs.value; + return *this; + } + + Integer& Integer::operator+=(const Integer& rhs){ + value += rhs.value; + return *this; + } + + Integer::operator long int(){ + if(!initialized) + { + throw new ReadingUninitializedField(); + } + return value; + } + + std::istream& operator>>(std::istream& lop, Integer& rop) + { + std::string tmp; + lop >> tmp; + + rop.value = stol(tmp); + rop.initialized = true; + return lop; + } +} diff --git a/cimgen/languages/cpp/static/Integer.hpp b/cimgen/languages/cpp/static/Integer.hpp new file mode 100644 index 00000000..a39f6c81 --- /dev/null +++ b/cimgen/languages/cpp/static/Integer.hpp @@ -0,0 +1,41 @@ +#ifndef INTEGER_H +#define INTEGER_H + +#include +#include +#include + +#include "BaseClass.hpp" + +namespace CIMPP { + + /** + * An Integer number. The range is unspecified and not limited. + */ + class Integer + { + public: + Integer(); + Integer(long int value); + virtual ~Integer(); + static const BaseClassDefiner declare(); + Integer& operator=(long int &rop); + Integer& operator+=(const Integer& rhs); + Integer& operator-=(const Integer& rhs); + Integer& operator*=(const Integer& rhs); + Integer& operator/=(const Integer& rhs); + friend std::istream& operator>>(std::istream& lop, Integer& rop); + operator long int(); + + long int value = 0; + bool initialized = false; + + static const char debugName[]; + virtual const char* debugString(); + + static void addConstructToMap(std::unordered_map& factory_map); + static void addPrimitiveAssignFnsToMap(std::unordered_map&); + static void addClassAssignFnsToMap(std::unordered_map&); + }; +} +#endif // INTEGER_H diff --git a/cimgen/languages/cpp/static/String.hpp b/cimgen/languages/cpp/static/String.hpp new file mode 100644 index 00000000..9ed60335 --- /dev/null +++ b/cimgen/languages/cpp/static/String.hpp @@ -0,0 +1,14 @@ +#ifndef STRING_H +#define STRING_H + +#include "BaseClass.hpp" +#include "string" + +/* +A string consisting of a sequence of characters. The character encoding is UTF-8. The string length is unspecified and unlimited. +*/ +namespace CIMPP { + + typedef std::string String; +} +#endif From 3cb75c476154675950deffb95f47421abb14bf9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=BCnther?= Date: Sun, 27 Oct 2024 20:58:44 +0100 Subject: [PATCH 11/13] Remove String, Date, Float from static directory (they are now generated) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Thomas Günther --- cimgen/languages/cpp/static/Date.cpp | 20 -------- cimgen/languages/cpp/static/Date.hpp | 26 ---------- cimgen/languages/cpp/static/Float.cpp | 67 -------------------------- cimgen/languages/cpp/static/Float.hpp | 42 ---------------- cimgen/languages/cpp/static/String.hpp | 14 ------ 5 files changed, 169 deletions(-) delete mode 100644 cimgen/languages/cpp/static/Date.cpp delete mode 100644 cimgen/languages/cpp/static/Date.hpp delete mode 100644 cimgen/languages/cpp/static/Float.cpp delete mode 100644 cimgen/languages/cpp/static/Float.hpp delete mode 100644 cimgen/languages/cpp/static/String.hpp diff --git a/cimgen/languages/cpp/static/Date.cpp b/cimgen/languages/cpp/static/Date.cpp deleted file mode 100644 index 79a800c8..00000000 --- a/cimgen/languages/cpp/static/Date.cpp +++ /dev/null @@ -1,20 +0,0 @@ - -#include "BaseClass.hpp" -#include "Date.hpp" -#include "String.hpp" - -using namespace CIMPP; - -Date::Date() {} - -Date::Date(String s) -{ - value=s; -} - -Date::~Date() {} - -BaseClass* Date_factory() -{ - return new Date; -} diff --git a/cimgen/languages/cpp/static/Date.hpp b/cimgen/languages/cpp/static/Date.hpp deleted file mode 100644 index 6027d6e0..00000000 --- a/cimgen/languages/cpp/static/Date.hpp +++ /dev/null @@ -1,26 +0,0 @@ -#ifndef Date_H -#define Date_H - -#include "BaseClass.hpp" -#include "String.hpp" - - -/* -Date as "yyyy-mm-dd", which conforms with ISO 8601. UTC time zone is specified as "yyyy-mm-ddZ". A local timezone relative UTC is specified as "yyyy-mm-dd(+/-)hh:mm". -*/ -namespace CIMPP { - - class Date: public BaseClass - { - public: - Date(); - Date(String); - virtual ~Date(); - - private: - String value; - }; - - BaseClass* Date_factory(); -} -#endif diff --git a/cimgen/languages/cpp/static/Float.cpp b/cimgen/languages/cpp/static/Float.cpp deleted file mode 100644 index 4daa7662..00000000 --- a/cimgen/languages/cpp/static/Float.cpp +++ /dev/null @@ -1,67 +0,0 @@ -#include "Float.hpp" -#include "CIMExceptions.hpp" - -using namespace CIMPP; - -Float::Float(){} - -Float::~Float(){} - -Float::Float(long double value) : value(value), initialized(true) {} - -void Float::addConstructToMap(std::unordered_map& factory_map) {} - -void Float::addPrimitiveAssignFnsToMap(std::unordered_map& assign_map) {} - -void Float::addClassAssignFnsToMap(std::unordered_map& assign_map) {} - -const char Float::debugName[] = "Float"; -const char* Float::debugString() { - return Float::debugName; -} - -const BaseClassDefiner Float::declare() { - return BaseClassDefiner(Float::addConstructToMap, Float::addPrimitiveAssignFnsToMap, Float::addClassAssignFnsToMap, Float::debugName); -} - -Float& Float::operator=(long double &rop) { - value = rop; - initialized = true; - return *this; -} - -Float& Float::operator-=(const Float& rhs) { - value -= rhs.value; - return *this; -} - -Float& Float::operator*=(const Float& rhs) { - value *= rhs.value; - return *this; -} - -Float& Float::operator/=(const Float& rhs) { - value /= rhs.value; - return *this; -} - -Float& Float::operator+=(const Float& rhs) { - value += rhs.value; - return *this; -} - -Float::operator long double() { - if(!initialized) - { - throw new ReadingUninitializedField(); - } - return value; -} - -std::istream& operator>>(std::istream& lop, Float& rop) { - std::string tmp; - lop >> tmp; - rop.value = stold(tmp); - rop.initialized = true; - return lop; -} diff --git a/cimgen/languages/cpp/static/Float.hpp b/cimgen/languages/cpp/static/Float.hpp deleted file mode 100644 index ab30f43d..00000000 --- a/cimgen/languages/cpp/static/Float.hpp +++ /dev/null @@ -1,42 +0,0 @@ -#ifndef FLOAT_H -#define FLOAT_H - -#include -#include -#include - -#include "BaseClass.hpp" - -namespace CIMPP { - - /** - * A floating point number. The range is unspecified and not limited. - */ - class Float : public BaseClass - { - - public: - Float(); - virtual ~Float(); - Float(long double value); - static const BaseClassDefiner declare(); - Float& operator=(long double &rop); - Float& operator+=(const Float& rhs); - Float& operator-=(const Float& rhs); - Float& operator*=(const Float& rhs); - Float& operator/=(const Float& rhs); - friend std::istream& operator>>(std::istream& lop, Float& rop); - operator long double(); - - long double value = 0.0; - bool initialized = false; - - static const char debugName[]; - virtual const char* debugString(); - - static void addConstructToMap(std::unordered_map& factory_map); - static void addPrimitiveAssignFnsToMap(std::unordered_map&); - static void addClassAssignFnsToMap(std::unordered_map&); - }; -} -#endif // FLOAT_H diff --git a/cimgen/languages/cpp/static/String.hpp b/cimgen/languages/cpp/static/String.hpp deleted file mode 100644 index 9ed60335..00000000 --- a/cimgen/languages/cpp/static/String.hpp +++ /dev/null @@ -1,14 +0,0 @@ -#ifndef STRING_H -#define STRING_H - -#include "BaseClass.hpp" -#include "string" - -/* -A string consisting of a sequence of characters. The character encoding is UTF-8. The string length is unspecified and unlimited. -*/ -namespace CIMPP { - - typedef std::string String; -} -#endif From d0ce341171c56277d8dfdf87cd1de2e50bdfbda3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=BCnther?= Date: Sun, 27 Oct 2024 21:19:34 +0100 Subject: [PATCH 12/13] Unify BaseClass, Integer, Boolean with generated classes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Thomas Günther --- cimgen/languages/cpp/static/BaseClass.cpp | 16 ++-- cimgen/languages/cpp/static/BaseClass.hpp | 19 +++-- cimgen/languages/cpp/static/Boolean.cpp | 63 +++++++-------- cimgen/languages/cpp/static/Boolean.hpp | 31 +++----- cimgen/languages/cpp/static/Integer.cpp | 96 ++++++++++++----------- cimgen/languages/cpp/static/Integer.hpp | 43 +++++----- 6 files changed, 127 insertions(+), 141 deletions(-) diff --git a/cimgen/languages/cpp/static/BaseClass.cpp b/cimgen/languages/cpp/static/BaseClass.cpp index 20dc2088..ff3a94a5 100644 --- a/cimgen/languages/cpp/static/BaseClass.cpp +++ b/cimgen/languages/cpp/static/BaseClass.cpp @@ -1,22 +1,20 @@ - #include "BaseClass.hpp" using namespace CIMPP; BaseClass::~BaseClass() {} -void BaseClass::addConstructToMap(std::unordered_map& factory_map) {} - -void BaseClass::addPrimitiveAssignFnsToMap(std::unordered_map& assign_map) {} - -void BaseClass::addClassAssignFnsToMap(std::unordered_map& assign_map) {} - const char BaseClass::debugName[] = "BaseClass"; -const char* BaseClass::debugString() +const char* BaseClass::debugString() const { return BaseClass::debugName; } -CIMPP::BaseClassDefiner BaseClass::declare() { +void BaseClass::addConstructToMap(std::unordered_map& factory_map) {} +void BaseClass::addPrimitiveAssignFnsToMap(std::unordered_map& assign_map) {} +void BaseClass::addClassAssignFnsToMap(std::unordered_map& assign_map) {} + +const BaseClassDefiner BaseClass::declare() +{ return BaseClassDefiner(BaseClass::addConstructToMap, BaseClass::addPrimitiveAssignFnsToMap, BaseClass::addClassAssignFnsToMap, BaseClass::debugName); } diff --git a/cimgen/languages/cpp/static/BaseClass.hpp b/cimgen/languages/cpp/static/BaseClass.hpp index a7cd6129..7af998c6 100644 --- a/cimgen/languages/cpp/static/BaseClass.hpp +++ b/cimgen/languages/cpp/static/BaseClass.hpp @@ -5,18 +5,23 @@ #define CGMES_BUILD #endif +#include #include + #include "BaseClassDefiner.hpp" -class BaseClass { +class BaseClass +{ public: enum cgmesProfile {EQ = 0, SSH = 1, TP = 2, SV = 3, DY = 4, GL = 5, DI = 6}; virtual ~BaseClass(); - static CIMPP::BaseClassDefiner declare(); - static void addConstructToMap(std::unordered_map&); - static void addPrimitiveAssignFnsToMap(std::unordered_map&); - static void addClassAssignFnsToMap(std::unordered_map&); - const static char debugName[]; - virtual const char* debugString(); + + static const char debugName[]; + virtual const char* debugString() const; + + static void addConstructToMap(std::unordered_map& factory_map); + static void addPrimitiveAssignFnsToMap(std::unordered_map& assign_map); + static void addClassAssignFnsToMap(std::unordered_map& assign_map); + static const CIMPP::BaseClassDefiner declare(); }; #endif // BASECLASS_HPP diff --git a/cimgen/languages/cpp/static/Boolean.cpp b/cimgen/languages/cpp/static/Boolean.cpp index 35f7fa9a..1818ab8a 100644 --- a/cimgen/languages/cpp/static/Boolean.cpp +++ b/cimgen/languages/cpp/static/Boolean.cpp @@ -1,21 +1,13 @@ #include "Boolean.hpp" -#include "CIMExceptions.hpp" -using namespace CIMPP; - -Boolean::Boolean(){} - -Boolean::~Boolean(){} +#include +#include -Boolean::Boolean(bool value) - : value(value), initialized(true) {} +#include "../src/CIMExceptions.hpp" -const BaseClassDefiner Boolean::declare() -{ - return BaseClassDefiner(Boolean::addConstructToMap, Boolean::addPrimitiveAssignFnsToMap, Boolean::addClassAssignFnsToMap, Boolean::debugName); -} +using namespace CIMPP; -Boolean& Boolean::operator=(bool &rop) +Boolean& Boolean::operator=(bool rop) { value = rop; initialized = true; @@ -24,56 +16,57 @@ Boolean& Boolean::operator=(bool &rop) Boolean::operator bool() { - if(!initialized) + if (!initialized) { throw new ReadingUninitializedField(); } return value; } -void Boolean::addConstructToMap(std::unordered_map& factory_map) {} - -void Boolean::addPrimitiveAssignFnsToMap(std::unordered_map& assign_map) {} - -void Boolean::addClassAssignFnsToMap(std::unordered_map& assign_map) {} - const char Boolean::debugName[] = "Boolean"; -const char* Boolean::debugString() +const char* Boolean::debugString() const { return Boolean::debugName; } -namespace CIMPP { +namespace CIMPP +{ std::istream& operator>>(std::istream& lop, Boolean& rop) { + rop.initialized = false; + std::string tmp; lop >> tmp; - if(tmp == "true" || tmp == "True" || tmp == "TRUE") + + if (tmp == "true" || tmp == "True" || tmp == "TRUE") { rop.value = true; rop.initialized = true; return lop; } - if(tmp == "false" || tmp == "False" || tmp == "FALSE") + if (tmp == "false" || tmp == "False" || tmp == "FALSE") { rop.value = false; rop.initialized = true; return lop; } - else - { - lop.setstate(std::ios::failbit); - return lop; - } + + lop.setstate(std::ios::failbit); + return lop; } - std::ostream& operator<<(std::ostream& os, Boolean& rop) + std::ostream& operator<<(std::ostream& os, const Boolean& obj) { - if (rop) { - os << "true"; - } - else { - os << "false"; + if (obj.initialized) + { + if (obj.value) + { + os << "true"; + } + else + { + os << "false"; + } } return os; } diff --git a/cimgen/languages/cpp/static/Boolean.hpp b/cimgen/languages/cpp/static/Boolean.hpp index 34e30d18..9dc8d09c 100644 --- a/cimgen/languages/cpp/static/Boolean.hpp +++ b/cimgen/languages/cpp/static/Boolean.hpp @@ -1,38 +1,31 @@ #ifndef BOOLEAN_H #define BOOLEAN_H -#include -#include #include +#include -#include "BaseClass.hpp" - -namespace CIMPP { +namespace CIMPP +{ /** * A type with the value space "true" and "false". */ class Boolean { public: - Boolean(); - virtual ~Boolean(); - static const BaseClassDefiner declare(); + Boolean() : value(false), initialized(false) {} + Boolean(bool value) : value(value), initialized(true) {} - Boolean(bool value); - Boolean& operator=(bool &rop); - friend std::istream& operator>>(std::istream& lop, Boolean& rop); - friend std::ostream& operator<<(std::ostream& os, Boolean& rop); + Boolean& operator=(bool rop); operator bool(); - bool value = false; - bool initialized = false; + bool value; + bool initialized; static const char debugName[]; - virtual const char* debugString(); + const char* debugString() const; - static void addConstructToMap(std::unordered_map& factory_map); - static void addPrimitiveAssignFnsToMap(std::unordered_map&); - static void addClassAssignFnsToMap(std::unordered_map&); + friend std::istream& operator>>(std::istream& lop, Boolean& rop); + friend std::ostream& operator<<(std::ostream& os, const Boolean& obj); }; } -#endif +#endif // BOOLEAN_H diff --git a/cimgen/languages/cpp/static/Integer.cpp b/cimgen/languages/cpp/static/Integer.cpp index 638dd417..2a1cda43 100644 --- a/cimgen/languages/cpp/static/Integer.cpp +++ b/cimgen/languages/cpp/static/Integer.cpp @@ -1,72 +1,74 @@ #include "Integer.hpp" -#include "CIMExceptions.hpp" -using namespace CIMPP; +#include -Integer::Integer(){} +#include "../src/CIMExceptions.hpp" -Integer::~Integer(){} +using namespace CIMPP; -Integer::Integer(long int value) - : value(value), initialized(true) {} +Integer& Integer::operator=(long int rop) +{ + value = rop; + initialized = true; + return *this; +} -const BaseClassDefiner Integer::declare() { - return BaseClassDefiner(Integer::addConstructToMap, Integer::addPrimitiveAssignFnsToMap, Integer::addClassAssignFnsToMap, Integer::debugName); +Integer::operator long int() +{ + if (!initialized) + { + throw new ReadingUninitializedField(); + } + return value; } const char Integer::debugName[] = "Integer"; -const char* Integer::debugString() { +const char* Integer::debugString() const +{ return Integer::debugName; } -void Integer::addConstructToMap(std::unordered_map& factory_map) {} - -void Integer::addPrimitiveAssignFnsToMap(std::unordered_map& assign_map) {} - -void Integer::addClassAssignFnsToMap(std::unordered_map& assign_map) {} - -namespace CIMPP { - Integer& Integer::operator=(long int &rop){ - value = rop; - initialized = true; - return *this; - } - - Integer& Integer::operator-=(const Integer& rhs){ - value -= rhs.value; - return *this; - } - - Integer& Integer::operator*=(const Integer& rhs){ - value *= rhs.value; - return *this; - } +Integer& Integer::operator+=(const Integer& rhs) +{ + value += rhs.value; + return *this; +} - Integer& Integer::operator/=(const Integer& rhs){ - value /= rhs.value; - return *this; - } +Integer& Integer::operator-=(const Integer& rhs) +{ + value -= rhs.value; + return *this; +} - Integer& Integer::operator+=(const Integer& rhs){ - value += rhs.value; - return *this; - } +Integer& Integer::operator*=(const Integer& rhs) +{ + value *= rhs.value; + return *this; +} - Integer::operator long int(){ - if(!initialized) - { - throw new ReadingUninitializedField(); - } - return value; - } +Integer& Integer::operator/=(const Integer& rhs) +{ + value /= rhs.value; + return *this; +} +namespace CIMPP +{ std::istream& operator>>(std::istream& lop, Integer& rop) { std::string tmp; lop >> tmp; - rop.value = stol(tmp); rop.initialized = true; return lop; } + + std::ostream& operator<<(std::ostream& os, const Integer& obj) + { + if (obj.initialized) + { + os << obj.value; + } + return os; + } } diff --git a/cimgen/languages/cpp/static/Integer.hpp b/cimgen/languages/cpp/static/Integer.hpp index a39f6c81..fab9a715 100644 --- a/cimgen/languages/cpp/static/Integer.hpp +++ b/cimgen/languages/cpp/static/Integer.hpp @@ -1,41 +1,36 @@ #ifndef INTEGER_H #define INTEGER_H -#include -#include #include +#include -#include "BaseClass.hpp" - -namespace CIMPP { - +namespace CIMPP +{ /** - * An Integer number. The range is unspecified and not limited. - */ + * An Integer number. The range is unspecified and not limited. + */ class Integer { public: - Integer(); - Integer(long int value); - virtual ~Integer(); - static const BaseClassDefiner declare(); - Integer& operator=(long int &rop); - Integer& operator+=(const Integer& rhs); - Integer& operator-=(const Integer& rhs); - Integer& operator*=(const Integer& rhs); - Integer& operator/=(const Integer& rhs); - friend std::istream& operator>>(std::istream& lop, Integer& rop); + Integer() : value(0), initialized(false) {} + Integer(long int value) : value(value), initialized(true) {} + + Integer& operator=(long int rop); operator long int(); - long int value = 0; - bool initialized = false; + long int value; + bool initialized; static const char debugName[]; - virtual const char* debugString(); + const char* debugString() const; - static void addConstructToMap(std::unordered_map& factory_map); - static void addPrimitiveAssignFnsToMap(std::unordered_map&); - static void addClassAssignFnsToMap(std::unordered_map&); + Integer& operator+=(const Integer& rhs); + Integer& operator-=(const Integer& rhs); + Integer& operator*=(const Integer& rhs); + Integer& operator/=(const Integer& rhs); + + friend std::istream& operator>>(std::istream& lop, Integer& rop); + friend std::ostream& operator<<(std::ostream& os, const Integer& obj); }; } #endif // INTEGER_H From 85078397dbf001c9e4f37026882b7badebc6c6bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=BCnther?= Date: Wed, 20 Nov 2024 01:47:49 +0100 Subject: [PATCH 13/13] Simplify create_assign function in cpp/lang_pack.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Thomas Günther --- cimgen/languages/cpp/lang_pack.py | 34 +++++-------------------------- 1 file changed, 5 insertions(+), 29 deletions(-) diff --git a/cimgen/languages/cpp/lang_pack.py b/cimgen/languages/cpp/lang_pack.py index 8e32123d..0522108e 100644 --- a/cimgen/languages/cpp/lang_pack.py +++ b/cimgen/languages/cpp/lang_pack.py @@ -275,20 +275,14 @@ def create_assign(text, render): attribute_txt = render(text) attribute_json = eval(attribute_txt) assign = "" - _class = attribute_json["attribute_class"] if not _attribute_is_primitive_or_datatype_or_enum(attribute_json): return "" label_without_keyword = attribute_json["label"] if label_without_keyword == "switch": label_without_keyword = "_switch" - if ( - attribute_json["is_enum_attribute"] - or attribute_json["is_datatype_attribute"] - or _class in ("Float", "Decimal", "Integer", "Boolean") - ): - assign = ( - """ + assign = ( + """ bool assign_CLASS_LABEL(std::stringstream &buffer, BaseClass* BaseClass_ptr1) { if (CLASS* element = dynamic_cast(BaseClass_ptr1)) @@ -300,30 +294,12 @@ def create_assign(text, render): return true; } return false; -}""".replace( # noqa: E101,W191 - "CLASS", attribute_json["domain"] - ) - .replace("LABEL", attribute_json["label"]) - .replace("LBL_WO_KEYWORD", label_without_keyword) - ) - else: # is_primitive_string_attribute - assign = """ -bool assign_CLASS_LABEL(std::stringstream &buffer, BaseClass* BaseClass_ptr1) -{ - if (CLASS* element = dynamic_cast(BaseClass_ptr1)) - { - element->LABEL = buffer.str(); - if (buffer.fail()) - return false; - else - return true; - } - return false; }""".replace( # noqa: E101,W191 "CLASS", attribute_json["domain"] - ).replace( - "LABEL", attribute_json["label"] ) + .replace("LABEL", attribute_json["label"]) + .replace("LBL_WO_KEYWORD", label_without_keyword) + ) return assign