FairRoot
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
BoostSerializer.h
Go to the documentation of this file.
1 /*
2  * File: BoostSerializer.h
3  * Author: winckler
4  *
5  * Created on October 7, 2014, 8:07 PM
6  */
7 
8 #ifndef BOOSTSERIALIZER_H
9 #define BOOSTSERIALIZER_H
10 
11 #include <FairMQLogger.h>
12 #include <FairMQMessage.h>
13 #include <TClonesArray.h>
14 
15 namespace boost {
16 namespace serialization {
17 class access;
18 }
19 } // namespace boost
20 #include <boost/archive/binary_iarchive.hpp> // input: a non-portable native binary archive
21 #include <boost/archive/binary_oarchive.hpp> // output: a non-portable native binary archive
22 #include <boost/serialization/vector.hpp>
23 #include <memory>
24 #include <sstream>
25 #include <string>
26 #include <type_traits>
27 #include <vector>
28 
29 namespace fair {
30 namespace base {
31 namespace serialization {
32 
33 template<typename, typename T>
35 {
36  static_assert(std::integral_constant<T, false>::value, "Second template parameter needs to be of function type.");
37 };
38 
39 template<typename C, typename Ret, typename... Args>
40 struct has_BoostSerialization<C, Ret(Args...)>
41 {
42  private:
43  template<typename T>
44  static constexpr auto check(T*) // attempt to call it and see if the return type is correct
45  -> std::is_same<decltype(std::declval<T>().serialize(std::declval<Args>()...)), Ret>;
46 
47  template<typename>
48  static constexpr std::false_type check(...);
49 
50  public:
51  static constexpr bool value = decltype(check<C>(0))::value;
52 };
53 
54 } // namespace serialization
55 } // namespace base
56 } // namespace fair
57 
58 template<typename DataType>
60 {
61  public:
62  void Serialize(FairMQMessage& msg, DataType* data)
63  {
64  std::ostringstream buffer;
65  boost::archive::binary_oarchive outputArchive(buffer);
66  outputArchive << *data;
67  int size = buffer.str().length();
68  msg.Rebuild(size);
69  std::memcpy(msg.GetData(), buffer.str().c_str(), size);
70  }
71 
72  void Serialize(FairMQMessage& msg, const DataType& data)
73  {
74  std::ostringstream buffer;
75  boost::archive::binary_oarchive outputArchive(buffer);
76  outputArchive << data;
77  int size = buffer.str().length();
78  msg.Rebuild(size);
79  std::memcpy(msg.GetData(), buffer.str().c_str(), size);
80  }
81 
82  void Serialize(FairMQMessage& msg, const std::vector<DataType>& dataVec)
83  {
84  std::ostringstream buffer;
85  boost::archive::binary_oarchive outputArchive(buffer);
86  outputArchive << dataVec;
87  int size = buffer.str().length();
88  msg.Rebuild(size);
89  std::memcpy(msg.GetData(), buffer.str().c_str(), size);
90  }
91 
92  void Serialize(FairMQMessage& msg, TClonesArray* input)
93  {
94  std::vector<DataType> dataVec;
95  for (int i = 0; i < input->GetEntriesFast(); ++i) {
96  DataType* data = static_cast<DataType*>(input->At(i));
97  if (!data) {
98  continue;
99  }
100  dataVec.push_back(*data);
101  }
102  Serialize(msg, dataVec);
103  }
104 
105  void Serialize(FairMQMessage& msg, std::unique_ptr<TClonesArray> input) { Serialize(msg, input.get()); }
106 
107  void Deserialize(FairMQMessage& msg, DataType& input)
108  {
109  std::string msgStr(static_cast<char*>(msg.GetData()), msg.GetSize());
110  std::istringstream buffer(msgStr);
111  boost::archive::binary_iarchive inputArchive(buffer);
112  try {
113  inputArchive >> input;
114  } catch (boost::archive::archive_exception& e) {
115  LOG(error) << e.what();
116  }
117  }
118 
119  void Deserialize(FairMQMessage& msg, std::vector<DataType>& input)
120  {
121  input.clear();
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;
126  }
127 
128  void Deserialize(FairMQMessage& msg, TClonesArray* input)
129  {
130  std::vector<DataType> dataVec;
131  Deserialize(msg, dataVec);
132  if (input) {
133  input->Delete();
134  for (unsigned int i = 0; i < dataVec.size(); ++i) {
135  new ((*input)[i]) DataType(dataVec.at(i));
136  }
137 
138  if (input->IsEmpty()) {
139  LOG(debug) << "BoostSerializer::Deserialize(FairMQMessage& msg, TClonesArray* input): No Output array!";
140  }
141  }
142  }
143 
144  void Deserialize(FairMQMessage& msg, std::unique_ptr<TClonesArray>& input) { Deserialize(msg, input.get()); }
145 };
146 
147 #endif /* BOOSTSERIALIZER_H */
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)