Minetest  5.4.0
pointer.h
Go to the documentation of this file.
1 /*
2 Minetest
3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
14 
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19 
20 #pragma once
21 
22 #include "irrlichttypes.h"
23 #include "debug.h" // For assert()
24 #include <cstring>
25 
26 template <typename T>
27 class Buffer
28 {
29 public:
31  {
32  m_size = 0;
33  data = NULL;
34  }
35  Buffer(unsigned int size)
36  {
37  m_size = size;
38  if(size != 0)
39  data = new T[size];
40  else
41  data = NULL;
42  }
43  Buffer(const Buffer &buffer)
44  {
45  m_size = buffer.m_size;
46  if(m_size != 0)
47  {
48  data = new T[buffer.m_size];
49  memcpy(data, buffer.data, buffer.m_size);
50  }
51  else
52  data = NULL;
53  }
54  Buffer(const T *t, unsigned int size)
55  {
56  m_size = size;
57  if(size != 0)
58  {
59  data = new T[size];
60  memcpy(data, t, size);
61  }
62  else
63  data = NULL;
64  }
66  {
67  drop();
68  }
69  Buffer& operator=(const Buffer &buffer)
70  {
71  if(this == &buffer)
72  return *this;
73  drop();
74  m_size = buffer.m_size;
75  if(m_size != 0)
76  {
77  data = new T[buffer.m_size];
78  memcpy(data, buffer.data, buffer.m_size);
79  }
80  else
81  data = NULL;
82  return *this;
83  }
84  T & operator[](unsigned int i) const
85  {
86  return data[i];
87  }
88  T * operator*() const
89  {
90  return data;
91  }
92  unsigned int getSize() const
93  {
94  return m_size;
95  }
96 private:
97  void drop()
98  {
99  delete[] data;
100  }
101  T *data;
102  unsigned int m_size;
103 };
104 
105 /************************************************
106  * !!! W A R N I N G !!! *
107  * *
108  * This smart pointer class is NOT thread safe. *
109  * ONLY use in a single-threaded context! *
110  * *
111  ************************************************/
112 template <typename T>
114 {
115 public:
117  {
118  m_size = 0;
119  data = NULL;
120  refcount = new unsigned int;
121  (*refcount) = 1;
122  }
123  SharedBuffer(unsigned int size)
124  {
125  m_size = size;
126  if(m_size != 0)
127  data = new T[m_size];
128  else
129  data = NULL;
130  refcount = new unsigned int;
131  memset(data,0,sizeof(T)*m_size);
132  (*refcount) = 1;
133  }
134  SharedBuffer(const SharedBuffer &buffer)
135  {
136  m_size = buffer.m_size;
137  data = buffer.data;
138  refcount = buffer.refcount;
139  (*refcount)++;
140  }
142  {
143  if(this == &buffer)
144  return *this;
145  drop();
146  m_size = buffer.m_size;
147  data = buffer.data;
148  refcount = buffer.refcount;
149  (*refcount)++;
150  return *this;
151  }
152  /*
153  Copies whole buffer
154  */
155  SharedBuffer(const T *t, unsigned int size)
156  {
157  m_size = size;
158  if(m_size != 0)
159  {
160  data = new T[m_size];
161  memcpy(data, t, m_size);
162  }
163  else
164  data = NULL;
165  refcount = new unsigned int;
166  (*refcount) = 1;
167  }
168  /*
169  Copies whole buffer
170  */
171  SharedBuffer(const Buffer<T> &buffer)
172  {
173  m_size = buffer.getSize();
174  if (m_size != 0) {
175  data = new T[m_size];
176  memcpy(data, *buffer, buffer.getSize());
177  }
178  else
179  data = NULL;
180  refcount = new unsigned int;
181  (*refcount) = 1;
182  }
184  {
185  drop();
186  }
187  T & operator[](unsigned int i) const
188  {
189  assert(i < m_size);
190  return data[i];
191  }
192  T * operator*() const
193  {
194  return data;
195  }
196  unsigned int getSize() const
197  {
198  return m_size;
199  }
200  operator Buffer<T>() const
201  {
202  return Buffer<T>(data, m_size);
203  }
204 private:
205  void drop()
206  {
207  assert((*refcount) > 0);
208  (*refcount)--;
209  if(*refcount == 0)
210  {
211  delete[] data;
212  delete refcount;
213  }
214  }
215  T *data;
216  unsigned int m_size;
217  unsigned int *refcount;
218 };
Definition: pointer.h:28
T & operator[](unsigned int i) const
Definition: pointer.h:84
T * data
Definition: pointer.h:101
Buffer & operator=(const Buffer &buffer)
Definition: pointer.h:69
Buffer(const T *t, unsigned int size)
Definition: pointer.h:54
T * operator*() const
Definition: pointer.h:88
Buffer(const Buffer &buffer)
Definition: pointer.h:43
Buffer(unsigned int size)
Definition: pointer.h:35
unsigned int m_size
Definition: pointer.h:102
~Buffer()
Definition: pointer.h:65
Buffer()
Definition: pointer.h:30
void drop()
Definition: pointer.h:97
unsigned int getSize() const
Definition: pointer.h:92
Definition: pointer.h:114
T * operator*() const
Definition: pointer.h:192
SharedBuffer(unsigned int size)
Definition: pointer.h:123
T & operator[](unsigned int i) const
Definition: pointer.h:187
unsigned int * refcount
Definition: pointer.h:217
SharedBuffer(const Buffer< T > &buffer)
Definition: pointer.h:171
SharedBuffer & operator=(const SharedBuffer &buffer)
Definition: pointer.h:141
unsigned int getSize() const
Definition: pointer.h:196
SharedBuffer(const SharedBuffer &buffer)
Definition: pointer.h:134
unsigned int m_size
Definition: pointer.h:216
SharedBuffer(const T *t, unsigned int size)
Definition: pointer.h:155
T * data
Definition: pointer.h:215
~SharedBuffer()
Definition: pointer.h:183
SharedBuffer()
Definition: pointer.h:116
void drop()
Definition: pointer.h:205