From e6592ca70633928dc0f70c2254c6d89aa68ced07 Mon Sep 17 00:00:00 2001 From: Thomas Krijnen Date: Sun, 14 Feb 2021 12:19:06 +0100 Subject: [PATCH] conversion rules for first citizin enum types #1276 --- .../ifcopenshell/express/implementation.py | 5 +++ .../ifcopenshell/express/schema_class.py | 5 ++- .../ifcopenshell/express/templates.py | 35 ++++++++++++++++--- src/ifcparse/IfcBaseClass.h | 2 +- 4 files changed, 41 insertions(+), 6 deletions(-) diff --git a/src/ifcopenshell-python/ifcopenshell/express/implementation.py b/src/ifcopenshell-python/ifcopenshell/express/implementation.py index 555331728b..e4f2522883 100644 --- a/src/ifcopenshell-python/ifcopenshell/express/implementation.py +++ b/src/ifcopenshell-python/ifcopenshell/express/implementation.py @@ -223,6 +223,9 @@ class Implementation(codegen.Base): schema_entity_statements += [ templates.schema_entity_stmt % locals() for name, type in mapping.schema.simpletypes.items() ] + schema_entity_statements += [ + templates.schema_entity_stmt % locals() for name, type in mapping.schema.enumerations.items() + ] schema_entity_statements += [ templates.schema_entity_stmt % locals() for name, type in mapping.schema.entities.items() ] @@ -329,6 +332,8 @@ class Implementation(codegen.Base): ("extern entity* %s_%%s_type;" % schema_name_upper) % n for n in mapping.schema.entities.keys() ] + [ ("extern type_declaration* %s_%%s_type;" % schema_name_upper) % n for n in mapping.schema.simpletypes.keys() + ] + [ + ("extern enumeration_type* %s_%%s_type;" % schema_name_upper) % n for n in mapping.schema.enumerations.keys() ] self.str = templates.implementation % { diff --git a/src/ifcopenshell-python/ifcopenshell/express/schema_class.py b/src/ifcopenshell-python/ifcopenshell/express/schema_class.py index 53230c6b91..84b2da5fe6 100644 --- a/src/ifcopenshell-python/ifcopenshell/express/schema_class.py +++ b/src/ifcopenshell-python/ifcopenshell/express/schema_class.py @@ -488,7 +488,10 @@ class SchemaClass(codegen.Base): for name, tys in subtypes.items(): x.entity_subtypes(name, tys) - can_be_instantiated_set = set(list(mapping.schema.entities.keys()) + list(mapping.schema.simpletypes.keys())) + can_be_instantiated_set = set(list(mapping.schema.entities.keys()) + \ + list(mapping.schema.simpletypes.keys()) + \ + list(mapping.schema.enumerations.keys())) + x.finalize(can_be_instantiated_set) self.str = str(x) diff --git a/src/ifcopenshell-python/ifcopenshell/express/templates.py b/src/ifcopenshell-python/ifcopenshell/express/templates.py index 7b18a9cf03..94001ee7ae 100644 --- a/src/ifcopenshell-python/ifcopenshell/express/templates.py +++ b/src/ifcopenshell-python/ifcopenshell/express/templates.py @@ -140,11 +140,19 @@ select = """%(documentation)s typedef IfcUtil::IfcBaseClass %(name)s; """ -enumeration = """struct %(name)s { +enumeration = """class IFC_PARSE_API %(name)s : public IfcUtil::IfcBaseType { %(documentation)s -typedef enum {%(values)s} Value; -IFC_PARSE_API static const char* ToString(Value v); -IFC_PARSE_API static Value FromString(const std::string& s); +public: + typedef enum {%(values)s} Value; + static const char* ToString(Value v); + static Value FromString(const std::string& s); + + virtual const IfcParse::enumeration_type& declaration() const; + static const IfcParse::enumeration_type& Class(); + %(name)s (IfcEntityInstanceData* e); + %(name)s (Value v); + %(name)s (const std::string& v); + operator Value() const; }; """ @@ -160,6 +168,25 @@ public: """ enumeration_function = """ +const IfcParse::enumeration_type& %(schema_name)s::%(name)s::declaration() const { return *%(schema_name_upper)s_%(name)s_type; } +const IfcParse::enumeration_type& %(schema_name)s::%(name)s::Class() { return *%(schema_name_upper)s_%(name)s_type; } + +%(schema_name)s::%(name)s::%(name)s(IfcEntityInstanceData* e) { + data_ = e; +} + +%(schema_name)s::%(name)s::%(name)s(Value v) { + IfcWrite::IfcWriteArgument* attr = new IfcWrite::IfcWriteArgument(); + attr->set(IfcWrite::IfcWriteArgument::EnumerationReference(v,ToString(v))); + data_->setArgument(0,attr); +} + +%(schema_name)s::%(name)s::%(name)s(const std::string& v) { + IfcWrite::IfcWriteArgument* attr = new IfcWrite::IfcWriteArgument(); + attr->set(IfcWrite::IfcWriteArgument::EnumerationReference(FromString(v),ToString(FromString(v)))); + data_->setArgument(0,attr); +} + const char* %(schema_name)s::%(name)s::ToString(Value v) { if ( v < 0 || v >= %(max_id)d ) throw IfcException("Unable to find find keyword in schema"); const char* names[] = { %(values)s }; diff --git a/src/ifcparse/IfcBaseClass.h b/src/ifcparse/IfcBaseClass.h index 917f4b8d26..0704e30407 100644 --- a/src/ifcparse/IfcBaseClass.h +++ b/src/ifcparse/IfcBaseClass.h @@ -104,7 +104,7 @@ namespace IfcUtil { IfcBaseType() : IfcBaseClass() {} IfcBaseType(IfcEntityInstanceData* d) : IfcBaseClass(d) {} - virtual const IfcParse::type_declaration& declaration() const = 0; + virtual const IfcParse::declaration& declaration() const = 0; }; }