99// / to a remote. Bitsery provides fast, compact binary serialization.
1010
1111#include " delegate/ISerializer.h"
12+
13+ // Core Bitsery
1214#include < bitsery/bitsery.h>
1315#include < bitsery/adapter/stream.h>
14- #include < bitsery/ext/std_tuple.h>
16+
17+ // Common Traits (Include these so standard types work out of the box)
18+ #include < bitsery/traits/string.h>
19+ #include < bitsery/traits/vector.h>
20+ #include < bitsery/traits/list.h>
21+
1522#include < sstream>
1623#include < iostream>
1724#include < type_traits>
1825
19- // Type trait to check if a type is const
20- template <typename T>
21- using is_const_type = std::is_const<std::remove_reference_t <T>>;
22-
2326template <class R >
2427struct Serializer ; // Not defined
2528
2629template <class RetType , class ... Args>
2730class Serializer <RetType(Args...)> : public dmq::ISerializer<RetType(Args...)>
2831{
2932public:
33+ // Bitsery Adapters for std::ostream / std::istream
3034 using OutputAdapter = bitsery::OutputStreamAdapter;
3135 using InputAdapter = bitsery::InputStreamAdapter;
3236
33- virtual std::ostream& Write (std::ostream& os, Args... args) override {
37+ // Write: Changed 'Args... args' to 'const Args&... args' for efficiency
38+ virtual std::ostream& Write (std::ostream& os, const Args&... args) override {
3439 try {
35- os. seekp ( 0 );
36- bitsery::Serializer<OutputAdapter> writer{ os };
40+ // Reset stream position.
41+ os. seekp ( 0 , std::ios::beg);
3742
38- // Serialize each argument using fold expression
43+ // Clear stringstreams explicitly to avoid appending new data to old data.
44+ // DelegateMQ often reuses the stream object.
45+ if (auto * ss = dynamic_cast <std::ostringstream*>(&os)) {
46+ ss->str (" " );
47+ }
48+
49+ // Construct the adapter properly passing the stream
50+ bitsery::Serializer<OutputAdapter> writer{ OutputAdapter{os} };
51+
52+ // Serialize each argument using C++17 fold expression
3953 (writer.object (args), ...);
54+
55+ // Ensure buffer is flushed to the stream
4056 writer.adapter ().flush ();
4157 }
4258 catch (const std::exception& e) {
@@ -48,11 +64,16 @@ class Serializer<RetType(Args...)> : public dmq::ISerializer<RetType(Args...)>
4864
4965 virtual std::istream& Read (std::istream& is, Args&... args) override {
5066 try {
51- bitsery::Deserializer<InputAdapter> reader{ is };
67+ // Construct the adapter properly passing the stream
68+ bitsery::Deserializer<InputAdapter> reader{ InputAdapter{is} };
5269
5370 // Deserialize each argument using fold expression
5471 (reader.object (args), ...);
5572
73+ // Optional: Check for deserialization errors
74+ if (reader.adapter ().error () != bitsery::ReaderError::NoError) {
75+ throw std::runtime_error (" Bitsery reported a read error" );
76+ }
5677 }
5778 catch (const std::exception& e) {
5879 std::cerr << " Bitsery deserialize error: " << e.what () << std::endl;
@@ -62,4 +83,4 @@ class Serializer<RetType(Args...)> : public dmq::ISerializer<RetType(Args...)>
6283 }
6384};
6485
65- #endif
86+ #endif // SERIALIZER_H
0 commit comments