Devel Lib
DynamicBuffer.h
1 #pragma once
2 
3 #include "IO/Buffer/Buffer.h"
4 
7 namespace Devel::IO {
27  class CDynamicBuffer : public IBuffer {
28  public:
31 
34  CDynamicBuffer(const size_t i_nSize);
35 
38  CDynamicBuffer(const CDynamicBuffer &i_oOther) {
39  this->operator=(i_oOther);
40  }
41 
44  CDynamicBuffer(CDynamicBuffer &&i_oOther) noexcept {
45  this->operator=(std::move(i_oOther));
46  }
47 
49  virtual ~CDynamicBuffer();
50 
51  private:
55  void deleteBuffer();
56 
57  private:
61  void checkBuffer() {
62  if (!this->m_pBuffer) {
63  throw NoBufferException;
64  }
65  }
66 
67  public:
70  void reallocate(const size_t i_nSize);
71 
72  public:
75  const char *buffer() const override { return this->m_pBuffer; }
76 
79  char *rawBuffer() const override { return this->m_pBuffer; }
80 
83  [[nodiscard]] size_t size() const override { return this->m_nSize; }
84 
87  [[nodiscard]] size_t allocatedSize() const { return this->m_nAllocatedSize; }
88 
89  public:
94  const char &operator[](const size_t i_nIndex) {
95  if (this->checkBuffer(); i_nIndex > this->m_nSize) {
96  throw IndexOutOfRangeException;
97  }
98  return this->m_pBuffer[i_nIndex];
99  }
100 
104  void operator=(const CDynamicBuffer &i_oOther) {
105  this->m_nAllocatedSize = i_oOther.m_nAllocatedSize;
106  this->m_pBuffer = new char[this->m_nAllocatedSize];
107  this->m_nSize = i_oOther.m_nSize;
108  memcpy(this->m_pBuffer, i_oOther.m_pBuffer, this->m_nSize);
109  }
110 
114  void operator=(CDynamicBuffer &&i_oOther) noexcept {
115  this->m_pBuffer = i_oOther.m_pBuffer;
116  this->m_nSize = i_oOther.m_nSize;
117  this->m_nAllocatedSize = i_oOther.m_nAllocatedSize;
118  i_oOther.m_pBuffer = nullptr;
119  i_oOther.m_nSize = 0;
120  i_oOther.m_nAllocatedSize = 0;
121  }
122 
123  private:
126  char *m_pBuffer;
127 
130  size_t m_nSize;
131 
134  size_t m_nAllocatedSize;
135  };
136 }
A dynamic implementation of the IBuffer interface.
Definition: DynamicBuffer.h:27
void operator=(CDynamicBuffer &&i_oOther) noexcept
Move assignment operator.
Definition: DynamicBuffer.h:114
CDynamicBuffer()
Default constructor.
Definition: DynamicBuffer.cpp:3
char * rawBuffer() const override
Get a mutable pointer to the start of the buffer.
Definition: DynamicBuffer.h:79
size_t allocatedSize() const
Get the total allocated size of the buffer.
Definition: DynamicBuffer.h:87
const char & operator[](const size_t i_nIndex)
Array access operator.
Definition: DynamicBuffer.h:94
void operator=(const CDynamicBuffer &i_oOther)
Copy assignment operator.
Definition: DynamicBuffer.h:104
virtual ~CDynamicBuffer()
Virtual destructor.
Definition: DynamicBuffer.cpp:12
void reallocate(const size_t i_nSize)
Reallocate the buffer to a new size.
Definition: DynamicBuffer.cpp:24
CDynamicBuffer(CDynamicBuffer &&i_oOther) noexcept
Move constructor.
Definition: DynamicBuffer.h:44
size_t size() const override
Get the size of the buffer.
Definition: DynamicBuffer.h:83
CDynamicBuffer(const CDynamicBuffer &i_oOther)
Copy constructor.
Definition: DynamicBuffer.h:38
const char * buffer() const override
Get a constant pointer to the start of the buffer.
Definition: DynamicBuffer.h:75
An interface for working with byte buffers.
Definition: Buffer.h:49
The namespace encapsulating I/O related classes and functions in the Devel framework.