DelegateMQ
Loading...
Searching...
No Matches
msgpack/Serializer.h
Go to the documentation of this file.
1#ifndef SERIALIZER_H
2#define SERIALIZER_H
3
10
11#include "msgpack.hpp"
13#include <iostream>
14#include <type_traits>
15
16// Type trait to check if a type is const
17template <typename T>
18using is_const_type = std::is_const<std::remove_reference_t<T>>;
19
20// make_serialized serializes each remote function argument
21template<typename... Args>
22void make_serialized(msgpack::sbuffer& buffer, Args&&... args) {
23 (msgpack::pack(buffer, args), ...); // C++17 fold expression to serialize
24}
25
26// make_unserialized unserializes each remote function argument
27template<typename Arg1, typename... Args>
28void make_unserialized(msgpack::unpacker& unpacker, Arg1& arg1, Args&&... args) {
29 static_assert(!std::is_pointer<Arg1>::value, "Arg1 cannot be a pointer.");
30 static_assert(!is_const_type<Arg1>::value, "Arg1 cannot be const.");
31 msgpack::object_handle oh;
32 if (!unpacker.next(oh))
33 throw std::runtime_error("Error during MsgPack unpacking.");
34 arg1 = oh.get().as<Arg1>();
35
36 // Recursively call for other arguments
37 if constexpr (sizeof...(args) > 0) {
38 make_unserialized(unpacker, args...);
39 }
40}
41
42template <class R>
43struct Serializer; // Not defined
44
45// Serialize all target function argument data using MessagePack library
46template<class RetType, class... Args>
47class Serializer<RetType(Args...)> : public dmq::ISerializer<RetType(Args...)>
48{
49public:
50 // Write arguments to a stream
51 virtual std::ostream& Write(std::ostream& os, Args... args) override {
52 try {
53 os.seekp(0, std::ios::beg);
54 msgpack::sbuffer buffer;
55 make_serialized(buffer, args...);
56 os.write(buffer.data(), buffer.size());
57 }
58 catch (const std::exception& e) {
59 std::cerr << "Serialize error: " << e.what() << std::endl;
60 throw;
61 }
62 return os;
63 }
64
65 // Read arguments from a stream
66 virtual std::istream& Read(std::istream& is, Args&... args) override {
67 try {
68 std::string buffer_data((std::istreambuf_iterator<char>(is)), std::istreambuf_iterator<char>());
69 msgpack::unpacker unpacker;
70 unpacker.reserve_buffer(buffer_data.size());
71 std::memcpy(unpacker.buffer(), buffer_data.data(), buffer_data.size());
72 unpacker.buffer_consumed(buffer_data.size());
73 make_unserialized(unpacker, args...);
74 }
75 catch (const msgpack::type_error& e) {
76 std::cerr << "Deserialize type conversion error: " << e.what() << std::endl;
77 throw;
78 }
79 catch (const std::exception& e) {
80 std::cerr << "Deserialize error: " << e.what() << std::endl;
81 throw;
82 }
83 return is;
84 }
85};
86
87#endif
Delegate serializer interface class.
std::is_const< std::remove_reference_t< T > > is_const_type
Definition bitsery/Serializer.h:21
virtual std::istream & Read(std::istream &is, Args &... args) override
Definition msgpack/Serializer.h:66
virtual std::ostream & Write(std::ostream &os, Args... args) override
Definition msgpack/Serializer.h:51
void make_serialized(msgpack::sbuffer &buffer, Args &&... args)
Definition msgpack/Serializer.h:22
void make_unserialized(msgpack::unpacker &unpacker, Arg1 &arg1, Args &&... args)
Definition msgpack/Serializer.h:28
Definition bitsery/Serializer.h:24
Definition ISerializer.h:12