-
-
Notifications
You must be signed in to change notification settings - Fork 902
Expand file tree
/
Copy pathIfcBaseClass.h
More file actions
234 lines (186 loc) · 7.77 KB
/
IfcBaseClass.h
File metadata and controls
234 lines (186 loc) · 7.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/********************************************************************************
* *
* This file is part of IfcOpenShell. *
* *
* IfcOpenShell is free software: you can redistribute it and/or modify *
* it under the terms of the Lesser GNU General Public License as published by *
* the Free Software Foundation, either version 3.0 of the License, or *
* (at your option) any later version. *
* *
* IfcOpenShell is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* Lesser GNU General Public License for more details. *
* *
* You should have received a copy of the Lesser GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
* *
********************************************************************************/
#ifndef IFCBASECLASS_H
#define IFCBASECLASS_H
#include "Argument.h"
#include "ifc_parse_api.h"
#include "IfcEntityInstanceData.h"
#include "IfcSchema.h"
#include "utils.h"
#include <atomic>
#include <boost/shared_ptr.hpp>
class aggregate_of_instance;
namespace IfcParse {
class IfcFile;
}
namespace IfcUtil {
class IFC_PARSE_API IfcBaseInterface {
protected:
static bool is_null(const IfcBaseInterface* not_this) {
return not_this == nullptr;
}
template <typename T>
std::enable_if_t<!std::is_same<IfcBaseClass, T>::value && !std::is_same<IfcBaseEntity, T>::value && !std::is_same<IfcBaseType, T>::value> raise_error_on_concrete_class() const {
throw IfcParse::IfcException("Instance of type " + this->declaration().name() + " cannot be cast to " + T::Class().name());
}
template <typename T>
std::enable_if_t<std::is_same<IfcBaseClass, T>::value || std::is_same<IfcBaseEntity, T>::value || std::is_same<IfcBaseType, T>::value> raise_error_on_concrete_class() const {
throw IfcParse::IfcException("Instance of type " + this->declaration().name() + " cannot be cast to base class");
}
public:
virtual const IfcEntityInstanceData& data() const = 0;
virtual IfcEntityInstanceData& data() = 0;
virtual const IfcParse::declaration& declaration() const = 0;
virtual ~IfcBaseInterface() {}
template <class T>
T* as(bool do_throw = false) {
// @todo: do not allow this to be null in the first place
if (is_null(this)) {
return static_cast<T*>(0);
}
auto type = dynamic_cast<T*>(this);
if (do_throw && !type) {
raise_error_on_concrete_class<T>();
}
return type;
}
template <class T>
const T* as(bool do_throw = false) const {
if (is_null(this)) {
return static_cast<const T*>(0);
}
auto type = dynamic_cast<const T*>(this);
if (do_throw && !type) {
raise_error_on_concrete_class<T>();
}
return type;
}
};
class IFC_PARSE_API IfcBaseClass : public virtual IfcBaseInterface {
protected:
static std::atomic_uint32_t counter_;
uint32_t identity_;
public:
uint32_t id_;
IfcParse::IfcFile* file_;
protected:
IfcEntityInstanceData data_;
public:
IfcBaseClass(IfcEntityInstanceData&& data);
const IfcEntityInstanceData& data() const { return data_; }
IfcEntityInstanceData& data() { return data_; }
virtual const IfcParse::declaration& declaration() const = 0;
template <typename T>
typename std::enable_if<
(!(std::is_pointer<T>::value && std::is_base_of<IfcUtil::IfcBaseClass, typename std::remove_pointer<T>::type>::value) || std::is_same_v<IfcUtil::IfcBaseClass, std::remove_pointer_t<T>>),
void>::type
set_attribute_value(size_t i, const T& t);
template <typename T>
typename std::enable_if<
(!(std::is_pointer<T>::value&& std::is_base_of<IfcUtil::IfcBaseClass, typename std::remove_pointer<T>::type>::value) || std::is_same_v<IfcUtil::IfcBaseClass, std::remove_pointer_t<T>>),
void>::type
set_attribute_value(const std::string& name, const T& t);
void set_attribute_value(size_t i, IfcUtil::IfcBaseClass* p);
void set_attribute_value(const std::string& name, IfcUtil::IfcBaseClass* p);
void unset_attribute_value(size_t i);
AttributeValue get_attribute_value(size_t index) const;
uint32_t identity() const { return identity_; }
uint32_t id() const { return id_; }
void toString(std::ostream&, bool upper = false) const;
typedef aggregate_of_instance list;
};
class IFC_PARSE_API IfcBaseEntity : public IfcBaseClass {
public:
IfcBaseEntity(IfcEntityInstanceData&& data);
IfcBaseEntity(size_t n)
: IfcBaseClass(IfcEntityInstanceData(in_memory_attribute_storage(n)))
{}
virtual const IfcParse::declaration& declaration() const = 0;
AttributeValue get(const std::string& name) const;
template <typename T>
T get_value(const std::string& name) const;
template <typename T>
T get_value(const std::string& name, const T& default_value) const;
boost::shared_ptr<aggregate_of_instance> get_inverse(const std::string& name) const;
unsigned set_id(const boost::optional<unsigned>& i);
void populate_derived();
};
class IFC_PARSE_API IfcLateBoundEntity : public IfcBaseEntity {
private:
const IfcParse::declaration* decl_;
public:
IfcLateBoundEntity(const IfcParse::declaration* decl, IfcEntityInstanceData&& data) : IfcBaseEntity(std::move(data)),
decl_(decl) {}
virtual const IfcParse::declaration& declaration() const {
return *decl_;
}
};
// TODO: Investigate whether these should be template classes instead
class IFC_PARSE_API IfcBaseType : public IfcBaseClass {
public:
IfcBaseType(IfcEntityInstanceData&& data)\
: IfcBaseClass(std::move(data))
{}
IfcBaseType()
: IfcBaseClass(IfcEntityInstanceData(in_memory_attribute_storage(1)))
{}
virtual const IfcParse::declaration& declaration() const = 0;
};
} // namespace IfcUtil
namespace IfcUtil {
template <typename T>
T IfcBaseEntity::get_value(const std::string& name) const {
auto attr = get(name);
return (T) attr;
}
template <typename T>
T IfcBaseEntity::get_value(const std::string& name, const T& default_value) const {
auto attr = get(name);
if (attr.isNull()) {
return default_value;
}
return (T) attr;
}
} // namespace IfcUtil
template <class U>
typename U::list::ptr aggregate_of_instance::as() {
typename U::list::ptr result(new typename U::list);
for (it i = begin(); i != end(); ++i) {
if ((*i)->template as<U>()) {
result->push((*i)->template as<U>());
}
}
return result;
}
template <class U>
typename aggregate_of_aggregate_of<U>::ptr aggregate_of_aggregate_of_instance::as() {
typename aggregate_of_aggregate_of<U>::ptr result(new aggregate_of_aggregate_of<U>);
for (outer_it outer = begin(); outer != end(); ++outer) {
const std::vector<IfcUtil::IfcBaseClass*>& from = *outer;
typename std::vector<U*> to;
for (inner_it inner = from.begin(); inner != from.end(); ++inner) {
if ((*inner)->template as<U>()) {
to.push_back((*inner)->template as<U>());
}
}
result->push(to);
}
return result;
}
#endif