Gobelijn API documentation  - generated for commit a0cbea7
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros Pages
buffer.h
Go to the documentation of this file.
1 #pragma once
2 /*
3  * This file is part of the gobelijn software.
4  * Gobelijn is free software: you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the
6  * Free Software Foundation, either version 3 of the License, or any later
7  * version. Gobelijn is distributed in the hope that it will be useful, but
8  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
9  * or FITNESS FOR A PARTICULAR PURPOSE.
10  * See the GNU General Public License for details. You should have received
11  * a copy of the GNU General Public License along with the software. If not,
12  * see <http://www.gnu.org/licenses/>.
13  *
14  * Copyright 2016, 2018, Jan Broeckhove.
15  */
21 #include <cstddef>
22 
23 namespace Raii {
24 
30 class Buffer
31 {
32 public:
34  Buffer() : m_bufSize(0), m_data(nullptr) {}
35 
37  explicit Buffer(size_t bufferSize) : m_bufSize(bufferSize), m_data(new char[bufferSize]()) {}
38 
40  Buffer(const Buffer& other);
41 
43  Buffer(Buffer&& other) noexcept;
44 
46  Buffer& operator=(const Buffer& other);
47 
49  Buffer& operator=(Buffer&& other) noexcept;
50 
52  ~Buffer() { delete[] m_data; }
53 
55  size_t GetSize() const { return m_bufSize; }
56 
58  char& operator[](size_t i) { return m_data[i]; }
59 
61  const char& operator[](size_t i) const { return m_data[i]; }
62 
63 private:
65  size_t m_bufSize;
66 
68  char* m_data;
69 };
70 
71 } // namespace Raii
size_t m_bufSize
Stores the size of this buffer's memory block.
Definition: buffer.h:65
char * m_data
Stores a pointer to this buffer's memory block.
Definition: buffer.h:68
size_t GetSize() const
Gets the buffer's size, in bytes.
Definition: buffer.h:55
Buffer(size_t bufferSize)
Allocates a new buffer, which is the given number of bytes in size.
Definition: buffer.h:37
A buffer of bytes, with a fixed size.
Definition: buffer.h:30
const char & operator[](size_t i) const
Indexed access. Returns reference to a constant char.
Definition: buffer.h:61
Buffer()
Creates a buffer class that manages an empty block of memory.
Definition: buffer.h:34
char & operator[](size_t i)
Indexed access. Returns reference to mutable char.
Definition: buffer.h:58