-
-
Notifications
You must be signed in to change notification settings - Fork 900
Expand file tree
/
Copy pathexpress.h
More file actions
261 lines (211 loc) · 8.04 KB
/
express.h
File metadata and controls
261 lines (211 loc) · 8.04 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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/********************************************************************************
* *
* 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 "schema.h"
#include "utils.h"
#include <atomic>
#include <boost/shared_ptr.hpp>
class aggregate_of_instance;
namespace ifcopenshell {
class file;
namespace impl {
struct in_memory_file_storage;
}
} // namespace ifcopenshell
class instance_data;
class attribute_value;
namespace express {
class Base;
class Select;
class Entity;
class DeclaredType;
class IFC_PARSE_API Base {
protected:
std::weak_ptr<instance_data> data_;
const instance_data* data() const;
instance_data* data();
public:
operator bool() const {
return !data_.expired();
}
bool operator<(const Base& other) const {
return data() < other.data();
}
bool operator==(const Base& other) const {
return data() == other.data();
}
bool operator!=(const Base& other) const {
return !(*this == other);
}
Base() {};
Base(const std::weak_ptr<instance_data>& data) : data_(data) {}
// @todo try and make this private over time too
const std::weak_ptr<instance_data>& data_weak() const { return data_; }
const ifcopenshell::declaration& declaration() const;
template <typename T>
typename std::enable_if<
(!std::is_base_of_v<express::Base, T> || std::is_same_v<express::Base, T>),
void>::type
set_attribute_value(size_t attribute_index, const T& value);
template <typename T>
typename std::enable_if<
(!std::is_base_of_v<express::Base, T> || std::is_same_v<express::Base, T>),
void>::type
set_attribute_value(const std::string& attribute_name, const T& value);
void set_attribute_value(size_t attribute_index, const express::Base& value);
void set_attribute_value(const std::string& attribute_name, const express::Base& value);
void unset_attribute_value(size_t attribute_index);
attribute_value get_attribute_value(size_t attribute_index) const;
uint32_t identity() const;
uint32_t id() const;
void to_string(std::ostream& stream, bool uppercase = false) const;
template <class T>
T as() const {
if constexpr (std::is_same_v<Entity, T>) {
if (declaration().as_entity() != nullptr) {
return T(data_weak());
} else {
return T{};
}
} else if constexpr (std::is_same_v<DeclaredType, T>) {
if (declaration().as_entity() == nullptr) {
return T(data_weak());
} else {
return T{};
}
} else if constexpr (std::is_same_v<Select, T>) {
static_assert(std::is_same_v<Select, T>, "Select is abstract");
} else {
if (declaration().is(T::Class())) {
return T(data_weak());
} else {
return T{};
}
}
}
ifcopenshell::file* file() const;
};
class IFC_PARSE_API Entity : public Base {
public:
Entity() {}
Entity(const std::weak_ptr<instance_data>& data) : Base(data) {}
attribute_value get(const std::string& attribute_name) const;
template <typename T>
T get_value(const std::string& attribute_name) const;
template <typename T>
T get_value(const std::string& attribute_name, const T& default_value) const;
std::vector<express::Entity> get_inverse(const std::string& attribute_name) const;
};
class IFC_PARSE_API Select : public Base {
public:
Select() {}
// Select are constructed from Base as cast functions, not from data directly
Select(const Base& base) : Base(base.data_weak()) {}
Base concrete() const {
return Base(data_weak());
}
};
// @todo Investigate whether these should be template classes instead
// @todo currently this class doesn't do much, decide whether to keep
// it or move certain functionality from Base downwards to
// Entity and DeclaredType
class IFC_PARSE_API DeclaredType : public Base {
public:
DeclaredType() {}
DeclaredType(const std::weak_ptr<instance_data>& data) : Base(data) {}
};
} // namespace express
namespace std {
template <>
struct hash<express::Base> {
std::size_t operator()(const express::Base& value) const noexcept {
return std::hash<uint32_t>{}(value.identity());
}
};
template <>
struct hash<express::Entity> {
std::size_t operator()(const express::Entity& value) const noexcept {
return std::hash<uint32_t>{}(value.identity());
}
};
} // namespace std
namespace boost {
template <>
struct hash<express::Base> {
std::size_t operator()(const express::Base& value) const noexcept {
return std::hash<uint32_t>{}(value.identity());
}
};
template <>
struct hash<express::Entity> {
std::size_t operator()(const express::Entity& value) const noexcept {
return std::hash<uint32_t>{}(value.identity());
}
};
} // namespace boost
namespace {
template <typename>
struct is_std_vector : std::false_type {};
template <typename X, typename A>
struct is_std_vector<std::vector<X, A>> : std::true_type {};
template <typename T>
constexpr bool is_std_vector_v = is_std_vector<T>::value;
template <typename T>
struct is_std_vector_vector : std::false_type {};
template <typename T, typename Alloc, typename Alloc2>
struct is_std_vector_vector<std::vector<std::vector<T, Alloc>, Alloc2>> : std::true_type {};
template <typename T>
constexpr bool is_std_vector_vector_v = is_std_vector_vector<T>::value;
}
template <typename T, typename U>
typename std::conditional_t<
is_std_vector<U>::value,
std::vector<std::vector<T>>,
std::vector<T>>
cast_vector(const std::vector<U>& values) {
if constexpr (is_std_vector<U>::value) {
using V = typename U::value_type;
std::vector<std::vector<T>> result;
result.reserve(values.size());
for (const auto& value : values) {
result.push_back(cast_vector<T>(value));
}
return result;
} else {
std::vector<T> result;
for (const auto& value : values) {
if constexpr (std::is_base_of_v<T, U>) {
// For a base or identity transform we can just rely on static cast
result.push_back(value);
} else if constexpr (std::is_base_of_v<express::Select, U> && std::is_same_v<T, express::Base>) {
// From a select to concrete we simply call the appropriate method
result.push_back(value.concrete());
} else {
if (auto cast_value = value.template as<T>()) {
result.push_back(cast_value);
}
}
}
return result;
}
}
#endif