ALib C++ Framework
by
Library Version: 2605 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
vtable.cpp
1namespace alib { namespace boxing { namespace detail {
2
3//##################################################################################################
4// Custom function hash map implementation
5//##################################################################################################
6#if !DOXYGEN
7#if ALIB_MONOMEM && ALIB_CONTAINERS
9 CustomFunctionKey, CustomFunctionMapped,
10 CustomFunctionHash,
11 CustomFunctionEqualTo > customFunctionMap(monomem::GLOBAL_ALLOCATOR);
12#else
13std::unordered_map< CustomFunctionKey, CustomFunctionMapped,
14 CustomFunctionHash,
15 CustomFunctionEqualTo > customFunctionMap;
16#endif
17
18#if ALIB_DEBUG
19} namespace debug {
20#if ALIB_MONOMEM && ALIB_CONTAINERS
24#else
25 std::unordered_set< lang::TypeFunctors::Key , lang::TypeFunctors::Hash, lang::TypeFunctors::EqualTo> DbgKnownCustomFunctions;
26 std::unordered_map< lang::TypeFunctors::Key, detail::VTable*, lang::TypeFunctors::Hash, lang::TypeFunctors::EqualTo> DbgKnownVTables;
27 std::unordered_map< lang::TypeFunctors::Key, detail::VTable*, lang::TypeFunctors::Hash, lang::TypeFunctors::EqualTo> DbgKnownVTablesArray;
28#endif
29
30IF_ALIB_THREADS( namespace { RecursiveLock dbgLock; } )
31void DbgLockMaps( bool doLock ) {
32 #if ALIB_SINGLE_THREADED
33 (void) doLock;
35 #else
36 # include "ALib.Lang.CIFunctions.H"
37 ALIB_DBG(dbgLock.Dbg.Name= "DbgBoxing";)
38 if( doLock )
39 dbgLock.AcquireRecursive( ALIB_CALLER_PRUNED );
40 else
41 dbgLock.ReleaseRecursive( ALIB_CALLER_PRUNED );
42 # include "ALib.Lang.CIMethods.H"
43 #endif
44}
45} namespace detail {
46#endif // ALIB_DEBUG
47
48#endif // !DOXYGEN
49
50//##################################################################################################
51// struct Functions
52//##################################################################################################
54
55#if (ALIB_MONOMEM && ALIB_CONTAINERS && ALIB_DEBUG)
57 #if ALIB_MONOMEM && ALIB_DEBUG_BOXING
58 debug::DbgKnownCustomFunctions.Reset();
59 debug::DbgKnownVTables .Reset();
60 debug::DbgKnownVTablesArray .Reset();
61 #endif
62 customFunctionMap .Reset();
63}
64#endif
65
66
67void* FunctionTable::getCustom( const std::type_info& rtti ALIB_DBG(, bool isInvocation) ) const {
68#if ALIB_MONOMEM && ALIB_CONTAINERS
69 auto it= customFunctionMap.Find( CustomFunctionKey(this, rtti) );
70#else
71 auto it= customFunctionMap.find( CustomFunctionKey(this, rtti) );
72#endif
73 if ( it != customFunctionMap.end() ) {
74 ALIB_DBG( if( isInvocation )
75 ++it->second.DbgCntInvocations; )
76 return it->second.Implementation;
77 }
78 return nullptr;
79}
80
81void FunctionTable::setCustom( const std::type_info& rtti, void* impl ) {
82 #if ALIB_DEBUG_BOXING
83 debug::DbgLockMaps(true);
84 #if ALIB_MONOMEM
85 debug::DbgKnownCustomFunctions.InsertIfNotExistent( &rtti );
86 #else
87 debug::DbgKnownCustomFunctions.emplace( &rtti );
88 #endif
89 debug::DbgLockMaps(false);
90 #endif
91
92 // search existing (replace)
93 #if ALIB_MONOMEM && ALIB_CONTAINERS
94 customFunctionMap.InsertOrAssign( CustomFunctionKey(this, rtti), CustomFunctionMapped(impl) );
95 #else
96 if( customFunctionMap.size() == 0 )
97 customFunctionMap.reserve( 50 );
98 customFunctionMap.insert_or_assign( CustomFunctionKey(this, rtti), CustomFunctionMapped(impl) );
99 #endif
100
101}
102
103} // namespace alib::boxing[::detail]
104
105using namespace detail;
106
107//##################################################################################################
108// Debug Function Lists Implementation
109// (located here due to anonymous function table)
110//##################################################################################################
111#if ALIB_DEBUG_BOXING
112std::vector<detail::VTable*> debug::GetKnownVTables() {
113 DbgLockMaps(true);
114
115 std::vector<detail::VTable*> result;
116 #if ALIB_MONOMEM
117 result.reserve( size_t(
118 DbgKnownVTables .Size()
119 + DbgKnownVTablesArray.Size() ) );
120 #else
121 result.reserve( DbgKnownVTables .size()
122 + DbgKnownVTablesArray.size() );
123 #endif
124
125 for( int type= 0 ; type < 2 ; ++type ) {
126 auto& map= type == 0 ? DbgKnownVTables
127 : DbgKnownVTablesArray;
128 for( auto it= map.begin() ; it!= map.end() ; ++it )
129 result.emplace_back( it->second );
130 }
131
132 DbgLockMaps(false);
133 return result;
134}
135
136std::vector<std::pair<const std::type_info*,uinteger>> debug::GetKnownFunctionTypes() {
137 std::vector<std::pair<const std::type_info*,uinteger>> result;
138 result.emplace_back( &typeid( FHashcode ), detail::DEFAULT_FUNCTIONS.fHashcode ? detail::DEFAULT_FUNCTIONS.DbgCntInvocationsFHashcode : (std::numeric_limits<uinteger>::max)() );
140 result.emplace_back( &typeid( FClone ), detail::DEFAULT_FUNCTIONS.fClone ? detail::DEFAULT_FUNCTIONS.DbgCntInvocationsFClone : (std::numeric_limits<uinteger>::max)() ); )
141 result.emplace_back( &typeid( FIsNotNull ), detail::DEFAULT_FUNCTIONS.fIsNotNull ? detail::DEFAULT_FUNCTIONS.DbgCntInvocationsFIsNotNull : (std::numeric_limits<uinteger>::max)() );
142 result.emplace_back( &typeid( FEquals ), detail::DEFAULT_FUNCTIONS.fEquals ? detail::DEFAULT_FUNCTIONS.DbgCntInvocationsFEquals : (std::numeric_limits<uinteger>::max)() );
143 result.emplace_back( &typeid( FIsLess ), detail::DEFAULT_FUNCTIONS.fIsLess ? detail::DEFAULT_FUNCTIONS.DbgCntInvocationsFIsLess : (std::numeric_limits<uinteger>::max)() );
144 result.emplace_back( &typeid( FIsTrue ), detail::DEFAULT_FUNCTIONS.fIsTrue ? detail::DEFAULT_FUNCTIONS.DbgCntInvocationsFIsTrue : (std::numeric_limits<uinteger>::max)() );
145
146 debug::DbgLockMaps(true);
147 {
148 for (auto* typeIt : debug::DbgKnownCustomFunctions ) {
149 // search corresponding default implementation.
150 auto usage= (std::numeric_limits<uinteger>::max)();
151
152 #if ALIB_MONOMEM
153 auto implIt= customFunctionMap.Find( CustomFunctionKey(&detail::DEFAULT_FUNCTIONS, *typeIt) );
154 #else
155 auto implIt= customFunctionMap.find( CustomFunctionKey(&detail::DEFAULT_FUNCTIONS, *typeIt) );
156 #endif
157 if( implIt != customFunctionMap.end() )
158 usage= implIt->second.DbgCntInvocations;
159
160 result.emplace_back( typeIt, usage );
161 } }
162 debug::DbgLockMaps(false);
163
164 return result;
165}
166
168 std::vector<std::pair<const std::type_info*,uinteger>>& output ) {
169 output.clear();
170 if(functionTable.fHashcode ) output.emplace_back( &typeid( FHashcode ), functionTable.DbgCntInvocationsFHashcode );
172 if(functionTable.fClone ) output.emplace_back( &typeid( FClone ), functionTable.DbgCntInvocationsFClone ); )
173 if(functionTable.fIsNotNull) output.emplace_back( &typeid( FIsNotNull), functionTable.DbgCntInvocationsFIsNotNull );
174 if(functionTable.fEquals ) output.emplace_back( &typeid( FEquals ), functionTable.DbgCntInvocationsFEquals );
175 if(functionTable.fIsLess ) output.emplace_back( &typeid( FIsLess ), functionTable.DbgCntInvocationsFIsLess );
176 if(functionTable.fIsTrue ) output.emplace_back( &typeid( FIsTrue ), functionTable.DbgCntInvocationsFIsTrue );
177
178 // add custom function types
179 {
180 for( auto funcIt= customFunctionMap.begin() ; funcIt != customFunctionMap.end() ; ++funcIt )
181 if( funcIt->first.Parent == &functionTable )
182 output.emplace_back( &funcIt->first.Type , funcIt->second.DbgCntInvocations );
183} }
184
185#endif // ALIB_DEBUG_BOXING
186
187}} // namespace [alib::boxing]
#define IF_ALIB_THREADS(...)
#define ALIB_DBG(...)
#define IF_ALIB_MONOMEM(...)
#define ALIB_CALLER_PRUNED
void SingleThreaded()
Definition assert.cpp:39
std::vector< std::pair< const std::type_info *, uinteger > > GetKnownFunctionTypes()
Definition vtable.cpp:136
std::vector< detail::VTable * > GetKnownVTables()
Definition vtable.cpp:112
void getFunctionTypes(const detail::FunctionTable &input, std::vector< std::pair< const std::type_info *, uinteger > > &output)
Definition vtable.cpp:167
This namespace implements internals of namespace #"alib::boxing;2".
Definition vtable.cpp:1
FunctionTable DEFAULT_FUNCTIONS
The default box-functions set.
Definition vtable.cpp:53
DOXYGEN.
Definition box.cpp:17
TMonoAllocator< lang::HeapAllocator > GLOBAL_ALLOCATOR
Definition alox.cpp:14
monomem::TMonoAllocator< lang::HeapAllocator > MonoAllocator
containers::HashSet< TAllocator, T, THash, TEqual, THashCaching, TRecycling > HashSet
Type alias in namespace #"%alib". See type definition #"alib::containers::HashSet".
containers::HashMap< TAllocator, TKey, TMapped, THash, TEqual, THashCaching, TRecycling > HashMap
Type alias in namespace #"%alib".
threads::RecursiveLock RecursiveLock
Type alias in namespace #"%alib".
void setCustom(const std::type_info &rtti, void *implementation)
Definition vtable.cpp:81
uinteger DbgCntInvocationsFHashcode
Debug-compilation counter for the number of invocations.
Definition vtable.hpp:36
uinteger DbgCntInvocationsFIsTrue
Debug-compilation counter for the number of invocations.
Definition vtable.hpp:40
FIsNotNull::Signature fIsNotNull
Entry for built-in function #"FIsNotNull".
Definition vtable.hpp:24
uinteger DbgCntInvocationsFIsLess
Debug-compilation counter for the number of invocations.
Definition vtable.hpp:39
FIsTrue::Signature fIsTrue
Entry for built-in function #"FIsTrue".
Definition vtable.hpp:27
FIsLess::Signature fIsLess
Entry for built-in function #"FIsLess".
Definition vtable.hpp:26
void * getCustom(const std::type_info &rtti, bool isInvocation) const
Definition vtable.cpp:67
FEquals::Signature fEquals
Entry for built-in function #"FEquals".
Definition vtable.hpp:25
uinteger DbgCntInvocationsFIsNotNull
Debug-compilation counter for the number of invocations.
Definition vtable.hpp:37
uinteger DbgCntInvocationsFEquals
Debug-compilation counter for the number of invocations.
Definition vtable.hpp:38
FClone::Signature fClone
Entry for built-in function #"FClone".
Definition vtable.hpp:29
uinteger DbgCntInvocationsFClone
Debug-compilation counter for the number of invocations.
Definition vtable.hpp:42
FHashcode::Signature fHashcode
Entry for built-in function #"FHashcode".
Definition vtable.hpp:23
static void Shutdown()
Needs to be called only in debug versions to shut down internal hashtables cleanly.
Definition vtable.cpp:56