importer_internal.hpp
1 /*
2  * This file is part of CasADi.
3  *
4  * CasADi -- A symbolic framework for dynamic optimization.
5  * Copyright (C) 2010-2023 Joel Andersson, Joris Gillis, Moritz Diehl,
6  * KU Leuven. All rights reserved.
7  * Copyright (C) 2011-2014 Greg Horn
8  *
9  * CasADi is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 3 of the License, or (at your option) any later version.
13  *
14  * CasADi is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with CasADi; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  *
23  */
24 
25 
26 #ifndef CASADI_IMPORTER_INTERNAL_HPP
27 #define CASADI_IMPORTER_INTERNAL_HPP
28 
29 #include "importer.hpp"
30 #include "function_internal.hpp"
31 #include "plugin_interface.hpp"
32 
33 
35 namespace casadi {
36 
44  class CASADI_EXPORT
46  public PluginInterface<ImporterInternal> {
47 
48  public:
50  explicit ImporterInternal(const std::string& name);
51 
53  ~ImporterInternal() override;
54 
58  std::string class_name() const override { return "ImporterInternal";}
59 
63  void disp(std::ostream& stream, bool more) const override;
64 
65  // Creator function for internal class
66  typedef ImporterInternal* (*Creator)(const std::string& name);
67 
73  void construct(const Dict& opts);
74 
76 
79  static const Options options_;
80  virtual const Options& get_options() const { return options_;}
82 
86  virtual void init(const Dict& opts);
87 
88  virtual void finalize() {}
89 
90  // No static functions exposed
91  struct Exposed{ };
92 
94  static std::map<std::string, Plugin> solvers_;
95 
96 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
97  static std::mutex mutex_solvers_;
98 #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
99 
101  static const std::string infix_;
102 
104  static std::string shortname() { return "importer";}
105 
107  const char* plugin_name() const override { return "none";}
108 
110  virtual signal_t get_function(const std::string& symname) { return nullptr;}
111 
113  bool has_function(const std::string& symname) const;
114 
118  bool has_meta(const std::string& cmd, casadi_int ind=-1) const;
119 
123  std::string get_meta(const std::string& cmd, casadi_int ind=-1) const;
124 
126  void read_meta(std::istream& file, casadi_int& offset);
127 
129  void read_external(const std::string& sym, bool inlined,
130  std::istream& file, casadi_int& offset);
131 
132  // Check if a function is inlined
133  bool inlined(const std::string& symname) const;
134 
136  std::string body(const std::string& symname) const;
137 
139  virtual std::string library() const;
140 
142  virtual bool can_have_meta() const { return true;}
143 
147  std::string to_text(const std::string& cmd, casadi_int ind=-1) const;
148 
150  static inline std::string indexed(const std::string& cmd, casadi_int ind) {
151  std::stringstream ss;
152  ss << cmd << "[" << ind << "]";
153  return ss.str();
154  }
155 
157  std::string name_;
158 
160  std::map<std::string, std::pair<casadi_int, std::string> > meta_;
161 
163  std::map<std::string, std::pair<bool, std::string> > external_;
164 
168  bool verbose_;
169 
170  void serialize(SerializingStream& s) const;
171 
172  virtual void serialize_type(SerializingStream& s) const;
173  virtual void serialize_body(SerializingStream& s) const;
174 
175  static ImporterInternal* deserialize(DeserializingStream& s);
176 
177  protected:
179  };
180 
187  class CASADI_EXPORT
188  DllLibrary : public ImporterInternal {
189  private:
190 #if defined(WITH_DL) && defined(_WIN32) // also for 64-bit
191  typedef HINSTANCE handle_t;
192 #else
193  typedef void* handle_t;
194 #endif
195  handle_t handle_;
196  public:
197 
198  // Constructor
199  explicit DllLibrary(const std::string& bin_name);
200 
201  void finalize() override;
202 
203  void init_handle();
204 
205  // Destructor
206  ~DllLibrary() override;
207 
211  std::string class_name() const override { return "DllLibrary";}
212 
213  // Dummy type
214  signal_t get_function(const std::string& symname) override;
215 
217  std::string library() const override;
218 
220  bool can_have_meta() const override { return false;}
221 
222  static ImporterInternal* deserialize(DeserializingStream& s);
223 
224  protected:
226  };
227 
228 } // namespace casadi
230 #endif // CASADI_IMPORTER_INTERNAL_HPP
Helper class for Serialization.
Dynamically linked library.
std::string class_name() const override
Get type name.
DllLibrary(DeserializingStream &s)
bool can_have_meta() const override
Can meta information be read?
Importer internal class.
std::string to_text(const std::string &cmd, casadi_int ind=-1) const
Get entry as a text.
std::map< std::string, std::pair< casadi_int, std::string > > meta_
Meta data.
bool verbose_
Verbose – for debugging purposes.
static std::string shortname()
Short 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.
virtual bool can_have_meta() const
Can meta information be read?
static std::string indexed(const std::string &cmd, casadi_int ind)
std::string name_
C filename.
virtual const Options & get_options() const
Options.
const char * plugin_name() const override
Queery plugin name.
std::map< std::string, std::pair< bool, std::string > > external_
External functions.
static std::map< std::string, Plugin > solvers_
Collection of solvers.
Interface for accessing input and output data structures.
Helper class for Serialization.
The casadi namespace.
Definition: archiver.cpp:28
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.
void(* signal_t)(void)
Function pointer types for the C API.
Options metadata for a class.
Definition: options.hpp:40