Skip to content

Commit f1e9358

Browse files
committed
Reuse constructors and return *this from initialize()
1 parent 91ae631 commit f1e9358

2 files changed

Lines changed: 13 additions & 15 deletions

File tree

src/ifcopenshell-python/ifcopenshell/express/header.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,10 @@ def get_select_super_types(nm, bases=[]):
115115
# with the v1 data model we're back to exactly one supertype, no more virtual inheritance to handle selects
116116
assert len(superclasses) == 1
117117
superclass_statement = superclasses[0]
118+
superclass_2 = superclass_statement.split('::')[-1]
118119

119120
write(
120-
templates.simpletype, name=name, type=type_str, attr_type=attr_type, superclass=superclass_statement
121+
templates.simpletype, name=name, type=type_str, attr_type=attr_type, superclass=superclass_statement, superclass_2=superclass_2
121122
)
122123

123124
class_definitions = []
@@ -181,6 +182,7 @@ def write_inverse(attr):
181182
supertypes = list(map(case_normalize, supertypes))
182183
assert len(supertypes) == 1
183184
superclass = supertypes[0]
185+
superclass_2 = superclass.split('::')[-1]
184186

185187
argument_count = mapping.argument_count(type)
186188

src/ifcopenshell-python/ifcopenshell/express/templates.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,10 @@ class spf_header;
113113
simpletype = """%(documentation)s
114114
class IFC_PARSE_API %(name)s : public %(superclass)s {
115115
public:
116-
%(name)s() {}
117-
explicit %(name)s (const std::weak_ptr<instance_data>& data) : %(superclass)s(data) {}
116+
using %(superclass)s::%(superclass_2)s;
118117
119118
static const ifcopenshell::type_declaration& Class();
120-
void initialize(%(type)s v);
119+
%(name)s initialize(%(type)s v);
121120
operator %(type)s() const;
122121
};
123122
"""
@@ -142,8 +141,7 @@ class IFC_PARSE_API %(name)s : public %(superclass)s {
142141
select = """%(documentation)s
143142
class IFC_PARSE_API %(name)s : public express::Select {
144143
public:
145-
%(name)s() {}
146-
explicit %(name)s(const express::Base& c) : express::Select(c) {}
144+
using express::Select::Select;
147145
148146
static const ifcopenshell::select_type& Class();
149147
%(template_items)s
@@ -162,29 +160,27 @@ class IFC_PARSE_API %(name)s : public express::Select {
162160
enumeration = """%(documentation)s
163161
class IFC_PARSE_API %(name)s : public express::DeclaredType {
164162
public:
165-
%(name)s() {}
166-
explicit %(name)s (const std::weak_ptr<instance_data>& data) : express::DeclaredType(data) {}
163+
using express::DeclaredType::DeclaredType;
167164
168165
typedef enum {%(values)s} Value;
169166
static const char* ToString(Value v);
170167
static Value FromString(const std::string& s);
171168
172169
static const ifcopenshell::enumeration_type& Class();
173-
void initialize(Value v);
174-
void initialize(const std::string& v);
170+
%(name)s initialize(Value v);
171+
%(name)s initialize(const std::string& v);
175172
operator Value() const;
176173
};
177174
"""
178175

179176
entity = """%(documentation)s
180177
class IFC_PARSE_API %(name)s : public %(superclass)s {
181178
public:
182-
%(name)s() {}
183-
explicit %(name)s (const std::weak_ptr<instance_data>& data) : %(superclass)s(data) {}
179+
using %(superclass)s::%(superclass_2)s;
184180
185181
%(attributes)s %(inverse)s
186182
static const ifcopenshell::entity& Class();
187-
void initialize(%(constructor_arguments)s);
183+
%(name)s initialize(%(constructor_arguments)s);
188184
};
189185
"""
190186

@@ -227,7 +223,7 @@ class IFC_PARSE_API %(name)s : public %(superclass)s {
227223
%(attributes)s
228224
%(inverse)s
229225
const ifcopenshell::entity& %(schema_name)s::%(name)s::Class() { return *((ifcopenshell::entity*)%(schema_name_upper)s_types[%(index_in_schema)d]); }
230-
void %(schema_name)s::%(name)s::initialize(%(constructor_arguments)s) { %(constructor_implementation)s }
226+
%(schema_name)s::%(name)s %(schema_name)s::%(name)s::initialize(%(constructor_arguments)s) { %(constructor_implementation)s; return *this; }
231227
"""
232228

233229
# data_ = e;
@@ -239,7 +235,7 @@ class IFC_PARSE_API %(name)s : public %(superclass)s {
239235
const_function = "%(return_type)s %(schema_name)s::%(class_name)s::%(name)s(%(arguments)s) const { %(body)s }"
240236
constructor = "%(schema_name)s::%(class_name)s::%(class_name)s(%(arguments)s) { %(body)s }"
241237
initialize_single_initlist = (
242-
"void %(schema_name)s::%(class_name)s::initialize(%(arguments)s) { %(body)s }"
238+
"%(schema_name)s::%(class_name)s %(schema_name)s::%(class_name)s::initialize(%(arguments)s) { %(body)s; return *this; }"
243239
)
244240
cast_function = "%(schema_name)s::%(class_name)s::operator %(return_type)s() const { %(body)s }"
245241

0 commit comments

Comments
 (0)