Wildvine Engine
Referencia Doxygen del codigo propio de Wildvine Engine.
Cargando...
Buscando...
Nada coincide
TSharedPointer.h
Ir a la documentación de este archivo.
1
6/*
7 * MIT License
8 *
9 * Copyright (c) 2025 Roberto Charreton
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included in all
19 * copies or substantial portions of the Software.
20 *
21 * In addition, any project or software that uses this library or class must include
22 * the following acknowledgment in the credits:
23 *
24 * "This project uses software developed by Roberto Charreton and Attribute Overload."
25 *
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
29 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
31 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 * SOFTWARE.
33*/
34#pragma once
35
36namespace EU {
44 template<typename T>
46 {
47 public:
53 TSharedPointer() : ptr(nullptr), refCount(nullptr) {}
54
60 explicit TSharedPointer(T* rawPtr) : ptr(rawPtr), refCount(new int(1)) {}
61
68 TSharedPointer(T* rawPtr, int* existingRefCount) : ptr(rawPtr), refCount(existingRefCount)
69 {
70 if (refCount)
71 {
72 ++(*refCount);
73 }
74 }
75
85 {
86 if (refCount)
87 {
88 ++(*refCount);
89 }
90 }
91
100 TSharedPointer(TSharedPointer<T>&& other) noexcept : ptr(other.ptr), refCount(other.refCount)
101 {
102 other.ptr = nullptr;
103 other.refCount = nullptr;
104 }
105
116 {
117 if (this != &other)
118 {
119 // Disminuir el recuento de referencias del objeto actual
120 if (refCount && --(*refCount) == 0)
121 {
122 delete ptr;
123 delete refCount;
124 }
125 // Copiar datos del otro puntero compartido
126 ptr = other.ptr;
127 refCount = other.refCount;
128 if (refCount)
129 {
130 ++(*refCount);
131 }
132 }
133 return *this;
134 }
135
146 {
147 if (this != &other)
148 {
149 // Liberar el objeto actual
150 if (refCount && --(*refCount) == 0)
151 {
152 delete ptr;
153 delete refCount;
154 }
155 // Transferir los datos del otro puntero compartido
156 ptr = other.ptr;
157 refCount = other.refCount;
158 other.ptr = nullptr;
159 other.refCount = nullptr;
160 }
161 return *this;
162 }
163
171 {
172 if (refCount && --(*refCount) == 0)
173 {
174 delete ptr;
175 delete refCount;
176 }
177 }
178
184 T& operator*() const { return *ptr; }
185
191 T* operator->() const { return ptr; }
192
193 // Agregar una función para comprobar si el puntero es válido
194 operator bool() const {
195 return ptr != nullptr;
196 }
197
203 T* get() const { return ptr; }
204
210 bool isNull() const { return ptr == nullptr; }
211
212
213 public:
214 T* ptr;
215 int* refCount;
216
224 void swap(TSharedPointer<T>& other) noexcept
225 {
226 T* tempPtr = other.ptr;
227 int* tempRefCount = other.refCount;
228
229 other.ptr = this->ptr;
230 other.refCount = this->refCount;
231
232 this->ptr = tempPtr;
233 this->refCount = tempRefCount;
234 }
235
241 void reset(T* newPtr = nullptr)
242 {
243 // Disminuir el recuento de referencias del objeto actual
244 if (refCount && --(*refCount) == 0)
245 {
246 delete ptr;
247 delete refCount;
248 }
249
250 // Si newPtr es nullptr, asignar nullptr al puntero y recuento de referencias
251 if (newPtr == nullptr)
252 {
253 ptr = nullptr;
254 refCount = nullptr;
255 }
256 else
257 {
258 // Asignar nuevo objeto y manejar el recuento de referencias
259 ptr = newPtr;
260 refCount = new int(1);
261 }
262 }
263
264 // Método de conversión para hacer cast dinámico
265 template<typename U>
267 // Intenta convertir el puntero de tipo T a U
268 U* castedPtr = dynamic_cast<U*>(ptr);
269 if (castedPtr) {
270 // Si la conversión es exitosa, devuelve un nuevo TSharedPointer<U>
271 return TSharedPointer<U>(castedPtr, refCount);
272 }
273 else {
274 // Si falla la conversión, devuelve un TSharedPointer<U> nulo
275 return TSharedPointer<U>();
276 }
277 }
278 };
279
280
289 template<typename T, typename... Args>
291 {
292 return TSharedPointer<T>(new T(args...));
293 }
294}
295
296
297
Clase TSharedPointer para manejar la gestión de memoria compartida.
bool isNull() const
Comprobar si el puntero es nulo.
~TSharedPointer()
Destructor.
TSharedPointer< T > & operator=(TSharedPointer< T > &&other) noexcept
Operador de asignación de movimiento.
T * ptr
Puntero al objeto gestionado.
void swap(TSharedPointer< T > &other) noexcept
Método swap.
TSharedPointer< U > dynamic_pointer_cast() const
T * get() const
Obtener el puntero crudo.
TSharedPointer(const TSharedPointer< T > &other)
Constructor de copia.
int * refCount
Puntero al recuento de referencias.
void reset(T *newPtr=nullptr)
Libera el objeto actual y opcionalmente asigna un nuevo objeto.
TSharedPointer(TSharedPointer< T > &&other) noexcept
Constructor de movimiento.
T * operator->() const
Operador de acceso a miembros.
TSharedPointer()
Constructor por defecto.
T & operator*() const
Operador de desreferenciación.
TSharedPointer(T *rawPtr, int *existingRefCount)
Constructor desde un puntero crudo y un recuento de referencias.
TSharedPointer(T *rawPtr)
Constructor que toma un puntero crudo.
TSharedPointer< T > & operator=(const TSharedPointer< T > &other)
Operador de asignación de copia.
Definition Matrix2x2.h:35
TSharedPointer< T > MakeShared(Args... args)
Función de utilidad para crear un TSharedPointer.