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.
22 #include "irrlichttypes.h"
23 #include "debug.h" // For assert()
25 #include <memory> // std::shared_ptr
28 template<typename T> class ConstSharedPtr {
30 ConstSharedPtr(T *ptr) : ptr(ptr) {}
31 ConstSharedPtr(const std::shared_ptr<T> &ptr) : ptr(ptr) {}
33 const T* get() const noexcept { return ptr.get(); }
34 const T& operator*() const noexcept { return *ptr.get(); }
35 const T* operator->() const noexcept { return ptr.get(); }
38 std::shared_ptr<T> ptr;
50 Buffer(unsigned int size)
60 Buffer(const Buffer &) = delete;
61 Buffer &operator=(const Buffer &) = delete;
63 Buffer(Buffer &&buffer)
65 m_size = buffer.m_size;
69 buffer.data = nullptr;
75 // Copies whole buffer
76 Buffer(const T *t, unsigned int size)
82 memcpy(data, t, size);
93 Buffer& operator=(Buffer &&buffer)
98 m_size = buffer.m_size;
102 buffer.data = nullptr;
110 void copyTo(Buffer &buffer) const
113 buffer.m_size = m_size;
115 buffer.data = new T[m_size];
116 memcpy(buffer.data, data, m_size);
118 buffer.data = nullptr;
122 T & operator[](unsigned int i) const
126 T * operator*() const
131 unsigned int getSize() const
145 /************************************************
146 * !!! W A R N I N G !!! *
148 * This smart pointer class is NOT thread safe. *
149 * ONLY use in a single-threaded context! *
151 ************************************************/
152 template <typename T>
160 refcount = new unsigned int;
163 SharedBuffer(unsigned int size)
167 data = new T[m_size];
170 refcount = new unsigned int;
171 memset(data,0,sizeof(T)*m_size);
174 SharedBuffer(const SharedBuffer &buffer)
176 m_size = buffer.m_size;
178 refcount = buffer.refcount;
181 SharedBuffer & operator=(const SharedBuffer & buffer)
186 m_size = buffer.m_size;
188 refcount = buffer.refcount;
195 SharedBuffer(const T *t, unsigned int size)
200 data = new T[m_size];
201 memcpy(data, t, m_size);
205 refcount = new unsigned int;
211 SharedBuffer(const Buffer<T> &buffer)
213 m_size = buffer.getSize();
215 data = new T[m_size];
216 memcpy(data, *buffer, buffer.getSize());
220 refcount = new unsigned int;
227 T & operator[](unsigned int i) const
232 T * operator*() const
236 unsigned int getSize() const
240 operator Buffer<T>() const
242 return Buffer<T>(data, m_size);
247 assert((*refcount) > 0);
257 unsigned int *refcount;