soui 5.0.0.1
Soui5 Doc
 
Loading...
Searching...
No Matches
SSingleton.h
Go to the documentation of this file.
1/**
2 * Copyright (C) 2014-2050
3 * All rights reserved.
4 *
5 * @file SSingleton.h
6 * @brief Singleton Template
7 * @version v1.0
8 * @author SOUI group
9 * @date 2014/08/02
10 *
11 * Description: Provides a template for implementing the Singleton design pattern in SOUI.
12 */
13
14#ifndef __SSINGLETON__H__
15#define __SSINGLETON__H__
16
17#include <assert.h>
18
19SNSBEGIN
20
21/**
22 * @class SSingleton
23 * @brief Singleton Template
24 *
25 * Description: Implements the Singleton design pattern, ensuring that a class has only one instance
26 * and providing a global point of access to it.
27 * @tparam T Type of the class that will be made a singleton.
28 */
29template <typename T>
31 protected:
32 /**
33 * @brief Static pointer to the singleton instance.
34 */
35 static T *ms_Singleton;
36
37 public:
38 /**
39 * @brief Constructor for SSingleton.
40 * @note Asserts that no instance already exists.
41 */
43 {
44 assert(!ms_Singleton);
45 ms_Singleton = static_cast<T *>(this);
46 }
47
48 /**
49 * @brief Destructor for SSingleton.
50 * @note Asserts that an instance exists and sets the singleton pointer to null.
51 */
52 virtual ~SSingleton(void)
53 {
54 assert(ms_Singleton);
55 ms_Singleton = 0;
56 }
57
58 /**
59 * @brief Gets the singleton instance.
60 * @return Reference to the singleton instance.
61 * @note Asserts that the singleton instance exists.
62 */
63 static T &getSingleton(void)
64 {
65 assert(ms_Singleton);
66 return (*ms_Singleton);
67 }
68
69 /**
70 * @brief Gets the pointer to the singleton instance.
71 * @return Pointer to the singleton instance.
72 */
73 static T *getSingletonPtr(void)
74 {
75 return (ms_Singleton);
76 }
77
78 private:
79 /**
80 * @brief Private copy assignment operator to prevent copying.
81 * @param rhs Right-hand side object.
82 * @return Reference to the current object.
83 */
84 SSingleton &operator=(const SSingleton &)
85 {
86 return *this;
87 }
88
89 /**
90 * @brief Private copy constructor to prevent copying.
91 * @param rhs Right-hand side object.
92 */
93 SSingleton(const SSingleton &)
94 {
95 }
96};
97
98SNSEND
99
100#endif // __SSINGLETON__H__
Singleton Template.
Definition SSingleton.h:30
static T & getSingleton(void)
Gets the singleton instance.
Definition SSingleton.h:63
SSingleton(void)
Constructor for SSingleton.
Definition SSingleton.h:42
static T * getSingletonPtr(void)
Gets the pointer to the singleton instance.
Definition SSingleton.h:73
static T * ms_Singleton
Static pointer to the singleton instance.
Definition SSingleton.h:35
virtual ~SSingleton(void)
Destructor for SSingleton.
Definition SSingleton.h:52