ALib C++ Library
Library Version: 2402 R1
Documentation generated by doxygen
Loading...
Searching...
No Matches
singleton.hpp
Go to the documentation of this file.
1/** ************************************************************************************************
2 * \file
3 * This header file is part of module \alib_singletons of the \aliblong.
4 *
5 * \emoji :copyright: 2013-2024 A-Worx GmbH, Germany.
6 * Published under \ref mainpage_license "Boost Software License".
7 **************************************************************************************************/
8#ifndef HPP_ALIB_SINGLETONS_SINGLETON
9#define HPP_ALIB_SINGLETONS_SINGLETON 1
10
11#if !defined(HPP_ALIB) && !defined(ALIB_DOX)
12# include "alib/alib.hpp"
13#endif
14
15#if ALIB_STRINGS && !defined(HPP_ALIB_STRINGS_FWDS)
16# include "alib/strings/fwds.hpp"
17#endif
18
19#if ALIB_FEAT_SINGLETON_MAPPED && !defined (_TYPEINFO) && !defined(_TYPEINFO_)
20 #include <typeinfo>
21#endif
22
23namespace alib { namespace singletons {
24
25// #################################################################################################
26// Unordered map utility for storing type_info objects
27// #################################################################################################
28
29//! @cond NO_DOX
30#if ALIB_FEAT_SINGLETON_MAPPED
31extern ALIB_API void* getSingleton ( const std::type_info& type );
32extern ALIB_API void storeSingleton ( const std::type_info& type, void* theSingleton );
33extern ALIB_API void removeSingleton( const std::type_info& type );
34#endif
35//! @endcond
36
37/** ************************************************************************************************
38 * This class implements the "singleton pattern" for C++ using a common templated approach.
39 * In case of Windows OS and DLL usage, the class overcomes the problem of having
40 * a global data segment per DLL in addition to the one associated with the process that is using
41 * the DLL.
42 *
43 * All details about implementation and usage of this class is provided in the module's
44 * \ref alib_mod_singletons "Programmer's Manual".
45 *
46 * @tparam TDerivedClass Template parameter that denotes the name of the class that implements
47 * the singleton.
48 **************************************************************************************************/
49template <typename TDerivedClass>
51{
52 protected:
53 /** A pointer to the one and only singleton. */
54 static TDerivedClass* singleton;
55
56 public:
57 /**
58 * Creates (if not done, yet) and returns the singleton of type \p{TDerivedClass}.
59 * @return The singleton instance.
60 */
61 static TDerivedClass& GetSingleton()
62 {
63 #if !ALIB_FEAT_SINGLETON_MAPPED
64
65 if( singleton == nullptr )
66 singleton= new TDerivedClass();
67
68 return *singleton;
69
70 #else
71 // already created and known
72 if( singleton != nullptr )
73 return *singleton;
74
75
76 // try loading from static map
77 void* storedSingleton= getSingleton( typeid(TDerivedClass) );
78 if( storedSingleton != nullptr )
79 return *(singleton= dynamic_cast < TDerivedClass* >(
80 reinterpret_cast<Singleton<TDerivedClass>*>(storedSingleton) ) );
81
82 // create and store in map
83 auto* firstInstance= new TDerivedClass();
84 storeSingleton( typeid(TDerivedClass), dynamic_cast<Singleton<TDerivedClass>*>( firstInstance ) );
85
86 // In debug mode, do not set this singleton right away. This "simulates"
87 // a windows DLL/Exec scope change
88 #if ALIB_DEBUG
89 return *firstInstance;
90 #else
91 return *(singleton= firstInstance);
92 #endif
93 #endif
94 }
95
96 /** Virtual destructor. */
97 virtual ~Singleton()
98 {
99 #if ALIB_FEAT_SINGLETON_MAPPED
100 removeSingleton( typeid(TDerivedClass) );
101 #endif
102 }
103
104};// class Singleton
105
106// The static singleton instance initialization
107template <typename TDerivedClass>
108TDerivedClass* Singleton<TDerivedClass>::singleton= nullptr;
109
110/** ************************************************************************************************
111 * Deletes the singletons.
112 * Upon exit of the process, programmers might want to explicitly free the hash table to avoid
113 * the detection of memory leaks by metrics tools like \http{Valgrind,valgrind.org/}.
114 * (Otherwise this can be omitted, as the memory is cleaned by the OS probably much faster when a
115 * process exits).
116 *
117 * The \ref alib_manual_bootstrapping "standard bootstrap" code of \alib, hence the (overloaded)
118 * functions \ref alib::Shutdown will call this function.
119 *
120 * \note This method is not thread-safe and hence must be called only on termination of the process
121 * when all threads which are using singletons are terminated.
122 **************************************************************************************************/
123ALIB_API void Shutdown();
124
125} // namespace alib[::singletons]
126
127/// Type alias in namespace \b alib.
128template<typename T>
130
131} // namespace alib
132
133
134
135#endif // HPP_ALIB_SINGLETONS_SINGLETON
static TDerivedClass * singleton
Definition singleton.hpp:54
static TDerivedClass & GetSingleton()
Definition singleton.hpp:61
#define ALIB_API
Definition alib.hpp:538
Definition alib.cpp:57