incrementaltp: respect physics overrides
[waspsaliva.git] / src / util / pointer.h
blobd29ec87399b0ada78530401e61c2b55dfeb1a124
1 /*
2 Minetest
3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
20 #pragma once
22 #include "irrlichttypes.h"
23 #include "debug.h" // For assert()
24 #include <cstring>
26 template <typename T>
27 class Buffer
29 public:
30 Buffer()
32 m_size = 0;
33 data = NULL;
35 Buffer(unsigned int size)
37 m_size = size;
38 if(size != 0)
39 data = new T[size];
40 else
41 data = NULL;
43 Buffer(const Buffer &buffer)
45 m_size = buffer.m_size;
46 if(m_size != 0)
48 data = new T[buffer.m_size];
49 memcpy(data, buffer.data, buffer.m_size);
51 else
52 data = NULL;
54 Buffer(const T *t, unsigned int size)
56 m_size = size;
57 if(size != 0)
59 data = new T[size];
60 memcpy(data, t, size);
62 else
63 data = NULL;
65 ~Buffer()
67 drop();
69 Buffer& operator=(const Buffer &buffer)
71 if(this == &buffer)
72 return *this;
73 drop();
74 m_size = buffer.m_size;
75 if(m_size != 0)
77 data = new T[buffer.m_size];
78 memcpy(data, buffer.data, buffer.m_size);
80 else
81 data = NULL;
82 return *this;
84 T & operator[](unsigned int i) const
86 return data[i];
88 T * operator*() const
90 return data;
92 unsigned int getSize() const
94 return m_size;
96 private:
97 void drop()
99 delete[] data;
101 T *data;
102 unsigned int m_size;
105 /************************************************
106 * !!! W A R N I N G !!! *
108 * This smart pointer class is NOT thread safe. *
109 * ONLY use in a single-threaded context! *
111 ************************************************/
112 template <typename T>
113 class SharedBuffer
115 public:
116 SharedBuffer()
118 m_size = 0;
119 data = NULL;
120 refcount = new unsigned int;
121 (*refcount) = 1;
123 SharedBuffer(unsigned int size)
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;
134 SharedBuffer(const SharedBuffer &buffer)
136 m_size = buffer.m_size;
137 data = buffer.data;
138 refcount = buffer.refcount;
139 (*refcount)++;
141 SharedBuffer & operator=(const SharedBuffer & buffer)
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;
153 Copies whole buffer
155 SharedBuffer(const T *t, unsigned int size)
157 m_size = size;
158 if(m_size != 0)
160 data = new T[m_size];
161 memcpy(data, t, m_size);
163 else
164 data = NULL;
165 refcount = new unsigned int;
166 (*refcount) = 1;
169 Copies whole buffer
171 SharedBuffer(const Buffer<T> &buffer)
173 m_size = buffer.getSize();
174 if (m_size != 0) {
175 data = new T[m_size];
176 memcpy(data, *buffer, buffer.getSize());
178 else
179 data = NULL;
180 refcount = new unsigned int;
181 (*refcount) = 1;
183 ~SharedBuffer()
185 drop();
187 T & operator[](unsigned int i) const
189 assert(i < m_size);
190 return data[i];
192 T * operator*() const
194 return data;
196 unsigned int getSize() const
198 return m_size;
200 operator Buffer<T>() const
202 return Buffer<T>(data, m_size);
204 private:
205 void drop()
207 assert((*refcount) > 0);
208 (*refcount)--;
209 if(*refcount == 0)
211 delete[] data;
212 delete refcount;
215 T *data;
216 unsigned int m_size;
217 unsigned int *refcount;