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 template <typename T>
189 refcount = new unsigned int;
192 SharedBuffer(unsigned int size)
196 data = new T[m_size];
199 refcount = new unsigned int;
200 memset(data,0,sizeof(T)*m_size);
203 SharedBuffer(const SharedBuffer &buffer)
205 //std::cout<<"SharedBuffer(const SharedBuffer &buffer)"<<std::endl;
206 m_size = buffer.m_size;
208 refcount = buffer.refcount;
211 SharedBuffer & operator=(const SharedBuffer & buffer)
213 //std::cout<<"SharedBuffer & operator=(const SharedBuffer & buffer)"<<std::endl;
217 m_size = buffer.m_size;
219 refcount = buffer.refcount;
226 SharedBuffer(const T *t, unsigned int size)
231 data = new T[m_size];
232 memcpy(data, t, m_size);
236 refcount = new unsigned int;
242 SharedBuffer(const Buffer<T> &buffer)
244 m_size = buffer.getSize();
247 data = new T[m_size];
248 memcpy(data, *buffer, buffer.getSize());
252 refcount = new unsigned int;
259 T & operator[](unsigned int i) const
264 T * operator*() const
268 unsigned int getSize() const
272 operator Buffer<T>() const
274 return Buffer<T>(data, m_size);
279 assert((*refcount) > 0);
290 unsigned int *refcount;
293 inline SharedBuffer<u8> SharedBufferFromString(const char *string)
295 SharedBuffer<u8> b((u8*)string, strlen(string)+1);