]> git.lizzy.rs Git - dragonfireclient.git/blob - src/util/pointer.h
Add function to get server info.
[dragonfireclient.git] / src / util / pointer.h
1 /*
2 Minetest
3 Copyright (C) 2010-2013 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 /************************************************
182  *           !!!  W A R N I N G  !!!            *
183  *           !!!  A C H T U N G  !!!            *
184  *                                              *
185  * This smart pointer class is NOT thread safe. *
186  * ONLY use in a single-threaded context!       *
187  *                                              *
188  ************************************************/
189 template <typename T>
190 class SharedBuffer
191 {
192 public:
193         SharedBuffer()
194         {
195                 m_size = 0;
196                 data = NULL;
197                 refcount = new unsigned int;
198                 (*refcount) = 1;
199         }
200         SharedBuffer(unsigned int size)
201         {
202                 m_size = size;
203                 if(m_size != 0)
204                         data = new T[m_size];
205                 else
206                         data = NULL;
207                 refcount = new unsigned int;
208                 memset(data,0,sizeof(T)*m_size);
209                 (*refcount) = 1;
210         }
211         SharedBuffer(const SharedBuffer &buffer)
212         {
213                 //std::cout<<"SharedBuffer(const SharedBuffer &buffer)"<<std::endl;
214                 m_size = buffer.m_size;
215                 data = buffer.data;
216                 refcount = buffer.refcount;
217                 (*refcount)++;
218         }
219         SharedBuffer & operator=(const SharedBuffer & buffer)
220         {
221                 //std::cout<<"SharedBuffer & operator=(const SharedBuffer & buffer)"<<std::endl;
222                 if(this == &buffer)
223                         return *this;
224                 drop();
225                 m_size = buffer.m_size;
226                 data = buffer.data;
227                 refcount = buffer.refcount;
228                 (*refcount)++;
229                 return *this;
230         }
231         /*
232                 Copies whole buffer
233         */
234         SharedBuffer(const T *t, unsigned int size)
235         {
236                 m_size = size;
237                 if(m_size != 0)
238                 {
239                         data = new T[m_size];
240                         memcpy(data, t, m_size);
241                 }
242                 else
243                         data = NULL;
244                 refcount = new unsigned int;
245                 (*refcount) = 1;
246         }
247         /*
248                 Copies whole buffer
249         */
250         SharedBuffer(const Buffer<T> &buffer)
251         {
252                 m_size = buffer.getSize();
253                 if(m_size != 0)
254                 {
255                         data = new T[m_size];
256                         memcpy(data, *buffer, buffer.getSize());
257                 }
258                 else
259                         data = NULL;
260                 refcount = new unsigned int;
261                 (*refcount) = 1;
262         }
263         ~SharedBuffer()
264         {
265                 drop();
266         }
267         T & operator[](unsigned int i) const
268         {
269                 assert(i < m_size);
270                 return data[i];
271         }
272         T * operator*() const
273         {
274                 return data;
275         }
276         unsigned int getSize() const
277         {
278                 return m_size;
279         }
280         operator Buffer<T>() const
281         {
282                 return Buffer<T>(data, m_size);
283         }
284 private:
285         void drop()
286         {
287                 assert((*refcount) > 0);
288                 (*refcount)--;
289                 if(*refcount == 0)
290                 {
291                         if(data)
292                                 delete[] data;
293                         delete refcount;
294                 }
295         }
296         T *data;
297         unsigned int m_size;
298         unsigned int *refcount;
299 };
300
301 inline SharedBuffer<u8> SharedBufferFromString(const char *string)
302 {
303         SharedBuffer<u8> b((u8*)string, strlen(string)+1);
304         return b;
305 }
306
307 #endif
308