List of all members | Public Types | Public Member Functions | Static Public Member Functions | Static Public Attributes
casadi::Ghc Class Referenceabstract

'ghc' plugin for Filesystem More...

#include <ghc.hpp>

Detailed Description

Interface to ghc functionality

Extra doc: https://github.com/casadi/casadi/wiki/L_2d0

Author
Joris Gillis
Date
2025

Definition at line 51 of file ghc.hpp.

Inheritance diagram for casadi::Ghc:
Inheritance graph
[legend]
Collaboration diagram for casadi::Ghc:
Collaboration graph
[legend]

Public Types

typedef bool(* IsDirectory) (const std::string &path)
 
typedef bool(* CreateDirectories) (const std::string &path)
 
typedef bool(* Remove) (const std::string &path)
 
typedef casadi_int(* RemoveAll) (const std::string &path)
 
typedef std::string(* Filename) (const std::string &path)
 
typedef bool(* HasParentPath) (const std::string &path)
 
typedef std::string(* ParentPath) (const std::string &path)
 
typedef std::vector< std::string >(* IterateDirectoryNames) (const std::string &path)
 
typedef std::string(* Absolute) (const std::string &path)
 
typedef Filesystem *(* Creator) ()
 
typedef int(* RegFcn) (Plugin *plugin)
 

Public Member Functions

virtual const char * plugin_name () const=0
 
void serialize_type (SerializingStream &s) const
 Serialize type information. More...
 

Static Public Member Functions

static void assert_enabled ()
 
static bool is_directory (const std::string &path)
 
static bool remove (const std::string &path)
 
static casadi_int remove_all (const std::string &path)
 
static std::string filename (const std::string &path)
 
static bool is_enabled ()
 
static bool has_parent_path (const std::string &path)
 
static std::string parent_path (const std::string &path)
 
static std::string absolute (const std::string &path)
 
static bool ensure_directory_exists (const std::string &path)
 
static bool create_directories (const std::string &path)
 
static std::vector< std::string > iterate_directory_names (const std::string &path)
 
static void open (std::ofstream &, const std::string &path, std::ios_base::openmode mode=std::ios_base::out)
 
static std::ofstream * ofstream_ptr (const std::string &path, std::ios_base::openmode mode=std::ios_base::out)
 
static bool has_plugin (const std::string &pname, bool verbose=false)
 Check if a plugin is available or can be loaded. More...
 
static const Optionsplugin_options (const std::string &pname)
 Get the plugin options. More...
 
static Deserialize plugin_deserialize (const std::string &pname)
 Get the plugin deserialize_map. More...
 
static Plugin pluginFromRegFcn (RegFcn regfcn)
 Instantiate a Plugin struct from a factory function. More...
 
static Plugin load_plugin (const std::string &pname, bool register_plugin=true, bool needs_lock=true)
 Load a plugin dynamically. More...
 
static handle_t load_library (const std::string &libname, std::string &resultpath, bool global)
 Load a library dynamically. More...
 
static void registerPlugin (const Plugin &plugin, bool needs_lock=true)
 Register an integrator in the factory. More...
 
static void registerPlugin (RegFcn regfcn, bool needs_lock=true)
 Register an integrator in the factory. More...
 
static Plugin & getPlugin (const std::string &pname)
 Load and get the creator function. More...
 
static Filesysteminstantiate (const std::string &fname, const std::string &pname, Problem problem)
 
static ProtoFunctiondeserialize (DeserializingStream &s)
 Deserialize with type disambiguation. More...
 

Static Public Attributes

static const std::string meta_doc
 A documentation string. More...
 
static std::map< std::string, Plugin > solvers_
 Collection of solvers. More...
 
static const std::string infix_ = "filesystem"
 Infix. More...
 

Member Typedef Documentation

◆ Absolute

typedef std::string(* casadi::Filesystem::Absolute) (const std::string &path)
inherited

Definition at line 55 of file filesystem_impl.hpp.

◆ CreateDirectories

typedef bool(* casadi::Filesystem::CreateDirectories) (const std::string &path)
inherited

Definition at line 48 of file filesystem_impl.hpp.

◆ Creator

typedef Filesystem*(* casadi::Filesystem::Creator) ()
inherited

Definition at line 58 of file filesystem_impl.hpp.

◆ Filename

typedef std::string(* casadi::Filesystem::Filename) (const std::string &path)
inherited

