26 #include "importer_internal.hpp"
27 #include "filesystem_impl.hpp"
43 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
44 std::mutex ImporterInternal::mutex_solvers_;
53 "Verbose evaluation -- for debugging"}}
77 casadi_int offset = 0;
78 std::ifstream file(
name_);
80 while (getline(file, line)) {
85 std::string cmd =
"/*CASADIMETA";
86 size_t pos = line.find(cmd);
87 if (pos != std::string::npos) {
93 cmd =
"/*CASADIEXTERNAL";
95 if (pos != std::string::npos) {
96 std::istringstream ss(line.substr(pos+cmd.size()));
100 casadi_assert_dev(ss.good());
105 size_t eqpos = line.find(
'=', pos+cmd.size());
106 if (eqpos != std::string::npos) {
107 std::string attr =
"inline";
108 if (line.compare(eqpos-attr.size(), attr.size(), attr)==0) {
109 casadi_assert_dev(line.size()>eqpos+1);
110 if (line.at(eqpos+1)==
'1') {
113 casadi_assert_dev(line.at(eqpos+1)==
'0');
125 for (
auto&& op : opts) {
126 if (op.first==
"verbose") {
135 while (getline(file, line)) {
139 if (line.find(
"*/") != std::string::npos)
return;
142 if (line.empty() || line.at(0)==
'#')
continue;
145 casadi_assert(line.at(0)==
':',
146 "Syntax error: " + line +
" is not a command string");
147 std::string cmd = line.substr(1, line.find(
' ')-1);
150 std::stringstream ss;
153 line = line.substr(cmd.size()+2);
156 size_t stop = line.find(
'\\');
159 ss << line.substr(0, stop);
162 if (stop == std::string::npos)
break;
166 if (!getline(file, line)) {
167 casadi_error(
"Failed to read \"" + cmd +
"\"");
173 auto new_el =
meta_.insert(std::make_pair(cmd, std::make_pair(offset, ss.str())));
174 casadi_assert(new_el.second,
"Duplicate entry: \"" + cmd +
"\"");
176 casadi_error(
"End-of-file reached while searching for \"*/\"");
180 read_external(
const std::string& sym,
bool inlined, std::istream& file, casadi_int& offset) {
182 std::stringstream ss;
185 bool in_declaration =
true;
189 while (getline(file, line)) {
193 if (in_declaration) {
194 size_t stop = line.find(
'{');
195 if (stop != std::string::npos) in_declaration =
false;
200 if (line.find(
"/*CASADIEXTERNAL") != std::string::npos) {
201 auto new_el =
external_.insert(std::make_pair(sym, std::make_pair(
inlined, ss.str())));
202 casadi_assert(new_el.second,
"Duplicate symbol: \"" + sym +
"\"");
208 ss << line << std::endl;
211 casadi_error(
"End-of-file reached while searching for \"/*CASADIEXTERNAL\"");
234 for (
const std::string&
path : search_paths) {
241 if (!dir.empty()) search_paths.push_back(dir);
245 handle_ = open_shared_library(
name_, search_paths,
"DllLibrary::init_handle");
247 casadi_error(
"CommonExternal: WITH_DL not activated");
257 if (handle_) close_shared_library(handle_);
265 #pragma GCC diagnostic push
266 #pragma GCC diagnostic ignored "-Wcast-function-type"
268 return reinterpret_cast<signal_t>(GetProcAddress(handle_, TEXT(sym.c_str())));
270 #pragma GCC diagnostic pop
285 casadi_assert(
has_meta(cmd),
"No such command: " + cmd);
286 return meta_.at(cmd).second;
296 return it!=
external_.end() && it->second.first;
301 casadi_assert_dev(it!=
external_.end() && it->second.first);
302 return it->second.second;
306 casadi_error(
"library not implemented.");
319 s.
version(
"ImporterInternal", 1);
326 s.
version(
"ImporterInternal", 1);
338 casadi_error(
"Cannot deserialize type '" +
class_name +
"'");
Helper class for Serialization.
void unpack(Sparsity &e)
Reconstruct an object from the input stream.
void version(const std::string &name, int v)
Dynamically linked library.
std::string library() const override
Get library name.
signal_t get_function(const std::string &symname) override
Get a function pointer for numerical evaluation.
DllLibrary(const std::string &bin_name)
static ImporterInternal * deserialize(DeserializingStream &s)
static bool has_parent_path(const std::string &path)
static std::string parent_path(const std::string &path)
~ImporterInternal() override
Destructor.
std::map< std::string, std::pair< casadi_int, std::string > > meta_
Meta data.
void read_meta(std::istream &file, casadi_int &offset)
Get meta information.
bool verbose_
Verbose – for debugging purposes.
bool inlined(const std::string &symname) const
virtual void init(const Dict &opts)
Initialize.
void serialize(SerializingStream &s) const
void disp(std::ostream &stream, bool more) const override
Print.
std::string get_meta(const std::string &cmd, casadi_int ind=-1) const
Get entry as a text.
ImporterInternal(const std::string &name)
Constructor.
virtual std::string library() const
Get library name.
std::string class_name() const override
Get type name.
virtual signal_t get_function(const std::string &symname)
Get a function pointer for numerical evaluation.
static const std::string infix_
Infix.
static const Options options_
Options.
bool has_meta(const std::string &cmd, casadi_int ind=-1) const
Does an entry exist?
virtual bool can_have_meta() const
Can meta information be read?
static std::string indexed(const std::string &cmd, casadi_int ind)
void construct(const Dict &opts)
Construct.
void read_external(const std::string &sym, bool inlined, std::istream &file, casadi_int &offset)
Get an external function declaration.
std::string body(const std::string &symname) const
Get the function body, if inlined.
bool has_function(const std::string &symname) const
Get a function pointer for numerical evaluation.
virtual void serialize_body(SerializingStream &s) const
static ImporterInternal * deserialize(DeserializingStream &s)
std::string name_
C filename.
virtual const Options & get_options() const
Options.
virtual void serialize_type(SerializingStream &s) const
std::map< std::string, std::pair< bool, std::string > > external_
External functions.
static std::map< std::string, Plugin > solvers_
Collection of solvers.
Helper class for Serialization.
void version(const std::string &name, int v)
void pack(const Sparsity &e)
Serializes an object to the output stream.
std::vector< std::string > get_search_paths()
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.
void(* signal_t)(void)
Function pointer types for the C API.
std::vector< casadi_int > path(const std::vector< casadi_int > &map, casadi_int i_start)
Options metadata for a class.
static bool is_sane(const Dict &opts)
Is the dictionary sane.
static Dict sanitize(const Dict &opts, bool top_level=true)
Sanitize a options dictionary.
void check(const Dict &opts) const
Check if options exist.