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
45  ImporterInternal : public SharedObjectInternal,
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 
97  static const std::string infix_;
98 
100  static std::string shortname() { return "importer";}
101 
103  const char* plugin_name() const override { return "none";}
104 
106  virtual signal_t get_function(const std::string& symname) { return nullptr;}
107 
109  bool has_function(const std::string& symname) const;
110 
114  bool has_meta(const std::string& cmd, casadi_int ind=-1) const;
115 
119  std::string get_meta(const std::string& cmd, casadi_int ind=-1) const;
120 
122  void read_meta(std::istream& file, casadi_int& offset);
123 
125  void read_external(const std::string& sym, bool inlined,
126  std::istream& file, casadi_int& offset);
127 
128  // Check if a function is inlined
129  bool inlined(const std::string& symname) const;
130 
132  std::string body(const std::string& symname) const;
133 
135  virtual std::string library() const;
136 
138  virtual bool can_have_meta() const { return true;}
139 
143  std::string to_text(const std::string& cmd, casadi_int ind=-1) const;
144 
146  static inline std::string indexed(const std::string& cmd, casadi_int ind) {
147  std::stringstream ss;
148  ss << cmd << "[" << ind << "]";
149  return ss.str();
150  }
151 
153  std::string name_;
154 
156  std::map<std::string, std::pair<casadi_int, std::string> > meta_;
157 
159  std::map<std::string, std::pair<bool, std::string> > external_;
160 
164  bool verbose_;
165 
166  void serialize(SerializingStream& s) const;
167 
168  virtual void serialize_type(SerializingStream& s) const;
169  virtual void serialize_body(SerializingStream& s) const;
170 
171  static ImporterInternal* deserialize(DeserializingStream& s);
172 
173  protected:
174  explicit ImporterInternal(DeserializingStream& s);
175  };
176 
183  class CASADI_EXPORT
184  DllLibrary : public ImporterInternal {
185  private:
186 #if defined(WITH_DL) && defined(_WIN32) // also for 64-bit
187  typedef HINSTANCE handle_t;
188 #else
189  typedef void* handle_t;
190 #endif
191  handle_t handle_;
192  public:
193 
194  // Constructor
195  explicit DllLibrary(const std::string& bin_name);
196 
197  void finalize() override;
198 
199  void init_handle();
200 
201  // Destructor
202  ~DllLibrary() override;
203 
207  std::string class_name() const override { return "DllLibrary";}
208 
209  // Dummy type
210  signal_t get_function(const std::string& symname) override;
211 
213  std::string library() const override;
214 
216  bool can_have_meta() const override { return false;}
217 
218  static ImporterInternal* deserialize(DeserializingStream& s);
219 
220  protected:
221  explicit DllLibrary(DeserializingStream& s) : ImporterInternal(s) {}
222  };
223 
224 } // namespace casadi
226 #endif // CASADI_IMPORTER_INTERNAL_HPP
The casadi namespace.
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.