codac 2.0.0
Loading...
Searching...
No Matches
codac2_serialization.h
Go to the documentation of this file.
1
9
10#pragma once
11
12#include <fstream>
13#include "codac2_Vector.h"
14#include "codac2_SampledTraj.h"
15
16namespace codac2
17{
18 // Trivial types (int, float, double...)
19
30 template <typename T>
31 requires (std::is_trivially_copyable_v<T>)
32 inline void serialize(std::ostream& f, const T& x)
33 {
34 f.write(reinterpret_cast<const char*>(&x), sizeof(T));
35 }
36
47 template <typename T>
48 requires (std::is_trivially_copyable_v<T>)
49 inline void deserialize(std::istream& f, T& x)
50 {
51 f.read(reinterpret_cast<char*>(&x), sizeof(T));
52 }
53
54 // Interval
55
66 inline void serialize(std::ostream& f, const Interval& x)
67 {
68 serialize(f,x.lb());
69 serialize(f,x.ub());
70 }
71
82 inline void deserialize(std::istream& f, Interval& x)
83 {
84 double lb, ub;
85 deserialize(f,lb);
86 deserialize(f,ub);
87 x = Interval(lb,ub);
88 }
89
90 // Vectors and matrix structures (real or interval objects)
91
103 template<typename T,int R=-1,int C=-1>
104 inline void serialize(std::ostream& f, const Eigen::Matrix<T,R,C>& x)
105 {
106 Index r = x.rows(), c = x.cols();
107 f.write(reinterpret_cast<const char*>(&r), sizeof(Index));
108 f.write(reinterpret_cast<const char*>(&c), sizeof(Index));
109 for(Index i = 0 ; i < r ; i++)
110 for(Index j = 0 ; j < c ; j++)
111 serialize(f,x(i,j));
112 }
113
125 template<typename T,int R=-1,int C=-1>
126 inline void deserialize(std::istream& f, Eigen::Matrix<T,R,C>& x)
127 {
128 Index r, c;
129 f.read(reinterpret_cast<char*>(&r), sizeof(Index));
130 f.read(reinterpret_cast<char*>(&c), sizeof(Index));
131
132 if constexpr(R == -1 && C == -1)
133 x.resize(r,c);
134 else if constexpr(R == -1 || C == -1)
135 x.resize(std::max(r,c));
136 else
137 assert_release(R == r && C == c);
138
139 for(Index i = 0 ; i < r ; i++)
140 for(Index j = 0 ; j < c ; j++)
141 deserialize(f,x(i,j));
142 }
143
144 // SampledTraj<T>
145
156 template <typename T>
157 inline void serialize(std::ostream& f, const SampledTraj<T>& x)
158 {
159 Index size = x.nb_samples();
160 f.write(reinterpret_cast<const char*>(&size), sizeof(size));
161
162 for(const auto& [ti, xi] : x)
163 {
164 serialize(f, ti);
165 serialize(f, xi);
166 }
167 }
168
179 template <typename T>
180 inline void deserialize(std::istream& f, SampledTraj<T>& x)
181 {
182 Index size;
183 x.clear();
184 f.read(reinterpret_cast<char*>(&size), sizeof(size));
185
186 for(Index i = 0 ; i < size ; i++)
187 {
188 double ti; deserialize(f,ti);
189 T xi; deserialize(f,xi);
190 x.set(xi,ti);
191 }
192 }
193}
Interval class, for representing closed and connected subsets of .
Definition codac2_Interval.h:49
double ub() const
Returns the upper bound of this.
Definition codac2_Interval_impl.h:115
double lb() const
Returns the lower bound of this.
Definition codac2_Interval_impl.h:110
auto lb() const
Returns a matrix containing the lower bounds of each interval element.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:91
auto ub() const
Returns a matrix containing the upper bounds of each interval element.
Definition codac2_MatrixBase_addons_IntervalMatrixBase.h:103
Definition codac2_OctaSym.h:21
void serialize(std::ostream &f, const T &x)
Writes the binary representation of a trivially copyable object to the given output stream.
Definition codac2_serialization.h:32
void deserialize(std::istream &f, T &x)
Reads the binary representation of a trivially copyable object from the given input stream.
Definition codac2_serialization.h:49