Definition at line 51 of file filesystem_impl.hpp.

◆ HasParentPath

typedef bool(* casadi::Filesystem::HasParentPath) (const std::string &path)
inherited

Definition at line 52 of file filesystem_impl.hpp.

◆ IsDirectory

typedef bool(* casadi::Filesystem::IsDirectory) (const std::string &path)
inherited

Definition at line 47 of file filesystem_impl.hpp.

◆ IterateDirectoryNames

typedef std::vector<std::string>(* casadi::Filesystem::IterateDirectoryNames) (const std::string &path)
inherited

Definition at line 54 of file filesystem_impl.hpp.

◆ ParentPath

typedef std::string(* casadi::Filesystem::ParentPath) (const std::string &path)
inherited

Definition at line 53 of file filesystem_impl.hpp.

◆ RegFcn

typedef int(* casadi::PluginInterface< Filesystem >::RegFcn) (Plugin *plugin)
inherited

Definition at line 73 of file plugin_interface.hpp.

◆ Remove

typedef bool(* casadi::Filesystem::Remove) (const std::string &path)
inherited

Definition at line 49 of file filesystem_impl.hpp.

◆ RemoveAll

typedef casadi_int(* casadi::Filesystem::RemoveAll) (const std::string &path)
inherited

Definition at line 50 of file filesystem_impl.hpp.

Member Function Documentation

◆ absolute()

std::string casadi::Filesystem::absolute ( const std::string &  path)
staticinherited

Definition at line 78 of file filesystem.cpp.

78  {
80  return Filesystem::getPlugin("ghc").exposed.absolute(path);
81 }
static void assert_enabled()
Definition: filesystem.cpp:87
static Plugin & getPlugin(const std::string &pname)
Load and get the creator function.
std::vector< casadi_int > path(const std::vector< casadi_int > &map, casadi_int i_start)

References casadi::Filesystem::assert_enabled(), casadi::PluginInterface< Filesystem >::getPlugin(), and casadi::path().

Referenced by casadi::FmuInternal::finalize().

◆ assert_enabled()

void casadi::Filesystem::assert_enabled ( )
staticinherited

◆ create_directories()

bool casadi::Filesystem::create_directories ( const std::string &  path)
staticinherited

Definition at line 63 of file filesystem.cpp.

63  {
65  return Filesystem::getPlugin("ghc").exposed.create_directories(path);
66 }

References casadi::Filesystem::assert_enabled(), casadi::PluginInterface< Filesystem >::getPlugin(), and casadi::path().

Referenced by casadi::Filesystem::ensure_directory_exists().

◆ deserialize()

static ProtoFunction* casadi::PluginInterface< Filesystem >::deserialize ( DeserializingStream s)
inlinestaticinherited

Extra doc: https://github.com/casadi/casadi/wiki/L_rr

Definition at line 121 of file plugin_interface.hpp.

121  {
122  std::string class_name, plugin_name;
123  s.unpack("PluginInterface::plugin_name", plugin_name);
125  return deserialize(s);
126  }
static Deserialize plugin_deserialize(const std::string &pname)
Get the plugin deserialize_map.
static ProtoFunction * deserialize(DeserializingStream &s)
Deserialize with type disambiguation.
virtual const char * plugin_name() const=0
ProtoFunction *(* Deserialize)(DeserializingStream &)

◆ ensure_directory_exists()

bool casadi::Filesystem::ensure_directory_exists ( const std::string &  path)
staticinherited

Definition at line 105 of file filesystem.cpp.

105  {
106  if (has_parent_path(filename)) {
107  std::string dir = parent_path(filename);
108  if (!is_directory(dir)) {
109  return create_directories(dir);
110  }
111  }
112  return true;
113 }
static bool has_parent_path(const std::string &path)
Definition: filesystem.cpp:53
static std::string parent_path(const std::string &path)
Definition: filesystem.cpp:58
static std::string filename(const std::string &path)
Definition: filesystem.cpp:73
static bool is_directory(const std::string &path)
Definition: filesystem.cpp:38
static bool create_directories(const std::string &path)
Definition: filesystem.cpp:63

References casadi::Filesystem::create_directories(), casadi::Filesystem::filename(), casadi::Filesystem::has_parent_path(), casadi::Filesystem::is_directory(), and casadi::Filesystem::parent_path().

