]> git.lizzy.rs Git - dragonfireclient.git/blob - src/util/pointer.h
Increase performance of getLight() by at least 2x
[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 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                 memset(data,0,sizeof(T)*m_size);
201                 (*refcount) = 1;
202         }
203         SharedBuffer(const SharedBuffer &buffer)
204         {
205                 //std::cout<<"SharedBuffer(const SharedBuffer &buffer)"<<std::endl;
206                 m_size = buffer.m_size;
207                 data = buffer.data;
208                 refcount = buffer.refcount;
209                 (*refcount)++;
210         }
211         SharedBuffer & operator=(const SharedBuffer & buffer)
212         {
213                 //std::cout<<"SharedBuffer & operator=(const SharedBuffer & buffer)"<<std::endl;
214                 if(this == &buffer)
215                         return *this;
216                 drop();
217                 m_size = buffer.m_size;
218                 data = buffer.data;
219                 refcount = buffer.refcount;
220                 (*refcount)++;
221                 return *this;
222         }
223         /*
224                 Copies whole buffer
225         */
226         SharedBuffer(const T *t, unsigned int size)
227         {
228                 m_size = size;
229                 if(m_size != 0)
230                 {
231                         data = new T[m_size];
232                         memcpy(data, t, m_size);
233                 }
234                 else
235                         data = NULL;
236                 refcount = new unsigned int;
237                 (*refcount) = 1;
238         }
239         /*
240                 Copies whole buffer
241         */
242         SharedBuffer(const Buffer<T> &buffer)
243         {
244                 m_size = buffer.getSize();
245                 if(m_size != 0)
246                 {
247                         data = new T[m_size];
248                         memcpy(data, *buffer, buffer.getSize());
249                 }
250                 else
251                         data = NULL;
252                 refcount = new unsigned int;
253                 (*refcount) = 1;
254         }
255         ~SharedBuffer()
256         {
257                 drop();
258         }
259         T & operator[](unsigned int i) const
260         {
261                 assert(i < m_size);
262                 return data[i];
263         }
264         T * operator*() const
265         {
266                 return data;
267         }
268         unsigned int getSize() const
269         {
270                 return m_size;
271         }
272         operator Buffer<T>() const
273         {
274                 return Buffer<T>(data, m_size);
275         }
276 private:
277         void drop()
278         {
279                 assert((*refcount) > 0);
280                 (*refcount)--;
281                 if(*refcount == 0)
282                 {
283                         if(data)
284                                 delete[] data;
285                         delete refcount;
286                 }
287         }
288         T *data;
289         unsigned int m_size;
290         unsigned int *refcount;
291 };
292
293 inline SharedBuffer<u8> SharedBufferFromString(const char *string)
294 {
295         SharedBuffer<u8> b((u8*)string, strlen(string)+1);
296         return b;
297 }
298
299 #endif
300