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
106 /************************************************
107 * !!! W A R N I N G !!! *
108 * !!! A C H T U N G !!! *
110 * This smart pointer class is NOT thread safe. *
111 * ONLY use in a single-threaded context! *
113 ************************************************/
114 template <typename T>
122 refcount = new unsigned int;
125 SharedBuffer(unsigned int size)
129 data = new T[m_size];
132 refcount = new unsigned int;
133 memset(data,0,sizeof(T)*m_size);
136 SharedBuffer(const SharedBuffer &buffer)
138 //std::cout<<"SharedBuffer(const SharedBuffer &buffer)"<<std::endl;
139 m_size = buffer.m_size;
141 refcount = buffer.refcount;
144 SharedBuffer & operator=(const SharedBuffer & buffer)
146 //std::cout<<"SharedBuffer & operator=(const SharedBuffer & buffer)"<<std::endl;
150 m_size = buffer.m_size;
152 refcount = buffer.refcount;
159 SharedBuffer(const T *t, unsigned int size)
164 data = new T[m_size];
165 memcpy(data, t, m_size);
169 refcount = new unsigned int;
175 SharedBuffer(const Buffer<T> &buffer)
177 m_size = buffer.getSize();
180 data = new T[m_size];
181 memcpy(data, *buffer, buffer.getSize());
185 refcount = new unsigned int;
192 T & operator[](unsigned int i) const
197 T * operator*() const
201 unsigned int getSize() const
205 operator Buffer<T>() const
207 return Buffer<T>(data, m_size);
212 assert((*refcount) > 0);
222 unsigned int *refcount;
225 inline SharedBuffer<u8> SharedBufferFromString(const char *string)
227 SharedBuffer<u8> b((u8*)string, strlen(string)+1);