serializer.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_SERIALIZER_HPP
27 #define CASADI_SERIALIZER_HPP
28 
29 #include <memory>
30 #include <set>
31 #include <sstream>
32 #include <unordered_map>
33 
34 namespace casadi {
35 
36  class Slice;
37  class Linsol;
38  class Sparsity;
39  class Function;
40  class MX;
41  class SXElem;
42  class GenericType;
43  class Importer;
44  class DeserializerBase;
45 
46 
47 
48  class CASADI_EXPORT SerializerBase {
49  friend class DeserializerBase;
50  public:
51 #ifndef SWIG
52  SerializerBase(std::unique_ptr<std::ostream> stream, const Dict& opts = Dict());
53 #endif // SWIG
54  ~SerializerBase();
55  void pack(const Sparsity& e);
56  void pack(const MX& e);
57  void pack(const Matrix<double>& e);
58  void pack(const Matrix<SXElem>& e);
59  void pack(const Linsol& e);
60  void pack(const Function& e);
61  void pack(const GenericType& e);
62  void pack(const casadi_int& e);
63  void pack(const double& e);
64  void pack(const std::string& e);
65  void pack(const std::vector<Sparsity>& e);
66  void pack(const std::vector<MX>& e);
67  void pack(const std::vector< Matrix<double> >& e);
68  void pack(const std::vector< Matrix<SXElem> >& e);
69  void pack(const std::vector<Linsol>& e);
70  void pack(const std::vector<Function>& e);
71  void pack(const std::vector<GenericType>& e);
72  void pack(const std::vector<casadi_int>& e);
73  void pack(const std::vector<double>& e);
74  void pack(const std::vector<std::string>& e);
75 
76 
77 
102  SERIALIZED_SX_VECTOR
103  };
104 
105  static std::string type_to_string(SerializationType type);
106 
107  void connect(DeserializerBase & s);
108  void reset();
109 
110  protected:
111  SerializingStream& serializer();
112  std::unique_ptr<std::ostream> sstream_;
113  std::unique_ptr<SerializingStream> serializer_;
114  };
115 
116  class CASADI_EXPORT DeserializerBase {
117  friend class SerializerBase;
118  public:
119 #ifndef SWIG
120  DeserializerBase(std::unique_ptr<std::istream> stream);
121 #endif // SWIG
122  ~DeserializerBase();
123 
125 
127  MX blind_unpack_mx();
128  MX blind_unpack_mx_v1();
130  Matrix<SXElem> blind_unpack_sx();
131  Matrix<SXElem> blind_unpack_sx_v1();
135  casadi_int blind_unpack_int();
137  std::string blind_unpack_string();
138  std::vector<Sparsity> blind_unpack_sparsity_vector();
139  std::vector<MX> blind_unpack_mx_vector();
140  std::vector<MX> blind_unpack_mx_vector_v1();
141  std::vector< Matrix<double> > blind_unpack_dm_vector();
142  std::vector< Matrix<SXElem> > blind_unpack_sx_vector();
143  std::vector< Matrix<SXElem> > blind_unpack_sx_vector_v1();
144  std::vector<Linsol> blind_unpack_linsol_vector();
145  std::vector<Function> blind_unpack_function_vector();
146  std::vector<GenericType> blind_unpack_generictype_vector();
147  std::vector<casadi_int> blind_unpack_int_vector();
148  std::vector<double> blind_unpack_double_vector();
149  std::vector<std::string> blind_unpack_string_vector();
150 
152  MX unpack_mx();
154  Matrix<SXElem> unpack_sx();
158  casadi_int unpack_int();
159  double unpack_double();
160  std::string unpack_string();
161  std::vector<Sparsity> unpack_sparsity_vector();
162  std::vector<MX> unpack_mx_vector();
163  std::vector< Matrix<double> > unpack_dm_vector();
164  std::vector< Matrix<SXElem> > unpack_sx_vector();
165  std::vector<Linsol> unpack_linsol_vector();
166  std::vector<Function> unpack_function_vector();
167  std::vector<GenericType> unpack_generictype_vector();
168  std::vector<casadi_int> unpack_int_vector();
169  std::vector<double> unpack_double_vector();
170  std::vector<std::string> unpack_string_vector();
171 
172  void connect(SerializerBase & s);
173  void reset();
174 
175  protected:
176  DeserializingStream& deserializer();
177  std::unique_ptr<std::istream> dstream_;
178  std::unique_ptr<DeserializingStream> deserializer_;
179  };
180 
181  class CASADI_EXPORT StringSerializer : public SerializerBase {
182  public:
211  StringSerializer(const Dict& opts = Dict());
212  ~StringSerializer();
213 
219  std::string encode();
220  };
221 
222  class CASADI_EXPORT FileSerializer : public SerializerBase {
223  public:
229  FileSerializer(const std::string& fname, const Dict& opts = Dict());
230  ~FileSerializer();
231  };
232 
233  class CASADI_EXPORT StringDeserializer : public DeserializerBase {
234  public:
235 
241  StringDeserializer(const std::string& string);
243 
244 
248  void decode(const std::string& string);
249  };
250 
251  class CASADI_EXPORT FileDeserializer : public DeserializerBase {
252  public:
258  FileDeserializer(const std::string& fname);
259  ~FileDeserializer();
260  };
261 
262 } // namespace casadi
263 
264 #endif // CASADI_SERIALIZER_HPP
std::vector< casadi_int > blind_unpack_int_vector()
std::vector< Linsol > unpack_linsol_vector()
std::unique_ptr< DeserializingStream > deserializer_
Definition: serializer.hpp:178
std::string blind_unpack_string()
std::string unpack_string()
std::vector< Function > unpack_function_vector()
Matrix< double > unpack_dm()
std::vector< casadi_int > unpack_int_vector()
casadi_int blind_unpack_int()
std::unique_ptr< std::istream > dstream_
Definition: serializer.hpp:177
std::vector< Linsol > blind_unpack_linsol_vector()
std::vector< Matrix< double > > unpack_dm_vector()
std::vector< double > blind_unpack_double_vector()
GenericType unpack_generictype()
std::vector< double > unpack_double_vector()
std::vector< std::string > blind_unpack_string_vector()
std::vector< Sparsity > unpack_sparsity_vector()
std::vector< GenericType > unpack_generictype_vector()
std::vector< GenericType > blind_unpack_generictype_vector()
Function blind_unpack_function()
GenericType blind_unpack_generictype()
Sparsity blind_unpack_sparsity()
std::vector< Function > blind_unpack_function_vector()
Matrix< double > blind_unpack_dm()
std::vector< std::string > unpack_string_vector()
std::vector< Sparsity > blind_unpack_sparsity_vector()
std::vector< Matrix< double > > blind_unpack_dm_vector()
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.
Linear solver.
Definition: linsol.hpp:55
MX - Matrix expression.
Definition: mx.hpp:92
void pack(const std::vector< double > &e)
void pack(const std::string &e)
void pack(const std::vector< Matrix< double > > &e)
void pack(const std::vector< casadi_int > &e)
std::unique_ptr< SerializingStream > serializer_
Definition: serializer.hpp:113
void pack(const std::vector< std::string > &e)
void pack(const Sparsity &e)
void pack(const Function &e)
std::unique_ptr< std::ostream > sstream_
Definition: serializer.hpp:112
void pack(const std::vector< Function > &e)
void pack(const std::vector< GenericType > &e)
void pack(const std::vector< Linsol > &e)
void pack(const Matrix< SXElem > &e)
void pack(const std::vector< Matrix< SXElem > > &e)
void pack(const Matrix< double > &e)
void pack(const GenericType &e)
void pack(const std::vector< Sparsity > &e)
void pack(const casadi_int &e)
void pack(const Linsol &e)
void pack(const double &e)
Helper class for Serialization.
General sparsity class.
Definition: sparsity.hpp:106
The casadi namespace.
Definition: archiver.cpp:28
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.