mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-08-19 19:54:07 +00:00
First start plug-in architecture
This commit is contained in:
+45
-2
@@ -19,14 +19,17 @@
|
||||
|
||||
#include "plugin.h"
|
||||
|
||||
#include <boost/algorithm/string/predicate.hpp>
|
||||
#include <boost/dll/shared_library.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <filesystem>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
|
||||
namespace {
|
||||
using plugin_abi_fn = ifcopenshell::plugin::abi_info(*)();
|
||||
using plugin_metadata_fn = ifcopenshell::plugin::metadata(*)();
|
||||
using plugin_abi_fn = ifcopenshell::plugin::abi_info();
|
||||
using plugin_metadata_fn = ifcopenshell::plugin::metadata();
|
||||
|
||||
std::string compiler_id() {
|
||||
#if defined(_MSC_VER)
|
||||
@@ -101,6 +104,13 @@ bool ifcopenshell::plugin::module::is_loaded() const {
|
||||
return data_->library_ && data_->library_->is_loaded();
|
||||
}
|
||||
|
||||
boost::dll::shared_library& ifcopenshell::plugin::module::library() const {
|
||||
if (!data_->library_) {
|
||||
throw std::runtime_error("Plugin module is not dynamic");
|
||||
}
|
||||
return *data_->library_;
|
||||
}
|
||||
|
||||
ifcopenshell::plugin::manager::manager() = default;
|
||||
|
||||
void ifcopenshell::plugin::manager::add_search_path(const std::filesystem::path& path) {
|
||||
@@ -111,6 +121,39 @@ const std::vector<std::filesystem::path>& ifcopenshell::plugin::manager::search_
|
||||
return search_paths_;
|
||||
}
|
||||
|
||||
std::vector<std::filesystem::path> ifcopenshell::plugin::manager::discover(const std::string& basename_prefix) const {
|
||||
std::vector<std::filesystem::path> result;
|
||||
const auto suffix = boost::dll::shared_library::suffix().string();
|
||||
const auto prefixed_basename = "lib" + basename_prefix;
|
||||
|
||||
for (const auto& search_path : search_paths_) {
|
||||
if (!std::filesystem::exists(search_path) || !std::filesystem::is_directory(search_path)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
for (const auto& entry : std::filesystem::directory_iterator(search_path)) {
|
||||
if (!entry.is_regular_file()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const auto filename = entry.path().filename().string();
|
||||
if (!boost::algorithm::iends_with(filename, suffix)) {
|
||||
continue;
|
||||
}
|
||||
if (!boost::algorithm::istarts_with(filename, basename_prefix) &&
|
||||
!boost::algorithm::istarts_with(filename, prefixed_basename)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
result.push_back(entry.path());
|
||||
}
|
||||
}
|
||||
|
||||
std::sort(result.begin(), result.end());
|
||||
result.erase(std::unique(result.begin(), result.end()), result.end());
|
||||
return result;
|
||||
}
|
||||
|
||||
ifcopenshell::plugin::module ifcopenshell::plugin::manager::load(const std::filesystem::path& path) const {
|
||||
auto library = std::make_shared<boost::dll::shared_library>(path, boost::dll::load_mode::default_mode);
|
||||
auto abi = library->get_alias<plugin_abi_fn>("ifcopenshell_plugin_abi_v1")();
|
||||
|
||||
@@ -22,6 +22,8 @@
|
||||
|
||||
#include "plugin_api.h"
|
||||
|
||||
#include <boost/dll/shared_library.hpp>
|
||||
|
||||
#include <cstdint>
|
||||
#include <filesystem>
|
||||
#include <memory>
|
||||
@@ -68,11 +70,17 @@ public:
|
||||
bool is_dynamic() const;
|
||||
bool is_loaded() const;
|
||||
|
||||
template <typename T>
|
||||
decltype(auto) get_alias(const char* name) const {
|
||||
return library().get_alias<T>(name);
|
||||
}
|
||||
|
||||
private:
|
||||
struct data;
|
||||
std::shared_ptr<data> data_;
|
||||
|
||||
explicit module(std::shared_ptr<data> data);
|
||||
boost::dll::shared_library& library() const;
|
||||
|
||||
friend class manager;
|
||||
};
|
||||
@@ -84,6 +92,7 @@ public:
|
||||
void add_search_path(const std::filesystem::path& path);
|
||||
const std::vector<std::filesystem::path>& search_paths() const;
|
||||
|
||||
std::vector<std::filesystem::path> discover(const std::string& basename_prefix) const;
|
||||
module load(const std::filesystem::path& path) const;
|
||||
|
||||
private:
|
||||
|
||||
@@ -20,7 +20,6 @@
|
||||
#ifndef PLUGIN_API_H
|
||||
#define PLUGIN_API_H
|
||||
|
||||
#ifdef IFC_SHARED_BUILD
|
||||
#ifdef _WIN32
|
||||
#ifdef PLUGIN_EXPORTS
|
||||
#define PLUGIN_API __declspec(dllexport)
|
||||
@@ -30,8 +29,5 @@
|
||||
#else
|
||||
#define PLUGIN_API __attribute__((visibility("default")))
|
||||
#endif
|
||||
#else
|
||||
#define PLUGIN_API
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user