generic_type.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_GENERIC_TYPE_HPP
27 #define CASADI_GENERIC_TYPE_HPP
28 
29 #include "shared_object.hpp"
30 #include "printable.hpp"
31 #include "casadi_common.hpp"
32 #include <string>
33 #include <vector>
34 
35 namespace casadi {
36  class SerializingStream;
37  class DeserializingStream;
38 #if !(defined(SWIG) && !defined(SWIGXML))
39 
43  enum TypeID {
44  OT_NULL,
45  OT_BOOL,
46  OT_INT,
47  OT_DOUBLE,
48  OT_STRING,
49  OT_INTVECTOR,
50  OT_INTVECTORVECTOR,
51  OT_BOOLVECTOR,
52  OT_DOUBLEVECTOR,
53  OT_DOUBLEVECTORVECTOR,
54  OT_STRINGVECTOR,
55  OT_DICT,
56  OT_FUNCTION,
57  OT_FUNCTIONVECTOR,
58  OT_VOIDPTR,
59  OT_UNKNOWN,
60  OT_STRINGVECTORVECTOR,
61  OT_DICTVECTOR,
62  OT_VECTORVECTOR,
63  OT_VECTOR};
64 #endif // SWIG
65 
72  class CASADI_EXPORT GenericType
73  : public SWIG_IF_ELSE(PrintableCommon, Printable<GenericType>)
74 #if !(defined(SWIG) && !defined(SWIGXML))
75  , public SharedObject
76 #endif // SWIG
77  {
78  public:
80  typedef std::map<std::string, GenericType> Dict;
81 
82 #if !(defined(SWIG) && !defined(SWIGXML))
83 
85  GenericType();
86 
88  GenericType(bool b);
89  GenericType(casadi_int i);
90  GenericType(int i) : GenericType(static_cast<casadi_int>(i)) {}
91  GenericType(double d);
92  GenericType(const std::string& s);
93  GenericType(const std::vector<bool>& iv);
94  GenericType(const std::vector<casadi_int>& iv);
95  GenericType(const std::vector<int>& iv);
96  GenericType(const std::vector< std::vector<casadi_int> >& ivv);
97  GenericType(const std::vector<double>& dv);
98  GenericType(const std::vector< std::vector<double> >& dv);
99  GenericType(const std::vector<std::string>& sv);
100  GenericType(const std::vector<std::vector<std::string> >& sv);
101  GenericType(const char s[]);
102  GenericType(const Function& f);
103  GenericType(const std::vector<Function>& f);
104  GenericType(const Dict& dict);
105  GenericType(const std::vector<Dict>& dictv);
106  GenericType(const std::vector<std::vector<GenericType> >& gvv);
107  GenericType(const std::vector<GenericType>& gv);
108  GenericType(void* ptr);
109 
111  static std::string type_name() {return "GenericType";}
112 
113 #ifndef SWIG
115  static std::string get_type_description(TypeID type);
116 #endif
117 
119 #ifndef SWIG
123  static GenericType create(SharedObjectInternal* node);
124 #endif // SWIG
126 
128  std::string get_description() const { return get_type_description(getType()); }
129 
131 #ifndef SWIG
132  static GenericType from_type(TypeID type);
133 
136  operator bool() const { return to_bool();}
137  operator casadi_int() const { return to_int();}
138  operator int() const { return to_int();}
139  operator double() const { return to_double();}
140  operator std::string() const { return to_string();}
141  operator std::vector<bool>() const { return to_bool_vector();}
142  operator std::vector<casadi_int>() const { return to_int_vector();}
143  operator std::vector<int>() const;
144  operator std::vector<std::vector<casadi_int> >() const { return to_int_vector_vector();}
145  operator std::vector<std::vector<int> >() const;
146  operator std::vector<double>() const { return to_double_vector();}
147  operator std::vector< std::vector<double> >() const {
148  return to_double_vector_vector();
149  }
150  operator std::vector<std::string>() const { return to_string_vector();}
151  operator std::vector<std::vector<std::string> >() const { return to_string_vector_vector();}
152  operator const Function&() const { return as_function();}
153  operator const std::vector<Function>&() const { return as_function_vector();}
154  operator const Dict&() const { return as_dict();}
155  operator const std::vector<Dict>&() const { return as_dict_vector();}
156  operator const std::vector<std::vector<GenericType> >&() const { return as_vector_vector();}
157  operator const std::vector<GenericType>&() const { return as_vector();}
159 
160  bool can_cast_to(TypeID other) const;
161  bool can_cast_to(const GenericType& other) const { return can_cast_to(other.getType()) ;}
162 #endif
163 
164 #if !(defined(SWIG) && !defined(SWIGXML))
165  // Get type of object
166  TypeID getType() const;
167 #endif
168 
170 
173  bool is_bool() const;
174  bool is_int() const;
175  bool is_double() const;
176  bool is_string() const;
177  bool is_empty_vector() const;
178  bool is_int_vector() const;
179  bool is_int_vector_vector() const;
180  bool is_double_vector() const;
181  bool is_double_vector_vector() const;
182  bool is_bool_vector() const;
183  bool is_string_vector() const;
184  bool is_string_vector_vector() const;
185  bool is_dict() const;
186  bool is_dict_vector() const;
187  bool is_vector_vector() const;
188  bool is_vector() const;
189  bool is_function() const;
190  bool is_function_vector() const;
191  bool is_void_pointer() const;
193 
195 
198  const bool& as_bool() const;
199  const casadi_int& as_int() const;
200  const double& as_double() const;
201  const std::string& as_string() const;
202  const std::vector<casadi_int>& as_int_vector() const;
203  const std::vector<casadi_int>& as_bool_vector() const;
204  const std::vector<std::vector<casadi_int> >& as_int_vector_vector() const;
205  const std::vector<double>& as_double_vector() const;
206  const std::vector< std::vector<double> >& as_double_vector_vector() const;
207  const std::vector<std::string>& as_string_vector() const;
208  const std::vector<std::vector<std::string> >& as_string_vector_vector() const;
209  const Dict& as_dict() const;
210  const std::vector<Dict>& as_dict_vector() const;
211  const std::vector<std::vector< GenericType> >& as_vector_vector() const;
212  const std::vector<GenericType>& as_vector() const;
213  const Function& as_function() const;
214  const std::vector<Function>& as_function_vector() const;
215  void* const & as_void_pointer() const;
217 
220  bool to_bool() const;
221  casadi_int to_int() const;
222  double to_double() const;
223  std::string to_string() const;
224  std::vector<casadi_int> to_int_vector() const;
225  std::vector<bool> to_bool_vector() const;
226  std::vector< std::vector<casadi_int> > to_int_vector_vector() const;
227  std::vector<double> to_double_vector() const;
228  std::vector< std::vector<double> > to_double_vector_vector() const;
229  std::vector<std::string> to_string_vector() const;
230  std::vector<std::vector<std::string> > to_string_vector_vector() const;
231  Dict to_dict() const;
232  std::vector<Dict> to_dict_vector() const;
233  std::vector<GenericType> to_vector() const;
234  std::vector< std::vector< GenericType> > to_vector_vector() const;
235  Function to_function() const;
236  std::vector<Function> to_function_vector() const;
237  void* to_void_pointer() const;
238  std::vector<int> to_int_type_vector() const;
240 
242  bool operator==(const GenericType& op2) const;
243  bool operator!=(const GenericType& op2) const;
244 #endif // SWIG
245 
249  void serialize(SerializingStream& s) const;
250 
255  };
256 
259 
260 #ifndef SWIG
261  template<class T>
262  T get_from_dict(const std::map<std::string, T>& d,
263  const std::string& key, const T& default_value) {
264  auto it = d.find(key);
265  if (it==d.end()) return default_value;
266  return it->second;
267  }
268 
269  template<class T>
270  T get_from_dict(const Dict& d, const std::string& key, const T& default_value) {
271  auto it = d.find(key);
272  if (it==d.end()) return default_value;
273  return it->second;
274  }
275 
276  template<class T>
277  Dict extract_from_dict(const Dict& d, const std::string& key, T& value) {
278  Dict ret = d;
279  auto it = ret.find(key);
280  if (it!=ret.end()) {
281  value = it->second;
282  ret.erase(it);
283  }
284  return ret;
285  }
286 
287  template<class T>
288  void extract_from_dict_inplace(Dict& d, const std::string& key, T& value) {
289  auto it = d.find(key);
290  if (it!=d.end()) {
291  value = it->second;
292  d.erase(it);
293  }
294  }
295 
300  CASADI_EXPORT Dict combine(const Dict& first, const Dict& second, bool recurse=false);
301 
305  CASADI_EXPORT void update_dict(Dict& target, const Dict& source, bool recurse=false);
306 #endif
307 
308 } // namespace casadi
309 
310 
311 #endif // CASADI_GENERIC_TYPE_HPP
Helper class for Serialization.
Function object.
Definition: function.hpp:60
Generic data type, can hold different types such as bool, casadi_int, std::string etc.
std::map< std::string, GenericType > Dict
C++ equivalent of Python's dict or MATLAB's struct.
static GenericType deserialize(DeserializingStream &s)
Deserialize with type disambiguation.
void serialize(SerializingStream &s) const
Serialize an object.
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.