1 #ifndef EUDAQ_INCLUDED_Serializer
2 #define EUDAQ_INCLUDED_Serializer
7 #include "eudaq/Serializable.hh"
9 #include "eudaq/Time.hh"
10 #include "eudaq/Exception.hh"
11 #include "eudaq/Platform.hh"
16 const char *what()
const throw() {
return "InterruptedException"; }
21 virtual void Flush() {}
22 void write(
const Serializable &t) { t.Serialize(*
this); }
23 template <
typename T>
void write(
const T &t);
24 template <
typename T>
void write(
const std::vector<T> &t);
25 template <
typename T,
typename U>
void write(
const std::map<T, U> &t);
26 template <
typename T,
typename U>
void write(
const std::pair<T, U> &t);
28 void append(
const unsigned char *data,
size_t size) {
29 Serialize(data, size);
32 virtual uint64_t GetCheckSum() {
return 0; }
38 virtual void Serialize(
const unsigned char *,
size_t) = 0;
42 typedef void (*writer)(
Serializer &ser,
const T &v);
43 static writer GetFunc(
Serializable *) {
return write_ser; }
44 static writer GetFunc(
float *) {
return write_float; }
45 static writer GetFunc(
double *) {
return write_double; }
46 static writer GetFunc(
bool *) {
return write_char; }
47 static writer GetFunc(uint8_t *) {
return write_char; }
48 static writer GetFunc(int8_t *) {
return write_char; }
49 static writer GetFunc(uint16_t *) {
return write_int; }
50 static writer GetFunc(int16_t *) {
return write_int; }
51 static writer GetFunc(uint32_t *) {
return write_int; }
52 static writer GetFunc(int32_t *) {
return write_int; }
53 static writer GetFunc(uint64_t *) {
return write_int; }
54 static writer GetFunc(int64_t *) {
return write_int; }
56 static void write_ser(
Serializer &sr,
const T &v) { v.Serialize(sr); }
57 static void write_char(
Serializer &sr,
const T &v) {
58 static_assert(
sizeof(v) == 1,
"Called write_char() in Serializer.hh "
59 "which only supports integers of size == 1 "
61 unsigned char buf[
sizeof(char)];
62 buf[0] =
static_cast<unsigned char>(v & 0xff);
63 sr.Serialize(buf,
sizeof(
char));
66 static void write_int(
Serializer &sr,
const T &v) {
67 static_assert(
sizeof(v) > 1,
"Called write_int() in Serializer.hh which "
68 "only supports integers of size > 1 byte!");
70 unsigned char buf[
sizeof v];
71 for (
size_t i = 0; i <
sizeof v; ++i) {
72 buf[i] =
static_cast<unsigned char>(t & 0xff);
75 sr.Serialize(buf,
sizeof v);
77 static void write_float(
Serializer &sr,
const float &v) {
78 unsigned t = *(
unsigned *)&v;
79 unsigned char buf[
sizeof t];
80 for (
size_t i = 0; i <
sizeof t; ++i) {
84 sr.Serialize(buf,
sizeof t);
86 static void write_double(
Serializer &sr,
const double &v) {
87 uint64_t t = *(uint64_t *)&v;
88 unsigned char buf[
sizeof t];
89 for (
size_t i = 0; i <
sizeof t; ++i) {
93 sr.Serialize(buf,
sizeof t);
97 template <
typename T>
inline void Serializer::write(
const T &v) {
101 template <>
inline void Serializer::write(
const std::string &t) {
102 write((
unsigned)t.length());
103 Serialize(reinterpret_cast<const unsigned char *>(&t[0]), t.length());
106 template <>
inline void Serializer::write(
const Time &t) {
107 write((
int)t.GetTimeval().tv_sec);
108 write((
int)t.GetTimeval().tv_usec);
111 template <>
inline void Serializer::write(
const std::vector<bool> &t) {
112 unsigned len = t.size();
114 for (
size_t i = 0; i < len; ++i) {
115 write((uint8_t)t[i]);
119 template <
typename T>
inline void Serializer::write(
const std::vector<T> &t) {
120 unsigned len = t.size();
122 for (
size_t i = 0; i < len; ++i) {
129 Serializer::write<unsigned char>(
const std::vector<unsigned char> &t) {
130 write((
unsigned)t.size());
131 Serialize(&t[0], t.size());
134 template <>
inline void Serializer::write<char>(
const std::vector<char> &t) {
135 write((
unsigned)t.size());
136 Serialize(reinterpret_cast<const unsigned char *>(&t[0]), t.size());
139 template <
typename T,
typename U>
140 inline void Serializer::write(
const std::map<T, U> &t) {
141 unsigned len = (unsigned)t.size();
143 for (
typename std::map<T, U>::const_iterator i = t.begin(); i != t.end();
150 template <
typename T,
typename U>
151 inline void Serializer::write(
const std::pair<T, U> &t) {
159 virtual bool HasData() = 0;
160 void Interrupt() { m_interrupting =
true; }
162 template <
typename T>
void read(T &t);
164 template <
typename T>
void read(std::vector<T> &t);
166 template <
typename T,
typename U>
void read(std::map<T, U> &t);
168 template <
typename T,
typename U>
void read(std::pair<T, U> &t);
170 template <
typename T> T read() {
176 void read(
unsigned char *dst,
size_t size) { Deserialize(dst, size); }
184 template <
typename T>
friend struct ReadHelper;
185 virtual void Deserialize(
unsigned char *,
size_t) = 0;
190 static reader GetFunc(
Serializable *) {
return read_ser; }
191 static reader GetFunc(
float *) {
return read_float; }
192 static reader GetFunc(
double *) {
return read_double; }
193 static reader GetFunc(
bool *) {
return read_char; }
194 static reader GetFunc(uint8_t *) {
return read_char; }
195 static reader GetFunc(int8_t *) {
return read_char; }
196 static reader GetFunc(uint16_t *) {
return read_int; }
197 static reader GetFunc(int16_t *) {
return read_int; }
198 static reader GetFunc(uint32_t *) {
return read_int; }
199 static reader GetFunc(int32_t *) {
return read_int; }
200 static reader GetFunc(uint64_t *) {
return read_int; }
201 static reader GetFunc(int64_t *) {
return read_int; }
205 unsigned char buf[
sizeof(char)];
206 ds.Deserialize(buf,
sizeof(
char));
213 static_assert(
sizeof(T) > 1,
"Called read_int() in Serializer.hh which "
214 "only supports integers of size > 1 byte!");
215 unsigned char buf[
sizeof(T)];
216 ds.Deserialize(buf,
sizeof(T));
218 for (
size_t i = 0; i <
sizeof t; ++i) {
220 t += buf[
sizeof t - 1 - i];
225 unsigned char buf[
sizeof(float)];
226 ds.Deserialize(buf,
sizeof buf);
228 for (
size_t i = 0; i <
sizeof t; ++i) {
230 t += buf[
sizeof t - 1 - i];
238 unsigned char b[
sizeof(double)];
241 ds.Deserialize(u.b,
sizeof u.b);
243 for (
size_t i = 0; i <
sizeof t; ++i) {
245 t += u.b[
sizeof t - 1 - i];
252 template <
typename T>
inline void Deserializer::read(T &t) {
256 template <>
inline void Deserializer::read(std::string &t) {
259 t = std::string(len,
' ');
261 Deserialize(reinterpret_cast<unsigned char *>(&t[0]), len);
264 template <>
inline void Deserializer::read(Time &t) {
271 template <
typename T>
inline void Deserializer::read(std::vector<T> &t) {
275 for (
size_t i = 0; i < len; ++i) {
276 t.push_back(read<T>());
281 inline void Deserializer::read<unsigned char>(std::vector<unsigned char> &t) {
285 Deserialize(&t[0], len);
288 template <>
inline void Deserializer::read<char>(std::vector<char> &t) {
292 Deserialize(reinterpret_cast<unsigned char *>(&t[0]), len);
295 template <
typename T,
typename U>
296 inline void Deserializer::read(std::map<T, U> &t) {
299 for (
size_t i = 0; i < len; ++i) {
300 std::string name = read<std::string>();
301 std::string val = read<std::string>();
306 template <
typename T,
typename U>
307 inline void Deserializer::read(std::pair<T, U> &t) {
309 t.second = read<U>();
313 #endif // EUDAQ_INCLUDED_Serializer
Definition: Serializer.hh:15
Definition: Serializer.hh:156
Definition: Serializer.hh:41
Definition: Serializable.hh:13
Definition: Serializer.hh:19
Definition: Serializer.hh:188