Belle II Software  release-08-01-10
BufferedWriter.cc
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 #include "daq/slc/system/BufferedWriter.h"
9 
10 #include <daq/slc/base/IOException.h>
11 
12 #include <cstring>
13 
14 using namespace Belle2;
15 
16 BufferedWriter::BufferedWriter()
17  : m_memory(NULL), m_size(0), m_pos(0), m_allocated(false) {}
18 
19 BufferedWriter::BufferedWriter(size_t size, unsigned char* memory)
20  : m_memory(memory), m_size(size), m_pos(0), m_allocated(false)
21 {
22  if (memory == NULL) {
23  m_memory = new unsigned char[size];
24  m_allocated = true;
25  }
26 }
27 
28 BufferedWriter::BufferedWriter(const BufferedWriter& writer)
29  : m_memory(NULL), m_size(writer.m_size),
30  m_pos(writer.m_pos), m_allocated(writer.m_allocated)
31 {
32  if (m_allocated) {
33  m_memory = new unsigned char [writer.m_size];
34  ::memcpy(m_memory, writer.m_memory, writer.m_size);
35  } else {
36  m_memory = writer.m_memory;
37  }
38  m_size = writer.m_size;
39  m_pos = writer.m_pos;
40 }
41 
42 BufferedWriter::~BufferedWriter()
43 {
44  if (m_allocated && m_memory != NULL) delete [] m_memory;
45 }
46 
47 const BufferedWriter& BufferedWriter::operator=(const BufferedWriter& writer)
48 {
49  if (m_allocated) {
50  delete [] m_memory;
51  }
52  m_allocated = writer.m_allocated;
53  if (m_allocated) {
54  m_memory = new unsigned char [writer.m_size];
55  ::memcpy(m_memory, writer.m_memory, writer.m_size);
56  } else {
57  m_memory = writer.m_memory;
58  }
59  m_size = writer.m_size;
60  m_pos = writer.m_pos;
61  return *this;
62 }
63 
64 size_t BufferedWriter::write(const void* buf, const size_t count)
65 {
66  if (m_pos + count > m_size) {
67  throw (IOException("out of buffer range: %d+%d>%d",
68  (int)m_pos, (int)count, (int)m_size));
69  }
70  memcpy((m_memory + m_pos), buf, count);
71  m_pos += count;
72  return count;
73 }
74 
Abstract base class for different kinds of events.