mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-08-19 03:33:48 +00:00
String pooling and reducing duplication in schema codegen
This commit is contained in:
@@ -113,6 +113,7 @@ class LateBoundSchemaInstantiator:
|
|||||||
|
|
||||||
class EarlyBoundCodeWriter:
|
class EarlyBoundCodeWriter:
|
||||||
def __init__(self, schema_name):
|
def __init__(self, schema_name):
|
||||||
|
self.strings = []
|
||||||
self.schema_name = schema_name
|
self.schema_name = schema_name
|
||||||
self.schema_name_title = schema_name.capitalize()
|
self.schema_name_title = schema_name.capitalize()
|
||||||
|
|
||||||
@@ -127,6 +128,14 @@ class EarlyBoundCodeWriter:
|
|||||||
|
|
||||||
self.names = []
|
self.names = []
|
||||||
|
|
||||||
|
def make_string(self, s):
|
||||||
|
try:
|
||||||
|
i = self.strings.index(s)
|
||||||
|
except ValueError:
|
||||||
|
self.strings.append(s)
|
||||||
|
i = len(self.strings) - 1
|
||||||
|
return "strings[%d]" % i
|
||||||
|
|
||||||
def aggregation_type(self, aggr_type, bound1, bound2, decl_type):
|
def aggregation_type(self, aggr_type, bound1, bound2, decl_type):
|
||||||
return (
|
return (
|
||||||
"new aggregation_type(aggregation_type::%(aggr_type)s_type, %(bound1)d, %(bound2)d, %(decl_type)s)"
|
"new aggregation_type(aggregation_type::%(aggr_type)s_type, %(bound1)d, %(bound2)d, %(decl_type)s)"
|
||||||
@@ -149,6 +158,9 @@ class EarlyBoundCodeWriter:
|
|||||||
|
|
||||||
self.statements.append("{factory_placeholder}")
|
self.statements.append("{factory_placeholder}")
|
||||||
|
|
||||||
|
self.statements.append("using namespace std::string_literals;")
|
||||||
|
self.statements.append("{strings_placeholder}")
|
||||||
|
|
||||||
self.statements.append(
|
self.statements.append(
|
||||||
"""
|
"""
|
||||||
#if defined(__clang__)
|
#if defined(__clang__)
|
||||||
@@ -164,107 +176,109 @@ __attribute__((optnone))
|
|||||||
self.statements.append("IfcParse::schema_definition* %s_populate_schema() {" % self.schema_name)
|
self.statements.append("IfcParse::schema_definition* %s_populate_schema() {" % self.schema_name)
|
||||||
|
|
||||||
def typedef(self, name, declared_type):
|
def typedef(self, name, declared_type):
|
||||||
|
name_string = self.make_string(name)
|
||||||
schema_name = self.schema_name
|
schema_name = self.schema_name
|
||||||
index_in_schema = self.names.index(name)
|
index_in_schema = self.names.index(name)
|
||||||
self.statements.append(
|
self.statements.append(
|
||||||
' %(schema_name)s_%(name)s_type = new type_declaration("%(name)s", %(index_in_schema)d, %(declared_type)s);'
|
' %(schema_name)s_%(name)s_type = new type_declaration(%(name_string)s, %(index_in_schema)d, %(declared_type)s);'
|
||||||
% locals()
|
% locals()
|
||||||
)
|
)
|
||||||
|
|
||||||
def enumeration(self, name, enum):
|
def enumeration(self, name, enum):
|
||||||
schema_name = self.schema_name
|
schema_name = self.schema_name
|
||||||
index_in_schema = self.names.index(name)
|
index_in_schema = self.names.index(name)
|
||||||
self.statements.append(" {")
|
name_string = self.make_string(name)
|
||||||
self.statements.append(" std::vector<std::string> items; items.reserve(%d);" % len(enum.values))
|
# @tfk we don't sort for correspondence with header file
|
||||||
self.statements.extend(map(lambda v: ' items.push_back("%s");' % v, sorted(enum.values)))
|
# values = sorted(enum.values)
|
||||||
|
values = enum.values
|
||||||
self.statements.append(
|
self.statements.append(
|
||||||
' %(schema_name)s_%(name)s_type = new enumeration_type("%(name)s", %(index_in_schema)d, items);'
|
' %(schema_name)s_%(name)s_type = new enumeration_type(%(name_string)s, %(index_in_schema)d, {'
|
||||||
% locals()
|
% locals()
|
||||||
)
|
)
|
||||||
self.statements.append(" }")
|
self.statements.extend(map(lambda v: ' %s%s' % (self.make_string(v), '' if v == values[-1] else ','), values))
|
||||||
|
self.statements.append(' });')
|
||||||
|
|
||||||
def entity(self, name, type):
|
def entity(self, name, type):
|
||||||
schema_name = self.schema_name
|
schema_name = self.schema_name
|
||||||
index_in_schema = self.names.index(name)
|
index_in_schema = self.names.index(name)
|
||||||
|
name_string = self.make_string(name)
|
||||||
supertype = "0" if len(type.supertypes) == 0 else "%s_%s_type" % (self.schema_name, type.supertypes[0])
|
supertype = "0" if len(type.supertypes) == 0 else "%s_%s_type" % (self.schema_name, type.supertypes[0])
|
||||||
is_abstract = "true" if type.abstract else "false"
|
is_abstract = "true" if type.abstract else "false"
|
||||||
self.statements.append(
|
self.statements.append(
|
||||||
' %(schema_name)s_%(name)s_type = new entity("%(name)s", %(is_abstract)s, %(index_in_schema)d, %(supertype)s);'
|
' %(schema_name)s_%(name)s_type = new entity(%(name_string)s, %(is_abstract)s, %(index_in_schema)d, %(supertype)s);'
|
||||||
% locals()
|
% locals()
|
||||||
)
|
)
|
||||||
|
|
||||||
def select(self, name, type):
|
def select(self, name, type):
|
||||||
schema_name = self.schema_name
|
schema_name = self.schema_name
|
||||||
index_in_schema = self.names.index(name)
|
index_in_schema = self.names.index(name)
|
||||||
self.statements.append(" {")
|
name_string = self.make_string(name)
|
||||||
self.statements.append(" std::vector<const declaration*> items; items.reserve(%d);" % len(type.values))
|
values = sorted(map(str, type.values))
|
||||||
self.statements.extend(
|
|
||||||
map(lambda v: " items.push_back(%s_%s_type);" % (self.schema_name, v), sorted(map(str, type.values)))
|
|
||||||
)
|
|
||||||
self.statements.append(
|
self.statements.append(
|
||||||
' %(schema_name)s_%(name)s_type = new select_type("%(name)s", %(index_in_schema)d, items);'
|
' %(schema_name)s_%(name)s_type = new select_type(%(name_string)s, %(index_in_schema)d, {'
|
||||||
% locals()
|
% locals()
|
||||||
)
|
)
|
||||||
self.statements.append(" }")
|
self.statements.extend(
|
||||||
|
map(lambda v: " %s_%s_type%s" % (self.schema_name, v, '' if v == values[-1] else ','), values)
|
||||||
|
)
|
||||||
|
self.statements.append(" });")
|
||||||
|
|
||||||
def entity_attributes(self, name, attribute_definitions, is_derived):
|
def entity_attributes(self, name, attribute_definitions, is_derived):
|
||||||
schema_name = self.schema_name
|
schema_name = self.schema_name
|
||||||
self.statements.append(" {")
|
|
||||||
self.statements.append(
|
self.statements.append(" %(schema_name)s_%(name)s_type->set_attributes({" % locals())
|
||||||
" std::vector<const attribute*> attributes; attributes.reserve(%d);" % len(attribute_definitions)
|
|
||||||
)
|
|
||||||
for attr_name, decl_type, optional in attribute_definitions:
|
for attr_name, decl_type, optional in attribute_definitions:
|
||||||
|
name_string = self.make_string(attr_name)
|
||||||
optional_cpp = str(optional).lower()
|
optional_cpp = str(optional).lower()
|
||||||
|
tail = '' if attr_name == attribute_definitions[-1][0] else ','
|
||||||
self.statements.append(
|
self.statements.append(
|
||||||
' attributes.push_back(new attribute("%(attr_name)s", %(decl_type)s, %(optional_cpp)s));'
|
' new attribute(%(name_string)s, %(decl_type)s, %(optional_cpp)s)%(tail)s'
|
||||||
% locals()
|
% locals()
|
||||||
)
|
)
|
||||||
self.statements.append(" std::vector<bool> derived; derived.reserve(%d);" % len(is_derived))
|
self.statements.append(" },{")
|
||||||
self.statements.append(
|
self.statements.append(
|
||||||
" " + " ".join(map(lambda b: "derived.push_back(%s);" % str(b).lower(), is_derived))
|
" " + " ".join(map(lambda i, b: "%s%s" % (str(b).lower(), '' if i == len(is_derived) - 1 else ','), range(len(is_derived)), is_derived))
|
||||||
)
|
)
|
||||||
self.statements.append(" %(schema_name)s_%(name)s_type->set_attributes(attributes, derived);" % locals())
|
self.statements.append(" });")
|
||||||
self.statements.append(" }")
|
|
||||||
|
|
||||||
def inverse_attributes(self, name, inv_attrs):
|
def inverse_attributes(self, name, inv_attrs):
|
||||||
schema_name = self.schema_name
|
schema_name = self.schema_name
|
||||||
self.statements.append(" {")
|
self.statements.append(" %(schema_name)s_%(name)s_type->set_inverse_attributes({" % locals())
|
||||||
self.statements.append(
|
|
||||||
" std::vector<const inverse_attribute*> attributes; attributes.reserve(%d);" % len(inv_attrs)
|
|
||||||
)
|
|
||||||
for attr_name, aggr_type, bound1, bound2, entity_ref, attribute_entity, attribute_entity_index in inv_attrs:
|
for attr_name, aggr_type, bound1, bound2, entity_ref, attribute_entity, attribute_entity_index in inv_attrs:
|
||||||
|
name_string = self.make_string(attr_name)
|
||||||
|
tail = '' if attr_name == inv_attrs[-1][0] else ','
|
||||||
self.statements.append(
|
self.statements.append(
|
||||||
' attributes.push_back(new inverse_attribute("%(attr_name)s", inverse_attribute::%(aggr_type)s_type, %(bound1)d, %(bound2)d, %(schema_name)s_%(entity_ref)s_type, %(schema_name)s_%(attribute_entity)s_type->attributes()[%(attribute_entity_index)d]));'
|
' new inverse_attribute(%(name_string)s, inverse_attribute::%(aggr_type)s_type, %(bound1)d, %(bound2)d, %(schema_name)s_%(entity_ref)s_type, %(schema_name)s_%(attribute_entity)s_type->attributes()[%(attribute_entity_index)d])%(tail)s'
|
||||||
% locals()
|
% locals()
|
||||||
)
|
)
|
||||||
self.statements.append(" %(schema_name)s_%(name)s_type->set_inverse_attributes(attributes);" % locals())
|
self.statements.append(" });")
|
||||||
self.statements.append(" }")
|
|
||||||
|
|
||||||
def entity_subtypes(self, name, tys):
|
def entity_subtypes(self, name, tys):
|
||||||
schema_name = self.schema_name
|
schema_name = self.schema_name
|
||||||
self.statements.append(" {")
|
self.statements.append(" %(schema_name)s_%(name)s_type->set_subtypes({" % locals())
|
||||||
self.statements.append(" std::vector<const entity*> defs; defs.reserve(%d);" % len(tys))
|
|
||||||
self.statements.append(
|
self.statements.append(
|
||||||
(" " + "".join(map(lambda t: ("defs.push_back(%%(schema_name)s_%s_type);" % t), tys))) % locals()
|
(" " + "".join(map(lambda t: ("%%(schema_name)s_%s_type%s" % (t, '' if t == tys[-1] else ',')), tys))) % locals()
|
||||||
)
|
)
|
||||||
self.statements.append(" %(schema_name)s_%(name)s_type->set_subtypes(defs);" % locals())
|
self.statements.append(" });")
|
||||||
self.statements.append(" }")
|
|
||||||
|
|
||||||
def finalize(self, can_be_instantiated_set):
|
def finalize(self, can_be_instantiated_set):
|
||||||
schema_name = self.schema_name
|
schema_name = self.schema_name
|
||||||
|
schema_name_string = self.make_string(self.schema_name)
|
||||||
schema_name_title = self.schema_name.capitalize()
|
schema_name_title = self.schema_name.capitalize()
|
||||||
|
|
||||||
num_declarations = len(self.names)
|
num_declarations = len(self.names)
|
||||||
|
|
||||||
self.statements.append("")
|
self.statements.append("")
|
||||||
self.statements.append(
|
self.statements.append(
|
||||||
" std::vector<const declaration*> declarations; declarations.reserve(%(num_declarations)d);" % locals()
|
" std::vector<const declaration*> declarations= {"
|
||||||
)
|
)
|
||||||
for type_name in self.names:
|
for type_name in self.names:
|
||||||
self.statements.append(" declarations.push_back(%(schema_name)s_%(type_name)s_type);" % locals())
|
tail = '' if type_name == self.names[-1] else ','
|
||||||
|
self.statements.append(" %(schema_name)s_%(type_name)s_type%(tail)s" % locals())
|
||||||
|
self.statements.append(" };")
|
||||||
|
|
||||||
self.statements.append(
|
self.statements.append(
|
||||||
' return new schema_definition("%(schema_name)s", declarations, new %(schema_name)s_instance_factory());'
|
' return new schema_definition(%(schema_name_string)s, declarations, new %(schema_name)s_instance_factory());'
|
||||||
% locals()
|
% locals()
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -331,6 +345,12 @@ class %(schema_name)s_instance_factory : public IfcParse::instance_factory {
|
|||||||
% locals()
|
% locals()
|
||||||
)
|
)
|
||||||
|
|
||||||
|
strings_list = ",\n".join('"%s"s' % s for s in self.strings)
|
||||||
|
|
||||||
|
self.statements[self.statements.index("{strings_placeholder}")] = (
|
||||||
|
"static std::string strings[] = {%s};" % strings_list
|
||||||
|
)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return "\n".join(self.statements)
|
return "\n".join(self.statements)
|
||||||
|
|
||||||
|
|||||||
@@ -199,14 +199,11 @@ const IfcParse::enumeration_type& %(schema_name)s::%(name)s::Class() { return *%
|
|||||||
}
|
}
|
||||||
|
|
||||||
const char* %(schema_name)s::%(name)s::ToString(Value v) {
|
const char* %(schema_name)s::%(name)s::ToString(Value v) {
|
||||||
if ( v < 0 || v >= %(max_id)d ) throw IfcException("Unable to find keyword in schema");
|
return %(schema_name_upper)s_%(name)s_type->lookup_enum_value((size_t) v);
|
||||||
const char* names[] = { %(values)s };
|
|
||||||
return names[v];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
%(schema_name)s::%(name)s::Value %(schema_name)s::%(name)s::FromString(const std::string& s) {
|
%(schema_name)s::%(name)s::Value %(schema_name)s::%(name)s::FromString(const std::string& s) {
|
||||||
%(from_string_statements)s
|
return (%(schema_name)s::%(name)s::Value) %(schema_name_upper)s_%(name)s_type->lookup_enum_offset(s);
|
||||||
throw IfcException("Unable to find keyword in schema: " + s);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
%(schema_name)s::%(name)s::operator %(schema_name)s::%(name)s::Value() const {
|
%(schema_name)s::%(name)s::operator %(schema_name)s::%(name)s::Value() const {
|
||||||
|
|||||||
@@ -200,6 +200,23 @@ namespace IfcParse {
|
|||||||
|
|
||||||
const std::vector<std::string>& enumeration_items() const { return enumeration_items_; }
|
const std::vector<std::string>& enumeration_items() const { return enumeration_items_; }
|
||||||
|
|
||||||
|
const char* lookup_enum_value(size_t i) const {
|
||||||
|
if (i >= enumeration_items_.size()) {
|
||||||
|
throw IfcParse::IfcException("Unable to find keyword in schema for index " + std::to_string(i));
|
||||||
|
}
|
||||||
|
return enumeration_items_[i].c_str();
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t lookup_enum_offset(const std::string& s) const {
|
||||||
|
size_t i = 0;
|
||||||
|
for (auto it = enumeration_items_.begin(); it != enumeration_items_.end(); ++it, ++i) {
|
||||||
|
if (s == *it) {
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw IfcParse::IfcException("Unable to find keyword in schema: " + s);
|
||||||
|
}
|
||||||
|
|
||||||
virtual const enumeration_type* as_enumeration_type() const { return this; }
|
virtual const enumeration_type* as_enumeration_type() const { return this; }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user