Wildvine Engine
Referencia Doxygen del codigo propio de Wildvine Engine.
Cargando...
Buscando...
Nada coincide
TStaticPtr.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
35namespace EU {
43 template<typename T>
44 class TStaticPtr {
45 public:
51 TStaticPtr() = default;
52
58 explicit TStaticPtr(T* rawPtr) {
59 if (instance != nullptr) {
60 delete instance;
61 }
62 instance = rawPtr;
63 }
64
71 if (instance != nullptr) {
72 delete instance;
73 instance = nullptr;
74 }
75 }
76
82 static T*
83 get() {
84 return instance;
85 }
86
92 static bool
94 return instance == nullptr;
95 }
96
104 static void
105 reset(T* rawPtr = nullptr) {
106 if (instance != nullptr) {
107 delete instance;
108 }
109 instance = rawPtr;
110 }
111
112 private:
113 static T* instance;
114 };
115
116 /*
117 // Inicializar el puntero estático
118 template<typename T>
119 T* TStaticPtr<T>::instance = nullptr;
120
121 // Ejemplo de uso de TStaticPtr
122 class MyClass
123 {
124 public:
125 MyClass(int value) : value(value)
126 {
127 std::cout << "MyClass constructor: " << value << std::endl;
128 }
129
130 ~MyClass()
131 {
132 std::cout << "MyClass destructor: " << value << std::endl;
133 }
134
135 void display() const
136 {
137 std::cout << "Value: " << value << std::endl;
138 }
139
140 private:
141 int value;
142 };
143
144 int main()
145 {
146 {
147 // Crear un TStaticPtr usando un puntero crudo
148 TStaticPtr<MyClass>::reset(new MyClass(10));
149 TStaticPtr<MyClass>::get()->display(); // Output: Value: 10
150
151 // Comprobar si el puntero no es nulo
152 if (!TStaticPtr<MyClass>::isNull())
153 {
154 std::cout << "TStaticPtr is not null" << std::endl;
155 }
156
157 // Reiniciar TStaticPtr con un nuevo objeto
158 TStaticPtr<MyClass>::reset(new MyClass(20));
159 TStaticPtr<MyClass>::get()->display(); // Output: Value: 20
160
161 // Reiniciar con nullptr
162 TStaticPtr<MyClass>::reset();
163 if (TStaticPtr<MyClass>::isNull())
164 {
165 std::cout << "TStaticPtr is null after reset" << std::endl;
166 }
167 }
168
169 return 0;
170 }
171 */
172}
173
174
Clase TStaticPtr para manejo de un puntero estático.
Definition TStaticPtr.h:44
TStaticPtr()=default
Inicializa el puntero estático al objeto.
static void reset(T *rawPtr=nullptr)
Reiniciar el puntero estático con un nuevo objeto.
Definition TStaticPtr.h:105
TStaticPtr(T *rawPtr)
Constructor que toma un puntero crudo.
Definition TStaticPtr.h:58
static T * get()
Obtener el puntero crudo.
Definition TStaticPtr.h:83
static T * instance
Puntero estático al objeto gestionado.
Definition TStaticPtr.h:113
static bool isNull()
Verificar si el puntero es nulo.
Definition TStaticPtr.h:93
~TStaticPtr()
Destructor.
Definition TStaticPtr.h:70
Definition Matrix2x2.h:35