Files
IfcOpenShell/src/ifcparse/schema.cpp
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

282 lines
9.8 KiB
C++
Raw Normal View History

2023-09-17 12:30:17 +02:00
/********************************************************************************
2023-09-18 16:13:44 +02:00
* *
* 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/>. *
* *
********************************************************************************/
2023-09-17 12:30:17 +02:00
2026-03-31 15:32:36 +02:00
#include "schema.h"
2023-09-17 12:30:17 +02:00
#include "express.h"
2023-09-17 12:30:17 +02:00
#include <map>
2026-04-14 13:50:23 +02:00
#include <mutex>
2023-09-17 12:30:17 +02:00
#ifdef HAS_SCHEMA_2x3
2026-03-31 10:02:00 +02:00
#include "schemas/Ifc2x3.h"
2023-09-17 12:30:17 +02:00
#endif
#ifdef HAS_SCHEMA_4
2026-03-31 10:02:00 +02:00
#include "schemas/Ifc4.h"
2023-09-17 12:30:17 +02:00
#endif
#ifdef HAS_SCHEMA_4x1
2026-03-31 10:02:00 +02:00
#include "schemas/Ifc4x1.h"
2023-09-17 12:30:17 +02:00
#endif
#ifdef HAS_SCHEMA_4x2
2026-03-31 10:02:00 +02:00
#include "schemas/Ifc4x2.h"
2023-09-17 12:30:17 +02:00
#endif
#ifdef HAS_SCHEMA_4x3_rc1
2026-03-31 10:02:00 +02:00
#include "schemas/Ifc4x3_rc1.h"
2023-09-17 12:30:17 +02:00
#endif
#ifdef HAS_SCHEMA_4x3_rc2
2026-03-31 10:02:00 +02:00
#include "schemas/Ifc4x3_rc2.h"
2023-09-17 12:30:17 +02:00
#endif
#ifdef HAS_SCHEMA_4x3_rc3
2026-03-31 10:02:00 +02:00
#include "schemas/Ifc4x3_rc3.h"
2023-09-17 12:30:17 +02:00
#endif
#ifdef HAS_SCHEMA_4x3_rc4
2026-03-31 10:02:00 +02:00
#include "schemas/Ifc4x3_rc4.h"
2023-09-17 12:30:17 +02:00
#endif
#ifdef HAS_SCHEMA_4x3
2026-03-31 10:02:00 +02:00
#include "schemas/Ifc4x3.h"
2023-09-17 12:30:17 +02:00
#endif
#ifdef HAS_SCHEMA_4x3_tc1
2026-03-31 10:02:00 +02:00
#include "schemas/Ifc4x3_tc1.h"
2023-09-17 12:30:17 +02:00
#endif
#ifdef HAS_SCHEMA_4x3_add1
2026-03-31 10:02:00 +02:00
#include "schemas/Ifc4x3_add1.h"
2023-09-17 12:30:17 +02:00
#endif
2023-10-18 09:46:29 +02:00
#ifdef HAS_SCHEMA_4x3_add2
2026-03-31 10:02:00 +02:00
#include "schemas/Ifc4x3_add2.h"
2023-10-18 09:46:29 +02:00
#endif
2026-03-31 15:32:36 +02:00
#include "schemas/Header_section_schema.h"
2015-09-08 20:40:54 +02:00
2026-03-31 15:32:36 +02:00
bool ifcopenshell::declaration::is(const std::string& name) const {
2023-09-17 12:30:17 +02:00
const std::string* name_ptr = &name;
if (std::any_of(name.begin(), name.end(), [](char character) { return std::islower(character); })) {
2023-09-17 12:30:17 +02:00
temp_string_() = name;
boost::to_upper(temp_string_());
name_ptr = &temp_string_();
}
if (name_upper_ == *name_ptr) {
return true;
}
if ((this->as_entity() != nullptr) && (this->as_entity()->supertype() != nullptr)) {
2023-09-17 12:30:17 +02:00
return this->as_entity()->supertype()->is(name);
}
if (this->as_type_declaration() != nullptr) {
2026-03-31 15:32:36 +02:00
const ifcopenshell::named_type* named_type = this->as_type_declaration()->declared_type()->as_named_type();
if (named_type != nullptr) {
return named_type->is(name);
2023-09-17 12:30:17 +02:00
}
}
return false;
2017-12-13 13:58:58 +01:00
}
2026-03-31 15:32:36 +02:00
bool ifcopenshell::declaration::is(const ifcopenshell::declaration& decl) const {
2023-09-17 12:30:17 +02:00
if (this == &decl) {
return true;
}
2017-12-13 13:58:58 +01:00
if (decl.as_select_type() != nullptr) {
const auto& li = decl.as_select_type()->select_list();
for (const auto* selected_decl : li) {
if (is(*selected_decl)) {
return true;
}
}
}
if ((this->as_entity() != nullptr) && (this->as_entity()->supertype() != nullptr)) {
2023-09-17 12:30:17 +02:00
return this->as_entity()->supertype()->is(decl);
}
if (this->as_type_declaration() != nullptr) {
2026-03-31 15:32:36 +02:00
const ifcopenshell::named_type* named_type = this->as_type_declaration()->declared_type()->as_named_type();
if (named_type != nullptr) {
return named_type->is(decl);
2023-09-17 12:30:17 +02:00
}
}
2017-12-13 13:58:58 +01:00
2023-09-17 12:30:17 +02:00
return false;
2015-09-08 20:40:54 +02:00
}
2026-03-31 15:32:36 +02:00
bool ifcopenshell::named_type::is(const std::string& name) const {
2023-09-17 12:30:17 +02:00
return declared_type()->is(name);
2015-09-08 20:40:54 +02:00
}
2026-03-31 15:32:36 +02:00
bool ifcopenshell::named_type::is(const ifcopenshell::declaration& decl) const {
2023-09-17 12:30:17 +02:00
return declared_type()->is(decl);
2017-12-12 10:33:24 +01:00
}
2026-03-31 15:32:36 +02:00
ifcopenshell::entity::~entity() {
for (const auto* attribute : attributes_) {
2023-09-17 12:30:17 +02:00
delete attribute;
}
for (const auto* inverse_attribute : inverse_attributes_) {
2023-09-17 12:30:17 +02:00
delete inverse_attribute;
}
}
2026-04-14 13:50:23 +02:00
namespace {
std::string schema_key(const std::string& schema_name) {
return boost::to_upper_copy(schema_name);
}
ifcopenshell::plugin::module builtin_schema_module(const std::string& schema_name) {
ifcopenshell::plugin::metadata metadata;
metadata.kind_ = ifcopenshell::plugin::kind::parse_schema;
metadata.id = "parse.schema." + boost::to_lower_copy(schema_name);
metadata.schema = schema_name;
return ifcopenshell::plugin::module::builtin(metadata);
}
void register_builtin_schemas(ifcopenshell::schema_registry& registry) {
registry.bind("HEADER_SECTION_SCHEMA", &Header_section_schema::get_schema, &Header_section_schema::clear_schema, builtin_schema_module("HEADER_SECTION_SCHEMA"));
#ifdef HAS_SCHEMA_2x3
registry.bind("IFC2X3", &Ifc2x3::get_schema, &Ifc2x3::clear_schema, builtin_schema_module("IFC2X3"));
#endif
#ifdef HAS_SCHEMA_4
registry.bind("IFC4", &Ifc4::get_schema, &Ifc4::clear_schema, builtin_schema_module("IFC4"));
#endif
#ifdef HAS_SCHEMA_4x1
registry.bind("IFC4X1", &Ifc4x1::get_schema, &Ifc4x1::clear_schema, builtin_schema_module("IFC4X1"));
#endif
#ifdef HAS_SCHEMA_4x2
registry.bind("IFC4X2", &Ifc4x2::get_schema, &Ifc4x2::clear_schema, builtin_schema_module("IFC4X2"));
#endif
#ifdef HAS_SCHEMA_4x3_rc1
registry.bind("IFC4X3_RC1", &Ifc4x3_rc1::get_schema, &Ifc4x3_rc1::clear_schema, builtin_schema_module("IFC4X3_RC1"));
#endif
#ifdef HAS_SCHEMA_4x3_rc2
registry.bind("IFC4X3_RC2", &Ifc4x3_rc2::get_schema, &Ifc4x3_rc2::clear_schema, builtin_schema_module("IFC4X3_RC2"));
#endif
#ifdef HAS_SCHEMA_4x3_rc3
registry.bind("IFC4X3_RC3", &Ifc4x3_rc3::get_schema, &Ifc4x3_rc3::clear_schema, builtin_schema_module("IFC4X3_RC3"));
#endif
#ifdef HAS_SCHEMA_4x3_rc4
registry.bind("IFC4X3_RC4", &Ifc4x3_rc4::get_schema, &Ifc4x3_rc4::clear_schema, builtin_schema_module("IFC4X3_RC4"));
#endif
#ifdef HAS_SCHEMA_4x3
registry.bind("IFC4X3", &Ifc4x3::get_schema, &Ifc4x3::clear_schema, builtin_schema_module("IFC4X3"));
#endif
#ifdef HAS_SCHEMA_4x3_tc1
registry.bind("IFC4X3_TC1", &Ifc4x3_tc1::get_schema, &Ifc4x3_tc1::clear_schema, builtin_schema_module("IFC4X3_TC1"));
#endif
#ifdef HAS_SCHEMA_4x3_add1
registry.bind("IFC4X3_ADD1", &Ifc4x3_add1::get_schema, &Ifc4x3_add1::clear_schema, builtin_schema_module("IFC4X3_ADD1"));
#endif
#ifdef HAS_SCHEMA_4x3_add2
registry.bind("IFC4X3_ADD2", &Ifc4x3_add2::get_schema, &Ifc4x3_add2::clear_schema, builtin_schema_module("IFC4X3_ADD2"));
#endif
}
}
2017-12-12 10:33:24 +01:00
2026-03-31 15:32:36 +02:00
ifcopenshell::schema_definition::schema_definition(const std::string& name, const std::vector<const declaration*>& declarations)
: name_(name)
, declarations_(declarations)
{
2023-09-17 12:30:17 +02:00
std::sort(declarations_.begin(), declarations_.end(), declaration_by_index_sort());
for (std::vector<const declaration*>::iterator it = declarations_.begin(); it != declarations_.end(); ++it) {
(**it).schema_ = this;
if ((**it).as_type_declaration() != nullptr) {
2023-09-17 12:30:17 +02:00
type_declarations_.push_back((**it).as_type_declaration());
}
if ((**it).as_select_type() != nullptr) {
2023-09-17 12:30:17 +02:00
select_types_.push_back((**it).as_select_type());
}
if ((**it).as_enumeration_type() != nullptr) {
2023-09-17 12:30:17 +02:00
enumeration_types_.push_back((**it).as_enumeration_type());
}
if ((**it).as_entity() != nullptr) {
2023-09-17 12:30:17 +02:00
entities_.push_back((**it).as_entity());
}
}
2026-04-14 13:50:23 +02:00
register_schema(this);
2017-12-12 10:33:24 +01:00
}
2026-03-31 15:32:36 +02:00
ifcopenshell::schema_definition::~schema_definition() {
2023-09-17 12:30:17 +02:00
for (std::vector<const declaration*>::const_iterator it = declarations_.begin(); it != declarations_.end(); ++it) {
delete *it;
}
}
2026-03-31 15:32:36 +02:00
void ifcopenshell::register_schema(schema_definition* schema) {
2026-04-14 13:50:23 +02:00
schema_registry_instance().bind(schema);
}
2026-04-14 13:50:23 +02:00
ifcopenshell::schema_registry& ifcopenshell::schema_registry_instance() {
static schema_registry registry;
static std::once_flag once;
std::call_once(once, register_builtin_schemas, std::ref(registry));
return registry;
}
2017-12-21 14:14:04 +01:00
2026-04-14 13:50:23 +02:00
void ifcopenshell::schema_registry::bind(const std::string& schema_name, get_schema_fn get, clear_schema_fn clear, const plugin::module& module) {
auto& entry = entries_[schema_key(schema_name)];
entry.get_ = get;
entry.clear_ = clear;
entry.module_ = module;
}
2021-12-02 11:05:36 +01:00
2026-04-14 13:50:23 +02:00
void ifcopenshell::schema_registry::bind(schema_definition* schema) {
auto& entry = entries_[schema_key(schema->name())];
entry.schema_ = schema;
}
2023-09-17 12:30:17 +02:00
2026-04-14 13:50:23 +02:00
const ifcopenshell::schema_definition* ifcopenshell::schema_registry::get(const std::string& schema_name) {
auto iter = entries_.find(schema_key(schema_name));
if (iter == entries_.end()) {
throw ifcopenshell::exception("No schema named " + schema_name);
}
if (!iter->second.schema_ && iter->second.get_) {
iter->second.schema_ = &iter->second.get_();
}
if (!iter->second.schema_) {
throw ifcopenshell::exception("No schema named " + schema_name);
}
return iter->second.schema_;
}
2023-09-17 12:30:17 +02:00
2026-04-14 13:50:23 +02:00
std::vector<std::string> ifcopenshell::schema_registry::names() const {
std::vector<std::string> names;
for (const auto& pair : entries_) {
names.push_back(pair.first);
}
return names;
2021-12-02 11:05:36 +01:00
}
2026-04-14 13:50:23 +02:00
void ifcopenshell::schema_registry::clear() {
for (auto& pair : entries_) {
if (pair.second.clear_) {
pair.second.clear_();
}
pair.second.schema_ = nullptr;
}
}
2026-04-14 13:50:23 +02:00
const ifcopenshell::schema_definition* ifcopenshell::schema_by_name(const std::string& name) {
return schema_registry_instance().get(name);
}
std::vector<std::string> ifcopenshell::schema_names() {
return schema_registry_instance().names();
}
void ifcopenshell::clear_schemas() {
schema_registry_instance().clear();
}