soui 5.0.0.1
Soui5 Doc
 
Loading...
Searching...
No Matches
SSingleton2.h
Go to the documentation of this file.
1/**
2 * Copyright (C) 2014-2050
3 * All rights reserved.
4 *
5 * @file SSingleton2.h
6 * @brief Pseudo-Singleton Template
7 * @version v1.0
8 * @author SOUI group
9 * @date 2014/08/02
10 *
11 * Description: Provides a pseudo-singleton template in SOUI, which uses the true singleton SApplication to manage instances.
12 */
13
14#ifndef __SSINGLETON2__H__
15#define __SSINGLETON2__H__
16
17#include <assert.h>
18
19SNSBEGIN
20
21#define SINGLETON2_TYPE(x) \
22 public: \
23 friend class SApplication; \
24 static SingletonType GetType() \
25 { \
26 return x; \
27 }
28
29/**
30 * @class SSingleton2
31 * @brief Pseudo-Singleton Template
32 *
33 * Description: Implements a pseudo-singleton pattern where the actual singleton management is handled by the SApplication class.
34 * This allows for centralized management of singleton instances.
35 * @tparam T Type of the class that will be made a pseudo-singleton.
36 */
37template <typename T>
39 public:
40 /**
41 * @brief Constructor for SSingleton2.
42 */
44 {
45 }
46
47 /**
48 * @brief Destructor for SSingleton2.
49 */
50 virtual ~SSingleton2(void)
51 {
52 }
53
54 /**
55 * @brief Gets the singleton instance.
56 * @return Reference to the singleton instance.
57 * @note Asserts that the singleton instance exists.
58 */
59 static T &getSingleton(void)
60 {
61 assert(getObjPtr());
62 return (*getObjPtr());
63 }
64
65 /**
66 * @brief Gets the pointer to the singleton instance.
67 * @return Pointer to the singleton instance.
68 * @note Asserts that the singleton instance exists.
69 */
70 static T *getSingletonPtr(void)
71 {
72 assert(getObjPtr());
73 return getObjPtr();
74 }
75
76 private:
77 /**
78 * @brief Gets the pointer to the singleton object managed by SApplication.
79 * @return Pointer to the singleton object.
80 */
81 static T *getObjPtr()
82 {
83 return (T *)SApplication::getSingletonPtr()->GetInnerSingleton(T::GetType());
84 }
85
86 /**
87 * @brief Private copy assignment operator to prevent copying.
88 * @param rhs Right-hand side object.
89 * @return Reference to the current object.
90 */
91 SSingleton2 &operator=(const SSingleton2 &)
92 {
93 return *this;
94 }
95
96 /**
97 * @brief Private copy constructor to prevent copying.
98 * @param rhs Right-hand side object.
99 */
100 SSingleton2(const SSingleton2 &)
101 {
102 }
103};
104
105SNSEND
106
107#endif // __SSINGLETON2__H__
void * GetInnerSingleton(SingletonType nType) OVERRIDE
Get an inner singleton.
Definition SApp.cpp:347
static T * getSingletonPtr(void)
Gets the pointer to the singleton instance.
Definition SSingleton2.h:70
SSingleton2(void)
Constructor for SSingleton2.
Definition SSingleton2.h:43
static T & getSingleton(void)
Gets the singleton instance.
Definition SSingleton2.h:59
virtual ~SSingleton2(void)
Destructor for SSingleton2.
Definition SSingleton2.h:50
static SApplication * getSingletonPtr(void)
Definition SSingleton.h:73