30#include <webapps_config.hpp>
47 static_cast<void>(flags);
48 HMODULE dl = LoadLibrary(filename);
50 throw std::runtime_error(
"LoadLibrary - error: " + GetLastError());
51 handle.reset(dl, FreeLibrary);
52#elif defined(__APPLE__) || defined(__linux__) || defined(__unix)
53 void *dl = ::dlopen(filename, flags);
55 throw std::runtime_error(::dlerror());
56 handle.reset(dl, ::dlclose);
58#error("Unsupported operating system")
65 throw std::runtime_error(
66 "An error occured while accessing the dynamic library");
70 *(
void **)(&symbol) = (
void *)GetProcAddress(handle.get(), name);
71#elif defined(__APPLE__) || defined(__linux__) || defined(__unix)
72 *(
void **)(&symbol) = ::dlsym(handle.get(), name);
75 throw std::runtime_error(
76 "An error occured while getting the symbol from the dynamic library");
82 operator bool()
const {
return (
bool)handle; }
87 std::shared_ptr<std::remove_pointer<HMODULE>::type> handle;
88#elif defined(__APPLE__) || defined(__linux__) || defined(__unix)
89 std::shared_ptr<void> handle;
99 std::map<std::string, std::shared_ptr<ModelHandler>>
models;
120 for (
const auto &path : paths) {
122 const std::filesystem::path fspath{path};
123 if (std::filesystem::exists(fspath)) {
124 for (
const auto &entry :
125 std::filesystem::directory_iterator{fspath}) {
126 if (entry.path().extension() ==
".dll" ||
127 entry.path().extension() ==
".dylib" ||
128 entry.path().extension() ==
".so") {
131 std::make_shared<ModelHandler>(entry.path().c_str());
132 std::shared_ptr<Model<iganet::real_t>> (*create)(
133 const nlohmann::json &);
135 reinterpret_cast<std::shared_ptr<Model<iganet::real_t>
> (*)(
136 const nlohmann::json &)>(handler->getSymbol(
"create"));
139 std::clog <<
"Unable to create model " << entry << std::endl;
145 std::clog <<
"Unable to open path " << path << std::endl;
152 inline std::shared_ptr<Model<iganet::real_t>>
153 create(
const std::string &
name,
const nlohmann::json &json = NULL)
const {
156 if (model ==
models.end())
158 std::shared_ptr<Model<iganet::real_t>> (*create)(
const nlohmann::json &);
159 create =
reinterpret_cast<std::shared_ptr<Model<iganet::real_t>
> (*)(
160 const nlohmann::json &)>(model->second->getSymbol(
"create"));
169 inline std::shared_ptr<Model<iganet::real_t>>
170 load(
const nlohmann::json &json)
const {
172 for (
auto &model :
models) {
174 std::shared_ptr<Model<iganet::real_t>> (*load)(
const nlohmann::json &);
175 load =
reinterpret_cast<std::shared_ptr<Model<iganet::real_t>
> (*)(
176 const nlohmann::json &)>(model.second->getSymbol(
"load"));
188 auto data = nlohmann::json::array();
189 for (
auto const &model :
models)
190 data.push_back(
create(model.first)->getModel());
Model handler.
Definition modelmanager.hpp:37
ModelHandler(const char *filename, int flags=RTLD_NOW)
Constructor from file.
Definition modelmanager.hpp:45
void * getSymbol(const char *name) const
Gets symbol from dynamic library.
Definition modelmanager.hpp:63
ModelHandler(const ModelHandler &)=delete
ModelHandler(ModelHandler &&)=delete
ModelHandler()=delete
Default constructor deleted.
Model manager.
Definition modelmanager.hpp:96
std::map< std::string, std::shared_ptr< ModelHandler > > models
List of models.
Definition modelmanager.hpp:99
ModelManager(const std::vector< std::string > &paths)
Constructor from filesystem.
Definition modelmanager.hpp:110
ModelManager(const std::string &path)
Constructor from filesystem.
Definition modelmanager.hpp:107
std::shared_ptr< Model< iganet::real_t > > load(const nlohmann::json &json) const
Returns a new model instance from binary data stream throws an exception if model cannot be created.
Definition modelmanager.hpp:170
void addModelPath(const std::string &path)
Adds models from given path.
Definition modelmanager.hpp:114
void addModelPath(const std::vector< std::string > &paths)
Adds models from list of directories.
Definition modelmanager.hpp:119
virtual void pretty_print(std::ostream &os=std::cout) const noexcept override
Returns a string representation of the model manager object.
Definition modelmanager.hpp:196
std::shared_ptr< Model< iganet::real_t > > create(const std::string &name, const nlohmann::json &json=NULL) const
Returns a new instance of the requested model and throws an exception if model cannot be found.
Definition modelmanager.hpp:153
nlohmann::json getModels() const
Serializes the list of models to JSON.
Definition modelmanager.hpp:187
ModelManager()=delete
Default constructor deleted.
Full qualified name descriptor.
Definition fqn.hpp:26
virtual const std::string & name() const noexcept
Returns the full qualified name of the object.
Definition fqn.hpp:31
Definition boundary.hpp:22
std::ostream & operator<<(std::ostream &os, const Boundary< Spline > &obj)
Print (as string) a Boundary object.
Definition boundary.hpp:1963
InvalidModel exception.
Definition model.hpp:37