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 Arg1, typename... Args>
22void make_serialized(msgpack::sbuffer& buffer, Arg1& arg1, Args... args) {
23 msgpack::pack(buffer, arg1);
24
25 // Recursively call for other arguments
26 if constexpr (sizeof...(args) > 0) {
27 make_serialized(buffer, args...);
28 }
29}
30
31// make_unserialized unserializes each remote function argument
32template<typename Arg1, typename... Args>
33void make_unserialized(msgpack::unpacker& unpacker, Arg1& arg1, Args&&... args) {
34 static_assert(!std::is_pointer<Arg1>::value, "Arg1 cannot be a pointer.");
35 static_assert(!is_const_type<Arg1>::value, "Arg1 cannot be const.");
36 msgpack::object_handle oh;
37 if (!unpacker.next(oh))
38 throw std::runtime_error("Error during MsgPack unpacking.");
39 arg1 = oh.get().as<Arg1>();
40
41 // Recursively call for other arguments
42 if constexpr (sizeof...(args) > 0) {
43 make_unserialized(unpacker, args...);
44 }
45}
46
47template <class R>
48struct Serializer; // Not defined
49
50// Serialize all target function argument data using MessagePack library
51template<class RetType, class... Args>
52class Serializer<RetType(Args...)> : public dmq::ISerializer<RetType(Args...)>
53{
54public:
55 // Write arguments to a stream
56 virtual std::ostream& Write(std::ostream& os, Args... args) override {
57 try {
58 os.seekp(0, std::ios::beg);
59 msgpack::sbuffer buffer;
60 make_serialized(buffer, args...);
61 os.write(buffer.data(), buffer.size());
62 }
63 catch (const std::exception& e) {
64 std::cerr << "Serialize error: " << e.what() << std::endl;
65 throw;
66 }
67 return os;
68 }
69
70 // Read arguments from a stream
71 virtual std::istream& Read(std::istream& is, Args&... args) override {
72 try {
73 std::string buffer_data((std::istreambuf_iterator<char>(is)), std::istreambuf_iterator<char>());
74 msgpack::unpacker unpacker;
75 unpacker.reserve_buffer(buffer_data.size());
76 std::memcpy(unpacker.buffer(), buffer_data.data(), buffer_data.size());
77 unpacker.buffer_consumed(buffer_data.size());
78 make_unserialized(unpacker, args...);
79 }
80 catch (const msgpack::type_error& e) {
81 std::cerr << "Deserialize type conversion error: " << e.what() << std::endl;
82 throw;
83 }
84 catch (const std::exception& e) {
85 std::cerr << "Deserialize error: " << e.what() << std::endl;
86 throw;
87 }
88 return is;
89 }
90};
91
92#endif
Delegate serializer interface class.
virtual std::istream & Read(std::istream &is, Args &... args) override
Definition msgpack/Serializer.h:71
virtual std::ostream & Write(std::ostream &os, Args... args) override
Definition msgpack/Serializer.h:56
void make_unserialized(msgpack::unpacker &unpacker, Arg1 &arg1, Args &&... args)
Definition msgpack/Serializer.h:33
std::is_const< std::remove_reference_t< T > > is_const_type
Definition msgpack/Serializer.h:18
void make_serialized(msgpack::sbuffer &buffer, Arg1 &arg1, Args... args)
Definition msgpack/Serializer.h:22
Definition msgpack/Serializer.h:48
Definition ISerializer.h:12