23 #ifndef CASADI_PLUGIN_INTERFACE_HPP
24 #define CASADI_PLUGIN_INTERFACE_HPP
26 #include "function_internal.hpp"
27 #include "global_options.hpp"
28 #include "serializing_stream.hpp"
29 #include "casadi_os.hpp"
30 #include <casadi/core/casadi_common.hpp>
31 #include "casadi_meta.hpp"
41 casadi_assert(t!=0,
"Static function not implemented for plugin");
53 template<
class Derived>
76 static bool has_plugin(
const std::string& pname,
bool verbose=
false);
89 bool register_plugin=
true,
bool needs_lock=
true);
105 template<
class Problem>
107 const std::string& pname, Problem problem);
133 template<
class Derived>
136 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
137 std::lock_guard<std::mutex> lock(Derived::mutex_solvers_);
141 if (Derived::solvers_.
find(pname) != Derived::solvers_.end()) {
147 (void)load_plugin(pname,
false,
false);
151 casadi_warning(ex.
what());
157 template<
class Derived>
159 const Options *op = getPlugin(pname).options;
160 casadi_assert(op!=
nullptr,
"Plugin \"" + pname +
"\" does not support options");
164 template<
class Derived>
167 casadi_assert(m,
"Plugin \"" + pname +
"\" does not support deserialize");
171 template<
class Derived>
178 int flag = regfcn(&plugin);
179 casadi_assert(flag==0,
"Registration of plugin failed.");
185 template<
class Derived>
187 std::string& resultpath,
bool global) {
190 casadi_error(
"WITH_DL option needed for dynamic loading");
199 return open_shared_library(lib, search_paths, resultpath,
200 "PluginInterface::load_plugin", global);
205 template<
class Derived>
208 bool register_plugin,
bool needs_lock) {
210 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
215 if (Derived::solvers_.
find(pname) != Derived::solvers_.end()) {
216 casadi_warning(
"PluginInterface: Solver " + pname +
" is already in use. Ignored.");
227 casadi_error(
"WITH_DL option needed for dynamic loading");
233 std::string regName =
"casadi_register_" + Derived::infix_ +
"_" + pname;
235 std::string searchpath;
236 handle_t handle = load_library(
"casadi_" + Derived::infix_ +
"_" + pname, searchpath,
242 #pragma GCC diagnostic push
243 #pragma GCC diagnostic ignored "-Wcast-function-type"
245 reg =
reinterpret_cast<RegFcn
>(GetProcAddress(handle, TEXT(regName.c_str())));
247 #pragma GCC diagnostic pop
255 reg =
reinterpret_cast<RegFcn
>(dlsym(handle, regName.c_str()));
257 casadi_assert(reg!=
nullptr,
258 "PluginInterface::load_plugin: no \"" + regName +
"\" found in " + searchpath +
".");
261 Plugin plugin = pluginFromRegFcn(reg);
263 if (register_plugin) {
264 registerPlugin(plugin,
false);
272 template<
class Derived>
274 registerPlugin(pluginFromRegFcn(regfcn), needs_lock);
277 template<
class Derived>
280 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
285 typename std::map<std::string, Plugin>::iterator it=Derived::solvers_.find(plugin.
name);
286 casadi_assert(it==Derived::solvers_.end(),
287 "Solver " +
str(plugin.
name) +
" is already in use");
290 Derived::solvers_[plugin.
name] = plugin;
293 template<
class Derived>
297 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
298 std::lock_guard<std::mutex> lock(Derived::mutex_solvers_);
302 auto it=Derived::solvers_.find(pname);
305 if (it==Derived::solvers_.end()) {
306 load_plugin(pname,
true,
false);
307 it=Derived::solvers_.find(pname);
309 casadi_assert_dev(it!=Derived::solvers_.end());
313 template<
class Derived>
314 template<
class Problem>
317 const std::string& pname, Problem problem) {
320 if (!has_plugin(pname,
true)) {
321 casadi_error(
"Plugin '" + pname +
"' is not found.");
323 return getPlugin(pname).creator(fname, problem);
const char * what() const override
Display error.
Helper class for Serialization.
void unpack(Sparsity &e)
Reconstruct an object from the input stream.
Interface for accessing input and output data structures.
static bool has_plugin(const std::string &pname, bool verbose=false)
Check if a plugin is available or can be loaded.
static handle_t load_library(const std::string &libname, std::string &resultpath, bool global)
Load a library dynamically.
static Derived * instantiate(const std::string &fname, const std::string &pname, Problem problem)
void serialize_type(SerializingStream &s) const
Serialize type information.
int(* RegFcn)(Plugin *plugin)
static Plugin pluginFromRegFcn(RegFcn regfcn)
Instantiate a Plugin struct from a factory function.
static const Options & plugin_options(const std::string &pname)
Get the plugin options.
static Plugin & getPlugin(const std::string &pname)
Load and get the creator function.
static Deserialize plugin_deserialize(const std::string &pname)
Get the plugin deserialize_map.
static ProtoFunction * deserialize(DeserializingStream &s)
Deserialize with type disambiguation.
static void registerPlugin(const Plugin &plugin, bool needs_lock=true)
Register an integrator in the factory.
virtual void deps_version_check(const std::string &stage) const
static void registerPlugin(RegFcn regfcn, bool needs_lock=true)
Register an integrator in the factory.
virtual const char * plugin_name() const =0
static Plugin load_plugin(const std::string &pname, bool register_plugin=true, bool needs_lock=true)
Load a plugin dynamically.
Base class for FunctionInternal and LinsolInternal.
Helper class for Serialization.
void pack(const Sparsity &e)
Serializes an object to the output stream.
ProtoFunction *(* Deserialize)(DeserializingStream &)
std::vector< casadi_int > find(const std::vector< T > &v)
find nonzeros
std::vector< std::string > get_search_paths()
std::string str(const T &v)
String representation, any type.
Options metadata for a class.