Luanti 5.11.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
46 // Disable class copy
47 Buffer(const Buffer &) = delete;
48 Buffer &operator=(const Buffer &) = delete;
49
50 Buffer(Buffer &&buffer)
51 {
52 m_size = buffer.m_size;
53 if (m_size != 0) {
54 data = buffer.data;
55 buffer.data = nullptr;
56 buffer.m_size = 0;
57 } else {
58 data = nullptr;
59 }
60 }
61 // Copies whole buffer
62 Buffer(const T *t, unsigned int size)
63 {
64 m_size = size;
65 if (size != 0) {
66 data = new T[size];
67 memcpy(data, t, sizeof(T) * size);
68 } else {
69 data = nullptr;
70 }
71 }
72
74 {
75 drop();
76 }
77
79 {
80 if (this == &buffer) {
81 return *this;
82 }
83 drop();
84 m_size = buffer.m_size;
85 if (m_size != 0) {
86 data = buffer.data;
87 buffer.data = nullptr;
88 buffer.m_size = 0;
89 } else {
90 data = nullptr;
91 }
92 return *this;
93 }
94
95 void copyTo(Buffer &buffer) const
96 {
97 buffer.drop();
98 buffer.m_size = m_size;
99 if (m_size != 0) {
100 buffer.data = new T[m_size];
101 memcpy(buffer.data, data, sizeof(T) * m_size);
102 } else {
103 buffer.data = nullptr;
104 }
105 }
106
107 T & operator[](unsigned int i) const
108 {
109 return data[i];
110 }
111 T * operator*() const
112 {
113 return data;
114 }
115
116 unsigned int getSize() const
117 {
118 return m_size;
119 }
120
121 operator std::string_view() const
122 {
123 if (!data) {
124 return std::string_view();
125 }
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 }
164
165 refcount = new unsigned int;
166 memset(data, 0, sizeof(T) * m_size);
167 (*refcount) = 1;
168 }
170 {
171 m_size = buffer.m_size;
172 data = buffer.data;
173 refcount = buffer.refcount;
174 (*refcount)++;
175 }
177 {
178 if (this == &buffer) {
179 return *this;
180 }
181
182 drop();
183 m_size = buffer.m_size;
184 data = buffer.data;
185 refcount = buffer.refcount;
186 (*refcount)++;
187 return *this;
188 }
190 SharedBuffer(const T *t, unsigned int size)
191 {
192 m_size = size;
193 if (m_size != 0) {
194 data = new T[m_size];
195 memcpy(data, t, sizeof(T) * m_size);
196 } else {
197 data = nullptr;
198 }
199 refcount = new unsigned int;
200 (*refcount) = 1;
201 }
203 SharedBuffer(const Buffer<T> &buffer) : SharedBuffer(*buffer, buffer.getSize())
204 {
205 }
207 {
208 drop();
209 }
210 T & operator[](unsigned int i) const
211 {
212 assert(i < m_size);
213 return data[i];
214 }
215 T * operator*() const
216 {
217 return data;
218 }
219 unsigned int getSize() const
220 {
221 return m_size;
222 }
223 operator Buffer<T>() const
224 {
225 return Buffer<T>(data, m_size);
226 }
227private:
228 void drop()
229 {
230 assert((*refcount) > 0);
231 (*refcount)--;
232 if (*refcount == 0) {
233 delete[] data;
234 delete refcount;
235 }
236 }
238 unsigned int m_size;
239 unsigned int *refcount;
240};
241
242// This class is not thread-safe!
244public:
246 virtual ~IntrusiveReferenceCounted() = default;
247 void grab() noexcept { ++m_refcount; }
248 void drop() noexcept { if (--m_refcount == 0) delete this; }
249
251private:
252 u32 m_refcount = 1;
253};
#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:78
Buffer(unsigned int size)
Definition pointer.h:36
T & operator[](unsigned int i) const
Definition pointer.h:107
unsigned int m_size
Definition pointer.h:135
void copyTo(Buffer &buffer) const
Definition pointer.h:95
~Buffer()
Definition pointer.h:73
Buffer()
Definition pointer.h:31
T * operator*() const
Definition pointer.h:111
Buffer(const Buffer &)=delete
Buffer(Buffer &&buffer)
Definition pointer.h:50
Buffer & operator=(const Buffer &)=delete
void drop()
Definition pointer.h:130
unsigned int getSize() const
Definition pointer.h:116
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:243
virtual ~IntrusiveReferenceCounted()=default
void grab() noexcept
Definition pointer.h:247
u32 m_refcount
Definition pointer.h:252
void drop() noexcept
Definition pointer.h:248
IntrusiveReferenceCounted()=default
Definition pointer.h:147
T * operator*() const
Definition pointer.h:215
SharedBuffer(unsigned int size)
Definition pointer.h:156
unsigned int * refcount
Definition pointer.h:239
SharedBuffer(const Buffer< T > &buffer)
Copies whole buffer.
Definition pointer.h:203
unsigned int getSize() const
Definition pointer.h:219
SharedBuffer(const SharedBuffer &buffer)
Definition pointer.h:169
SharedBuffer & operator=(const SharedBuffer &buffer)
Definition pointer.h:176
unsigned int m_size
Definition pointer.h:238
SharedBuffer(const T *t, unsigned int size)
Copies whole buffer.
Definition pointer.h:190
T * data
Definition pointer.h:237
~SharedBuffer()
Definition pointer.h:206
T & operator[](unsigned int i) const
Definition pointer.h:210
SharedBuffer()
Definition pointer.h:149
void drop()
Definition pointer.h:228