Skip to content

Commit

Permalink
Add generation of getter functions for cpp (this will be used by CIM …
Browse files Browse the repository at this point in the history
…writer to export CIM data into several files based on CGMES profiles)

Signed-off-by: Thomas Günther <[email protected]>
  • Loading branch information
tom-hg57 committed Dec 15, 2024
1 parent aebcf0c commit 2ead869
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 0 deletions.
141 changes: 141 additions & 0 deletions cimgen/languages/cpp/lang_pack.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}}",
}


Expand Down Expand Up @@ -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() == "":
Expand Down Expand Up @@ -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<const BaseClass*>& BaseClass_list)
{
if (const OBJECT_CLASS* element = dynamic_cast<const OBJECT_CLASS*>(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<const BaseClass*>& BaseClass_list)
{
if (const OBJECT_CLASS* element = dynamic_cast<const OBJECT_CLASS*>(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<const CLASS*>(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<const CLASS*>(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)
Expand Down
3 changes: 3 additions & 0 deletions cimgen/languages/cpp/static/BaseClass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ const char* BaseClass::debugString() const
void BaseClass::addConstructToMap(std::unordered_map<std::string, BaseClass* (*)()>& factory_map) {}
void BaseClass::addPrimitiveAssignFnsToMap(std::unordered_map<std::string, assign_function>& assign_map) {}
void BaseClass::addClassAssignFnsToMap(std::unordered_map<std::string, class_assign_function>& assign_map) {}
void BaseClass::addPrimitiveGetFnsToMap(std::map<std::string, get_function>& get_map) const {}
void BaseClass::addClassGetFnsToMap(std::map<std::string, class_get_function>& get_map) const {}
void BaseClass::addEnumGetFnsToMap(std::map<std::string, get_function>& get_map) const {}

const BaseClassDefiner BaseClass::declare()
{
Expand Down
7 changes: 7 additions & 0 deletions cimgen/languages/cpp/static/BaseClass.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
#include "BaseClassDefiner.hpp"
#include "CGMESProfile.hpp"

class BaseClass;
typedef bool (*class_get_function)(const BaseClass*, std::list<const BaseClass*>&);
typedef bool (*get_function)(const BaseClass*, std::stringstream&);

class BaseClass
{
std::string rdfid;
Expand All @@ -31,6 +35,9 @@ class BaseClass
static void addConstructToMap(std::unordered_map<std::string, BaseClass* (*)()>& factory_map);
static void addPrimitiveAssignFnsToMap(std::unordered_map<std::string, assign_function>& assign_map);
static void addClassAssignFnsToMap(std::unordered_map<std::string, class_assign_function>& assign_map);
virtual void addPrimitiveGetFnsToMap(std::map<std::string, get_function>& get_map) const;
virtual void addClassGetFnsToMap(std::map<std::string, class_get_function>& get_map) const;
virtual void addEnumGetFnsToMap(std::map<std::string, get_function>& get_map) const;
static const CIMPP::BaseClassDefiner declare();
};
#endif // BASECLASS_HPP
3 changes: 3 additions & 0 deletions cimgen/languages/cpp/templates/cpp_header_template.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ namespace CIMPP
static void addConstructToMap(std::unordered_map<std::string, BaseClass* (*)()>& factory_map);
static void addPrimitiveAssignFnsToMap(std::unordered_map<std::string, assign_function>& assign_map);
static void addClassAssignFnsToMap(std::unordered_map<std::string, class_assign_function>& assign_map);
void addPrimitiveGetFnsToMap(std::map<std::string, get_function>& get_map) const override;
void addClassGetFnsToMap(std::map<std::string, class_get_function>& get_map) const override;
void addEnumGetFnsToMap(std::map<std::string, get_function>& get_map) const override;
static const BaseClassDefiner declare();
};

Expand Down
38 changes: 38 additions & 0 deletions cimgen/languages/cpp/templates/cpp_object_template.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Generated from the CGMES files via cimgen: https://github.com/sogno-platform/cim
#include "{{class_name}}.hpp"

#include <algorithm>
#include <ios>
#include <iterator>
#include <sstream>

{{#attributes}}
Expand Down Expand Up @@ -52,6 +54,18 @@ std::map<std::string, std::list<CGMESProfile>>
{{#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
{
Expand All @@ -77,6 +91,30 @@ void {{class_name}}::addClassAssignFnsToMap(std::unordered_map<std::string, clas
{{/attributes}}
}

void {{class_name}}::addPrimitiveGetFnsToMap(std::map<std::string, get_function>& get_map) const
{
{{sub_class_of}}::addPrimitiveGetFnsToMap(get_map);
{{#attributes}}
{{> insert_get}}
{{/attributes}}
}

void {{class_name}}::addClassGetFnsToMap(std::map<std::string, class_get_function>& get_map) const
{
{{sub_class_of}}::addClassGetFnsToMap(get_map);
{{#attributes}}
{{> insert_class_get}}
{{/attributes}}
}

void {{class_name}}::addEnumGetFnsToMap(std::map<std::string, get_function>& 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);
Expand Down

0 comments on commit 2ead869

Please sign in to comment.