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
 
virtual void deps_version_check (const std::string &stage) const
 
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 std::unique_ptr< std::ostream > ofstream_ptr (const std::string &path, std::ios_base::openmode mode=std::ios_base::out)
 
static std::unique_ptr< std::istream > ifstream_ptr (const std::string &path, std::ios_base::openmode mode=std::ios_base::in, bool fail=true)
 
static void copy_file (const std::string &src, const std::string &dest)
 
static bool exists (const std::string &path)
 
static bool is_absolute (const std::string &path)
 
static std::string ensure_trailing_slash (const std::string &path)
 
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::FunctionInternal::get_jit_directory(), casadi::FmuInternal::init(), and casadi::TemporaryDirResource::setup_temp_dir().

◆ assert_enabled()

void casadi::Filesystem::assert_enabled ( )
staticinherited

◆ copy_file()

void casadi::Filesystem::copy_file ( const std::string &  src,
const std::string &  dest 
)
staticinherited

Definition at line 144 of file filesystem.cpp.

144  {
145  auto in = ifstream_ptr(src, std::ios::binary, true);
146  auto out = ofstream_ptr(dest, std::ios::binary);
147  *out << in->rdbuf();
148 }
static std::unique_ptr< std::ostream > ofstream_ptr(const std::string &path, std::ios_base::openmode mode=std::ios_base::out)
Definition: filesystem.cpp:115
static std::unique_ptr< std::istream > ifstream_ptr(const std::string &path, std::ios_base::openmode mode=std::ios_base::in, bool fail=true)
Definition: filesystem.cpp:135

References casadi::Filesystem::ifstream_ptr(), and casadi::Filesystem::ofstream_ptr().

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

◆ create_directories()

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

◆ deps_version_check()

virtual void casadi::PluginInterface< Filesystem >::deps_version_check ( const std::string &  stage) const
inlinevirtualinherited

Definition at line 112 of file plugin_interface.hpp.

112 {}

◆ deserialize()

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

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

Definition at line 124 of file plugin_interface.hpp.

124  {
125  std::string class_name, plugin_name;
126  s.unpack("PluginInterface::plugin_name", plugin_name);
128  return deserialize(s);
129  }
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::ensure_directory_exists(), casadi::CodeGenerator::generate_dump(), casadi::Filesystem::ofstream_ptr(), and casadi::temporary_file().

◆ ensure_trailing_slash()

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

Definition at line 155 of file filesystem.cpp.

155  {
156  if (!path.empty() && path.back() != '/' && path.back() != '\\') {
157  return path + '/';
158  }
159  return path;
160 }

References casadi::path().

Referenced by casadi::ensure_directory_exists(), casadi::CodeGenerator::generate_dump(), casadi::FunctionInternal::get_jit_directory(), casadi::GlobalOptions::setTempWorkDir(), and casadi::temporary_file().

◆ exists()

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

Definition at line 150 of file filesystem.cpp.

150  {
151  auto ret = ifstream_compat(path);
152  return static_cast<bool>(ret);
153 }
std::unique_ptr< std::istream > ifstream_compat(const std::string &utf8_path, std::ios::openmode mode)
Definition: casadi_os.cpp:497

References casadi::ifstream_compat(), and casadi::path().

Referenced by casadi::DaeBuilderInternal::import_model_structure(), and casadi::DaeBuilderInternal::load_fmi_description().

◆ 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.

295  {
296 
297 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
298  std::lock_guard<std::mutex> lock(Derived::mutex_solvers_);
299 #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
300 
301  // Check if the solver has been loaded
302  auto it=Derived::solvers_.find(pname);
303 
304  // Load the solver if needed
305  if (it==Derived::solvers_.end()) {
306  load_plugin(pname, true, false);
307  it=Derived::solvers_.find(pname);
308  }
309  casadi_assert_dev(it!=Derived::solvers_.end());
310  return it->second;
311  }
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.

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

◆ ifstream_ptr()

std::unique_ptr< std::istream > casadi::Filesystem::ifstream_ptr ( const std::string &  path,
std::ios_base::openmode  mode = std::ios_base::in,
bool  fail = true 
)
staticinherited

◆ 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.

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

◆ is_absolute()

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

Definition at line 162 of file filesystem.cpp.

162  {
163  if (path.empty()) {
164  return false;
165  }
166  if (path.front() == '.') {
167  return false;
168  }
169  if (path.front() == '/') {
170  return true;
171  }
172  if (path.size() > 1 && path[1] == ':') {
173  return true;
174  }
175  return false;
176 }

References casadi::path().

Referenced by casadi::FunctionInternal::get_jit_directory().

◆ 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.

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

◆ 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.

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

◆ ofstream_ptr()

std::unique_ptr< std::ostream > casadi::Filesystem::ofstream_ptr ( 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(path),
119  "Unable to create the required directory for '" + path + "'.");
120  }
121  auto stream_ptr = ofstream_compat(path, mode);
122  if (is_enabled()) {
123  casadi_assert(stream_ptr,
124  "Error opening stream '" + path + "'.");
125  } else {
126  casadi_assert(stream_ptr,
127  "Error opening stream '" + path + "'. "
128  "Does the directory exits? "
129  "Note that CasADi needs to be compiled with WITH_GHC_FILESYSTEM=ON "
130  "for directories to be automatically created");
131  }
132  return stream_ptr;
133 }
static bool ensure_directory_exists(const std::string &path)
Definition: filesystem.cpp:105
std::unique_ptr< std::ostream > ofstream_compat(const std::string &utf8_path, std::ios::openmode mode)
Definition: casadi_os.cpp:527

References casadi::Filesystem::ensure_directory_exists(), casadi::Filesystem::is_enabled(), casadi::ofstream_compat(), and casadi::path().

Referenced by casadi::FmuInternal::compile_fmu(), casadi::conic_debug(), casadi::Filesystem::copy_file(), casadi::FmuInternal::eval_fd(), casadi::Function::export_code(), casadi::extract_zip_internal(), casadi::FunctionInternal::FunctionInternal(), casadi::CodeGenerator::generate(), casadi::FunctionInternal::generate_in(), casadi::Function::generate_in(), casadi::Function::generate_out(), casadi::FunctionInternal::generate_out(), casadi::DaeBuilderInternal::generate_wrapper(), casadi::FmuFunction::init(), casadi::Linsol::nfact(), casadi::AmplInterface::solve(), casadi::SparsityInternal::spy_matlab(), casadi::Sparsity::to_file(), and casadi::zip_to_path().

◆ 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.

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

◆ 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.

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

◆ pluginFromRegFcn()

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

Definition at line 85 of file plugin_interface.hpp.

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

◆ 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.

278  {
279 
280 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
281  casadi::conditional_lock_guard<std::mutex> lock(Derived::mutex_solvers_, needs_lock);
282 #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
283 
284  // Check if the solver name is in use
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");
288 
289  // Add to list of solvers
290  Derived::solvers_[plugin.name] = plugin;
291  }
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.

273  {
274  registerPlugin(pluginFromRegFcn(regfcn), needs_lock);
275  }

◆ 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 117 of file plugin_interface.hpp.

117  {
118  s.pack("PluginInterface::plugin_name", std::string(plugin_name()));
119  }

Member Data Documentation

◆ infix_

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

Definition at line 110 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: