Wildvine Engine
Referencia Doxygen del codigo propio de Wildvine Engine.
Cargando...
Buscando...
Nada coincide
TWeakPointer.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#include "TSharedPointer.h"
36
37namespace
38 EU {
46 template<typename T>
48 public:
52 TWeakPointer() : ptr(nullptr), refCount(nullptr) {}
53
60 : ptr(sharedPtr.ptr), refCount(sharedPtr.refCount) {
61 }
62
69 lock() const {
70 if (refCount && *refCount > 0) {
71 return TSharedPointer<T>(ptr, refCount);
72 }
73 return TSharedPointer<T>();
74 }
75
76 // Reset
77 void
79 ptr = nullptr;
80 refCount = nullptr;
81 }
82
83 // Hacer que TSharedPointer sea un amigo para acceder a los miembros privados.
84 template<typename U>
85 friend class TSharedPointer;
86
87 private:
88 T* ptr;
89 int* refCount;
90 };
91
92 /*
93 #include "TSharedPointer.h"
94#include "TWeakPointer.h"
95
96class MyClass
97{
98public:
99 MyClass(int val) : value(val) {}
100 void display() const { std::cout << "Value: " << value << std::endl; }
101
102private:
103 int value;
104};
105
106int main()
107{
108 {
109 // Crear un TSharedPointer que gestiona un objeto MyClass
110 EU::TSharedPointer<MyClass> sp1 = EU::MakeShared<MyClass>(10);
111 sp1->display();
112
113 // Crear un TWeakPointer a partir del TSharedPointer
114 EU::TWeakPointer<MyClass> wp1(sp1);
115
116 // Intentar obtener un TSharedPointer a partir del TWeakPointer
117 EU::TSharedPointer<MyClass> sp2 = wp1.lock();
118 if (!sp2.isNull())
119 {
120 sp2->display(); // Debería mostrar el valor 10
121 }
122 else
123 {
124 std::cout << "wp1 expired." << std::endl;
125 }
126
127 // Crear un nuevo TSharedPointer y mover el puntero compartido
128 EU::TSharedPointer<MyClass> sp3 = EU::MakeShared<MyClass>(20);
129 sp3 = std::move(sp1); // Mueve la propiedad de sp1 a sp3
130
131 // El puntero compartido original (sp1) ahora está vacío
132 EU::TSharedPointer<MyClass> sp4 = wp1.lock();
133 if (sp4.isNull())
134 {
135 std::cout << "sp1 has been moved and is now null." << std::endl;
136 }
137
138 // Intentar obtener un TSharedPointer después del movimiento
139 if (sp3.isNull())
140 {
141 std::cout << "sp3 is null." << std::endl;
142 }
143 else
144 {
145 sp3->display(); // Debería mostrar el valor 20
146 }
147 } // Aquí, tanto sp2 como sp4 se destruyen y la memoria de MyClass se libera automáticamente
148
149 return 0;
150}
151 */
152}
153
Declara la API de TSharedPointer dentro del subsistema Memory.
Clase TSharedPointer para manejar la gestión de memoria compartida.
Clase TWeakPointer para observar objetos gestionados por TSharedPointer sin aumentar el recuento de r...
TSharedPointer< T > lock() const
Convertir TWeakPointer a TSharedPointer.
int * refCount
Puntero al recuento de referencias del TSharedPointer original.
TWeakPointer()
Constructor por defecto.
T * ptr
Puntero al objeto observado.
TWeakPointer(const TSharedPointer< T > &sharedPtr)
Constructor que toma un TSharedPointer.
Definition Matrix2x2.h:35