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
33 template <typename T>
34 requires std::is_trivially_copyable_v<T>
35 inline void serialize(std::ostream& f, const T& x)
36 {
37 f.write(reinterpret_cast<const char*>(&x), sizeof(T));
38 }
39
53 template <typename T>
54 requires std::is_trivially_copyable_v<T>
55 inline void deserialize(std::istream& f, T& x)
56 {
57 f.read(reinterpret_cast<char*>(&x), sizeof(T));
58 }
59
60 // Interval
61
72 inline void serialize(std::ostream& f, const Interval& x)
73 {
74 serialize(f,x.lb());
75 serialize(f,x.ub());
76 }
77
88 inline void deserialize(std::istream& f, Interval& x)
89 {
90 double lb, ub;
91 deserialize(f,lb);
92 deserialize(f,ub);
93 x = Interval(lb,ub);
94 }
95
96 // Vectors and matrix structures (real or interval objects)
97
109 template<typename T,int R=-1,int C=-1>
110 inline void serialize(std::ostream& f, const Eigen::Matrix<T,R,C>& x)
111 {
112 Index r = x.rows(), c = x.cols();
113 serialize(f,r);
114 serialize(f,c);
115 for(Index i = 0 ; i < r ; i++)
116 for(Index j = 0 ; j < c ; j++)
117 serialize(f,x(i,j));
118 }
119
131 template<typename T,int R=-1,int C=-1>
132 inline void deserialize(std::istream& f, Eigen::Matrix<T,R,C>& x)
133 {
134 Index r, c;
135 deserialize(f,r);
136 deserialize(f,c);
137
138 if constexpr(R == -1 && C == -1)
139 x.resize(r,c);
140 else if constexpr(R == -1 || C == -1)
141 x.resize(std::max(r,c));
142 else
143 {
144 assert_release(R == r && C == c);
145 }
146
147 for(Index i = 0 ; i < r ; i++)
148 for(Index j = 0 ; j < c ; j++)
149 deserialize(f,x(i,j));
150 }
151
152 // SampledTraj<T>
153
164 template <typename T>
165 inline void serialize(std::ostream& f, const SampledTraj<T>& x)
166 {
167 Index size = x.nb_samples();
168 serialize(f, size);
169
170 for(const auto& [ti, xi] : x)
171 {
172 serialize(f, ti);
173 serialize(f, xi);
174 }
175 }
176
187 template <typename T>
188 inline void deserialize(std::istream& f, SampledTraj<T>& x)
189 {
190 Index size;
191 x.clear();
192 deserialize(f,size);
193
194 for(Index i = 0 ; i < size ; i++)
195 {
196 double ti; deserialize(f,ti);
197 T xi; deserialize(f,xi);
198 x.set(xi,ti);
199 }
200 }
201}
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 deserialize(std::istream &f, T &x)
Reads the binary representation of a trivially copyable object from the given input stream.
Definition codac2_serialization.h:55
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:35