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()
36 Buffer(unsigned int size)
44 Buffer(const Buffer &buffer)
46 m_size = buffer.m_size;
49 data = new T[buffer.m_size];
50 memcpy(data, buffer.data, buffer.m_size);
55 Buffer(const T *t, unsigned int size)
61 memcpy(data, t, size);
70 Buffer& operator=(const Buffer &buffer)
75 m_size = buffer.m_size;
78 data = new T[buffer.m_size];
79 memcpy(data, buffer.data, buffer.m_size);
85 T & operator[](unsigned int i) const
93 unsigned int getSize() const
107 /************************************************
108 * !!! W A R N I N G !!! *
109 * !!! A C H T U N G !!! *
111 * This smart pointer class is NOT thread safe. *
112 * ONLY use in a single-threaded context! *
114 ************************************************/
115 template <typename T>
123 refcount = new unsigned int;
126 SharedBuffer(unsigned int size)
130 data = new T[m_size];
133 refcount = new unsigned int;
134 memset(data,0,sizeof(T)*m_size);
137 SharedBuffer(const SharedBuffer &buffer)
139 //std::cout<<"SharedBuffer(const SharedBuffer &buffer)"<<std::endl;
140 m_size = buffer.m_size;
142 refcount = buffer.refcount;
145 SharedBuffer & operator=(const SharedBuffer & buffer)
147 //std::cout<<"SharedBuffer & operator=(const SharedBuffer & buffer)"<<std::endl;
151 m_size = buffer.m_size;
153 refcount = buffer.refcount;
160 SharedBuffer(const T *t, unsigned int size)
165 data = new T[m_size];
166 memcpy(data, t, m_size);
170 refcount = new unsigned int;
176 SharedBuffer(const Buffer<T> &buffer)
178 m_size = buffer.getSize();
181 data = new T[m_size];
182 memcpy(data, *buffer, buffer.getSize());
186 refcount = new unsigned int;
193 T & operator[](unsigned int i) const
198 T * operator*() const
202 unsigned int getSize() const
206 operator Buffer<T>() const
208 return Buffer<T>(data, m_size);
213 assert((*refcount) > 0);
224 unsigned int *refcount;
227 inline SharedBuffer<u8> SharedBufferFromString(const char *string)
229 SharedBuffer<u8> b((u8*)string, strlen(string)+1);