8 #ifndef BOOSTSERIALIZER_H
9 #define BOOSTSERIALIZER_H
11 #include <FairMQLogger.h>
12 #include <FairMQMessage.h>
13 #include <TClonesArray.h>
16 namespace serialization {
20 #include <boost/archive/binary_iarchive.hpp>
21 #include <boost/archive/binary_oarchive.hpp>
22 #include <boost/serialization/vector.hpp>
26 #include <type_traits>
31 namespace serialization {
33 template<
typename,
typename T>
36 static_assert(std::integral_constant<T, false>::value,
"Second template parameter needs to be of function type.");
39 template<
typename C,
typename Ret,
typename... Args>
44 static constexpr
auto check(T*)
45 -> std::is_same<decltype(std::declval<T>().
serialize(std::declval<Args>()...)), Ret>;
48 static constexpr std::false_type check(...);
51 static constexpr
bool value = decltype(check<C>(0))::value;
58 template<
typename DataType>
64 std::ostringstream buffer;
65 boost::archive::binary_oarchive outputArchive(buffer);
66 outputArchive << *data;
67 int size = buffer.str().length();
69 std::memcpy(msg.GetData(), buffer.str().c_str(), size);
72 void Serialize(FairMQMessage& msg,
const DataType& data)
74 std::ostringstream buffer;
75 boost::archive::binary_oarchive outputArchive(buffer);
76 outputArchive << data;
77 int size = buffer.str().length();
79 std::memcpy(msg.GetData(), buffer.str().c_str(), size);
82 void Serialize(FairMQMessage& msg,
const std::vector<DataType>& dataVec)
84 std::ostringstream buffer;
85 boost::archive::binary_oarchive outputArchive(buffer);
86 outputArchive << dataVec;
87 int size = buffer.str().length();
89 std::memcpy(msg.GetData(), buffer.str().c_str(), size);
92 void Serialize(FairMQMessage& msg, TClonesArray* input)
94 std::vector<DataType> dataVec;
95 for (
int i = 0; i < input->GetEntriesFast(); ++i) {
96 DataType* data =
static_cast<DataType*
>(input->At(i));
100 dataVec.push_back(*data);
105 void Serialize(FairMQMessage& msg, std::unique_ptr<TClonesArray> input) {
Serialize(msg, input.get()); }
109 std::string msgStr(static_cast<char*>(msg.GetData()), msg.GetSize());
110 std::istringstream buffer(msgStr);
111 boost::archive::binary_iarchive inputArchive(buffer);
113 inputArchive >> input;
114 }
catch (boost::archive::archive_exception& e) {
115 LOG(error) << e.what();
119 void Deserialize(FairMQMessage& msg, std::vector<DataType>& input)
122 std::string msgStr(static_cast<char*>(msg.GetData()), msg.GetSize());
123 std::istringstream buffer(msgStr);
124 boost::archive::binary_iarchive inputArchive(buffer);
125 inputArchive >> input;
130 std::vector<DataType> dataVec;
134 for (
unsigned int i = 0; i < dataVec.size(); ++i) {
135 new ((*input)[i]) DataType(dataVec.at(i));
138 if (input->IsEmpty()) {
139 LOG(debug) <<
"BoostSerializer::Deserialize(FairMQMessage& msg, TClonesArray* input): No Output array!";
void Serialize(FairMQMessage &msg, TClonesArray *input)
void Deserialize(FairMQMessage &msg, DataType &input)
void Serialize(FairMQMessage &msg, DataType *data)
void Serialize(FairMQMessage &msg, std::unique_ptr< TClonesArray > input)
void Deserialize(FairMQMessage &msg, std::unique_ptr< TClonesArray > &input)
void Deserialize(FairMQMessage &msg, std::vector< DataType > &input)
void Serialize(FairMQMessage &msg, const DataType &data)
void serialize(Archive &ar, Ex2Header &header, const unsigned int)
void Deserialize(FairMQMessage &msg, TClonesArray *input)
void Serialize(FairMQMessage &msg, const std::vector< DataType > &dataVec)