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
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 
99  };
100 
101  static std::string type_to_string(SerializationType type);
102 
104  void reset();
105 
106  protected:
108  std::unique_ptr<std::ostream> sstream_;
109  std::unique_ptr<SerializingStream> serializer_;
110  };
111 
112  class CASADI_EXPORT DeserializerBase {
113  friend class SerializerBase;
114  public:
115 #ifndef SWIG
116  DeserializerBase(std::unique_ptr<std::istream> stream);
117 #endif // SWIG
119 
121 
129  casadi_int blind_unpack_int();
131  std::string blind_unpack_string();
132  std::vector<Sparsity> blind_unpack_sparsity_vector();
133  std::vector<MX> blind_unpack_mx_vector();
134  std::vector< Matrix<double> > blind_unpack_dm_vector();
135  std::vector< Matrix<SXElem> > blind_unpack_sx_vector();
136  std::vector<Linsol> blind_unpack_linsol_vector();
137  std::vector<Function> blind_unpack_function_vector();
138  std::vector<GenericType> blind_unpack_generictype_vector();
139  std::vector<casadi_int> blind_unpack_int_vector();
140  std::vector<double> blind_unpack_double_vector();
141  std::vector<std::string> blind_unpack_string_vector();
142 
150  casadi_int unpack_int();
151  double unpack_double();
152  std::string unpack_string();
153  std::vector<Sparsity> unpack_sparsity_vector();
154  std::vector<MX> unpack_mx_vector();
155  std::vector< Matrix<double> > unpack_dm_vector();
156  std::vector< Matrix<SXElem> > unpack_sx_vector();
157  std::vector<Linsol> unpack_linsol_vector();
158  std::vector<Function> unpack_function_vector();
159  std::vector<GenericType> unpack_generictype_vector();
160  std::vector<casadi_int> unpack_int_vector();
161  std::vector<double> unpack_double_vector();
162  std::vector<std::string> unpack_string_vector();
163 
165  void reset();
166 
167  protected:
169  std::unique_ptr<std::istream> dstream_;
170  std::unique_ptr<DeserializingStream> deserializer_;
171  };
172 
173  class CASADI_EXPORT StringSerializer : public SerializerBase {
174  public:
203  StringSerializer(const Dict& opts = Dict());
205 
211  std::string encode();
212  };
213 
214  class CASADI_EXPORT FileSerializer : public SerializerBase {
215  public:
221  FileSerializer(const std::string& fname, const Dict& opts = Dict());
223  };
224 
225  class CASADI_EXPORT StringDeserializer : public DeserializerBase {
226  public:
227 
233  StringDeserializer(const std::string& string);
235 
236 
240  void decode(const std::string& string);
241  };
242 
243  class CASADI_EXPORT FileDeserializer : public DeserializerBase {
244  public:
250  FileDeserializer(const std::string& fname);
252  };
253 
254 } // namespace casadi
255 
256 #endif // CASADI_SERIALIZER_HPP
std::vector< casadi_int > blind_unpack_int_vector()
std::vector< Linsol > unpack_linsol_vector()
Matrix< SXElem > blind_unpack_sx()
std::unique_ptr< DeserializingStream > deserializer_
Definition: serializer.hpp:170
std::string blind_unpack_string()
std::string unpack_string()
DeserializingStream & deserializer()
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:169
std::vector< MX > blind_unpack_mx_vector()
std::vector< Linsol > blind_unpack_linsol_vector()
std::vector< Matrix< SXElem > > unpack_sx_vector()
std::vector< Matrix< double > > unpack_dm_vector()
std::vector< double > blind_unpack_double_vector()
std::vector< MX > unpack_mx_vector()
GenericType unpack_generictype()
Matrix< SXElem > unpack_sx()
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()
void connect(SerializerBase &s)
GenericType blind_unpack_generictype()
Sparsity blind_unpack_sparsity()
std::vector< Matrix< SXElem > > blind_unpack_sx_vector()
SerializerBase::SerializationType pop_type()
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.
FileDeserializer(const std::string &fname)
Advanced deserialization of CasADi objects.
FileSerializer(const std::string &fname, const Dict &opts=Dict())
Advanced serialization of CasADi objects.
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:84
Sparse matrix class. SX and DM are specializations.
Definition: matrix_decl.hpp:92
void pack(const std::vector< double > &e)
void connect(DeserializerBase &s)
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:109
void pack(const MX &e)
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:108
void pack(const std::vector< Function > &e)
SerializingStream & serializer()
void pack(const std::vector< MX > &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)
static std::string type_to_string(SerializationType type)
void pack(const double &e)
Helper class for Serialization.
General sparsity class.
Definition: sparsity.hpp:99
StringDeserializer(const std::string &string)
Advanced deserialization of CasADi objects.
void decode(const std::string &string)
Sets the string to deserialize objects from.
std::string encode()
Returns a string that holds the serialized objects.
StringSerializer(const Dict &opts=Dict())
Advanced serialization of CasADi objects.
The casadi namespace.
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.