ALib C++ Framework
by
Library Version: 2605 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
singletons.cpp
1namespace alib {
2/// This is the namespace of \alibmod <b>"Singletons"</b>. Please refer to the
3/// #"alib_mod_singletons;Programmer's Manual Of ALib Singletons" for information about
4/// using this (single :-) #"%Singleton" class in this tiny namespace.
5namespace singletons {
6
7#if ALIB_FEAT_SINGLETON_MAPPED && !DOXYGEN
8namespace { bool inShutdown= false; }
9#endif
10
11#if ALIB_FEAT_SINGLETON_MAPPED && !DOXYGEN
12namespace hidden {
13 # if ALIB_MONOMEM && ALIB_CONTAINERS
19 Recycling::None > singletonMap(monomem::GLOBAL_ALLOCATOR);
20# else
21 std::unordered_map<lang::TypeFunctors::Key, void*,
23 lang::TypeFunctors::EqualTo > singletonMap;
24# endif
25# if !ALIB_MONOMEM && !ALIB_SINGLE_THREADED
26 std::mutex singletonMapMutex;
27# endif
28
30void storeSingleton( const std::type_info& type, void* theSingleton )
31{
32 #if ALIB_MONOMEM && ALIB_CONTAINERS
33 if( singletonMap.Size() == 0)
34 {
35 singletonMap.MaxLoadFactor( 10 );
36 singletonMap.Reserve( 23, lang::ValueReference::Absolute );
37 }
38
39 singletonMap.EmplaceUnique( &type, theSingleton );
40 #else
41 if( singletonMap.size() == 0)
42 {
43 singletonMap.max_load_factor( 10 );
44 singletonMap.reserve( 23 );
45 }
46
47 singletonMap.emplace(&type, theSingleton);
48 #endif
49}
50
51void removeSingleton( const std::type_info& type )
52{
53 if(!inShutdown)
54 {
55 #if ALIB_MONOMEM && ALIB_CONTAINERS
56 ALIB_ASSERT_RESULT_EQUALS( singletonMap.erase(&type), 1)
57 #else
58 ALIB_ASSERT_RESULT_EQUALS( singletonMap.erase(&type), 1)
59 #endif
60 }
61}
62
63void* getSingleton ( const std::type_info& type )
64{
65 #if ALIB_MONOMEM && ALIB_CONTAINERS
66 auto entry= singletonMap.Find( &type );
67 if ( entry != singletonMap.end() )
68 {
69 void* result= entry->second;
70 return result;
71 }
72 return nullptr;
73 #else
74 auto entry= singletonMap.find( &type );
75 if ( entry != singletonMap.end() )
76 {
77 void* result= entry->second;
78 return result;
79 }
80 return nullptr;
81 #endif
82}
83
84} // namespace alib::singletons[::hidden]
85
86#endif //ALIB_FEAT_SINGLETON_MAPPED
87
88void shutdown() {
89 #if ALIB_FEAT_SINGLETON_MAPPED
90 inShutdown= true;
91 for( auto& mapPair : hidden::singletonMap) {
92 Singleton<void*>* theSingleton;
93 memcpy( &theSingleton, &mapPair.second, sizeof(void*) );
94 delete theSingleton;
95 }
96 #if ALIB_MONOMEM && ALIB_CONTAINERS
97 new (&hidden::singletonMap) HashMap< MonoAllocator,
102 Recycling::None >(monomem::GLOBAL_ALLOCATOR);
103 #endif
104 #endif
105}
106
107}} // namespace [alib::singletons]
108
109#if ALIB_DEBUG && ALIB_FEAT_SINGLETON_MAPPED
110namespace alib::singletons {
111# include "ALib.Lang.CIFunctions.H"
112
113
114 #if ALIB_MONOMEM && ALIB_CONTAINERS
117 lang::TypeFunctors::Hash,
118 lang::TypeFunctors::EqualTo,
120 Recycling::None >& DbgGetSingletons() { return hidden::singletonMap; }
121 #else
122 std::unordered_map<lang::TypeFunctors::Key, void*,
124 lang::TypeFunctors::EqualTo >& DbgGetSingletons() { return hidden::singletonMap; }
125 #endif
126
127
128 #if ALIB_STRINGS
130 {
131 auto& types= DbgGetSingletons();
132 for( auto& it : types )
133 target << lang::DbgTypeDemangler(*it.first).Get()
134 << " = 0x" << NHex(reinterpret_cast<uint64_t>(it.second) )
135 << NNEW_LINE;
136
137 #if ALIB_MONOMEM && ALIB_CONTAINERS
138 return int( types.Size() );
139 #else
140 return int( types.size() );
141 #endif
142 }
143 #endif // ALIB_SINGLETONS
144
145
146# include "ALib.Lang.CIMethods.H"
147
148} // namespace [alib::singletons]
149#endif // ALIB_DEBUG && ALIB_FEAT_SINGLETON_MAPPED
#define ALIB_ASSERT_RESULT_EQUALS( func, value)
@ Auto
Auto/default mode.
@ Absolute
Referring to an absolute value.
TMonoAllocator< lang::HeapAllocator > GLOBAL_ALLOCATOR
HashMap< MonoAllocator, lang::TypeFunctors::Key, void *, lang::TypeFunctors::Hash, lang::TypeFunctors::EqualTo, lang::Caching::Auto, Recycling::None > & DbgGetSingletons()
Definition alox.cpp:14
monomem::TMonoAllocator< lang::HeapAllocator > MonoAllocator
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".
constexpr NCString NNEW_LINE
A zero-terminated string containing the new-line character sequence.
Definition cstring.hpp:545
strings::THex< nchar > NHex
Type alias in namespace #"%alib".
Definition format.hpp:552
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.