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"
49 casadi_assert(in_s.good(),
"Invalid input stream. If you specified an input file, "
50 "make sure it exists relative to the current directory.");
56 "DeserializingStream sanity check failed. "
63 "Serialization protocol is not compatible. "
64 "Got version " +
str(v) +
", while " +
78 out(out_s), debug_(false) {
87 for (
auto&& op : opts) {
88 if (op.first==
"debug") {
91 casadi_error(
"Unknown option: '" + op.first +
"'.");
99 void SerializingStream::decorate(
char e) {
103 void DeserializingStream::assert_decoration(
char e) {
107 casadi_assert(t==e,
"DeserializingStream error '" +
str(e) +
"' vs '" +
str(t) +
"'.");
112 assert_decoration(
'J');
114 char* c =
reinterpret_cast<char*
>(&n);
116 for (
int j=0;j<8;++j)
unpack(c[j]);
123 const char* c =
reinterpret_cast<const char*
>(&n);
124 for (
int j=0;j<8;++j)
pack(c[j]);
130 const char* c =
reinterpret_cast<const char*
>(&n);
131 for (
int j=0;j<8;++j)
pack(c[j]);
135 assert_decoration(
'K');
137 char* c =
reinterpret_cast<char*
>(&n);
139 for (
int j=0;j<8;++j)
unpack(c[j]);
144 assert_decoration(
'i');
146 char* c =
reinterpret_cast<char*
>(&n);
148 for (
int j=0;j<4;++j)
unpack(c[j]);
155 const char* c =
reinterpret_cast<const char*
>(&n);
156 for (
int j=0;j<4;++j)
pack(c[j]);
159 #if SIZE_MAX != UINT_MAX || defined(__EMSCRIPTEN__)
161 assert_decoration(
'u');
163 char* c =
reinterpret_cast<char*
>(&n);
165 for (
int j=0;j<4;++j)
unpack(c[j]);
172 const char* c =
reinterpret_cast<const char*
>(&n);
173 for (
int j=0;j<4;++j)
pack(c[j]);
178 assert_decoration(
'b');
186 pack(
static_cast<char>(e));
190 unsigned char ref =
'a';
194 e = (
reinterpret_cast<unsigned char&
>(e)-ref) +
195 ((
reinterpret_cast<unsigned char&
>(t)-ref) << 4);
199 unsigned char ref =
'a';
202 out.put(ref + (
reinterpret_cast<unsigned char&
>(e) % 16));
203 out.put(ref + (
reinterpret_cast<unsigned char&
>(e) >> 4));
208 int s =
static_cast<int>(e.size());
210 const char* c = e.c_str();
211 for (
int j = 0; j < s; ++j)
pack(c[j]);
215 assert_decoration(
's');
219 for (
int j=0;j<s;++j)
unpack(e[j]);
223 assert_decoration(
'd');
224 char* c =
reinterpret_cast<char*
>(&e);
225 for (
int j=0;j<8;++j)
unpack(c[j]);
230 const char* c =
reinterpret_cast<const char*
>(&e);
231 for (
int j=0;j<8;++j)
pack(c[j]);
240 assert_decoration(
'S');
241 shared_unpack<Sparsity, SparsityInternal>(e);
250 assert_decoration(
'X');
251 shared_unpack<MX, MXNode>(e);
260 assert_decoration(
'F');
261 shared_unpack<Function, FunctionInternal>(e);
270 assert_decoration(
'M');
271 shared_unpack<Importer, ImporterInternal>(e);
280 assert_decoration(
'R');
281 shared_unpack<Resource, ResourceInternal>(e);
290 assert_decoration(
'F');
291 shared_unpack<Fmu, FmuInternal>(e);
300 assert_decoration(
'L');
301 shared_unpack<Linsol, LinsolInternal>(e);
310 assert_decoration(
'G');
311 shared_unpack<GenericType, SharedObjectInternal>(e);
316 s.seekg(0, std::ios::end);
317 size_t len = s.tellg();
318 s.seekg(0, std::ios::beg);
321 for (
size_t i=0;i<len;++i) {
322 s.read(buffer, 1024);
323 size_t c = s.gcount();
324 for (
size_t j=0;j<c;++j) {
327 if (s.rdstate() & std::ifstream::eofbit)
break;
332 assert_decoration(
'B');
335 for (
size_t i=0;i<len;++i) {
348 assert_decoration(
'S');
358 assert_decoration(
'E');
359 shared_unpack<SXElem, SXNode>(e);
364 assert_decoration(
'V');
368 for (casadi_int i=0;i<s;++i) {
377 unpack(name+
"::serialization::version", load_version);
382 int load_version =
version(name);
383 casadi_assert(load_version>=min && load_version<=max,
384 "DeSerialization of " + name +
" failed. "
385 "Object written in version " +
str(load_version) +
386 " but can only read version " +
str(min) +
"..." +
str(max) +
".");
391 int load_version =
version(name);
392 casadi_assert(load_version==v,
393 "DeSerialization of " + name +
" failed. "
394 "Object written in version " +
str(load_version) +
395 " but can only read in version " +
str(v) +
".");
399 pack(name+
"::serialization::version", v);
403 node(obj), is_sx(false) {
404 if (node) obj->count++;
408 node(obj), is_sx(true) {
409 if (node) obj->
count++;
413 node(std::move(rhs.node)), is_sx(std::move(rhs.is_sx)) {
418 std::swap(node, other.node);
419 std::swap(is_sx, other.is_sx);
426 if (--
static_cast<SXNode*
>(node)->count == 0) {
427 delete static_cast<SXNode*
>(node);
441 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.