Referenced by casadi::Filesystem::open().

◆ filename()

std::string casadi::Filesystem::filename ( const std::string &  path)
staticinherited

◆ getPlugin()

PluginInterface< Filesystem >::Plugin & casadi::PluginInterface< Filesystem >::getPlugin ( const std::string &  pname)
staticinherited

Definition at line 102 of file plugin_interface.hpp.

291  {
292 
293 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
294  std::lock_guard<std::mutex> lock(Derived::mutex_solvers_);
295 #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
296 
297  // Check if the solver has been loaded
298  auto it=Derived::solvers_.find(pname);
299 
300  // Load the solver if needed
301  if (it==Derived::solvers_.end()) {
302  load_plugin(pname, true, false);
303  it=Derived::solvers_.find(pname);
304  }
305  casadi_assert_dev(it!=Derived::solvers_.end());
306  return it->second;
307  }
static Plugin load_plugin(const std::string &pname, bool register_plugin=true, bool needs_lock=true)
Load a plugin dynamically.

◆ has_parent_path()

bool casadi::Filesystem::has_parent_path ( const std::string &  path)
staticinherited

◆ has_plugin()

bool casadi::PluginInterface< Filesystem >::has_plugin ( const std::string &  pname,
bool  verbose = false 
)
staticinherited

Definition at line 76 of file plugin_interface.hpp.

131  {
132 
133 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
134  std::lock_guard<std::mutex> lock(Derived::mutex_solvers_);
135 #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
136 
137  // Quick return if available
138  if (Derived::solvers_.find(pname) != Derived::solvers_.end()) {
139  return true;
140  }
141 
142  // Try loading the plugin
143  try {
144  (void)load_plugin(pname, false, false);
145  return true;
146  } catch (CasadiException& ex) {
147  if (verbose) {
148  casadi_warning(ex.what());
149  }
150  return false;
151  }
152  }
std::vector< casadi_int > find(const std::vector< T > &v)
find nonzeros

◆ instantiate()

Filesystem * casadi::PluginInterface< Filesystem >::instantiate ( const std::string &  fname,
const std::string &  pname,
Problem  problem 
)
staticinherited

Definition at line 106 of file plugin_interface.hpp.

313  {
314 
315  // Assert the plugin exists (needed for adaptors)
316  if (!has_plugin(pname, true)) {
317  casadi_error("Plugin '" + pname + "' is not found.");
318  }
319  return getPlugin(pname).creator(fname, problem);
320  }
static bool has_plugin(const std::string &pname, bool verbose=false)
Check if a plugin is available or can be loaded.

◆ is_directory()

bool casadi::Filesystem::is_directory ( const std::string &  path)
staticinherited

◆ is_enabled()

bool casadi::Filesystem::is_enabled ( )
staticinherited

◆ iterate_directory_names()

std::vector< std::string > casadi::Filesystem::iterate_directory_names ( const std::string &  path)
staticinherited

Definition at line 68 of file filesystem.cpp.

68  {
70  return Filesystem::getPlugin("ghc").exposed.iterate_directory_names(path);
71 }

References casadi::Filesystem::assert_enabled(), casadi::PluginInterface< Filesystem >::getPlugin(), and casadi::path().

◆ load_library()

handle_t casadi::PluginInterface< Filesystem >::load_library ( const std::string &  libname,
std::string &  resultpath,
bool  global 
)
staticinherited

Definition at line 92 of file plugin_interface.hpp.

184  {
185 
186 #ifndef WITH_DL
187  casadi_error("WITH_DL option needed for dynamic loading");
188 #else // WITH_DL
189 
190  // Get the name of the shared library
191  std::string lib = SHARED_LIBRARY_PREFIX + libname + SHARED_LIBRARY_SUFFIX;
192 
193  // Build up search paths;
194  std::vector<std::string> search_paths = get_search_paths();
195  return open_shared_library(lib, search_paths, resultpath,
196  "PluginInterface::load_plugin", global);
197 
198 #endif // WITH_DL
199  }
std::vector< std::string > get_search_paths()
Definition: casadi_os.cpp:49

◆ load_plugin()

PluginInterface< Filesystem >::Plugin casadi::PluginInterface< Filesystem >::load_plugin ( const std::string &  pname,
bool  register_plugin = true,
bool  needs_lock = true 
)
staticinherited

Definition at line 88 of file plugin_interface.hpp.

