mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-08-10 09:48:32 +00:00
More work on isolating into plug-ins
This commit is contained in:
@@ -21,35 +21,117 @@
|
||||
|
||||
#include <boost/algorithm/string/case_conv.hpp>
|
||||
|
||||
#include "../ifcparse/logger.h"
|
||||
|
||||
#include <mutex>
|
||||
#include <set>
|
||||
|
||||
namespace {
|
||||
|
||||
std::string document_serializer_plugin_prefix(const std::string& format) {
|
||||
return "document." + boost::to_lower_copy(format) + ".";
|
||||
std::string document_serializer_key(const std::string& format) {
|
||||
if (format.empty()) {
|
||||
return format;
|
||||
}
|
||||
|
||||
auto key = boost::to_lower_copy(format);
|
||||
if (key.front() == '.') {
|
||||
key.erase(key.begin());
|
||||
}
|
||||
return key;
|
||||
}
|
||||
|
||||
template <typename registry_t, typename register_plugin_t>
|
||||
void load_document_serializer_plugins_impl(registry_t& registry, const std::string& format) {
|
||||
ifcopenshell::plugin::manager manager;
|
||||
manager.add_search_path(ifcopenshell::serializers::document_serializer_plugin_directory());
|
||||
std::string document_serializer_schema_key(const std::string& schema_name) {
|
||||
return boost::to_upper_copy(schema_name);
|
||||
}
|
||||
|
||||
const auto format_lower = boost::to_lower_copy(format);
|
||||
const auto prefix = document_serializer_plugin_prefix(format_lower);
|
||||
for (const auto& path : manager.discover(prefix)) {
|
||||
auto module = manager.load(path);
|
||||
if (module.meta().kind_ != ifcopenshell::plugin::kind::document_serializer) {
|
||||
continue;
|
||||
}
|
||||
if (boost::to_lower_copy(module.meta().format) != format_lower) {
|
||||
continue;
|
||||
}
|
||||
std::string document_serializer_plugin_prefix(const std::string& format = std::string()) {
|
||||
const auto format_key = document_serializer_key(format);
|
||||
return format_key.empty() ? "document." : "document." + format_key;
|
||||
}
|
||||
|
||||
auto register_plugin = module.get_alias<register_plugin_t>(ifcopenshell::serializers::document_serializer_plugin_registration_symbol());
|
||||
register_plugin(registry, module);
|
||||
void add_document_serializer_search_paths(ifcopenshell::plugin::manager& manager) {
|
||||
const auto directory = ifcopenshell::serializers::document_serializer_plugin_directory();
|
||||
manager.add_search_path(directory);
|
||||
|
||||
const auto sibling_directory = directory.parent_path().parent_path() / "serializers" / directory.filename();
|
||||
if (sibling_directory != directory && std::filesystem::exists(sibling_directory)) {
|
||||
manager.add_search_path(sibling_directory);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void ifcopenshell::serializers::document_serializer_registry::bind(const document_serializer_info& info, create_fn create, const plugin::module& module) {
|
||||
entry entry;
|
||||
entry.info_ = info;
|
||||
entry.info_.format = document_serializer_key(entry.info_.format);
|
||||
entry.info_.schema_name = document_serializer_schema_key(entry.info_.schema_name);
|
||||
entry.create_ = create;
|
||||
entry.module_ = module.meta().id.empty() ? plugin::module(document_serializer_plugin_metadata(entry.info_.format, entry.info_.schema_name)) : module;
|
||||
|
||||
entries_[entry.info_.format].push_back(entry);
|
||||
}
|
||||
|
||||
const ifcopenshell::serializers::document_serializer_registry::entry* ifcopenshell::serializers::document_serializer_registry::find_entry_(const std::string& format, const std::string& schema_name) const {
|
||||
const auto format_key = document_serializer_key(format);
|
||||
const auto iter = entries_.find(format_key);
|
||||
if (iter == entries_.end()) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
const auto schema_key = document_serializer_schema_key(schema_name);
|
||||
const entry* fallback = nullptr;
|
||||
|
||||
for (const auto& candidate : iter->second) {
|
||||
if (candidate.info_.schema_name.empty()) {
|
||||
if (!fallback) {
|
||||
fallback = &candidate;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (!schema_key.empty() && candidate.info_.schema_name == schema_key) {
|
||||
return &candidate;
|
||||
}
|
||||
if (schema_key.empty() && !fallback) {
|
||||
fallback = &candidate;
|
||||
}
|
||||
}
|
||||
|
||||
return fallback;
|
||||
}
|
||||
|
||||
const ifcopenshell::serializers::document_serializer_info* ifcopenshell::serializers::document_serializer_registry::find(const std::string& format, const std::string& schema_name) const {
|
||||
const auto* entry = find_entry_(format, schema_name);
|
||||
return entry ? &entry->info_ : nullptr;
|
||||
}
|
||||
|
||||
boost::shared_ptr<Serializer> ifcopenshell::serializers::document_serializer_registry::create(const std::string& format, const document_serializer_context& context) const {
|
||||
const auto schema_name = !context.schema_name.empty() ? context.schema_name :
|
||||
(context.file ? context.file->schema()->name() : std::string());
|
||||
const auto* entry = find_entry_(format, schema_name);
|
||||
if (!entry) {
|
||||
throw ifcopenshell::exception("No document serializer registered for " + format + (schema_name.empty() ? std::string() : " and schema " + schema_name));
|
||||
}
|
||||
return entry->create_(context);
|
||||
}
|
||||
|
||||
std::vector<ifcopenshell::serializers::document_serializer_info> ifcopenshell::serializers::document_serializer_registry::serializers() const {
|
||||
std::vector<document_serializer_info> result;
|
||||
std::set<std::string> seen;
|
||||
|
||||
for (const auto& pair : entries_) {
|
||||
for (const auto& entry : pair.second) {
|
||||
const auto key = entry.info_.format + "|" + entry.info_.schema_name;
|
||||
if (!seen.insert(key).second) {
|
||||
continue;
|
||||
}
|
||||
result.push_back(entry.info_);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
const char* ifcopenshell::serializers::document_serializer_plugin_registration_symbol() {
|
||||
return "ifcopenshell_register_document_serializer_plugin_v1";
|
||||
}
|
||||
@@ -57,9 +139,12 @@ const char* ifcopenshell::serializers::document_serializer_plugin_registration_s
|
||||
ifcopenshell::plugin::metadata ifcopenshell::serializers::document_serializer_plugin_metadata(const std::string& format, const std::string& schema_name) {
|
||||
plugin::metadata metadata;
|
||||
metadata.kind_ = plugin::kind::document_serializer;
|
||||
metadata.id = document_serializer_plugin_prefix(format) + boost::to_lower_copy(schema_name);
|
||||
metadata.schema = boost::to_upper_copy(schema_name);
|
||||
metadata.format = boost::to_lower_copy(format);
|
||||
metadata.id = document_serializer_plugin_prefix(format);
|
||||
if (!schema_name.empty()) {
|
||||
metadata.id += "." + boost::to_lower_copy(schema_name);
|
||||
}
|
||||
metadata.schema = document_serializer_schema_key(schema_name);
|
||||
metadata.format = document_serializer_key(format);
|
||||
return metadata;
|
||||
}
|
||||
|
||||
@@ -67,12 +152,30 @@ std::filesystem::path ifcopenshell::serializers::document_serializer_plugin_dire
|
||||
return plugin::module_directory(reinterpret_cast<const void*>(&ifcopenshell::serializers::document_serializer_plugin_directory));
|
||||
}
|
||||
|
||||
void ifcopenshell::serializers::load_document_serializer_plugins(XmlSerializerFactory::Factory& registry, const std::string& format) {
|
||||
load_document_serializer_plugins_impl<XmlSerializerFactory::Factory, register_xml_document_serializer_plugin_fn>(registry, format);
|
||||
void ifcopenshell::serializers::load_document_serializer_plugins(document_serializer_registry& registry) {
|
||||
ifcopenshell::plugin::manager manager;
|
||||
add_document_serializer_search_paths(manager);
|
||||
|
||||
for (const auto& path : manager.discover(document_serializer_plugin_prefix())) {
|
||||
ifcopenshell::plugin::module module;
|
||||
try {
|
||||
module = manager.load(path);
|
||||
} catch (const std::exception& e) {
|
||||
logger::error(e);
|
||||
continue;
|
||||
}
|
||||
if (module.meta().kind_ != ifcopenshell::plugin::kind::document_serializer) {
|
||||
continue;
|
||||
}
|
||||
|
||||
auto register_plugin = module.get_alias<register_document_serializer_plugin_fn>(document_serializer_plugin_registration_symbol());
|
||||
register_plugin(registry, module);
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef WITH_GLTF
|
||||
void ifcopenshell::serializers::load_document_serializer_plugins(JsonSerializerFactory::Factory& registry, const std::string& format) {
|
||||
load_document_serializer_plugins_impl<JsonSerializerFactory::Factory, register_json_document_serializer_plugin_fn>(registry, format);
|
||||
ifcopenshell::serializers::document_serializer_registry& ifcopenshell::serializers::document_serializer_registry_instance() {
|
||||
static document_serializer_registry registry;
|
||||
static std::once_flag once;
|
||||
std::call_once(once, load_document_serializer_plugins, std::ref(registry));
|
||||
return registry;
|
||||
}
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user