Luanti 5.10.0-dev
 
Loading...
Searching...
No Matches
pointer.h
Go to the documentation of this file.
1// Luanti
2// SPDX-License-Identifier: LGPL-2.1-or-later
3// Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5#pragma once
6
7#include "irrlichttypes.h"
8#include "debug.h" // For assert()
9#include <cstring>
10#include <memory> // std::shared_ptr
11#include <string_view>
12
13
14template<typename T> class ConstSharedPtr {
15public:
17 ConstSharedPtr(const std::shared_ptr<T> &ptr) : ptr(ptr) {}
18
19 const T* get() const noexcept { return ptr.get(); }
20 const T& operator*() const noexcept { return *ptr.get(); }
21 const T* operator->() const noexcept { return ptr.get(); }
22
23private:
24 std::shared_ptr<T> ptr;
25};
26
27template <typename T>
28class Buffer
29{
30public:
32 {
33 m_size = 0;
34 data = nullptr;
35 }
36 Buffer(unsigned int size)
37 {
38 m_size = size;
39 if(size != 0)
40 data = new T[size];
41 else
42 data = nullptr;
43 }
44
45 // Disable class copy
46 Buffer(const Buffer &) = delete;
47 Buffer &operator=(const Buffer &) = delete;
48
49 Buffer(Buffer &&buffer)
50 {
51 m_size = buffer.m_size;
52 if(m_size != 0)
53 {
54 data = buffer.data;
55 buffer.data = nullptr;
56 buffer.m_size = 0;
57 }
58 else
59 data = nullptr;
60 }
61 // Copies whole buffer
62 Buffer(const T *t, unsigned int size)
63 {
64 m_size = size;
65 if(size != 0)
66 {
67 data = new T[size];
68 memcpy(data, t, size);
69 }
70 else
71 data = nullptr;
72 }
73
75 {
76 drop();
77 }
78
80 {
81 if(this == &buffer)
82 return *this;
83 drop();
84 m_size = buffer.m_size;
85 if(m_size != 0)
86 {
87 data = buffer.data;
88 buffer.data = nullptr;
89 buffer.m_size = 0;
90 }
91 else
92 data = nullptr;
93 return *this;
94 }
95
96 void copyTo(Buffer &buffer) const
97 {
98 buffer.drop();
99 buffer.m_size = m_size;
100 if (m_size != 0) {
101 buffer.data = new T[m_size];
102 memcpy(buffer.data, data, m_size);
103 } else {
104 buffer.data = nullptr;
105 }
106 }
107
108 T & operator[](unsigned int i) const
109 {
110 return data[i];
111 }
112 T * operator*() const
113 {
114 return data;
115 }
116
117 unsigned int getSize() const
118 {
119 return m_size;
120 }
121
122 operator std::string_view() const
123 {
124 if (!data)
125 return std::string_view();
126 return std::string_view(reinterpret_cast<char*>(data), m_size);
127 }
128
129private:
130 void drop()
131 {
132 delete[] data;
133 }
135 unsigned int m_size;
136};
137
138/************************************************
139 * !!! W A R N I N G !!! *
140 * *
141 * This smart pointer class is NOT thread safe. *
142 * ONLY use in a single-threaded context! *
143 * *
144 ************************************************/
145template <typename T>
147{
148public:
150 {
151 m_size = 0;
152 data = NULL;
153 refcount = new unsigned int;
154 (*refcount) = 1;
155 }
156 SharedBuffer(unsigned int size)
157 {
158 m_size = size;
159 if(m_size != 0)
160 data = new T[m_size];
161 else
162 data = nullptr;
163 refcount = new unsigned int;
164 memset(data,0,sizeof(T)*m_size);
165 (*refcount) = 1;
166 }
168 {
169 m_size = buffer.m_size;
170 data = buffer.data;
171 refcount = buffer.refcount;
172 (*refcount)++;
173 }
175 {
176 if(this == &buffer)
177 return *this;
178 drop();
179 m_size = buffer.m_size;
180 data = buffer.data;
181 refcount = buffer.refcount;
182 (*refcount)++;
183 return *this;
184 }
185 /*
186 Copies whole buffer
187 */
188 SharedBuffer(const T *t, unsigned int size)
189 {
190 m_size = size;
191 if(m_size != 0)
192 {
193 data = new T[m_size];
194 memcpy(data, t, m_size);
195 }
196 else
197 data = nullptr;
198 refcount = new unsigned int;
199 (*refcount) = 1;
200 }
201 /*
202 Copies whole buffer
203 */
204 SharedBuffer(const Buffer<T> &buffer)
205 {
206 m_size = buffer.getSize();
207 if (m_size != 0) {
208 data = new T[m_size];
209 memcpy(data, *buffer, buffer.getSize());
210 }
211 else
212 data = nullptr;
213 refcount = new unsigned int;
214 (*refcount) = 1;
215 }
217 {
218 drop();
219 }
220 T & operator[](unsigned int i) const
221 {
222 assert(i < m_size);
223 return data[i];
224 }
225 T * operator*() const
226 {
227 return data;
228 }
229 unsigned int getSize() const
230 {
231 return m_size;
232 }
233 operator Buffer<T>() const
234 {
235 return Buffer<T>(data, m_size);
236 }
237private:
238 void drop()
239 {
240 assert((*refcount) > 0);
241 (*refcount)--;
242 if(*refcount == 0)
243 {
244 delete[] data;
245 delete refcount;
246 }
247 }
249 unsigned int m_size;
250 unsigned int *refcount;
251};
252
253// This class is not thread-safe!
255public:
257 virtual ~IntrusiveReferenceCounted() = default;
258 void grab() noexcept { ++m_refcount; }
259 void drop() noexcept { if (--m_refcount == 0) delete this; }
260
262private:
263 u32 m_refcount = 1;
264};
#define DISABLE_CLASS_COPY(C)
Definition basic_macros.h:26
Definition pointer.h:29
T * data
Definition pointer.h:134
Buffer(const T *t, unsigned int size)
Definition pointer.h:62
Buffer & operator=(Buffer &&buffer)
Definition pointer.h:79
Buffer(unsigned int size)
Definition pointer.h:36
T & operator[](unsigned int i) const
Definition pointer.h:108
unsigned int m_size
Definition pointer.h:135
void copyTo(Buffer &buffer) const
Definition pointer.h:96
~Buffer()
Definition pointer.h:74
Buffer()
Definition pointer.h:31
T * operator*() const
Definition pointer.h:112
Buffer(const Buffer &)=delete
Buffer(Buffer &&buffer)
Definition pointer.h:49
Buffer & operator=(const Buffer &)=delete
void drop()
Definition pointer.h:130
unsigned int getSize() const
Definition pointer.h:117
Definition pointer.h:14
const T * operator->() const noexcept
Definition pointer.h:21
const T & operator*() const noexcept
Definition pointer.h:20
ConstSharedPtr(T *ptr)
Definition pointer.h:16
std::shared_ptr< T > ptr
Definition pointer.h:24
const T * get() const noexcept
Definition pointer.h:19
ConstSharedPtr(const std::shared_ptr< T > &ptr)
Definition pointer.h:17
Definition pointer.h:254
virtual ~IntrusiveReferenceCounted()=default
void grab() noexcept
Definition pointer.h:258
u32 m_refcount
Definition pointer.h:263
void drop() noexcept
Definition pointer.h:259
IntrusiveReferenceCounted()=default
Definition pointer.h:147
T * operator*() const
Definition pointer.h:225
SharedBuffer(unsigned int size)
Definition pointer.h:156
unsigned int * refcount
Definition pointer.h:250
SharedBuffer(const Buffer< T > &buffer)
Definition pointer.h:204
unsigned int getSize() const
Definition pointer.h:229
SharedBuffer(const SharedBuffer &buffer)
Definition pointer.h:167
SharedBuffer & operator=(const SharedBuffer &buffer)
Definition pointer.h:174
unsigned int m_size
Definition pointer.h:249
SharedBuffer(const T *t, unsigned int size)
Definition pointer.h:188
T * data
Definition pointer.h:248
~SharedBuffer()
Definition pointer.h:216
T & operator[](unsigned int i) const
Definition pointer.h:220
SharedBuffer()
Definition pointer.h:149
void drop()
Definition pointer.h:238