VR-Engage  2.2
Loading...
Searching...
No Matches
baseClass.h
Go to the documentation of this file.
1/*******************************************************************************
2** Copyright (c) 2025 MAK Technologies, Inc.
3** All rights reserved.
4*******************************************************************************/
5
6#pragma once
7
8//! \file baseClass.h
9//! \brief Provides base classes for shared pointer managed objects with factory creation support
10
11#include "vreUtil/factory.h"
12#include "vreUtil/assert.h"
13
14#include <memory>
15#include <map>
16
17namespace makVre
18{
19#if 0
20template <class T>
21class DtSafeObjectPtr;
22
23template <class T>
24class DtSafePtrObject
25{
26public:
27 DtSafePtrObject()
28 : mySharedSafePtr()
29 {
30 mySharedSafePtr = std::make_shared<T*>(this);
31 }
32
33 virtual ~DtSafePtrObject()
34 {
35 *mySharedSafePtr.get() = nullptr;
36 }
37
38protected:
39 std::shared_ptr<std::atomic<T*>> mySharedSafePtr;
40
41 friend class DtSafeObjectPtr<T>;
42};
43
44template <class T>
45class DtSafeObjectPtr
46{
47 std::weak_ptr<std::atomic<T*>> myPtrObjPtr;
48
49public:
50 DtSafeObjectPtr() {}
51 DtSafeObjectPtr(T* obj)
52 {
53 myPtrObjPtr = obj->mySharedSafePtr;;
54 }
55 virtual ~DtSafeObjectPtr() {}
56
57 DtSafeObjectPtr& operator = (T* obj)
58 {
59 myPtrObjPtr = obj->mySharedSafePtr;
60 }
61
62 bool valid() const
63 {
64 return !myPtrObjPtr.expired() && bool(*myPtrObjPtr.lock().get());
65 }
66
67 T* operator-> () const
68 {
69 DtASSERT(!myPtrObjPtr.expired(), "Dereferencing invalid object ref");
70 return *myPtrObjPtr.lock().get();
71 };
72
73 T& operator* () const
74 {
75 DtASSERT(!myPtrObjPtr.expired(), "Dereferencing invalid object ref");
76 return *myPtrObjPtr.lock().get();
77 };
78
79 operator T* () const
80 {
81 DtASSERT(!myPtrObjPtr.expired(), "Dereferencing invalid object ref");
82 return myPtrObjPtr.lock().get();
83 };
84};
85#endif
86
87//! \brief Base class for shared_ptr-managed objects
88//!
89//! This class provides a foundation for objects that need to be managed by
90//! shared pointers. It includes self-referencing mechanisms, type information,
91//! and naming functionality.
93{
94public:
95 //! \brief Raw pointer type alias
96 using Ptr = DtSpObject*;
97
98 //! \brief Shared pointer type alias
99 using Sptr = std::shared_ptr<DtSpObject>;
100
101 //! \brief Weak pointer type alias
102 using Wptr = std::weak_ptr<DtSpObject>;
103
104 //! \brief Default constructor
106
107 //! \brief Virtual destructor
108 virtual ~DtSpObject() {}
109
110 //! \brief Gets a shared pointer to this object
111 //!
112 //! If the weak reference to self is expired, creates a new shared pointer;
113 //! otherwise returns the existing shared pointer
114 //!
115 //! \return A shared pointer to this object
116 Sptr self() { return mySelf.expired() ? (mySelf = Sptr(this)).lock() : mySelf.lock(); }
117
118 //! \brief Safely casts a base shared pointer to a derived type
119 //!
120 //! \param p The base shared pointer to cast
121 //! \return A shared pointer to the derived type, or null if the cast fails
122 template <class AS_T>
123 static std::shared_ptr<AS_T> cast(Sptr p)
124 {
125 return std::dynamic_pointer_cast<AS_T>(p);
126 }
127
128 //! \brief Performs an unsafe static cast of a shared pointer
129 //!
130 //! Use only when you are certain the object is of the target type
131 //!
132 //! \param p The base shared pointer to cast
133 //! \return A shared pointer to the target type
134 template <class AS_T>
135 static std::shared_ptr<AS_T> unsafe_cast(Sptr p)
136 {
137 return std::static_pointer_cast<AS_T>(p);
138 }
139
140 //! \brief Gets the type of this object
141 //!
142 //! \return A string representing the type of this object
143 virtual std::string type() const = 0;
144
145 //! \brief Sets the name of this object
146 //!
147 //! \param name The new name for the object
148 virtual void setName(const std::string& name) { myName = name; }
149
150 //! \brief Gets the name of this object
151 //!
152 //! \return The name of this object
153 virtual std::string name() const { return myName; }
154
155protected:
156 //! \brief The name of this object
157 std::string myName;
158
159private:
160 //! \brief Weak pointer to self for shared_ptr management
162};
163
164//! \brief Subclass template for shared_ptr-managed objects
165//!
166//! Provides Super, Sptr, Wptr aliases, type function overrides, and
167//! a factory creation interface. This template enables the creation
168//! of objects through a factory pattern, allowing for runtime customization
169//! of object creation.
170//!
171//! \tparam SELF_T The derived class type
172//! \tparam SUPER_T The base class type, defaults to DtSpObject
173template <class SELF_T, class SUPER_T = DtSpObject>
174class DtSpSubClass : public SUPER_T
175{
176public:
177 //! \brief Super class type alias
178 using Super = SUPER_T;
179
180 //! \brief Raw pointer type alias
181 using Ptr = SELF_T*;
182
183 //! \brief Shared pointer type alias
184 using Sptr = std::shared_ptr<SELF_T>;
185
186 //! \brief Weak pointer type alias
187 using Wptr = std::weak_ptr<SELF_T>;
188
189 //! \brief Default constructor
191
192 //! \brief Virtual destructor
193 virtual ~DtSpSubClass() {}
194
195 //! \brief Gets a shared pointer to this object of the derived type
196 //!
197 //! \return A shared pointer to this object cast to the derived type
198 Sptr self() { return std::static_pointer_cast<SELF_T>(Super::self()); }
199
200 //! \brief Gets the static type name of this class
201 //!
202 //! \return A string representing the type name
203 static std::string Type() { return typeid(SELF_T).name(); }
204
205 //! \brief Gets the runtime type of this object
206 //!
207 //! \return A string representing the type of this object
208 std::string type() const override { return typeid(SELF_T).name(); }
209
210 //-------------------------------------------------
211 // Object Creator Interface
212
213 //! \brief Creates an instance of this class or its registered extension
214 //!
215 //! This method allows for extension of base classes. Use CLASS::create()
216 //! instead of new when you want to allow custom extensions to replace what
217 //! class is actually realized. By default, CLASS::create() creates a new
218 //! instance of CLASS. To override with a derived extension class, call
219 //! CLASS::registerCreator<CUSTOM>() with the new custom class.
220 //!
221 //! The custom class should inherit from CLASS.
222 //!
223 //! The custom class will then be instantiated wherever CLASS::create()
224 //! is used. Use cast<CUSTOM>(shared_ptr) for convenient dynamic cast to
225 //! custom class type if needing to access custom extended class interface.
226 //!
227 //! \return A shared pointer to the newly created instance
228 static std::shared_ptr<SELF_T> create()
229 {
230 if (!creator())
231 {
232 LOG_FATAL("UTIL") << SELF_T::Type() << "::create called with no creator class registered." << std::endl;
233 DtASSERT(false, "create() called with no creator class registered. See log.");
234 }
235 return creator()->create()->self();
236 }
237
238 //! \brief Registers a custom extended class to be created instead of this type
239 //!
240 //! \tparam CUSTOM_T The custom class type to register as the creator
241 template <class CUSTOM_T>
242 static void registerCreator()
243 {
244 creator() = std::make_shared<DtCreator<SELF_T, CUSTOM_T>>();
245 }
246
247protected:
248 //! \brief Gets the reference to the creator object
249 //!
250 //! \return A reference to the shared pointer containing the creator
251 static std::shared_ptr<DtBaseCreator<SELF_T>>& creator()
252 {
253 static std::shared_ptr<DtBaseCreator<SELF_T>> creator_sp;
254 return creator_sp;
255 }
256
257 // Versioned creator interface
258 //-----------------------------
259public:
260 //! \brief Creates an instance of this class for a specific version
261 //!
262 //! This allows for version-specific implementations to be selected at runtime
263 //!
264 //! \param version The version number to create
265 //! \return A shared pointer to the newly created instance
266 static std::shared_ptr<SELF_T> createVersion(unsigned int version)
267 {
268 if (!creatorVersion(version))
269 {
270 LOG_FATAL("UTIL") << SELF_T::Type() << "::createVersion(" << version
271 << ") called with no creator class registered." << std::endl;
272 DtASSERT(false, "create() called with no creator class registered. See log.");
273 }
274 return creatorVersion(version)->create()->self();
275 }
276
277 //! \brief Registers a custom class for a specific version
278 //!
279 //! \tparam CUSTOM_T The custom class type to register
280 //! \param version The version number to associate with this creator
281 template <class CUSTOM_T>
282 static void registerCreatorVersion(unsigned int version)
283 {
284 creatorVersion(version) = std::make_shared<DtCreator<SELF_T, CUSTOM_T>>();
285 }
286
287protected:
288 //! \brief Gets the reference to the creator object for a specific version
289 //!
290 //! \param version The version number to get the creator for
291 //! \return A reference to the shared pointer containing the creator
292 static std::shared_ptr<DtBaseCreator<SELF_T>>& creatorVersion(unsigned int version)
293 {
294 static std::map<unsigned int, std::shared_ptr<DtBaseCreator<SELF_T>>> creatorMap_sp;
295 return creatorMap_sp[version];
296 }
297 //-------------------------------------------------
298};
299
300} // namespace makVre
Provides assertion macros and functions for error detection and handling.
#define DtASSERT(exp, description)
Primary assertion macro for runtime validation.
Definition assert.h:53
virtual void setName(const std::string &name)
Sets the name of this object.
Definition baseClass.h:148
static std::shared_ptr< AS_T > cast(Sptr p)
Safely casts a base shared pointer to a derived type.
Definition baseClass.h:123
DtSpObject()
Default constructor.
Definition baseClass.h:105
std::string myName
The name of this object.
Definition baseClass.h:157
Sptr self()
Gets a shared pointer to this object.
Definition baseClass.h:116
Wptr mySelf
Weak pointer to self for shared_ptr management.
Definition baseClass.h:161
virtual std::string name() const
Gets the name of this object.
Definition baseClass.h:153
virtual ~DtSpObject()
Virtual destructor.
Definition baseClass.h:108
std::shared_ptr< DtSpObject > Sptr
Shared pointer type alias.
Definition baseClass.h:99
static std::shared_ptr< AS_T > unsafe_cast(Sptr p)
Performs an unsafe static cast of a shared pointer.
Definition baseClass.h:135
DtSpObject * Ptr
Raw pointer type alias.
Definition baseClass.h:96
std::weak_ptr< DtSpObject > Wptr
Weak pointer type alias.
Definition baseClass.h:102
virtual std::string type() const =0
Gets the type of this object.
std::string type() const override
Gets the runtime type of this object.
Definition baseClass.h:208
static std::shared_ptr< DtBaseCreator< SELF_T > > & creatorVersion(unsigned int version)
Gets the reference to the creator object for a specific version.
Definition baseClass.h:292
static void registerCreatorVersion(unsigned int version)
Registers a custom class for a specific version.
Definition baseClass.h:282
static std::string Type()
Gets the static type name of this class.
Definition baseClass.h:203
virtual ~DtSpSubClass()
Virtual destructor.
Definition baseClass.h:193
Sptr self()
Gets a shared pointer to this object of the derived type.
Definition baseClass.h:198
std::weak_ptr< SELF_T > Wptr
Weak pointer type alias.
Definition baseClass.h:187
static std::shared_ptr< DtBaseCreator< SELF_T > > & creator()
Gets the reference to the creator object.
Definition baseClass.h:251
SELF_T * Ptr
Raw pointer type alias.
Definition baseClass.h:181
static std::shared_ptr< SELF_T > createVersion(unsigned int version)
Creates an instance of this class for a specific version.
Definition baseClass.h:266
static void registerCreator()
Registers a custom extended class to be created instead of this type.
Definition baseClass.h:242
static std::shared_ptr< SELF_T > create()
Creates an instance of this class or its registered extension.
Definition baseClass.h:228
DtSpSubClass()
Default constructor.
Definition baseClass.h:190
SUPER_T Super
Super class type alias.
Definition baseClass.h:178
std::shared_ptr< SELF_T > Sptr
Shared pointer type alias.
Definition baseClass.h:184
Provides a template-based factory system for object creation.
#define LOG_FATAL(channel)
Macro to log a fatal message to log files.
Definition logger.h:64
Include export definitions for this library.
Definition glsVreMessageUtil.h:49