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