VR-Engage  2.3
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 Common abstract base for all VR-Engage managed objects
88//!
89//! Provides the shared interface for type identification and object naming.
90//! Both \c DtVreSharedObject and \c DtVreUniqueObject inherit from this class,
91//! making it the common root of the managed-object hierarchy.
93{
94public:
95 //! \brief Default constructor
97
98 //! \brief Virtual destructor
99 virtual ~DtVreBase() = default;
100
101 //! \brief Gets the type of this object
102 //!
103 //! \return A string representing the concrete type of this object
104 virtual std::string type() const = 0;
105
106 //! \brief Sets the name of this object
107 //!
108 //! \param name The new name for the object
109 virtual void setName(const std::string& name) { myName = name; }
110
111 //! \brief Gets the name of this object
112 //!
113 //! \return The name of this object
114 virtual std::string name() const { return myName; }
115
116protected:
117 //! \brief The name of this object
118 std::string myName;
119};
120
121//! \brief Base class for shared_ptr-managed objects
122//!
123//! Provides self-referencing mechanisms via \c self(), safe and unsafe
124//! shared pointer casts, and inherits type identification and naming from
125//! \c DtVreBase. Use \c DtVreSharedBase to derive classes that participate
126//! in the per-class creator factory.
128{
129public:
130 //! \brief Raw pointer type alias
132
133 //! \brief Shared pointer type alias
134 using Sptr = std::shared_ptr<DtVreSharedObject>;
135
136 //! \brief Weak pointer type alias
137 using Wptr = std::weak_ptr<DtVreSharedObject>;
138
139 //! \brief Default constructor
141
142 //! \brief Virtual destructor
144
145 //! \brief Gets a shared pointer to this object
146 //!
147 //! If the weak reference to self is expired, creates a new shared pointer;
148 //! otherwise returns the existing shared pointer.
149 //!
150 //! \return A shared pointer to this object
151 Sptr self() { return mySelf.expired() ? (mySelf = Sptr(this)).lock() : mySelf.lock(); }
152
153 //! \brief Safely casts a base shared pointer to a derived type
154 //!
155 //! \param p The base shared pointer to cast
156 //! \return A shared pointer to the derived type, or null if the cast fails
157 template <class AS_T>
158 static std::shared_ptr<AS_T> cast(Sptr p)
159 {
160 return std::dynamic_pointer_cast<AS_T>(p);
161 }
162
163 //! \brief Performs an unsafe static cast of a shared pointer
164 //!
165 //! Use only when you are certain the object is of the target type.
166 //!
167 //! \param p The base shared pointer to cast
168 //! \return A shared pointer to the target type
169 template <class AS_T>
170 static std::shared_ptr<AS_T> unsafe_cast(Sptr p)
171 {
172 return std::static_pointer_cast<AS_T>(p);
173 }
174
175private:
176 //! \brief Weak pointer to self for shared_ptr management
178};
179
180//! \brief CRTP template for shared_ptr-managed objects
181//!
182//! Provides \c Super, \c Sptr, \c Wptr aliases, type function overrides, and
183//! a per-class creator factory interface. Derive from this template to enable
184//! runtime customization of object creation via \c registerCreator<CUSTOM_T>().
185//!
186//! \tparam SELF_T The derived class type (CRTP parameter)
187//! \tparam SUPER_T The base class type, defaults to DtVreSharedObject
188template <class SELF_T, class SUPER_T = DtVreSharedObject>
189class DtVreSharedBase : public SUPER_T
190{
191public:
192 //! \brief Super class type alias
193 using Super = SUPER_T;
194
195 //! \brief Raw pointer type alias
196 using Ptr = SELF_T*;
197
198 //! \brief Shared pointer type alias
199 using Sptr = std::shared_ptr<SELF_T>;
200
201 //! \brief Weak pointer type alias
202 using Wptr = std::weak_ptr<SELF_T>;
203
204 //! \brief Default constructor
206
207 //! \brief Virtual destructor
208 virtual ~DtVreSharedBase() {}
209
210 //! \brief Gets a shared pointer to this object of the derived type
211 //!
212 //! \return A shared pointer to this object cast to the derived type
213 Sptr self() { return std::static_pointer_cast<SELF_T>(Super::self()); }
214
215 //! \brief Gets the static type name of this class
216 //!
217 //! \return A string representing the type name
218 static std::string Type() { return typeid(SELF_T).name(); }
219
220 //! \brief Gets the runtime type of this object
221 //!
222 //! \return A string representing the type of this object
223 std::string type() const override { return typeid(SELF_T).name(); }
224
225 //-------------------------------------------------
226 // Object Creator Interface
227
228 //! \brief Creates an instance of this class or its registered extension
229 //!
230 //! This method allows for extension of base classes. Use CLASS::create()
231 //! instead of new when you want to allow custom extensions to replace what
232 //! class is actually realized. By default, CLASS::create() creates a new
233 //! instance of CLASS. To override with a derived extension class, call
234 //! CLASS::registerCreator<CUSTOM>() with the new custom class.
235 //!
236 //! The custom class should inherit from CLASS.
237 //!
238 //! The custom class will then be instantiated wherever CLASS::create()
239 //! is used. Use cast<CUSTOM>(shared_ptr) for convenient dynamic cast to
240 //! custom class type if needing to access custom extended class interface.
241 //!
242 //! \return A shared pointer to the newly created instance
243 static std::shared_ptr<SELF_T> create()
244 {
245 if (!creator())
246 {
247 LOG_FATAL("UTIL", "%s::create called with no creator class registered.\n", SELF_T::Type().c_str());
248 DtASSERT(false, "create() called with no creator class registered. See log.");
249 }
250 return creator()->create()->self();
251 }
252
253 //! \brief Registers a custom extended class to be created instead of this type
254 //!
255 //! \tparam CUSTOM_T The custom class type to register as the creator
256 template <class CUSTOM_T>
257 static void registerCreator()
258 {
259 creator() = std::make_shared<DtCreator<SELF_T, CUSTOM_T>>();
260 }
261
262protected:
263 //! \brief Gets the reference to the creator object
264 //!
265 //! \return A reference to the shared pointer containing the creator
266 static std::shared_ptr<DtBaseCreator<SELF_T>>& creator()
267 {
268 static std::shared_ptr<DtBaseCreator<SELF_T>> creator_sp;
269 return creator_sp;
270 }
271
272 // Versioned creator interface
273 //-----------------------------
274public:
275 //! \brief Creates an instance of this class for a specific version
276 //!
277 //! This allows for version-specific implementations to be selected at runtime
278 //!
279 //! \param version The version number to create
280 //! \return A shared pointer to the newly created instance
281 static std::shared_ptr<SELF_T> createVersion(unsigned int version)
282 {
283 if (!creatorVersion(version))
284 {
285 LOG_FATAL("UTIL", "%s::createVersion(%u) called with no creator class registered.\n",
286 SELF_T::Type().c_str(), version);
287 DtASSERT(false, "create() called with no creator class registered. See log.");
288 }
289 return creatorVersion(version)->create()->self();
290 }
291
292 //! \brief Registers a custom class for a specific version
293 //!
294 //! \tparam CUSTOM_T The custom class type to register
295 //! \param version The version number to associate with this creator
296 template <class CUSTOM_T>
297 static void registerCreatorVersion(unsigned int version)
298 {
299 creatorVersion(version) = std::make_shared<DtCreator<SELF_T, CUSTOM_T>>();
300 }
301
302protected:
303 //! \brief Gets the reference to the creator object for a specific version
304 //!
305 //! \param version The version number to get the creator for
306 //! \return A reference to the shared pointer containing the creator
307 static std::shared_ptr<DtBaseCreator<SELF_T>>& creatorVersion(unsigned int version)
308 {
309 static std::map<unsigned int, std::shared_ptr<DtBaseCreator<SELF_T>>> creatorMap_sp;
310 return creatorMap_sp[version];
311 }
312 //-------------------------------------------------
313};
314
315//! \brief Base class for unique_ptr-managed objects
316//!
317//! Provides a named intermediate base for objects managed by \c std::unique_ptr,
318//! symmetric with \c DtVreSharedObject in the managed-object hierarchy.
319//! Use \c DtVreUniqueBase to derive classes that participate in the per-class
320//! creator factory.
322{
323public:
324 //! \brief Default constructor
326
327 //! \brief Virtual destructor
329};
330
331//! \brief CRTP template for unique_ptr-managed objects
332//!
333//! Provides \c Ptr (raw non-owning observer) and \c Uptr (\c std::unique_ptr)
334//! aliases, type function overrides, and a per-class creator factory interface.
335//! Mirrors \c DtVreSharedBase but produces \c std::unique_ptr<SELF_T> from
336//! \c create(). Use \c registerCreator<CUSTOM_T>() to replace the default
337//! creator at runtime.
338//!
339//! Per the MAK coding standard, \c Ptr is the non-owning observer handle and
340//! \c Uptr is the owning handle.
341//!
342//! \tparam SELF_T The derived class type (CRTP parameter)
343//! \tparam SUPER_T The base class type, defaults to DtVreUniqueObject
344template <class SELF_T, class SUPER_T = DtVreUniqueObject>
345class DtVreUniqueBase : public SUPER_T
346{
347public:
348 //! \brief Super class type alias
349 using Super = SUPER_T;
350
351 //! \brief Raw non-owning pointer type alias
352 using Ptr = SELF_T*;
353
354 //! \brief Owning unique pointer type alias
355 using Uptr = std::unique_ptr<SELF_T>;
356
357 //! \brief Default constructor
359
360 //! \brief Virtual destructor
361 virtual ~DtVreUniqueBase() {}
362
363 //! \brief Gets the static type name of this class
364 //!
365 //! \return A string representing the type name
366 static std::string Type() { return typeid(SELF_T).name(); }
367
368 //! \brief Gets the runtime type of this object
369 //!
370 //! \return A string representing the type of this object
371 std::string type() const override { return typeid(SELF_T).name(); }
372
373 //-------------------------------------------------
374 // Object Creator Interface
375
376 //! \brief Creates an instance of this class or its registered extension
377 //!
378 //! Use \c CLASS::create() instead of \c new when you want to allow custom
379 //! extensions to replace the concrete type produced at runtime. By default,
380 //! \c CLASS::create() constructs a new instance of \c CLASS. Call
381 //! \c CLASS::registerCreator<CUSTOM_T>() to substitute a derived class.
382 //!
383 //! \return A \c std::unique_ptr to the newly created instance
384 static Uptr create()
385 {
386 if (!creator())
387 {
388 LOG_FATAL("UTIL", "%s::create called with no creator class registered.\n", SELF_T::Type().c_str());
389 DtASSERT(false, "create() called with no creator class registered. See log.");
390 }
391 return Uptr(static_cast<SELF_T*>(creator()->create()));
392 }
393
394 //! \brief Registers a custom extended class to be created instead of this type
395 //!
396 //! \tparam CUSTOM_T The custom class type to register as the creator
397 template <class CUSTOM_T>
398 static void registerCreator()
399 {
400 creator() = std::make_shared<DtCreator<SELF_T, CUSTOM_T>>();
401 }
402
403protected:
404 //! \brief Gets the reference to the creator object
405 //!
406 //! \return A reference to the shared pointer containing the creator
407 static std::shared_ptr<DtBaseCreator<SELF_T>>& creator()
408 {
409 static std::shared_ptr<DtBaseCreator<SELF_T>> creator_sp;
410 return creator_sp;
411 }
412 //-------------------------------------------------
413};
414
415} // 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:58
virtual std::string name() const
Gets the name of this object.
Definition baseClass.h:114
virtual std::string type() const =0
Gets the type of this object.
std::string myName
The name of this object.
Definition baseClass.h:118
virtual ~DtVreBase()=default
Virtual destructor.
DtVreBase()
Default constructor.
Definition baseClass.h:96
virtual void setName(const std::string &name)
Sets the name of this object.
Definition baseClass.h:109
static std::string Type()
Gets the static type name of this class.
Definition baseClass.h:218
std::string type() const override
Gets the runtime type of this object.
Definition baseClass.h:223
static void registerCreator()
Registers a custom extended class to be created instead of this type.
Definition baseClass.h:257
virtual ~DtVreSharedBase()
Virtual destructor.
Definition baseClass.h:208
static std::shared_ptr< DtBaseCreator< SELF_T > > & creator()
Gets the reference to the creator object.
Definition baseClass.h:266
SELF_T * Ptr
Raw pointer type alias.
Definition baseClass.h:196
static void registerCreatorVersion(unsigned int version)
Registers a custom class for a specific version.
Definition baseClass.h:297
std::weak_ptr< SELF_T > Wptr
Weak pointer type alias.
Definition baseClass.h:202
DtVreSharedBase()
Default constructor.
Definition baseClass.h:205
static std::shared_ptr< SELF_T > createVersion(unsigned int version)
Creates an instance of this class for a specific version.
Definition baseClass.h:281
SUPER_T Super
Super class type alias.
Definition baseClass.h:193
std::shared_ptr< SELF_T > Sptr
Shared pointer type alias.
Definition baseClass.h:199
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:307
static std::shared_ptr< SELF_T > create()
Creates an instance of this class or its registered extension.
Definition baseClass.h:243
Sptr self()
Gets a shared pointer to this object of the derived type.
Definition baseClass.h:213
static std::shared_ptr< AS_T > unsafe_cast(Sptr p)
Performs an unsafe static cast of a shared pointer.
Definition baseClass.h:170
DtVreSharedObject()
Default constructor.
Definition baseClass.h:140
Wptr mySelf
Weak pointer to self for shared_ptr management.
Definition baseClass.h:177
std::shared_ptr< DtVreSharedObject > Sptr
Shared pointer type alias.
Definition baseClass.h:134
DtVreSharedObject * Ptr
Raw pointer type alias.
Definition baseClass.h:131
static std::shared_ptr< AS_T > cast(Sptr p)
Safely casts a base shared pointer to a derived type.
Definition baseClass.h:158
Sptr self()
Gets a shared pointer to this object.
Definition baseClass.h:151
virtual ~DtVreSharedObject()
Virtual destructor.
Definition baseClass.h:143
std::weak_ptr< DtVreSharedObject > Wptr
Weak pointer type alias.
Definition baseClass.h:137
static std::shared_ptr< DtBaseCreator< SELF_T > > & creator()
Gets the reference to the creator object.
Definition baseClass.h:407
static std::string Type()
Gets the static type name of this class.
Definition baseClass.h:366
static Uptr create()
Creates an instance of this class or its registered extension.
Definition baseClass.h:384
DtVreUniqueBase()
Default constructor.
Definition baseClass.h:358
static void registerCreator()
Registers a custom extended class to be created instead of this type.
Definition baseClass.h:398
std::string type() const override
Gets the runtime type of this object.
Definition baseClass.h:371
virtual ~DtVreUniqueBase()
Virtual destructor.
Definition baseClass.h:361
std::unique_ptr< SELF_T > Uptr
Owning unique pointer type alias.
Definition baseClass.h:355
SUPER_T Super
Super class type alias.
Definition baseClass.h:349
SELF_T * Ptr
Raw non-owning pointer type alias.
Definition baseClass.h:352
virtual ~DtVreUniqueObject()
Virtual destructor.
Definition baseClass.h:328
DtVreUniqueObject()
Default constructor.
Definition baseClass.h:325
Provides a template-based factory system for object creation.
#define LOG_FATAL(channel, format,...)
Macro to log a fatal message using printf-style formatting.
Definition logger.h:144
!
Definition glsVreMessageUtil.h:50