Belle II Software  release-05-02-19
MsgHandler.h
1 //+
2 // File : MsgHandler.cc
3 // Description : Encode/Decode EvtMessage
4 //
5 // Author : SooHyung Lee, Ryosuke Itoh
6 // Date : 31 - Jul - 2008
7 // Modified : 4 - Jun - 2010
8 //-
9 
10 #pragma once
11 
12 #include <framework/pcore/EvtMessage.h>
13 #include <TMessage.h>
14 
15 #include <string>
16 #include <memory>
17 
18 class TObject;
19 
20 namespace Belle2 {
31  class CharBuffer {
32  std::unique_ptr<char[]> m_data;
33  size_t m_capacity{0};
34  size_t m_size{0};
35  public:
39  explicit CharBuffer(size_t initial_capacity = 0): m_data{initial_capacity > 0 ? new char[initial_capacity] : nullptr}
40  {}
44  void add(const void* data, size_t len)
45  {
46  auto old_size = m_size;
47  resize(m_size + len);
48  memcpy(m_data.get() + old_size, data, len);
49  }
53  char* data() { return m_data.get(); }
55  size_t size() const { return m_size; }
57  void clear() { m_size = 0; }
61  void resize(size_t size)
62  {
63  if (size > m_capacity) {
64  // grow by basically doubling each time, except if the size we need to
65  // grow to is even more than that
66  m_capacity = std::max(size, m_capacity * 2);
67  //c++14: auto newbuf = std::make_unique<char[]>(m_capacity);
68  std::unique_ptr<char[]> newbuf{new char[m_capacity]};
69  memcpy(newbuf.get(), m_data.get(), m_size);
70  std::swap(m_data, newbuf);
71  }
72  m_size = size;
73  }
74  };
75 
77  class InMessage : public TMessage {
78  public:
79  InMessage() : TMessage()
80  {
81  SetReadMode();
82  SetWhat(kMESS_OBJECT);
83  }
84 
86  void SetBuffer(const void* ptr, UInt_t bufsize)
87  {
88  TBuffer::SetBuffer(const_cast<void*>(ptr), bufsize, false);
89  // TMessage has an extra header of two ints: a reserved size field and a
90  // fWhat pointer indicating the type of the message. We force the message
91  // to be MESS_OBJECT so we don't care. Let's put the buffer where we need
92  // it to be. The original constructor has a ReadClass() here but we skip
93  // it since we read everything as TObject.
94  SetBufferOffset(sizeof(UInt_t) * 2);
95  // and reset the map of objects/classes seen so far
96  ResetMap();
97  }
98 
100  TObject* readTObject() { return static_cast<TObject*>(ReadObjectAny(TObject::Class())); }
101  };
102 
104  class MsgHandler {
105  public:
116  explicit MsgHandler(int complevel = 0);
118  virtual ~MsgHandler();
119 
121  virtual void clear();
123  virtual void add(const TObject*, const std::string& name);
124 
126  virtual EvtMessage* encode_msg(ERecordType rectype);
128  virtual void decode_msg(EvtMessage* msg, std::vector<TObject*>& objlist, std::vector<std::string>& namelist);
129 
130  private:
133  std::unique_ptr<TMessage> m_msg;
139  };
140 
142 } // namespace Belle2
Belle2::MsgHandler::m_compBuf
CharBuffer m_compBuf
EvtMessage character buffer for compressing/decompressing.
Definition: MsgHandler.h:132
Belle2::MsgHandler::m_msg
std::unique_ptr< TMessage > m_msg
Used for serialising objects into m_buf.
Definition: MsgHandler.h:133
Belle2::InMessage::SetBuffer
void SetBuffer(const void *ptr, UInt_t bufsize)
Replace buffer (doesn't take ownership).
Definition: MsgHandler.h:86
Belle2::EvtMessage
Class to manage streamed object.
Definition: EvtMessage.h:60
Belle2::MsgHandler::~MsgHandler
virtual ~MsgHandler()
Destructor.
Belle2::CharBuffer::resize
void resize(size_t size)
resize, similar to std::vector<char>::resize in that it will copy the existing buffer to a new,...
Definition: MsgHandler.h:61
Belle2::MsgHandler::encode_msg
virtual EvtMessage * encode_msg(ERecordType rectype)
Stream object list into an EvtMessage.
Definition: MsgHandler.cc:68
Belle2::CharBuffer::data
char * data()
return raw pointer.
Definition: MsgHandler.h:53
Belle2::MsgHandler::clear
virtual void clear()
Clear object list.
Definition: MsgHandler.cc:41
Belle2::InMessage
Reusable Message class derived from TMessage (for reading only)
Definition: MsgHandler.h:77
Belle2::MsgHandler::m_inMsg
InMessage m_inMsg
Used for deserializing in decode_msg()
Definition: MsgHandler.h:134
Belle2::MsgHandler::m_buf
CharBuffer m_buf
EvtMessage character buffer for encode_msg().
Definition: MsgHandler.h:131
Belle2::CharBuffer::clear
void clear()
reset (without deallocating)
Definition: MsgHandler.h:57
Belle2::CharBuffer::add
void add(const void *data, size_t len)
copy data to end of buffer, expanding buffer if needed.
Definition: MsgHandler.h:44
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::MsgHandler::m_complevel
int m_complevel
compression algorithm * 100 + compression level.
Definition: MsgHandler.h:135
Belle2::CharBuffer::m_size
size_t m_size
current size, <= m_capacity
Definition: MsgHandler.h:34
Belle2::ERecordType
ERecordType
What type of message is this?
Definition: EvtMessage.h:26
Belle2::MsgHandler::decode_msg
virtual void decode_msg(EvtMessage *msg, std::vector< TObject * > &objlist, std::vector< std::string > &namelist)
Decode an EvtMessage into a vector list of objects with names.
Definition: MsgHandler.cc:107
Belle2::CharBuffer::m_capacity
size_t m_capacity
size of allocated memory in m_data
Definition: MsgHandler.h:33
Belle2::CharBuffer::m_data
std::unique_ptr< char[]> m_data
data buffer.
Definition: MsgHandler.h:32
Belle2::CharBuffer::CharBuffer
CharBuffer(size_t initial_capacity=0)
Constructor, with the initial capacity of the buffer to allocate (in bytes).
Definition: MsgHandler.h:39
Belle2::InMessage::readTObject
TObject * readTObject()
Read one object from the message assuming it inherits from TObject.
Definition: MsgHandler.h:100
Belle2::CharBuffer::size
size_t size() const
return buffer size (do not access data() beyond this)
Definition: MsgHandler.h:55
Belle2::MsgHandler::MsgHandler
MsgHandler(int complevel=0)
Constructor.
Definition: MsgHandler.cc:26
Belle2::MsgHandler::add
virtual void add(const TObject *, const std::string &name)
Add an object to be streamed.
Definition: MsgHandler.cc:47
Belle2::MsgHandler
A class to encode/decode an EvtMessage.
Definition: MsgHandler.h:104
Belle2::CharBuffer
dynamic character buffer that knows its size.
Definition: MsgHandler.h:31