ALib C++ Library
Library Version: 2511 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
singletons.cpp
1//##################################################################################################
2// ALib C++ Library
3//
4// Copyright 2013-2025 A-Worx GmbH, Germany
5// Published under 'Boost Software License' (a free software license, see LICENSE.txt)
6//##################################################################################################
7#include "alib_precompile.hpp"
8#if !defined(ALIB_C20_MODULES) || ((ALIB_C20_MODULES != 0) && (ALIB_C20_MODULES != 1))
9# error "Symbol ALIB_C20_MODULES has to be given to the compiler as either 0 or 1"
10#endif
11#if ALIB_C20_MODULES
12 module;
13#endif
14//========================================= Global Fragment ========================================
16#if !ALIB_MONOMEM || !ALIB_CONTAINERS
17# include <unordered_map>
18#endif
19#if !ALIB_MONOMEM && !ALIB_SINGLE_THREADED
20# include <mutex>
21#endif
22#include <cstring>
23//============================================== Module ============================================
24#if ALIB_C20_MODULES
25 module ALib.Singletons;
26 import ALib.Lang;
27# if ALIB_MONOMEM
28 import ALib.Monomem;
29# endif
30#else
31# include "ALib.Lang.H"
32# include "ALib.Monomem.H"
33# include "ALib.Singletons.H"
34#endif
35//========================================== Implementation ========================================
36namespace alib {
37/// This is the namespace of \alibmod <b>"Singletons"</b>. Please refer to the
38/// \ref alib_mod_singletons "Programmer's Manual Of ALib Singletons" for information about
39/// using this (single :-) \b %Singleton class in this tiny namespace.
40namespace singletons {
41
42#if ALIB_FEAT_SINGLETON_MAPPED && !DOXYGEN
43namespace { bool inShutdown= false; }
44#endif
45
46#if ALIB_FEAT_SINGLETON_MAPPED && !DOXYGEN
47namespace hidden {
48 # if ALIB_MONOMEM && ALIB_CONTAINERS
54 Recycling::None > singletonMap(monomem::GLOBAL_ALLOCATOR);
55# else
56 std::unordered_map<lang::TypeFunctors::Key, void*,
58 lang::TypeFunctors::EqualTo > singletonMap;
59# endif
60# if !ALIB_MONOMEM && !ALIB_SINGLE_THREADED
61 std::mutex singletonMapMutex;
62# endif
63
65void storeSingleton( const std::type_info& type, void* theSingleton )
66{
67 #if ALIB_MONOMEM && ALIB_CONTAINERS
68 if( singletonMap.Size() == 0)
69 {
70 singletonMap.MaxLoadFactor( 10 );
71 singletonMap.Reserve( 23, lang::ValueReference::Absolute );
72 }
73
74 singletonMap.EmplaceUnique( &type, theSingleton );
75 #else
76 if( singletonMap.size() == 0)
77 {
78 singletonMap.max_load_factor( 10 );
79 singletonMap.reserve( 23 );
80 }
81
82 singletonMap.emplace(&type, theSingleton);
83 #endif
84}
85
86void removeSingleton( const std::type_info& type )
87{
88 if(!inShutdown)
89 {
90 #if ALIB_MONOMEM && ALIB_CONTAINERS
91 ALIB_ASSERT_RESULT_EQUALS( singletonMap.erase(&type), 1)
92 #else
93 ALIB_ASSERT_RESULT_EQUALS( singletonMap.erase(&type), 1)
94 #endif
95 }
96}
97
98void* getSingleton ( const std::type_info& type )
99{
100 #if ALIB_MONOMEM && ALIB_CONTAINERS
101 auto entry= singletonMap.Find( &type );
102 if ( entry != singletonMap.end() )
103 {
104 void* result= entry->second;
105 return result;
106 }
107 return nullptr;
108 #else
109 auto entry= singletonMap.find( &type );
110 if ( entry != singletonMap.end() )
111 {
112 void* result= entry->second;
113 return result;
114 }
115 return nullptr;
116 #endif
117}
118
119} // namespace alib::singletons[::hidden]
120
121#endif //ALIB_FEAT_SINGLETON_MAPPED
122
123void shutdown() {
124 #if ALIB_FEAT_SINGLETON_MAPPED
125 inShutdown= true;
126 for( auto& mapPair : hidden::singletonMap) {
127 Singleton<void*>* theSingleton;
128 memcpy( &theSingleton, &mapPair.second, sizeof(void*) );
129 delete theSingleton;
130 }
131 #if ALIB_MONOMEM && ALIB_CONTAINERS
132 new (&hidden::singletonMap) HashMap< MonoAllocator,
137 Recycling::None >(monomem::GLOBAL_ALLOCATOR);
138 #endif
139 #endif
140}
141
142}} // namespace [alib::singletons]
143
144#if ALIB_DEBUG && ALIB_FEAT_SINGLETON_MAPPED
145namespace alib::singletons {
146# include "ALib.Lang.CIFunctions.H"
147
148
149 #if ALIB_MONOMEM && ALIB_CONTAINERS
152 lang::TypeFunctors::Hash,
153 lang::TypeFunctors::EqualTo,
155 Recycling::None >& DbgGetSingletons() { return hidden::singletonMap; }
156 #else
157 std::unordered_map<lang::TypeFunctors::Key, void*,
159 lang::TypeFunctors::EqualTo >& DbgGetSingletons() { return hidden::singletonMap; }
160 #endif
161
162
163 #if ALIB_STRINGS
165 {
166 auto& types= DbgGetSingletons();
167 for( auto& it : types )
168 target << lang::DbgTypeDemangler(*it.first).Get()
169 << " = 0x" << NHex(reinterpret_cast<uint64_t>(it.second) )
170 << NNEW_LINE;
171
172 #if ALIB_MONOMEM && ALIB_CONTAINERS
173 return int( types.Size() );
174 #else
175 return int( types.size() );
176 #endif
177 }
178 #endif // ALIB_SINGLETONS
179
180
181# include "ALib.Lang.CIMethods.H"
182
183} // namespace [alib::singletons]
184#endif // ALIB_DEBUG && ALIB_FEAT_SINGLETON_MAPPED
ALIB_DLL const char * Get()
#define ALIB_ASSERT_RESULT_EQUALS( func, value)
Definition alib.inl:1083
@ Auto
Auto/default mode.
@ Absolute
Referring to an absolute value.
ALIB_DLL TMonoAllocator< lang::HeapAllocator > GLOBAL_ALLOCATOR
HashMap< MonoAllocator, lang::TypeFunctors::Key, void *, lang::TypeFunctors::Hash, lang::TypeFunctors::EqualTo, lang::Caching::Auto, Recycling::None > & DbgGetSingletons()
strings::TAString< nchar, lang::HeapAllocator > NAString
Type alias in namespace alib.
containers::HashMap< TAllocator, TKey, TMapped, THash, TEqual, THashCaching, TRecycling > HashMap
Type alias in namespace alib.
monomem::TMonoAllocator< lang::HeapAllocator > MonoAllocator
constexpr NCString NNEW_LINE
A zero-terminated string containing the new-line character sequence.
Definition cstring.inl:625
strings::THex< nchar > NHex
Type alias in namespace alib.
Definition format.inl:553
Comparison functor for type const std::type_info*.
Hash code functor for type const std::type_info*.
const ::std::type_info * Key
The key type.