26 #include "function.hpp"
27 #include "serializing_stream.hpp"
30 #include "importer.hpp"
31 #include "resource_internal.hpp"
33 #include "generic_type.hpp"
34 #include "shared_object.hpp"
35 #include "sx_node.hpp"
36 #include "sparsity_internal.hpp"
37 #include "mx_node.hpp"
38 #include "function_internal.hpp"
39 #include "fmu_impl.hpp"
48 casadi_assert(in_s.good(),
"Invalid input stream. If you specified an input file, "
49 "make sure it exists relative to the current directory.");
51 if (in_s.peek() != std::char_traits<char>::eof()) {
62 "DeserializingStream sanity check failed. "
69 "Serialization protocol is not compatible. "
70 "Got version " +
str(v) +
", while " +
84 out(out_s), debug_(false) {
93 for (
auto&& op : opts) {
94 if (op.first==
"debug") {
97 casadi_error(
"Unknown option: '" + op.first +
"'.");
105 void SerializingStream::decorate(
char e) {
109 void DeserializingStream::assert_decoration(
char e) {
113 casadi_assert(t==e,
"DeserializingStream error '" +
str(e) +
"' vs '" +
str(t) +
"'.");
118 assert_decoration(
'J');
120 char* c =
reinterpret_cast<char*
>(&n);
122 for (
int j=0;j<8;++j)
unpack(c[j]);
129 const char* c =
reinterpret_cast<const char*
>(&n);
130 for (
int j=0;j<8;++j)
pack(c[j]);
136 const char* c =
reinterpret_cast<const char*
>(&n);
137 for (
int j=0;j<8;++j)
pack(c[j]);
141 assert_decoration(
'K');
143 char* c =
reinterpret_cast<char*
>(&n);
145 for (
int j=0;j<8;++j)
unpack(c[j]);
150 assert_decoration(
'i');
152 char* c =
reinterpret_cast<char*
>(&n);
154 for (
int j=0;j<4;++j)
unpack(c[j]);
161 const char* c =
reinterpret_cast<const char*
>(&n);
162 for (
int j=0;j<4;++j)
pack(c[j]);
165 #if SIZE_MAX != UINT_MAX || defined(__EMSCRIPTEN__) || defined(__POWERPC__)
167 assert_decoration(
'u');
169 char* c =
reinterpret_cast<char*
>(&n);
171 for (
int j=0;j<4;++j)
unpack(c[j]);
178 const char* c =
reinterpret_cast<const char*
>(&n);
179 for (
int j=0;j<4;++j)
pack(c[j]);
184 assert_decoration(
'b');
192 pack(
static_cast<char>(e));
196 unsigned char ref =
'a';
200 e = (
reinterpret_cast<unsigned char&
>(e)-ref) +
201 ((
reinterpret_cast<unsigned char&
>(t)-ref) << 4);
205 unsigned char ref =
'a';
208 out.put(ref + (
reinterpret_cast<unsigned char&
>(e) % 16));
209 out.put(ref + (
reinterpret_cast<unsigned char&
>(e) >> 4));
214 int s =
static_cast<int>(e.size());
216 const char* c = e.c_str();
217 for (
int j = 0; j < s; ++j)
pack(c[j]);
221 assert_decoration(
's');
225 for (
int j=0;j<s;++j)
unpack(e[j]);
229 assert_decoration(
'd');
230 char* c =
reinterpret_cast<char*
>(&e);
231 for (
int j=0;j<8;++j)
unpack(c[j]);
236 const char* c =
reinterpret_cast<const char*
>(&e);
237 for (
int j=0;j<8;++j)
pack(c[j]);
246 assert_decoration(
'S');
247 shared_unpack<Sparsity, SparsityInternal>(e);
256 assert_decoration(
'X');
257 shared_unpack<MX, MXNode>(e);
266 assert_decoration(
'F');
267 shared_unpack<Function, FunctionInternal>(e);
276 assert_decoration(
'M');
277 shared_unpack<Importer, ImporterInternal>(e);
286 assert_decoration(
'R');
287 shared_unpack<Resource, ResourceInternal>(e);
296 assert_decoration(
'F');
297 shared_unpack<Fmu, FmuInternal>(e);
306 assert_decoration(
'L');
307 shared_unpack<Linsol, LinsolInternal>(e);
316 assert_decoration(
'G');
317 shared_unpack<GenericType, SharedObjectInternal>(e);
322 s.seekg(0, std::ios::end);
323 size_t len = s.tellg();
324 s.seekg(0, std::ios::beg);
328 s.read(buffer, 1024);
329 size_t c = s.gcount();
330 for (
size_t j=0;j<c;++j) {
338 assert_decoration(
'B');
341 for (
size_t i=0;i<len;++i) {
354 assert_decoration(
'S');
364 assert_decoration(
'E');
365 shared_unpack<SXElem, SXNode>(e);
370 assert_decoration(
'V');
374 for (casadi_int i=0;i<s;++i) {
383 unpack(name+
"::serialization::version", load_version);
388 int load_version =
version(name);
389 casadi_assert(load_version>=min && load_version<=max,
390 "DeSerialization of " + name +
" failed. "
391 "Object written in version " +
str(load_version) +
392 " but can only read version " +
str(min) +
"..." +
str(max) +
".");
397 int load_version =
version(name);
398 casadi_assert(load_version==v,
399 "DeSerialization of " + name +
" failed. "
400 "Object written in version " +
str(load_version) +
401 " but can only read in version " +
str(v) +
".");
405 pack(name+
"::serialization::version", v);
409 node(obj), is_sx(false) {
410 if (node) obj->count++;
414 node(obj), is_sx(true) {
415 if (node) obj->
count++;
419 node(std::move(rhs.node)), is_sx(std::move(rhs.is_sx)) {
424 std::swap(node, other.node);
425 std::swap(is_sx, other.is_sx);
432 if (--
static_cast<SXNode*
>(node)->count == 0) {
433 delete static_cast<SXNode*
>(node);
447 shared_map_ = &s.shared_map_;
Helper class for Serialization.
DeserializingStream(std::istream &in_s)
Constructor.
void unpack(Sparsity &e)
Reconstruct an object from the input stream.
void version(const std::string &name, int v)
void connect(SerializingStream &s)
Generic data type, can hold different types such as bool, casadi_int, std::string etc.
RAII class for reading from a zip file.
The basic scalar symbolic class of CasADi.
Internal node class for SX.
Helper class for Serialization.
void version(const std::string &name, int v)
SerializingStream(std::ostream &out)
Constructor.
void pack(const Sparsity &e)
Serializes an object to the output stream.
void connect(DeserializingStream &s)
Class representing a Slice.
void serialize(SerializingStream &s) const
Serialize an object.
static Slice deserialize(DeserializingStream &s)
Deserialize without type information.
UniversalNodeOwner & operator=(const UniversalNodeOwner &other)=delete
UniversalNodeOwner()=delete
static casadi_int serialization_check
static casadi_int serialization_protocol_version
std::string str(const T &v)
String representation, any type.
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.