Files
IfcOpenShell/src/ifcgeom/abstract_mapping.cpp
T

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

84 lines
3.2 KiB
C++
Raw Normal View History

2019-08-16 17:40:13 +02:00
#include "abstract_mapping.h"
2026-03-31 15:32:36 +02:00
#include "../ifcparse/file.h"
2019-08-16 17:40:13 +02:00
2023-03-21 20:15:01 +01:00
#include <boost/preprocessor/stringize.hpp>
#include <boost/preprocessor/seq/for_each.hpp>
2026-04-14 13:50:23 +02:00
#include <mutex>
2023-03-21 20:15:01 +01:00
2023-06-15 21:27:42 +02:00
#ifndef SCHEMA_SEQ
static_assert(false, "A boost preprocessor sequence of schema identifiers is needed for this file to compile.");
#endif
2023-03-21 20:15:01 +01:00
// Declares the schema-based external kernel initialization routines:
2026-04-14 13:50:23 +02:00
// - extern void init_MappingImplementation_Ifc2x3(IfcGeom::impl::mapping_registry*);
2023-03-21 20:15:01 +01:00
// - ...
#define EXTERNAL_DEFS(r, data, elem) \
2026-04-14 13:50:23 +02:00
extern void BOOST_PP_CAT(init_MappingImplementation_Ifc, elem)(ifcopenshell::geometry::impl::mapping_registry*);
2023-03-21 20:15:01 +01:00
// Declares the schema-based external iterator initialization routines:
// - init_MappingImplementation_Ifc2x3(this);
// - ...
#define CALL_DEFS(r, data, elem) \
2026-04-14 13:50:23 +02:00
BOOST_PP_CAT(init_MappingImplementation_Ifc, elem)(&registry);
2023-03-21 20:15:01 +01:00
BOOST_PP_SEQ_FOR_EACH(EXTERNAL_DEFS, , SCHEMA_SEQ)
2019-08-16 17:40:13 +02:00
2026-04-14 13:50:23 +02:00
namespace {
std::string mapping_key(const std::string& schema_name) {
return boost::to_lower_copy(schema_name);
}
ifcopenshell::plugin::module builtin_mapping_module(const std::string& schema_name) {
ifcopenshell::plugin::metadata metadata;
metadata.kind_ = ifcopenshell::plugin::kind::mapping;
metadata.id = "geometry.mapping." + boost::to_lower_copy(schema_name);
metadata.schema = schema_name;
return ifcopenshell::plugin::module::builtin(metadata);
}
2019-08-16 17:40:13 +02:00
}
2026-04-14 13:50:23 +02:00
ifcopenshell::geometry::impl::mapping_registry& ifcopenshell::geometry::impl::mapping_registry_instance() {
static mapping_registry registry;
static std::once_flag once;
std::call_once(once, []() {
BOOST_PP_SEQ_FOR_EACH(CALL_DEFS, , SCHEMA_SEQ)
});
return registry;
2019-08-16 17:40:13 +02:00
}
2026-04-14 13:50:23 +02:00
void ifcopenshell::geometry::impl::mapping_registry::bind(const std::string& schema_name, ifcopenshell::geometry::impl::mapping_fn fn, const plugin::module& module) {
auto& entry = entries_[mapping_key(schema_name)];
entry.fn_ = fn;
entry.module_ = module.meta().id.empty() ? builtin_mapping_module(schema_name) : module;
}
ifcopenshell::geometry::abstract_mapping* ifcopenshell::geometry::impl::mapping_registry::construct(ifcopenshell::file* file, Settings& s) {
2019-08-16 17:40:13 +02:00
const std::string schema_name_lower = boost::to_lower_copy(file->schema()->name());
2026-04-14 13:50:23 +02:00
const auto it = entries_.find(schema_name_lower);
if (it == entries_.end()) {
2026-03-31 15:32:36 +02:00
throw ifcopenshell::exception("No geometry mapping registered for " + schema_name_lower);
2019-08-16 17:40:13 +02:00
}
2026-04-14 13:50:23 +02:00
auto new_mapping = it->second.fn_(file, s);
2023-03-21 20:15:01 +01:00
new_mapping->initialize_settings();
return new_mapping;
2019-08-16 17:40:13 +02:00
}
2026-04-14 13:50:23 +02:00
ifcopenshell::geometry::impl::MappingFactoryImplementation& ifcopenshell::geometry::impl::mapping_implementations() {
static MappingFactoryImplementation impl;
return impl;
}
ifcopenshell::geometry::impl::MappingFactoryImplementation::MappingFactoryImplementation() {
mapping_registry_instance();
}
void ifcopenshell::geometry::impl::MappingFactoryImplementation::bind(const std::string& schema_name, ifcopenshell::geometry::impl::mapping_fn fn) {
mapping_registry_instance().bind(schema_name, fn, builtin_mapping_module(schema_name));
}
ifcopenshell::geometry::abstract_mapping* ifcopenshell::geometry::impl::MappingFactoryImplementation::construct(ifcopenshell::file* file, Settings& s) {
return mapping_registry_instance().construct(file, s);
}