26 #ifndef CASADI_SERIALIZING_STREAM_HPP
27 #define CASADI_SERIALIZING_STREAM_HPP
31 #include <unordered_map>
47 class SharedObjectInternal;
49 class SerializingStream;
60 void*
get() {
return node; }
65 typedef std::map<std::string, GenericType>
Dict;
104 #if SIZE_MAX != UINT_MAX || defined(__EMSCRIPTEN__)
105 void unpack(
unsigned int& e);
115 assert_decoration(
'V');
119 for (
T& i : e) unpack(i);
122 template <
class K,
class V>
124 assert_decoration(
'D');
128 for (casadi_int i=0;i<s;++i) {
137 template <
class A,
class B>
139 assert_decoration(
'p');
145 void unpack(
const std::string& descr, T& e) {
149 casadi_assert(d==descr,
"Mismatch: '" + descr +
"' expected, got '" + d +
"'.");
157 int version(
const std::string& name,
int min,
int max);
169 template <
class T,
class M>
170 void shared_unpack(T& e) {
172 unpack(
"Shared::flag", i);
175 e = T::deserialize(*
this);
176 if (shared_map_) (*shared_map_)[e.get()] = nodes_.size();
177 nodes_.emplace_back(e.get());
182 unpack(
"Shared::reference", k);
184 e = T::create(
static_cast<M*
>(t.
get()));
188 casadi_assert_dev(
false);
197 void assert_decoration(
char e);
200 std::vector<UniversalNodeOwner> nodes_;
201 std::unordered_map<void*, casadi_int>* shared_map_ =
nullptr;
242 #if SIZE_MAX != UINT_MAX || defined(__EMSCRIPTEN__)
243 void pack(
unsigned int e);
249 void pack(
const std::string& e);
252 void pack(
const std::vector<T>& e) {
254 pack(
static_cast<casadi_int
>(e.size()));
255 for (
auto&& i : e) pack(i);
257 template <
class K,
class V>
258 void pack(
const std::map<K, V>& e) {
260 pack(
static_cast<casadi_int
>(e.size()));
261 for (
const auto & i : e) {
266 template <
class A,
class B>
267 void pack(
const std::pair<A, B>& e) {
273 void pack(
const std::string& descr,
const T& e) {
274 if (debug_) pack(descr);
278 void pack(
const std::string& descr, T& e) {
279 if (debug_) pack(descr);
295 void decorate(
char e);
303 void shared_pack(
const T& e) {
304 auto it = shared_map_.find(e.get());
305 if (it==shared_map_.end()) {
307 pack(
"Shared::flag",
'd');
309 casadi_int r = shared_map_.size();
310 shared_map_[e.get()] = r;
311 if (nodes_) nodes_->emplace_back(e.get());
313 pack(
"Shared::flag",
'r');
314 pack(
"Shared::reference", it->second);
319 std::unordered_map<void*, casadi_int> shared_map_;
320 std::vector<UniversalNodeOwner>* nodes_ =
nullptr;
Helper class for Serialization.
void unpack(std::string &e)
DeserializingStream(std::istream &in_s)
Constructor.
void unpack(Sparsity &e)
Reconstruct an object from the input stream.
void unpack(casadi_int &e)
DeserializingStream(const DeserializingStream &)=delete
int version(const std::string &name)
int version(const std::string &name, int min, int max)
void unpack(Matrix< T > &e)
void unpack(std::pair< A, B > &e)
void unpack(std::ostream &s)
void unpack(std::vector< T > &e)
void unpack(GenericType &e)
void unpack(std::map< K, V > &e)
void unpack(const std::string &descr, T &e)
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.
Sparse matrix class. SX and DM are specializations.
static Matrix< Scalar > deserialize(std::istream &stream)
Build Sparsity from serialization.
std::string serialize() const
Serialize.
RAII class for reading from a zip file.
Helper class for Serialization.
void pack(const std::string &descr, const T &e)
void pack(const GenericType &e)
void version(const std::string &name, int v)
void pack(const std::map< K, V > &e)
void pack(const std::pair< A, B > &e)
void pack(const std::string &e)
SerializingStream(std::ostream &out)
Constructor.
void pack(const Sparsity &e)
Serializes an object to the output stream.
void pack(const std::string &descr, T &e)
void pack(const Importer &e)
void pack(const std::vector< T > &e)
void pack(const Function &e)
void pack(std::istream &s)
void pack(const Linsol &e)
void pack(const Resource &e)
void pack(const Slice &e)
SerializingStream(std::ostream &out, const Dict &opts)
void connect(DeserializingStream &s)
void pack(const Matrix< T > &e)
void pack(const SXElem &e)
Class representing a Slice.
UniversalNodeOwner(UniversalNodeOwner &&rhs) noexcept
UniversalNodeOwner & operator=(const UniversalNodeOwner &other)=delete
UniversalNodeOwner()=delete
UniversalNodeOwner(SharedObjectInternal *obj)
UniversalNodeOwner(SXNode *obj)
UniversalNodeOwner(const UniversalNodeOwner &)=delete
UniversalNodeOwner & operator=(UniversalNodeOwner &&other) noexcept
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.