DelegateMQ
Loading...
Searching...
No Matches
serialize/Serializer.h
Go to the documentation of this file.
1#ifndef SERIALIZER_H
2#define SERIALIZER_H
3
10
12#include "msg_serialize.h"
13#include <iostream>
14
15template <class R>
16struct Serializer; // Not defined
17
18// Serialize all target function argument data using serialize class
19template<class RetType, class... Args>
20class Serializer<RetType(Args...)> : public dmq::ISerializer<RetType(Args...)>
21{
22public:
23 // Write arguments to a stream
24 virtual std::ostream& Write(std::ostream& os, Args... args) override {
25 os.seekp(0, std::ios::beg);
26 serialize ser;
27#if defined(__cpp_exceptions)
28 try {
29 (ser.write(os, args), ...); // C++17 fold expression to serialize each argument
30 }
31 catch (const std::exception& e) {
32 std::cerr << "Serialize error: " << e.what() << std::endl;
33 throw;
34 }
35#else
36 // STM32 / No Exceptions
37 (ser.write(os, args), ...);
38#endif
39 return os;
40 }
41
42 // Read arguments from a stream
43 virtual std::istream& Read(std::istream& is, Args&... args) override {
44#if defined(__cpp_exceptions)
45 try {
46 serialize ser;
47 (ser.read(is, args), ...); // C++17 fold expression to unserialize each argument
48 }
49 catch (const std::exception& e) {
50 std::cerr << "Deserialize error: " << e.what() << std::endl;
51 throw;
52 }
53#else
54 // STM32 / No Exceptions
55 serialize ser;
56 (ser.read(is, args), ...);
57#endif
58 return is;
59 }
60};
61
62#endif
Interface for custom argument serialization/deserialization.
virtual std::istream & Read(std::istream &is, Args &... args) override
Deserializes data from the input stream into function arguments.
Definition serialize/Serializer.h:43
virtual std::ostream & Write(std::ostream &os, Args... args) override
Serializes function arguments into the output stream.
Definition serialize/Serializer.h:24
The serialize class binary serializes and deserializes C++ objects.
Definition msg_serialize.h:135
std::istream & read(std::istream &is, I *t_)
Definition msg_serialize.h:225
std::ostream & write(std::ostream &os, I *t_)
Definition msg_serialize.h:379
A robust, header-only C++ binary serialization library.
Definition bitsery/Serializer.h:27
Definition ISerializer.h:14