]> git.lizzy.rs Git - minetest.git/blob - src/util/pointer.h
Change Minetest-c55 to Minetest
[minetest.git] / src / util / pointer.h
1 /*
2 Minetest
3 Copyright (C) 2010-2012 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #ifndef UTIL_POINTER_HEADER
21 #define UTIL_POINTER_HEADER
22
23 #include "../irrlichttypes.h"
24 #include "../debug.h" // For assert()
25 #include <cstring>
26
27 template <typename T>
28 class SharedPtr
29 {
30 public:
31         SharedPtr(T *t=NULL)
32         {
33                 refcount = new int;
34                 *refcount = 1;
35                 ptr = t;
36         }
37         SharedPtr(SharedPtr<T> &t)
38         {
39                 //*this = t;
40                 drop();
41                 refcount = t.refcount;
42                 (*refcount)++;
43                 ptr = t.ptr;
44         }
45         ~SharedPtr()
46         {
47                 drop();
48         }
49         SharedPtr<T> & operator=(T *t)
50         {
51                 drop();
52                 refcount = new int;
53                 *refcount = 1;
54                 ptr = t;
55                 return *this;
56         }
57         SharedPtr<T> & operator=(SharedPtr<T> &t)
58         {
59                 drop();
60                 refcount = t.refcount;
61                 (*refcount)++;
62                 ptr = t.ptr;
63                 return *this;
64         }
65         T* operator->()
66         {
67                 return ptr;
68         }
69         T & operator*()
70         {
71                 return *ptr;
72         }
73         bool operator!=(T *t)
74         {
75                 return ptr != t;
76         }
77         bool operator==(T *t)
78         {
79                 return ptr == t;
80         }
81         T & operator[](unsigned int i)
82         {
83                 return ptr[i];
84         }
85 private:
86         void drop()
87         {
88                 assert((*refcount) > 0);
89                 (*refcount)--;
90                 if(*refcount == 0)
91                 {
92                         delete refcount;
93                         if(ptr != NULL)
94                                 delete ptr;
95                 }
96         }
97         T *ptr;
98         int *refcount;
99 };
100
101 template <typename T>
102 class Buffer
103 {
104 public:
105         Buffer()
106         {
107                 m_size = 0;
108                 data = NULL;
109         }
110         Buffer(unsigned int size)
111         {
112                 m_size = size;
113                 if(size != 0)
114                         data = new T[size];
115                 else
116                         data = NULL;
117         }
118         Buffer(const Buffer &buffer)
119         {
120                 m_size = buffer.m_size;
121                 if(m_size != 0)
122                 {
123                         data = new T[buffer.m_size];
124                         memcpy(data, buffer.data, buffer.m_size);
125                 }
126                 else
127                         data = NULL;
128         }
129         Buffer(const T *t, unsigned int size)
130         {
131                 m_size = size;
132                 if(size != 0)
133                 {
134                         data = new T[size];
135                         memcpy(data, t, size);
136                 }
137                 else
138                         data = NULL;
139         }
140         ~Buffer()
141         {
142                 drop();
143         }
144         Buffer& operator=(const Buffer &buffer)
145         {
146                 if(this == &buffer)
147                         return *this;
148                 drop();
149                 m_size = buffer.m_size;
150                 if(m_size != 0)
151                 {
152                         data = new T[buffer.m_size];
153                         memcpy(data, buffer.data, buffer.m_size);
154                 }
155                 else
156                         data = NULL;
157                 return *this;
158         }
159         T & operator[](unsigned int i) const
160         {
161                 return data[i];
162         }
163         T * operator*() const
164         {
165                 return data;
166         }
167         unsigned int getSize() const
168         {
169                 return m_size;
170         }
171 private:
172         void drop()
173         {
174                 if(data)
175                         delete[] data;
176         }
177         T *data;
178         unsigned int m_size;
179 };
180
181 template <typename T>
182 class SharedBuffer
183 {
184 public:
185         SharedBuffer()
186         {
187                 m_size = 0;
188                 data = NULL;
189                 refcount = new unsigned int;
190                 (*refcount) = 1;
191         }
192         SharedBuffer(unsigned int size)
193         {
194                 m_size = size;
195                 if(m_size != 0)
196                         data = new T[m_size];
197                 else
198                         data = NULL;
199                 refcount = new unsigned int;
200                 (*refcount) = 1;
201         }
202         SharedBuffer(const SharedBuffer &buffer)
203         {
204                 //std::cout<<"SharedBuffer(const SharedBuffer &buffer)"<<std::endl;
205                 m_size = buffer.m_size;
206                 data = buffer.data;
207                 refcount = buffer.refcount;
208                 (*refcount)++;
209         }
210         SharedBuffer & operator=(const SharedBuffer & buffer)
211         {
212                 //std::cout<<"SharedBuffer & operator=(const SharedBuffer & buffer)"<<std::endl;
213                 if(this == &buffer)
214                         return *this;
215                 drop();
216                 m_size = buffer.m_size;
217                 data = buffer.data;
218                 refcount = buffer.refcount;
219                 (*refcount)++;
220                 return *this;
221         }
222         /*
223                 Copies whole buffer
224         */
225         SharedBuffer(const T *t, unsigned int size)
226         {
227                 m_size = size;
228                 if(m_size != 0)
229                 {
230                         data = new T[m_size];
231                         memcpy(data, t, m_size);
232                 }
233                 else
234                         data = NULL;
235                 refcount = new unsigned int;
236                 (*refcount) = 1;
237         }
238         /*
239                 Copies whole buffer
240         */
241         SharedBuffer(const Buffer<T> &buffer)
242         {
243                 m_size = buffer.getSize();
244                 if(m_size != 0)
245                 {
246                         data = new T[m_size];
247                         memcpy(data, *buffer, buffer.getSize());
248                 }
249                 else
250                         data = NULL;
251                 refcount = new unsigned int;
252                 (*refcount) = 1;
253         }
254         ~SharedBuffer()
255         {
256                 drop();
257         }
258         T & operator[](unsigned int i) const
259         {
260                 //assert(i < m_size)
261                 return data[i];
262         }
263         T * operator*() const
264         {
265                 return data;
266         }
267         unsigned int getSize() const
268         {
269                 return m_size;
270         }
271         operator Buffer<T>() const
272         {
273                 return Buffer<T>(data, m_size);
274         }
275 private:
276         void drop()
277         {
278                 assert((*refcount) > 0);
279                 (*refcount)--;
280                 if(*refcount == 0)
281                 {
282                         if(data)
283                                 delete[] data;
284                         delete refcount;
285                 }
286         }
287         T *data;
288         unsigned int m_size;
289         unsigned int *refcount;
290 };
291
292 inline SharedBuffer<u8> SharedBufferFromString(const char *string)
293 {
294         SharedBuffer<u8> b((u8*)string, strlen(string)+1);
295         return b;
296 }
297
298 #endif
299