3 Copyright (C) 2010-2012 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 #ifndef UTIL_POINTER_HEADER
21 #define UTIL_POINTER_HEADER
23 #include "../irrlichttypes.h"
24 #include "../debug.h" // For assert()
36 SharedPtr(SharedPtr<T> &t)
40 refcount = t.refcount;
48 SharedPtr<T> & operator=(T *t)
56 SharedPtr<T> & operator=(SharedPtr<T> &t)
59 refcount = t.refcount;
80 T & operator[](unsigned int i)
87 assert((*refcount) > 0);
100 template <typename T>
109 Buffer(unsigned int size)
117 Buffer(const Buffer &buffer)
119 m_size = buffer.m_size;
122 data = new T[buffer.m_size];
123 memcpy(data, buffer.data, buffer.m_size);
128 Buffer(const T *t, unsigned int size)
134 memcpy(data, t, size);
143 Buffer& operator=(const Buffer &buffer)
148 m_size = buffer.m_size;
151 data = new T[buffer.m_size];
152 memcpy(data, buffer.data, buffer.m_size);
158 T & operator[](unsigned int i) const
162 T * operator*() const
166 unsigned int getSize() const
180 template <typename T>
188 refcount = new unsigned int;
191 SharedBuffer(unsigned int size)
195 data = new T[m_size];
198 refcount = new unsigned int;
201 SharedBuffer(const SharedBuffer &buffer)
203 //std::cout<<"SharedBuffer(const SharedBuffer &buffer)"<<std::endl;
204 m_size = buffer.m_size;
206 refcount = buffer.refcount;
209 SharedBuffer & operator=(const SharedBuffer & buffer)
211 //std::cout<<"SharedBuffer & operator=(const SharedBuffer & buffer)"<<std::endl;
215 m_size = buffer.m_size;
217 refcount = buffer.refcount;
224 SharedBuffer(T *t, unsigned int size)
229 data = new T[m_size];
230 memcpy(data, t, m_size);
234 refcount = new unsigned int;
240 SharedBuffer(const Buffer<T> &buffer)
242 m_size = buffer.getSize();
245 data = new T[m_size];
246 memcpy(data, *buffer, buffer.getSize());
250 refcount = new unsigned int;
257 T & operator[](unsigned int i) const
262 T * operator*() const
266 unsigned int getSize() const
270 operator Buffer<T>() const
272 return Buffer<T>(data, m_size);
277 assert((*refcount) > 0);
288 unsigned int *refcount;
291 inline SharedBuffer<u8> SharedBufferFromString(const char *string)
293 SharedBuffer<u8> b((u8*)string, strlen(string)+1);