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()
35 Buffer(unsigned int size
)
43 Buffer(const Buffer
&buffer
)
45 m_size
= buffer
.m_size
;
48 data
= new T
[buffer
.m_size
];
49 memcpy(data
, buffer
.data
, buffer
.m_size
);
54 Buffer(const T
*t
, unsigned int size
)
60 memcpy(data
, t
, size
);
69 Buffer
& operator=(const Buffer
&buffer
)
74 m_size
= buffer
.m_size
;
77 data
= new T
[buffer
.m_size
];
78 memcpy(data
, buffer
.data
, buffer
.m_size
);
84 T
& operator[](unsigned int i
) const
92 unsigned int getSize() const
105 /************************************************
106 * !!! W A R N I N G !!! *
108 * This smart pointer class is NOT thread safe. *
109 * ONLY use in a single-threaded context! *
111 ************************************************/
112 template <typename T
>
120 refcount
= new unsigned int;
123 SharedBuffer(unsigned int size
)
127 data
= new T
[m_size
];
130 refcount
= new unsigned int;
131 memset(data
,0,sizeof(T
)*m_size
);
134 SharedBuffer(const SharedBuffer
&buffer
)
136 m_size
= buffer
.m_size
;
138 refcount
= buffer
.refcount
;
141 SharedBuffer
& operator=(const SharedBuffer
& buffer
)
146 m_size
= buffer
.m_size
;
148 refcount
= buffer
.refcount
;
155 SharedBuffer(const T
*t
, unsigned int size
)
160 data
= new T
[m_size
];
161 memcpy(data
, t
, m_size
);
165 refcount
= new unsigned int;
171 SharedBuffer(const Buffer
<T
> &buffer
)
173 m_size
= buffer
.getSize();
175 data
= new T
[m_size
];
176 memcpy(data
, *buffer
, buffer
.getSize());
180 refcount
= new unsigned int;
187 T
& operator[](unsigned int i
) const
192 T
* operator*() const
196 unsigned int getSize() const
200 operator Buffer
<T
>() const
202 return Buffer
<T
>(data
, m_size
);
207 assert((*refcount
) > 0);
217 unsigned int *refcount
;