26 #ifndef CASADI_SERIALIZING_STREAM_HPP
27 #define CASADI_SERIALIZING_STREAM_HPP
31 #include <unordered_map>
46 class SharedObjectInternal;
48 class SerializingStream;
59 void*
get() {
return node; }
64 typedef std::map<std::string, GenericType>
Dict;
102 #if SIZE_MAX != UINT_MAX || defined(__EMSCRIPTEN__)
103 void unpack(
unsigned int& e);
113 assert_decoration(
'V');
117 for (T& i : e) unpack(i);
120 template <
class K,
class V>
122 assert_decoration(
'D');
126 for (casadi_int i=0;i<s;++i) {
135 template <
class A,
class B>
137 assert_decoration(
'p');
143 void unpack(
const std::string& descr, T& e) {
147 casadi_assert(d==descr,
"Mismatch: '" + descr +
"' expected, got '" + d +
"'.");
155 int version(
const std::string& name,
int min,
int max);
167 template <
class T,
class M>
168 void shared_unpack(T& e) {
170 unpack(
"Shared::flag", i);
173 e = T::deserialize(*
this);
174 if (shared_map_) (*shared_map_)[e.get()] = nodes_.size();
175 nodes_.emplace_back(e.get());
180 unpack(
"Shared::reference", k);
182 e = T::create(
static_cast<M*
>(t.
get()));
186 casadi_assert_dev(
false);
195 void assert_decoration(
char e);
198 std::vector<UniversalNodeOwner> nodes_;
199 std::unordered_map<void*, casadi_int>* shared_map_ =
nullptr;
239 #if SIZE_MAX != UINT_MAX || defined(__EMSCRIPTEN__)
240 void pack(
unsigned int e);
246 void pack(
const std::string& e);
249 void pack(
const std::vector<T>& e) {
251 pack(
static_cast<casadi_int
>(e.size()));
252 for (
auto&& i : e) pack(i);
254 template <
class K,
class V>
255 void pack(
const std::map<K, V>& e) {
257 pack(
static_cast<casadi_int
>(e.size()));
258 for (
const auto & i : e) {
263 template <
class A,
class B>
264 void pack(
const std::pair<A, B>& e) {
270 void pack(
const std::string& descr,
const T& e) {
271 if (debug_) pack(descr);
275 void pack(
const std::string& descr, T& e) {
276 if (debug_) pack(descr);
292 void decorate(
char e);
300 void shared_pack(
const T& e) {
301 auto it = shared_map_.find(e.get());
302 if (it==shared_map_.end()) {
304 pack(
"Shared::flag",
'd');
306 casadi_int r = shared_map_.size();
307 shared_map_[e.get()] = r;
308 if (nodes_) nodes_->emplace_back(e.get());
310 pack(
"Shared::flag",
'r');
311 pack(
"Shared::reference", it->second);
316 std::unordered_map<void*, casadi_int> shared_map_;
317 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.
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 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.