Gobelijn API documentation  - generated for commit a0cbea7
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros Pages
buffer.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of the gobelijn software.
3  * Gobelijn is free software: you can redistribute it and/or modify it
4  * under the terms of the GNU General Public License as published by the
5  * Free Software Foundation, either version 3 of the License, or any later
6  * version. Gobelijn is distributed in the hope that it will be useful, but
7  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
8  * or FITNESS FOR A PARTICULAR PURPOSE.
9  * See the GNU General Public License for details. You should have received
10  * a copy of the GNU General Public License along with the software. If not,
11  * see <http://www.gnu.org/licenses/>.
12  *
13  * Copyright 2016, Jan Broeckhove.
14  */
20 #include "buffer.h"
21 
22 namespace Raii {
23 
24 Buffer::Buffer(const Buffer& other) : m_bufSize(other.m_bufSize), m_data(new char[m_bufSize])
25 {
26  // Copies the given buffer's memory block.
27  for (size_t i = 0; i < m_bufSize; i++)
28  m_data[i] = other.m_data[i];
29 }
30 
31 Buffer::Buffer(Buffer&& other) noexcept
32  : m_bufSize(other.m_bufSize), m_data(other.m_data)
33 {
34  // Turn the moved buffer into an empty buffer, because we must leave
35  // the argument in a valid (but otherwise indeterminate) state.
36  other.m_bufSize = 0;
37  other.m_data = nullptr;
38 }
39 
41 {
42  if (this == &other)
43  // Never perform self-assignment.
44  return *this;
45 
46  // Delete this buffer's old block of memory. If we were to simply
47  // overwrite this pointer, then we'd get a memory leak.
48  delete[] m_data;
49 
50  // Set this buffer's size.
51  m_bufSize = other.m_bufSize;
52  m_data = new char[m_bufSize];
53  // Allocate a new (uninitialized) block of memory.
54  for (size_t i = 0; i < m_bufSize; i++)
55  // Copies the given buffer's memory block contents to this
56  // buffer's memory block.
57  m_data[i] = other.m_data[i];
58 
59  // Return a reference to the '*this' value, to support
60  // chaining assignments, i.e. 'a = b = c;'
61  return *this;
62 }
63 
64 Buffer& Buffer::operator=(Buffer&& other) noexcept
65 {
66  if (this == &other)
67  // Never perform self-assignment.
68  return *this;
69 
70  // Delete this buffer's old block of memory. If we were to simply
71  // overwrite this pointer, then we'd get a memory leak.
72  delete[] m_data;
73 
74  // Set this buffer's size.
75  m_bufSize = other.m_bufSize;
76  // Sets this buffer's data pointer to the moved buffer's data pointer.
77  m_data = other.m_data;
78 
79  // Turn the moved buffer into an empty buffer, because we must leave
80  // the argument in a valid (but otherwise indeterminate) state.
81  other.m_bufSize = 0;
82  other.m_data = nullptr;
83 
84  // Return a reference to the '*this' value, to support
85  // chaining assignments, i.e. 'a = b = c;'
86  return *this;
87 }
88 } // 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
Buffer & operator=(const Buffer &other)
Creates a copy of the given buffer, and assigns that to this buffer.
Definition: buffer.cpp:40
A buffer of bytes, with a fixed size.
Definition: buffer.h:30
RAII Buffer class.
Buffer()
Creates a buffer class that manages an empty block of memory.
Definition: buffer.h:34