204  {
205 
206 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
207  casadi::conditional_lock_guard<std::mutex> lock(Derived::mutex_solvers_, needs_lock);
208 #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
209 
210  // Issue warning and quick return if already loaded
211  if (Derived::solvers_.find(pname) != Derived::solvers_.end()) {
212  casadi_warning("PluginInterface: Solver " + pname + " is already in use. Ignored.");
213  return Plugin();
214  }
215 
216  // Logger singletons are lazily instantiated on first uout()/uerr() calls
217  // This instantation may lead to a data race with potential instatiations in plugin
218  // To be safe, trigger instantatin before any plugin loading
219  uout();
220  uerr();
221 
222 #ifndef WITH_DL
223  casadi_error("WITH_DL option needed for dynamic loading");
224 #else // WITH_DL
225  // Retrieve the registration function
226  RegFcn reg;
227 
228  // Load the dll
229  std::string regName = "casadi_register_" + Derived::infix_ + "_" + pname;
230 
231  std::string searchpath;
232  handle_t handle = load_library("casadi_" + Derived::infix_ + "_" + pname, searchpath,
233  false);
234 
235 #ifdef _WIN32
236 
237 #if __GNUC__
238 #pragma GCC diagnostic push
239 #pragma GCC diagnostic ignored "-Wcast-function-type"
240 #endif
241  reg = reinterpret_cast<RegFcn>(GetProcAddress(handle, TEXT(regName.c_str())));
242 #if __GNUC__
243 #pragma GCC diagnostic pop
244 #endif
245 
246 #else // _WIN32
247  // Reset error
248  dlerror();
249 
250  // Load creator
251  reg = reinterpret_cast<RegFcn>(dlsym(handle, regName.c_str()));
252 #endif // _WIN32
253  casadi_assert(reg!=nullptr,
254  "PluginInterface::load_plugin: no \"" + regName + "\" found in " + searchpath + ".");
255 
256  // Create a temporary struct
257  Plugin plugin = pluginFromRegFcn(reg);
258  // Register the plugin
259  if (register_plugin) {
260  registerPlugin(plugin, false);
261  }
262 
263  return plugin;
264 
265 #endif // WITH_DL
266  }
static handle_t load_library(const std::string &libname, std::string &resultpath, bool global)
Load a library dynamically.
static Plugin pluginFromRegFcn(RegFcn regfcn)
Instantiate a Plugin struct from a factory function.
static void registerPlugin(const Plugin &plugin, bool needs_lock=true)
Register an integrator in the factory.
std::ostream & uerr()
std::ostream & uout()

◆ ofstream_ptr()

std::ofstream * casadi::Filesystem::ofstream_ptr ( const std::string &  path,
std::ios_base::openmode  mode = std::ios_base::out 
)
staticinherited

Definition at line 134 of file filesystem.cpp.

135  {
136  std::ofstream* ret = new std::ofstream();
137  Filesystem::open(*ret, path, mode);
138  return ret;
139 }
static void open(std::ofstream &, const std::string &path, std::ios_base::openmode mode=std::ios_base::out)
Definition: filesystem.cpp:115

References casadi::Filesystem::open(), and casadi::path().

◆ open()

void casadi::Filesystem::open ( std::ofstream &  stream,
const std::string &  path,
std::ios_base::openmode  mode = std::ios_base::out 
)
staticinherited

Definition at line 115 of file filesystem.cpp.

116  {
117  if (is_enabled()) {
118  casadi_assert(ensure_directory_exists(filename),
119  "Unable to create the required directory for '" + filename + "'.");
120  }
121  stream.open(filename.c_str(), mode);
122  if (is_enabled()) {
123  casadi_assert(stream.good(),
124  "Error opening stream '" + filename + "'.");
125  } else {
126  casadi_assert(stream.good(),
127  "Error opening stream '" + filename + "'. "
128  "Does the directory exits? "
129  "Note that CasADi needs to be compiled with WITH_GC=ON "
130  "for directories to be automatically created");
131  }
132 }
static bool ensure_directory_exists(const std::string &path)
Definition: filesystem.cpp:105

References casadi::Filesystem::ensure_directory_exists(), casadi::Filesystem::filename(), and casadi::Filesystem::is_enabled().

