ALib C++ Library
Library Version: 2412 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
singleton.cpp
1// #################################################################################################
2// ALib C++ Library
3//
4// Copyright 2013-2024 A-Worx GmbH, Germany
5// Published under 'Boost Software License' (a free software license, see LICENSE.txt)
6// #################################################################################################
8
9#if !DOXYGEN
12# if ALIB_FEAT_SINGLETON_MAPPED && ALIB_DEBUG
14# if ALIB_STRINGS
16# endif
17# endif
18# if ALIB_STRINGS
20# endif
21# if ALIB_MONOMEM && ALIB_CONTAINERS
24# else
25# include <unordered_map>
26# include <mutex>
27# include <cstring>
28# endif
29#endif // !DOXYGEN
30
31namespace alib {
32
33#if ALIB_FEAT_SINGLETON_MAPPED && !DOXYGEN
34namespace { bool inShutdown= false; }
35#endif
36/// This is the namespace of \alibmod <b>"Singletons"</b>. Please refer to the
37/// \ref alib_mod_singletons "Programmer's Manual Of ALib Singletons" for information about
38/// using this (single :-) \b %Singleton class in this tiny namespace.
39namespace singletons {
40#if ALIB_FEAT_SINGLETON_MAPPED && !DOXYGEN
41
42#if ALIB_MONOMEM && ALIB_CONTAINERS
43extern
45 TypeFunctors::Key, void*,
46 TypeFunctors::Hash,
47 TypeFunctors::EqualTo,
49 Recycling::None > singletonMap;
51 TypeFunctors::Key, void*,
52 TypeFunctors::Hash,
53 TypeFunctors::EqualTo,
55 Recycling::None > singletonMap(monomem::GLOBAL_ALLOCATOR);
56#else
57extern ALIB_API std::unordered_map<TypeFunctors::Key, void*,
58 TypeFunctors::Hash,
59 TypeFunctors::EqualTo > singletonMap;
60ALIB_API std::unordered_map<TypeFunctors::Key, void*,
61 TypeFunctors::Hash,
62 TypeFunctors::EqualTo > singletonMap;
63
65extern std::recursive_mutex singletonLock;
66 std::recursive_mutex singletonLock; )
67#endif
68
69#include "alib/lang/callerinfo_functions.hpp"
70void storeSingleton( const std::type_info& type, void* theSingleton )
71 {
72 #if ALIB_MONOMEM && ALIB_CONTAINERS
73 if( singletonMap.Size() == 0)
74 {
75 singletonMap.MaxLoadFactor( 10 );
76 singletonMap.Reserve( 23, lang::ValueReference::Absolute );
77 }
78
79 singletonMap.EmplaceUnique( &type, theSingleton );
80
81 // we unlock now as we were locked in getSingleton
83 #else
84 if( singletonMap.size() == 0)
85 {
86 singletonMap.max_load_factor( 10 );
87 singletonMap.reserve( 23 );
88 }
89
90 singletonMap.emplace(&type, theSingleton);
91
92 // we unlock now as we were locked in getSingleton
93 IF_ALIB_THREADS( singletonLock.unlock(); )
94 #endif
95 }
96
97void removeSingleton( const std::type_info& type )
98 {
99 if( !inShutdown)
100 {
101 #if ALIB_MONOMEM && ALIB_CONTAINERS
103 ALIB_ASSERT_RESULT_EQUALS( singletonMap.Erase(&type), 1)
104 #else
105 IF_ALIB_THREADS( singletonLock.lock(); )
106 ALIB_ASSERT_RESULT_EQUALS( singletonMap.erase(&type), 1)
107 IF_ALIB_THREADS( singletonLock.unlock(); )
108 #endif
109 }
110 }
111
112#if !DOXYGEN && ALIB_FEAT_SINGLETON_MAPPED && ALIB_THREADS
113void unlock() {
115}
116#endif
117
118void* getSingleton ( const std::type_info& type )
119{
120 #if ALIB_MONOMEM && ALIB_CONTAINERS
122 auto entry= singletonMap.Find( &type );
123 if ( entry != singletonMap.end() )
124 {
125 void* result= entry->second;
126 return result;
127 }
128
129 // Attn: we do not unlock when we have not found the singleton!
130 return nullptr;
131 #else
132 IF_ALIB_THREADS( singletonLock.lock(); )
133 auto entry= singletonMap.find( &type );
134 if ( entry != singletonMap.end() )
135 {
136 void* result= entry->second;
137 IF_ALIB_THREADS( singletonLock.unlock(); )
138 return result;
139 }
140
141 // Attn: we do not unlock when we have not found the singleton!
142 return nullptr;
143 #endif
144}
145
146
147#endif //ALIB_FEAT_SINGLETON_MAPPED
148
150{
151 #if ALIB_FEAT_SINGLETON_MAPPED
152 inShutdown= true;
153 for( auto mapPair : singletonMap)
154 {
155 Singleton<void*>* theSingleton;
156 memcpy( &theSingleton, &mapPair.second, sizeof(void*) );
157 delete theSingleton;
158 }
159 #if ALIB_MONOMEM && ALIB_CONTAINERS
160 new (&singletonMap) HashMap< MonoAllocator,
161 TypeFunctors::Key, void*,
165 Recycling::None >(monomem::GLOBAL_ALLOCATOR);
166 #endif
167 #endif
168}
169
170
171
172#if ALIB_DEBUG && ALIB_FEAT_SINGLETON_MAPPED
173 #if ALIB_MONOMEM && ALIB_CONTAINERS
175 TypeFunctors::Key, void*,
179 Recycling::None >& DbgGetSingletons() { return singletonMap; }
180 #else
181 std::unordered_map<TypeFunctors::Key, void*,
183 TypeFunctors::EqualTo >& DbgGetSingletons() { return singletonMap; }
184 #endif
185
186 #if ALIB_STRINGS && ALIB_DEBUG
188 {
189 auto& types= DbgGetSingletons();
190 for( auto& it : types )
191 target << lang::DbgTypeDemangler(*it.first).Get()
192 << " = 0x" << NFormat::Hex(reinterpret_cast<uint64_t>(it.second) )
193 << NNEW_LINE;
194
195 #if ALIB_MONOMEM && ALIB_CONTAINERS
196 return static_cast<int>( types.Size() );
197 #else
198 return static_cast<int>( types.size() );
199 #endif
200 }
201 #endif
202#endif
203
205
206}} // namespace [alib::singletons]
207
ALIB_API const char * Get()
ALIB_API void ReleaseRecursive(ALIB_DBG_TAKE_CI)
Definition locks.cpp:531
#define ALIB_ASSERT_RESULT_EQUALS( func, value)
Definition alib.hpp:1286
#define IF_ALIB_THREADS(...)
Definition alib.hpp:352
#define ALIB_API
Definition alib.hpp:639
#define ALIB_LOCK_RECURSIVE_WITH(lock)
Definition owner.hpp:457
#define ALIB_CALLER_PRUNED
Definition alib.hpp:1170
@ Absolute
Referring to an absolute value.
@ Auto
Auto/default mode.
ALIB_API MonoAllocator GLOBAL_ALLOCATOR
ALIB_API RecursiveLock GLOBAL_ALLOCATOR_LOCK
ALIB_API HashMap< MonoAllocator, TypeFunctors::Key, void *, TypeFunctors::Hash, TypeFunctors::EqualTo, lang::Caching::Auto, Recycling::None > & DbgGetSingletons()
Definition alib.cpp:69
constexpr NCString NNEW_LINE
A zero-terminated string containing the new-line character sequence.
Definition cstring.hpp:589
monomem::TMonoAllocator< lang::HeapAllocator > MonoAllocator
containers::HashMap< TAllocator, TKey, TMapped, THash, TEqual, THashCaching, TRecycling > HashMap
Type alias in namespace alib.
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.