mn_buffer.hpp
Go to the documentation of this file.
1 
19 #ifndef __MINILIB_BASIC_BUFFER_H__
20 #define __MINILIB_BASIC_BUFFER_H__
21 
22 #include "mn_config.hpp"
23 
24 #include <cassert>
25 
26 #include "mn_allocator.hpp"
27 #include "mn_typetraits.hpp"
28 #include "mn_algorithm.hpp"
29 
30 namespace mn {
31 
36  template <typename TVALUE, class TALLOCATOR = memory::default_allocator>
38  public:
40  using value_type = TVALUE;
41  using allocator = TALLOCATOR;
42  using pointer = value_type*;
45 
46  using const_reference = const value_type&;
47  using const_pointer = const value_type*;
48 
51  using iterator = pointer;
52  using const_iterator = const value_type*;
53 
58  buffer(const size_type& size)
59  : m_sSize(size), m_sUsed(size), m_pRawBuffer(0), m_bOwnMem(true), m_allocator() {
61  }
62 
70  : m_sSize(size), m_sUsed(size), m_pRawBuffer(buffer), m_bOwnMem(false), m_allocator() { }
71 
72 
80  : m_sSize(size), m_sUsed(size), m_pRawBuffer(0), m_bOwnMem(true), m_allocator() {
82  }
83 
84 
88  buffer(const self_type& other)
89  : m_sSize(other.m_sSize), m_sUsed(other.m_sUsed), m_pRawBuffer(0), m_bOwnMem(true), m_allocator() {
91  }
92 
96  ~buffer() {
98  }
99 
105 
110  const_iterator begin() const { return m_pRawBuffer; }
111 
116  iterator end() { return m_pRawBuffer; }
117 
122  const_iterator end() const { return m_pRawBuffer; }
123 
129  void append(const_pointer pBuffer, std::size_t sBufferSize) {
130  if (sBufferSize == 0) return;
131  if(resize(m_sUsed + sBufferSize, true))
132  memcpy(m_pRawBuffer + m_sUsed - sBufferSize, pBuffer, sBufferSize * sizeof(value_type));
133  }
134 
139  void append(value_type value) {
140  if(resize(m_sUsed + 1, true))
141  m_pRawBuffer[m_sUsed - 1] = value;
142  }
143 
148  void append(const self_type& refBuffer) {
149  append(refBuffer.begin(), refBuffer.used());
150  }
151 
159  bool resize(size_type newSize, bool bReserve = true) {
160  if(!m_bOwnMem) return false;
161  if(newSize > m_sSize) {
162  pointer __pNewRawBuffer = m_allocator.alloc(newSize * sizeof(value_type));
163  if(__pNewRawBuffer == 0) return false;
164 
165  if(bReserve) memcpy(__pNewRawBuffer, m_pRawBuffer, m_sUsed * sizeof(value_type));
166 
168  m_pRawBuffer = __pNewRawBuffer;
169  m_sSize = newSize;
170  }
171  m_sUsed = newSize;
172 
173  return true;
174  }
175 
183  bool change_size(size_type newSize, bool bReserve = true) {
184  if(!m_bOwnMem) return false;
185  if (newSize == m_sSize) return true;
186 
187  pointer __pNewRawBuffer = NULL;
188  if(newSize > 0) {
189  __pNewRawBuffer = m_allocator.alloc(newSize * sizeof(value_type));
190  if(bReserve) memcpy(__pNewRawBuffer, m_pRawBuffer,
191  (m_sUsed < newSize ? m_sUsed : newSize) * sizeof(value_type));
192  }
193 
195  m_pRawBuffer = __pNewRawBuffer;
196  m_sSize = newSize;
197 
198  if (newSize < m_sUsed) m_sUsed = newSize;
199 
200  }
201 
206  void assign(const_pointer pBuffer, size_type size) {
207  if (0 == size) return;
208  if (size > m_sSize) resize(size, false);
209 
210  memcpy(m_pRawBuffer, pBuffer, size * sizeof(value_type));
211  m_sUsed = size;
212  }
213 
217  void clear() {
218  memset(m_pRawBuffer, 0, m_sUsed * sizeof(value_type));
219  }
225  bool is_equel(const buffer& other) const {
226  if(m_sSize != other.m_sSize) return false;
227  if(m_sUsed != other.m_sUsed) return false;
228 
229  return (memcmp(m_pRawBuffer, other.m_pRawBuffer, m_sUsed * sizeof(value_type)) == 0);
230  }
231 
236  constexpr bool is_empty() const noexcept { return m_sUsed == 0; }
237 
242  constexpr bool is_full() const noexcept { return m_sUsed == m_sSize; }
247  constexpr size_type get_size() const noexcept { return m_sSize; }
248 
253  constexpr size_type get_size_bytes() const noexcept { return m_sSize * sizeof(value_type); }
254 
259  constexpr size_type get_used() const noexcept { return m_sUsed; }
260 
265  constexpr size_type get_used_bytes() const noexcept { return m_sUsed * sizeof(value_type); }
266 
270  bool operator == (const buffer& other) const {
271  return is_equel(other);
272  }
276  bool operator != (const buffer& other) const {
277  return !(is_equel(other));
278  }
279 
281  assert (index < m_sUsed);
282  return m_pRawBuffer[index];
283  }
284 
286  assert (index < m_sUsed);
287  return m_pRawBuffer[index];
288  }
289  private:
295  if(m_sSize > 0)
296  m_pRawBuffer = m_allocator.allocate(m_sSize, sizeof(value_type),
298  }
299 
305  if(m_sSize > 0) {
306  m_pRawBuffer = m_allocator.allocate(m_sSize, sizeof(value_type),
308 
309  memcpy(m_pRawBuffer, buffer, m_sUsed * sizeof(value_type));
310  }
311  }
312 
314  if(m_bOwnMem && (m_pRawBuffer != 0))
316  }
317  private:
321  bool m_bOwnMem;
323  };
324 
325 }
326 
327 #endif // __MINILIB_BASIC_BUFFER_H__
A buffer class that allocates a buffer of a given type and size.
Definition: mn_buffer.hpp:37
constexpr bool is_full() const noexcept
is the buffer full?
Definition: mn_buffer.hpp:242
size_type m_sSize
Definition: mn_buffer.hpp:318
bool is_equel(const buffer &other) const
Is the given buffer equel with this?
Definition: mn_buffer.hpp:225
reference operator[](size_type index)
Definition: mn_buffer.hpp:280
buffer(const size_type &size)
Consructs and allocates the Buffer.
Definition: mn_buffer.hpp:58
buffer(const self_type &other)
Move constructor.
Definition: mn_buffer.hpp:88
bool operator!=(const buffer &other) const
Not compare operator.
Definition: mn_buffer.hpp:276
iterator begin()
Get the iterator to the beginning of the buffer.
Definition: mn_buffer.hpp:104
const_iterator end() const
Get the iterator to end of the buffer.
Definition: mn_buffer.hpp:122
constexpr bool is_empty() const noexcept
is the buffer empty?
Definition: mn_buffer.hpp:236
void append(const_pointer pBuffer, std::size_t sBufferSize)
Resizes this buffer and appends the given data.
Definition: mn_buffer.hpp:129
mn::size_t size_type
Definition: mn_buffer.hpp:44
const value_type * const_pointer
Definition: mn_buffer.hpp:47
void clear()
Clear the used content.
Definition: mn_buffer.hpp:217
bool resize(size_type newSize, bool bReserve=true)
Resizes the buffer capacity and size.
Definition: mn_buffer.hpp:159
~buffer()
Deconstrut the buffer. Is allocated the memory by buffer, then deallocated it.
Definition: mn_buffer.hpp:96
bool operator==(const buffer &other) const
Compare operator.
Definition: mn_buffer.hpp:270
pointer iterator
Definition: mn_buffer.hpp:51
void assign(const_pointer pBuffer, size_type size)
Assigns the argument buffer to this buffer.
Definition: mn_buffer.hpp:206
void destroy_internal_buffer()
Definition: mn_buffer.hpp:313
void init_internal_buffer(const pointer buffer)
Definition: mn_buffer.hpp:304
constexpr size_type get_used() const noexcept
Get the used size of the buffer in elements.
Definition: mn_buffer.hpp:259
void init_internal_buffer()
Definition: mn_buffer.hpp:294
bool change_size(size_type newSize, bool bReserve=true)
Change the buffer size.
Definition: mn_buffer.hpp:183
constexpr size_type get_size_bytes() const noexcept
Get the allocated memory size in bytes.
Definition: mn_buffer.hpp:253
TVALUE value_type
Definition: mn_buffer.hpp:40
buffer(pointer buffer, size_type size)
Consructs the buffer from a given memory pointer.
Definition: mn_buffer.hpp:69
bool m_bOwnMem
Definition: mn_buffer.hpp:321
value_type & reference
Definition: mn_buffer.hpp:43
constexpr size_type get_size() const noexcept
Get the allocated memory size in elements.
Definition: mn_buffer.hpp:247
value_type * pointer
Definition: mn_buffer.hpp:42
const value_type & const_reference
Definition: mn_buffer.hpp:46
pointer m_pRawBuffer
Definition: mn_buffer.hpp:320
TALLOCATOR allocator
Definition: mn_buffer.hpp:41
const value_type * const_iterator
Definition: mn_buffer.hpp:52
buffer(const_pointer buffer, size_type size)
Consructs and allocates the Buffer. copies the contents of the supplied memory into the buffer.
Definition: mn_buffer.hpp:79
const_iterator begin() const
Get the iterator to the beginning of the buffer.
Definition: mn_buffer.hpp:110
void append(value_type value)
Resizes this buffer and appends the given data.
Definition: mn_buffer.hpp:139
constexpr size_type get_used_bytes() const noexcept
Get the used size of the buffer in bytes.
Definition: mn_buffer.hpp:265
void append(const self_type &refBuffer)
Resizes this buffer and appends the given data.
Definition: mn_buffer.hpp:148
ptrdiff_t difference_type
Definition: mn_buffer.hpp:50
iterator end()
Get the iterator to end of the buffer.
Definition: mn_buffer.hpp:116
allocator m_allocator
Definition: mn_buffer.hpp:322
size_type m_sUsed
Definition: mn_buffer.hpp:319
Basic algorithmens This file is part of the Mini Thread Library (https://github.com/RoseLeBlood/MiniT...
This file is part of the Mini Thread Library (https://github.com/RoseLeBlood/MiniThread ).
#define MN_ONCOPYABLE_CLASS
Definition: mn_copyable.hpp:25
Definition: mn_allocator_typetraits.hpp:25
long ptrdiff_t
Definition: mn_def.hpp:49
typename internal::type_with_alignment< alignment_of< T >::res > res
Definition: mn_alignment.hpp:133
MN_THREAD_CONFIG_SIZE_TYPE size_t
Definition: mn_def.hpp:48
Definition: mn_iterator.hpp:36