FairRoot
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
RootSerializer.h
Go to the documentation of this file.
1 /*
2  * File: RootSerializer.h
3  * Author: winckler
4  *
5  * Created on January 14, 2015, 3:44 PM
6  */
7 
8 #ifndef ROOTSERIALIZER_H
9 #define ROOTSERIALIZER_H
10 
11 #include <FairMQMessage.h>
12 #include <TClonesArray.h>
13 #include <TMessage.h>
14 #include <memory>
15 
16 // special class to expose protected TMessage constructor
17 class FairTMessage : public TMessage
18 {
19  public:
20  FairTMessage(void* buf, Int_t len)
21  : TMessage(buf, len)
22  {
23  ResetBit(kIsOwner);
24  }
25 };
26 
28 {
29  RootSerializer() = default;
30  virtual ~RootSerializer() = default;
31 
32  template<typename T>
33  void Serialize(FairMQMessage& msg, T* input)
34  {
35  TMessage* tm = new TMessage(kMESS_OBJECT);
36  tm->WriteObject(input);
37  msg.Rebuild(
38  tm->Buffer(), tm->BufferSize(), [](void*, void* tmsg) { delete static_cast<TMessage*>(tmsg); }, tm);
39  }
40 
41  template<typename T>
42  void Serialize(FairMQMessage& msg, const std::unique_ptr<T>& input)
43  {
44  TMessage* tm = new TMessage(kMESS_OBJECT);
45  tm->WriteObject(input.get());
46  msg.Rebuild(
47  tm->Buffer(), tm->BufferSize(), [](void*, void* tmsg) { delete static_cast<TMessage*>(tmsg); }, tm);
48  }
49 
50  template<typename T>
51  void Deserialize(FairMQMessage& msg, T*& output)
52  {
53  delete output;
54  FairTMessage tm(msg.GetData(), msg.GetSize());
55  output = static_cast<T*>(tm.ReadObjectAny(tm.GetClass()));
56  }
57 
58  template<typename T>
59  void Deserialize(FairMQMessage& msg, std::unique_ptr<T>& output)
60  {
61  FairTMessage tm(msg.GetData(), msg.GetSize());
62  output.reset(static_cast<T*>(tm.ReadObjectAny(tm.GetClass())));
63  }
64 };
65 
66 #endif /* ROOTSERIALIZER_H */
FairTMessage(void *buf, Int_t len)
virtual ~RootSerializer()=default
void Serialize(FairMQMessage &msg, const std::unique_ptr< T > &input)
void Serialize(FairMQMessage &msg, T *input)
RootSerializer()=default
void Deserialize(FairMQMessage &msg, T *&output)
void Deserialize(FairMQMessage &msg, std::unique_ptr< T > &output)