Expose getPointedThing to Lua
[oweals/minetest.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 Buffer
29 {
30 public:
31         Buffer()
32         {
33                 m_size = 0;
34                 data = NULL;
35         }
36         Buffer(unsigned int size)
37         {
38                 m_size = size;
39                 if(size != 0)
40                         data = new T[size];
41                 else
42                         data = NULL;
43         }
44         Buffer(const Buffer &buffer)
45         {
46                 m_size = buffer.m_size;
47                 if(m_size != 0)
48                 {
49                         data = new T[buffer.m_size];
50                         memcpy(data, buffer.data, buffer.m_size);
51                 }
52                 else
53                         data = NULL;
54         }
55         Buffer(const T *t, unsigned int size)
56         {
57                 m_size = size;
58                 if(size != 0)
59                 {
60                         data = new T[size];
61                         memcpy(data, t, size);
62                 }
63                 else
64                         data = NULL;
65         }
66         ~Buffer()
67         {
68                 drop();
69         }
70         Buffer& operator=(const Buffer &buffer)
71         {
72                 if(this == &buffer)
73                         return *this;
74                 drop();
75                 m_size = buffer.m_size;
76                 if(m_size != 0)
77                 {
78                         data = new T[buffer.m_size];
79                         memcpy(data, buffer.data, buffer.m_size);
80                 }
81                 else
82                         data = NULL;
83                 return *this;
84         }
85         T & operator[](unsigned int i) const
86         {
87                 return data[i];
88         }
89         T * operator*() const
90         {
91                 return data;
92         }
93         unsigned int getSize() const
94         {
95                 return m_size;
96         }
97 private:
98         void drop()
99         {
100                 delete[] data;
101         }
102         T *data;
103         unsigned int m_size;
104 };
105
106 /************************************************
107  *           !!!  W A R N I N G  !!!            *
108  *           !!!  A C H T U N G  !!!            *
109  *                                              *
110  * This smart pointer class is NOT thread safe. *
111  * ONLY use in a single-threaded context!       *
112  *                                              *
113  ************************************************/
114 template <typename T>
115 class SharedBuffer
116 {
117 public:
118         SharedBuffer()
119         {
120                 m_size = 0;
121                 data = NULL;
122                 refcount = new unsigned int;
123                 (*refcount) = 1;
124         }
125         SharedBuffer(unsigned int size)
126         {
127                 m_size = size;
128                 if(m_size != 0)
129                         data = new T[m_size];
130                 else
131                         data = NULL;
132                 refcount = new unsigned int;
133                 memset(data,0,sizeof(T)*m_size);
134                 (*refcount) = 1;
135         }
136         SharedBuffer(const SharedBuffer &buffer)
137         {
138                 //std::cout<<"SharedBuffer(const SharedBuffer &buffer)"<<std::endl;
139                 m_size = buffer.m_size;
140                 data = buffer.data;
141                 refcount = buffer.refcount;
142                 (*refcount)++;
143         }
144         SharedBuffer & operator=(const SharedBuffer & buffer)
145         {
146                 //std::cout<<"SharedBuffer & operator=(const SharedBuffer & buffer)"<<std::endl;
147                 if(this == &buffer)
148                         return *this;
149                 drop();
150                 m_size = buffer.m_size;
151                 data = buffer.data;
152                 refcount = buffer.refcount;
153                 (*refcount)++;
154                 return *this;
155         }
156         /*
157                 Copies whole buffer
158         */
159         SharedBuffer(const T *t, unsigned int size)
160         {
161                 m_size = size;
162                 if(m_size != 0)
163                 {
164                         data = new T[m_size];
165                         memcpy(data, t, m_size);
166                 }
167                 else
168                         data = NULL;
169                 refcount = new unsigned int;
170                 (*refcount) = 1;
171         }
172         /*
173                 Copies whole buffer
174         */
175         SharedBuffer(const Buffer<T> &buffer)
176         {
177                 m_size = buffer.getSize();
178                 if(m_size != 0)
179                 {
180                         data = new T[m_size];
181                         memcpy(data, *buffer, buffer.getSize());
182                 }
183                 else
184                         data = NULL;
185                 refcount = new unsigned int;
186                 (*refcount) = 1;
187         }
188         ~SharedBuffer()
189         {
190                 drop();
191         }
192         T & operator[](unsigned int i) const
193         {
194                 assert(i < m_size);
195                 return data[i];
196         }
197         T * operator*() const
198         {
199                 return data;
200         }
201         unsigned int getSize() const
202         {
203                 return m_size;
204         }
205         operator Buffer<T>() const
206         {
207                 return Buffer<T>(data, m_size);
208         }
209 private:
210         void drop()
211         {
212                 assert((*refcount) > 0);
213                 (*refcount)--;
214                 if(*refcount == 0)
215                 {
216                         delete[] data;
217                         delete refcount;
218                 }
219         }
220         T *data;
221         unsigned int m_size;
222         unsigned int *refcount;
223 };
224
225 inline SharedBuffer<u8> SharedBufferFromString(const char *string)
226 {
227         SharedBuffer<u8> b((u8*)string, strlen(string)+1);
228         return b;
229 }
230
231 #endif
232