ALib C++ Library
Library Version: 2510 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
124{
125 #if ALIB_FEAT_SINGLETON_MAPPED
126 inShutdown= true;
127 for( auto& mapPair : hidden::singletonMap)
128 {
129 Singleton<void*>* theSingleton;
130 memcpy( &theSingleton, &mapPair.second, sizeof(void*) );
131 delete theSingleton;
132 }
133 #if ALIB_MONOMEM && ALIB_CONTAINERS
134 new (&hidden::singletonMap) HashMap< MonoAllocator,
139 Recycling::None >(monomem::GLOBAL_ALLOCATOR);
140 #endif
141 #endif
142}
143
144}} // namespace [alib::singletons]
145
146#if ALIB_DEBUG && ALIB_FEAT_SINGLETON_MAPPED
147namespace alib::singletons {
148# include "ALib.Lang.CIFunctions.H"
149
150
151 #if ALIB_MONOMEM && ALIB_CONTAINERS
154 lang::TypeFunctors::Hash,
155 lang::TypeFunctors::EqualTo,
157 Recycling::None >& DbgGetSingletons() { return hidden::singletonMap; }
158 #else
159 std::unordered_map<lang::TypeFunctors::Key, void*,
161 lang::TypeFunctors::EqualTo >& DbgGetSingletons() { return hidden::singletonMap; }
162 #endif
163
164
165 #if ALIB_STRINGS
167 {
168 auto& types= DbgGetSingletons();
169 for( auto& it : types )
170 target << lang::DbgTypeDemangler(*it.first).Get()
171 << " = 0x" << NHex(reinterpret_cast<uint64_t>(it.second) )
172 << NNEW_LINE;
173
174 #if ALIB_MONOMEM && ALIB_CONTAINERS
175 return int( types.Size() );
176 #else
177 return int( types.size() );
178 #endif
179 }
180 #endif // ALIB_SINGLETONS
181
182
183# include "ALib.Lang.CIMethods.H"
184
185} // namespace [alib::singletons]
186#endif // ALIB_DEBUG && ALIB_FEAT_SINGLETON_MAPPED
187
ALIB_DLL const char * Get()
#define ALIB_ASSERT_RESULT_EQUALS( func, value)
Definition alib.inl:1066
@ 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:653
strings::THex< nchar > NHex
Type alias in namespace alib.
Definition format.inl:557
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.