importer.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_HPP
27 #define CASADI_IMPORTER_HPP
28 
29 #include "function.hpp"
30 #include "printable.hpp"
31 
32 namespace casadi {
33 
34 #ifndef SWIG
38  template<typename T>
39  T text2type(const std::string& text) {
40  std::istringstream ss(text);
41  T ret;
42  ss >> ret;
43  return ret;
44  }
45 
49  template<typename T>
50  std::vector<T> text2vector(const std::string& text) {
51  std::istringstream ss(text);
52  T tmp;
53  std::vector<T> ret;
54  while (ss >> tmp) ret.push_back(tmp);
55  return ret;
56  }
57 
61  template<typename T>
62  std::set<T> text2set(const std::string& text) {
63  std::vector<T> v = text2vector<T>(text);
64  std::set<T> ret;
65  for (auto&& i : v) ret.insert(i);
66  return ret;
67  }
68 #endif // SWIG
69 
70  // Forward declaration of internal class
71  class ImporterInternal;
72 
84  class CASADI_EXPORT Importer
85  : public SharedObject,
86  public SWIG_IF_ELSE(PrintableCommon, Printable<Importer>) {
87  public:
91  static std::string type_name() {return "Importer";}
92 
95 
97  explicit Importer(const std::string& name,
98  const std::string& compiler,
99  const Dict& opts=Dict());
100 
102  ImporterInternal* operator->();
103  const ImporterInternal* operator->() const;
104 
106  static bool test_cast(const SharedObjectInternal* ptr);
107 
109  static bool has_plugin(const std::string& name);
110 
112  static void load_plugin(const std::string& name);
113 
115  static std::string doc(const std::string& name);
116 
118  std::string plugin_name() const;
119 
120  // Check if symbol exists
121  bool has_function(const std::string& symname) const;
122 
123 #ifndef SWIG
125  signal_t get_function(const std::string& symname);
126 #endif // SWIG
127 
129 #ifndef SWIG
133  static Importer create(ImporterInternal* node);
134 
138  static Importer create(ImporterInternal* node, const Dict& opts);
139 #endif // SWIG
141 
145  bool has_meta(const std::string& cmd, casadi_int ind=-1) const;
146 
150  std::string get_meta(const std::string& cmd, casadi_int ind=-1) const;
151 
153  bool inlined(const std::string& symname) const;
154 
156  std::string body(const std::string& symname) const;
157 
159  std::string library() const;
160 
161 #ifndef SWIG
163  static inline std::string indexed(const std::string& cmd, casadi_int ind) {
164  std::stringstream ss;
165  ss << cmd << "[" << ind << "]";
166  return ss.str();
167  }
168 
172  template<typename T>
173  T to(const std::string& cmd, casadi_int ind=-1) const {
174  return text2type<T>(get_meta(cmd, ind));
175  }
176 
180  std::string meta_string(const std::string& cmd, casadi_int ind=-1) const {
181  return to<std::string>(cmd, ind);
182  }
183 
187  template<typename T>
188  std::vector<T> meta_vector(const std::string& cmd, casadi_int ind=-1) const {
189  return text2vector<T>(get_meta(cmd, ind));
190  }
191 
195  template<typename T>
196  std::set<T> meta_set(const std::string& cmd, casadi_int ind=-1) const {
197  return text2set<T>(get_meta(cmd, ind));
198  }
199 
203  casadi_int meta_int(const std::string& cmd, casadi_int ind=-1) const {
204  return to<casadi_int>(cmd, ind);
205  }
206 #endif // SWIG
207 
211  void serialize(SerializingStream &s) const;
212 
217  };
218 
219 } // namespace casadi
220 
221 #endif // CASADI_IMPORTER_HPP
Helper class for Serialization.
Importer.
Definition: importer.hpp:86
static Importer deserialize(DeserializingStream &s)
Deserialize with type disambiguation.
std::string library() const
Get library name.
Importer(const std::string &name, const std::string &compiler, const Dict &opts=Dict())
Importer factory.
std::string body(const std::string &symname) const
Get the function body, if inlined.
ImporterInternal * operator->()
Access functions of the node.
void serialize(SerializingStream &s) const
Serialize an object.
static bool test_cast(const SharedObjectInternal *ptr)
Check if a particular cast is allowed.
Importer()
Default constructor.
bool has_meta(const std::string &cmd, casadi_int ind=-1) const
Does a meta entry exist?
std::string get_meta(const std::string &cmd, casadi_int ind=-1) const
Get entry as a text.
static std::string type_name()
Get type name.
Definition: importer.hpp:91
static void load_plugin(const std::string &name)
Explicitly load a plugin dynamically.
static std::string doc(const std::string &name)
Get solver specific documentation.
static bool has_plugin(const std::string &name)
Check if a plugin is available.
bool has_function(const std::string &symname) const
std::string plugin_name() const
Query plugin name.
bool inlined(const std::string &symname) const
Check if a function is inlined.
const ImporterInternal * operator->() const
Helper class for Serialization.
SharedObject implements a reference counting framework similar for efficient and.
The casadi namespace.
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.