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 #ifndef UTIL_POINTER_HEADER
21 #define UTIL_POINTER_HEADER
23 #include "../irrlichttypes.h"
24 #include "../debug.h" // For assert()
37 SharedPtr(SharedPtr<T> &t)
41 refcount = t.refcount;
49 SharedPtr<T> & operator=(T *t)
57 SharedPtr<T> & operator=(SharedPtr<T> &t)
60 refcount = t.refcount;
81 T & operator[](unsigned int i)
88 assert((*refcount) > 0);
101 template <typename T>
110 Buffer(unsigned int size)
118 Buffer(const Buffer &buffer)
120 m_size = buffer.m_size;
123 data = new T[buffer.m_size];
124 memcpy(data, buffer.data, buffer.m_size);
129 Buffer(const T *t, unsigned int size)
135 memcpy(data, t, size);
144 Buffer& operator=(const Buffer &buffer)
149 m_size = buffer.m_size;
152 data = new T[buffer.m_size];
153 memcpy(data, buffer.data, buffer.m_size);
159 T & operator[](unsigned int i) const
163 T * operator*() const
167 unsigned int getSize() const
181 /************************************************
182 * !!! W A R N I N G !!! *
183 * !!! A C H T U N G !!! *
185 * This smart pointer class is NOT thread safe. *
186 * ONLY use in a single-threaded context! *
188 ************************************************/
189 template <typename T>
197 refcount = new unsigned int;
200 SharedBuffer(unsigned int size)
204 data = new T[m_size];
207 refcount = new unsigned int;
208 memset(data,0,sizeof(T)*m_size);
211 SharedBuffer(const SharedBuffer &buffer)
213 //std::cout<<"SharedBuffer(const SharedBuffer &buffer)"<<std::endl;
214 m_size = buffer.m_size;
216 refcount = buffer.refcount;
219 SharedBuffer & operator=(const SharedBuffer & buffer)
221 //std::cout<<"SharedBuffer & operator=(const SharedBuffer & buffer)"<<std::endl;
225 m_size = buffer.m_size;
227 refcount = buffer.refcount;
234 SharedBuffer(const T *t, unsigned int size)
239 data = new T[m_size];
240 memcpy(data, t, m_size);
244 refcount = new unsigned int;
250 SharedBuffer(const Buffer<T> &buffer)
252 m_size = buffer.getSize();
255 data = new T[m_size];
256 memcpy(data, *buffer, buffer.getSize());
260 refcount = new unsigned int;
267 T & operator[](unsigned int i) const
272 T * operator*() const
276 unsigned int getSize() const
280 operator Buffer<T>() const
282 return Buffer<T>(data, m_size);
287 assert((*refcount) > 0);
298 unsigned int *refcount;
301 inline SharedBuffer<u8> SharedBufferFromString(const char *string)
303 SharedBuffer<u8> b((u8*)string, strlen(string)+1);