Referenced by casadi::conic_debug(), casadi::Function::export_code(), casadi::CodeGenerator::file_open(), casadi::FunctionInternal::generate_in(), casadi::Function::generate_in(), casadi::Function::generate_out(), casadi::FunctionInternal::generate_out(), casadi::FmuFunction::init(), casadi::Linsol::nfact(), casadi::Filesystem::ofstream_ptr(), and casadi::Sparsity::to_file().

◆ parent_path()

std::string casadi::Filesystem::parent_path ( const std::string &  path)
staticinherited

◆ plugin_deserialize()

Deserialize casadi::PluginInterface< Filesystem >::plugin_deserialize ( const std::string &  pname)
staticinherited

Definition at line 82 of file plugin_interface.hpp.

162  {
163  Deserialize m = getPlugin(pname).deserialize;
164  casadi_assert(m, "Plugin \"" + pname + "\" does not support deserialize");
165  return m;
166  }

◆ plugin_name()

virtual const char* casadi::PluginInterface< Filesystem >::plugin_name ( ) const
pure virtualinherited

◆ plugin_options()

const Options & casadi::PluginInterface< Filesystem >::plugin_options ( const std::string &  pname)
staticinherited

Definition at line 79 of file plugin_interface.hpp.

155  {
156  const Options *op = getPlugin(pname).options;
157  casadi_assert(op!=nullptr, "Plugin \"" + pname + "\" does not support options");
158  return *op;
159  }

◆ pluginFromRegFcn()

PluginInterface< Filesystem >::Plugin casadi::PluginInterface< Filesystem >::pluginFromRegFcn ( RegFcn  regfcn)
staticinherited

Definition at line 85 of file plugin_interface.hpp.

170  {
171  // Create a temporary struct
172  Plugin plugin;
173 
174  // Set the fields
175  int flag = regfcn(&plugin);
176  casadi_assert(flag==0, "Registration of plugin failed.");
177 
178  return plugin;
179  }

◆ registerPlugin() [1/2]

void casadi::PluginInterface< Filesystem >::registerPlugin ( const Plugin &  plugin,
bool  needs_lock = true 
)
staticinherited

Definition at line 96 of file plugin_interface.hpp.

274  {
275 
276 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
277  casadi::conditional_lock_guard<std::mutex> lock(Derived::mutex_solvers_, needs_lock);
278 #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
279 
280  // Check if the solver name is in use
281  typename std::map<std::string, Plugin>::iterator it=Derived::solvers_.find(plugin.name);
282  casadi_assert(it==Derived::solvers_.end(),
283  "Solver " + str(plugin.name) + " is already in use");
284 
285  // Add to list of solvers
286  Derived::solvers_[plugin.name] = plugin;
287  }
std::string str(const T &v)
String representation, any type.

◆ registerPlugin() [2/2]

void casadi::PluginInterface< Filesystem >::registerPlugin ( RegFcn  regfcn,
bool  needs_lock = true 
)
staticinherited

Definition at line 99 of file plugin_interface.hpp.

269  {
270  registerPlugin(pluginFromRegFcn(regfcn), needs_lock);
271  }

◆ remove()

bool casadi::Filesystem::remove ( const std::string &  path)
staticinherited

◆ remove_all()

casadi_int casadi::Filesystem::remove_all ( const std::string &  path)
staticinherited

◆ serialize_type()

void casadi::PluginInterface< Filesystem >::serialize_type ( SerializingStream s) const
inlineinherited

Extra doc: https://github.com/casadi/casadi/wiki/L_rq

Definition at line 114 of file plugin_interface.hpp.

114  {
115  s.pack("PluginInterface::plugin_name", std::string(plugin_name()));
116  }

Member Data Documentation

◆ infix_

const std::string casadi::Filesystem::infix_ = "filesystem"
staticinherited

Definition at line 103 of file filesystem_impl.hpp.

◆ meta_doc

const std::string casadi::Ghc::meta_doc
static
Initial value:
=
"\n"
"\n"
"\n"
"Extra doc: https://github.com/casadi/casadi/wiki/L_2d0 \n"
"\n"
"\n"

Definition at line 54 of file ghc.hpp.

Referenced by casadi::casadi_register_filesystem_ghc().

◆ solvers_

std::map< std::string, Filesystem::Plugin > casadi::Filesystem::solvers_
staticinherited

Definition at line 76 of file filesystem_impl.hpp.


The documentation for this class was generated from the following files: