]> git.lizzy.rs Git - minetest.git/blob - src/util/pointer.h
Don't allow banning in singleplayer
[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 #pragma once
21
22 #include "irrlichttypes.h"
23 #include "debug.h" // For assert()
24 #include <cstring>
25 #include <memory> // std::shared_ptr
26
27
28 template<typename T> class ConstSharedPtr {
29 public:
30         ConstSharedPtr(T *ptr) : ptr(ptr) {}
31         ConstSharedPtr(const std::shared_ptr<T> &ptr) : ptr(ptr) {}
32
33         const T* get() const noexcept { return ptr.get(); }
34         const T& operator*() const noexcept { return *ptr.get(); }
35         const T* operator->() const noexcept { return ptr.get(); }
36
37 private:
38         std::shared_ptr<T> ptr;
39 };
40
41 template <typename T>
42 class Buffer
43 {
44 public:
45         Buffer()
46         {
47                 m_size = 0;
48                 data = NULL;
49         }
50         Buffer(unsigned int size)
51         {
52                 m_size = size;
53                 if(size != 0)
54                         data = new T[size];
55                 else
56                         data = NULL;
57         }
58
59         // Disable class copy
60         Buffer(const Buffer &) = delete;
61         Buffer &operator=(const Buffer &) = delete;
62
63         Buffer(Buffer &&buffer)
64         {
65                 m_size = buffer.m_size;
66                 if(m_size != 0)
67                 {
68                         data = buffer.data;
69                         buffer.data = nullptr;
70                         buffer.m_size = 0;
71                 }
72                 else
73                         data = nullptr;
74         }
75         // Copies whole buffer
76         Buffer(const T *t, unsigned int size)
77         {
78                 m_size = size;
79                 if(size != 0)
80                 {
81                         data = new T[size];
82                         memcpy(data, t, size);
83                 }
84                 else
85                         data = NULL;
86         }
87
88         ~Buffer()
89         {
90                 drop();
91         }
92
93         Buffer& operator=(Buffer &&buffer)
94         {
95                 if(this == &buffer)
96                         return *this;
97                 drop();
98                 m_size = buffer.m_size;
99                 if(m_size != 0)
100                 {
101                         data = buffer.data;
102                         buffer.data = nullptr;
103                         buffer.m_size = 0;
104                 }
105                 else
106                         data = nullptr;
107                 return *this;
108         }
109
110         void copyTo(Buffer &buffer) const
111         {
112                 buffer.drop();
113                 buffer.m_size = m_size;
114                 if (m_size != 0) {
115                         buffer.data = new T[m_size];
116                         memcpy(buffer.data, data, m_size);
117                 } else {
118                         buffer.data = nullptr;
119                 }
120         }
121
122         T & operator[](unsigned int i) const
123         {
124                 return data[i];
125         }
126         T * operator*() const
127         {
128                 return data;
129         }
130
131         unsigned int getSize() const
132         {
133                 return m_size;
134         }
135
136 private:
137         void drop()
138         {
139                 delete[] data;
140         }
141         T *data;
142         unsigned int m_size;
143 };
144
145 /************************************************
146  *           !!!  W A R N I N G  !!!            *
147  *                                              *
148  * This smart pointer class is NOT thread safe. *
149  * ONLY use in a single-threaded context!       *
150  *                                              *
151  ************************************************/
152 template <typename T>
153 class SharedBuffer
154 {
155 public:
156         SharedBuffer()
157         {
158                 m_size = 0;
159                 data = NULL;
160                 refcount = new unsigned int;
161                 (*refcount) = 1;
162         }
163         SharedBuffer(unsigned int size)
164         {
165                 m_size = size;
166                 if(m_size != 0)
167                         data = new T[m_size];
168                 else
169                         data = NULL;
170                 refcount = new unsigned int;
171                 memset(data,0,sizeof(T)*m_size);
172                 (*refcount) = 1;
173         }
174         SharedBuffer(const SharedBuffer &buffer)
175         {
176                 m_size = buffer.m_size;
177                 data = buffer.data;
178                 refcount = buffer.refcount;
179                 (*refcount)++;
180         }
181         SharedBuffer & operator=(const SharedBuffer & buffer)
182         {
183                 if(this == &buffer)
184                         return *this;
185                 drop();
186                 m_size = buffer.m_size;
187                 data = buffer.data;
188                 refcount = buffer.refcount;
189                 (*refcount)++;
190                 return *this;
191         }
192         /*
193                 Copies whole buffer
194         */
195         SharedBuffer(const T *t, unsigned int size)
196         {
197                 m_size = size;
198                 if(m_size != 0)
199                 {
200                         data = new T[m_size];
201                         memcpy(data, t, m_size);
202                 }
203                 else
204                         data = NULL;
205                 refcount = new unsigned int;
206                 (*refcount) = 1;
207         }
208         /*
209                 Copies whole buffer
210         */
211         SharedBuffer(const Buffer<T> &buffer)
212         {
213                 m_size = buffer.getSize();
214                 if (m_size != 0) {
215                                 data = new T[m_size];
216                                 memcpy(data, *buffer, buffer.getSize());
217                 }
218                 else
219                         data = NULL;
220                 refcount = new unsigned int;
221                 (*refcount) = 1;
222         }
223         ~SharedBuffer()
224         {
225                 drop();
226         }
227         T & operator[](unsigned int i) const
228         {
229                 assert(i < m_size);
230                 return data[i];
231         }
232         T * operator*() const
233         {
234                 return data;
235         }
236         unsigned int getSize() const
237         {
238                 return m_size;
239         }
240         operator Buffer<T>() const
241         {
242                 return Buffer<T>(data, m_size);
243         }
244 private:
245         void drop()
246         {
247                 assert((*refcount) > 0);
248                 (*refcount)--;
249                 if(*refcount == 0)
250                 {
251                         delete[] data;
252                         delete refcount;
253                 }
254         }
255         T *data;
256         unsigned int m_size;
257         unsigned int *refcount;
258 };