ALib C++ Library
Library Version: 2412 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
vtable.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
10# include "alib/boxing/boxing.hpp"
12# if ALIB_DEBUG
14# endif
15#endif // !DOXYGEN
16
17#if ALIB_MONOMEM && ALIB_CONTAINERS
20#else
21# include <unordered_map>
22# include <unordered_set>
23#endif
24
25
26
27namespace alib { namespace boxing { namespace detail {
28
29#if !DOXYGEN
30
31#if ALIB_MONOMEM && ALIB_CONTAINERS
35#else
36 extern std::unordered_set<TypeFunctors::Key, TypeFunctors::Hash, TypeFunctors::EqualTo> DbgKnownCustomFunctions;
37 extern std::unordered_map<TypeFunctors::Key, detail::VTable*, TypeFunctors::Hash, TypeFunctors::EqualTo> DbgKnownVTables;
38 extern std::unordered_map<TypeFunctors::Key, detail::VTable*, TypeFunctors::Hash, TypeFunctors::EqualTo> DbgKnownVTablesArray;
39#endif
40
41extern ALIB_API void DbgLockMaps( bool doLock );
42
43
44// #################################################################################################
45// Custom function hash map implementation
46// #################################################################################################
47namespace
48{
49 struct CustomFunctionKey
50 {
51 const FunctionTable* Parent;
52 const std::type_info& Type;
53
54 CustomFunctionKey( const FunctionTable* parent, const std::type_info& type )
55 : Parent(parent)
56 , Type (type )
57 {}
58 };
59
60 struct CustomFunctionMapped
61 {
62 void* Implementation;
63ALIB_DBG(uinteger DbgCntInvocations; )
64
65 CustomFunctionMapped( void* implementation )
66 : Implementation (implementation)
67ALIB_DBG(,DbgCntInvocations (0 ) )
68 {}
69 };
70
71
72 struct CustomFunctionHash
73 {
74 std::size_t operator()(const CustomFunctionKey& key) const
75 {
76 size_t result= reinterpret_cast<size_t>(key.Parent)
77 ^ key.Type.hash_code();
78 result^= (result << 21 );
79 result^= (result >> 11);
80 return result;
81 }
82
83 };
84
85 struct CustomFunctionEqualTo
86 {
87 bool operator()(const CustomFunctionKey& lhs, const CustomFunctionKey& rhs) const
88 {
89 return lhs.Parent == rhs.Parent
90 && lhs.Type == rhs.Type;
91 }
92 };
93
94 #if ALIB_MONOMEM && ALIB_CONTAINERS
96 CustomFunctionKey, CustomFunctionMapped,
97 CustomFunctionHash,
98 CustomFunctionEqualTo > customFunctionMap(monomem::GLOBAL_ALLOCATOR);
99 #else
100 std::unordered_map< CustomFunctionKey, CustomFunctionMapped,
101 CustomFunctionHash,
102 CustomFunctionEqualTo > customFunctionMap;
103 #endif
104
105}// anonymous namespace
106
107#endif // !DOXYGEN
108
109// #################################################################################################
110// struct Functions
111// #################################################################################################
113
114#if (ALIB_MONOMEM && ALIB_CONTAINERS && ALIB_DEBUG)
116{
117 #if ALIB_MONOMEM && ALIB_DEBUG_BOXING
118 DbgKnownCustomFunctions.Reset();
119 DbgKnownVTables .Reset();
120 DbgKnownVTablesArray .Reset();
121 #endif
122 customFunctionMap .Reset();
123}
124#endif
125
126
127void* FunctionTable::getCustom( const std::type_info& rtti ALIB_DBG(, bool isInvocation) ) const
128{
129#if ALIB_MONOMEM && ALIB_CONTAINERS
130 auto it= customFunctionMap.Find( CustomFunctionKey(this, rtti) );
131#else
132 auto it= customFunctionMap.find( CustomFunctionKey(this, rtti) );
133#endif
134 if ( it != customFunctionMap.end() )
135 {
136 ALIB_DBG( if( isInvocation )
137 ++it->second.DbgCntInvocations; )
138 return it->second.Implementation;
139 }
140 return nullptr;
141}
142
143void FunctionTable::setCustom( const std::type_info& rtti, void* impl )
144{
145 #if ALIB_DEBUG_BOXING
146 detail::DbgLockMaps(true);
147 #if ALIB_MONOMEM
148 DbgKnownCustomFunctions.InsertIfNotExistent( &rtti );
149 #else
150 DbgKnownCustomFunctions.emplace( &rtti );
151 #endif
152 detail::DbgLockMaps(false);
153 #endif
154
155 // search existing (replace)
156 #if ALIB_MONOMEM && ALIB_CONTAINERS
157 customFunctionMap.InsertOrAssign( CustomFunctionKey(this, rtti), CustomFunctionMapped(impl) );
158 #else
159 if( customFunctionMap.size() == 0 )
160 customFunctionMap.reserve( 50 );
161 customFunctionMap.insert_or_assign( CustomFunctionKey(this, rtti), CustomFunctionMapped(impl) );
162 #endif
163
164}
165
166} // namespace alib::boxing[::detail]
167
168using namespace detail;
169
170// #################################################################################################
171// DBGBoxing Function Lists Implementation
172// (located here due to anonymous function table)
173// #################################################################################################
174#if ALIB_DEBUG_BOXING
175std::vector<detail::VTable*> DbgBoxing::GetKnownVTables()
176{
177 DbgLockMaps(true);
178
179 std::vector<detail::VTable*> result;
180 #if ALIB_MONOMEM
181 result.reserve( size_t(
182 DbgKnownVTables .Size()
183 + DbgKnownVTablesArray.Size() ) );
184 #else
185 result.reserve( DbgKnownVTables .size()
186 + DbgKnownVTablesArray.size() );
187 #endif
188
189 for( int type= 0 ; type < 2 ; ++type )
190 {
191 auto& map= type == 0 ? DbgKnownVTables
192 : DbgKnownVTablesArray;
193 for( auto it= map.begin() ; it!= map.end() ; ++it )
194 result.emplace_back( it->second );
195 }
196
197 DbgLockMaps(false);
198 return result;
199}
200
201std::vector<std::pair<const std::type_info*,uinteger>> DbgBoxing::GetKnownFunctionTypes()
202{
203 std::vector<std::pair<const std::type_info*,uinteger>> result;
204 result.emplace_back( &typeid( FHashcode ), detail::DEFAULT_FUNCTIONS.fHashcode ? detail::DEFAULT_FUNCTIONS.DbgCntInvocationsFHashcode : (std::numeric_limits<uinteger>::max)() );
206 result.emplace_back( &typeid( FClone ), detail::DEFAULT_FUNCTIONS.fClone ? detail::DEFAULT_FUNCTIONS.DbgCntInvocationsFClone : (std::numeric_limits<uinteger>::max)() ); )
207 result.emplace_back( &typeid( FIsNotNull ), detail::DEFAULT_FUNCTIONS.fIsNotNull ? detail::DEFAULT_FUNCTIONS.DbgCntInvocationsFIsNotNull : (std::numeric_limits<uinteger>::max)() );
208 result.emplace_back( &typeid( FEquals ), detail::DEFAULT_FUNCTIONS.fEquals ? detail::DEFAULT_FUNCTIONS.DbgCntInvocationsFEquals : (std::numeric_limits<uinteger>::max)() );
209 result.emplace_back( &typeid( FIsLess ), detail::DEFAULT_FUNCTIONS.fIsLess ? detail::DEFAULT_FUNCTIONS.DbgCntInvocationsFIsLess : (std::numeric_limits<uinteger>::max)() );
210 result.emplace_back( &typeid( FIsTrue ), detail::DEFAULT_FUNCTIONS.fIsTrue ? detail::DEFAULT_FUNCTIONS.DbgCntInvocationsFIsTrue : (std::numeric_limits<uinteger>::max)() );
212 result.emplace_back( &typeid( FAppend<character, lang::HeapAllocator>), detail::DEFAULT_FUNCTIONS.fAppend ? detail::DEFAULT_FUNCTIONS.DbgCntInvocationsFAppend : (std::numeric_limits<uinteger>::max)() ); )
213
214 detail::DbgLockMaps(true);
215 {
216 for (auto* typeIt : detail::DbgKnownCustomFunctions )
217 {
218 // search corresponding default implementation.
219 auto usage= (std::numeric_limits<uinteger>::max)();
220
221 #if ALIB_MONOMEM
222 auto implIt= customFunctionMap.Find( CustomFunctionKey(&detail::DEFAULT_FUNCTIONS, *typeIt) );
223 #else
224 auto implIt= customFunctionMap.find( CustomFunctionKey(&detail::DEFAULT_FUNCTIONS, *typeIt) );
225 #endif
226 if( implIt != customFunctionMap.end() )
227 usage= implIt->second.DbgCntInvocations;
228
229 result.emplace_back( typeIt, usage );
230 }
231 }
232 detail::DbgLockMaps(false);
233
234 return result;
235}
236
238 std::vector<std::pair<const std::type_info*,uinteger>>& output )
239{
240 output.clear();
241 if(functionTable.fHashcode ) output.emplace_back( &typeid( FHashcode ), functionTable.DbgCntInvocationsFHashcode );
243 if(functionTable.fClone ) output.emplace_back( &typeid( FClone ), functionTable.DbgCntInvocationsFClone ); )
244 if(functionTable.fIsNotNull) output.emplace_back( &typeid( FIsNotNull ), functionTable.DbgCntInvocationsFIsNotNull );
245 if(functionTable.fEquals ) output.emplace_back( &typeid( FEquals ), functionTable.DbgCntInvocationsFEquals );
246 if(functionTable.fIsLess ) output.emplace_back( &typeid( FIsLess ), functionTable.DbgCntInvocationsFIsLess );
247 if(functionTable.fIsTrue ) output.emplace_back( &typeid( FIsTrue ), functionTable.DbgCntInvocationsFIsTrue );
249 if(functionTable.fAppend ) output.emplace_back( &typeid( FAppend<character, lang::HeapAllocator>), functionTable.DbgCntInvocationsFAppend ); )
250
251 // add custom function types
252 {
253 for( auto funcIt= customFunctionMap.begin() ; funcIt != customFunctionMap.end() ; ++funcIt )
254 if( funcIt->first.Parent == &functionTable )
255 output.emplace_back( &funcIt->first.Type , funcIt->second.DbgCntInvocations );
256 }
257}
258
259
260#if ALIB_DEBUG_CONTAINERS
261void DbgBoxing::DumpCustomFunctionHashMapMetrics( AString& target, bool detailedBucketList )
262{
263 target << containers::DbgDumpDistribution( customFunctionMap, detailedBucketList );
264}
265#endif
266
267
268#endif // ALIB_DEBUG_BOXING
269
270
271}} // namespace [alib::boxing]
272
273
274
275// #################################################################################################
276// Static VTables for fundamentals
277// #################################################################################################
278
279ALIB_BOXING_VTABLE_DEFINE( void* , vt_voidP )
280ALIB_BOXING_VTABLE_DEFINE( BoxesHA*, vt_boxes )
281#if ALIB_MONOMEM
282ALIB_BOXING_VTABLE_DEFINE( BoxesMA*, vt_boxesma )
283#endif
284ALIB_BOXING_VTABLE_DEFINE_ARRAYTYPE( Box , vt_boxarray )
285
286DOX_MARKER([DOX_BOXING_OPTIMIZE_DEFINE_1])
287ALIB_BOXING_VTABLE_DEFINE( bool, vt_bool )
288DOX_MARKER([DOX_BOXING_OPTIMIZE_DEFINE_1])
289
290
291#if !ALIB_FEAT_BOXING_BIJECTIVE_INTEGRALS
292
293 ALIB_BOXING_VTABLE_DEFINE( integer , vt_integer )
294 ALIB_BOXING_VTABLE_DEFINE( uinteger , vt_uinteger)
295
296#else
297 ALIB_BOXING_VTABLE_DEFINE( int8_t , vt_int8_t)
298 ALIB_BOXING_VTABLE_DEFINE( uint8_t , vt_uint8_t)
299 ALIB_BOXING_VTABLE_DEFINE( int16_t , vt_int16_t)
300 ALIB_BOXING_VTABLE_DEFINE( uint16_t , vt_uint16_t)
301 ALIB_BOXING_VTABLE_DEFINE( int32_t , vt_int32_t)
302 ALIB_BOXING_VTABLE_DEFINE( uint32_t , vt_uint32_t)
303 ALIB_BOXING_VTABLE_DEFINE( intGap_t , vt_intGap_t)
304 ALIB_BOXING_VTABLE_DEFINE( uintGap_t , vt_uintGap_t)
305
306 #if ALIB_SIZEOF_INTEGER == 8
307 ALIB_BOXING_VTABLE_DEFINE( int64_t , vt_int64_t)
308 ALIB_BOXING_VTABLE_DEFINE( uint64_t , vt_uint64_t)
309 #endif
310
311#endif
312
313 ALIB_BOXING_VTABLE_DEFINE( double, vt_double )
314#if ALIB_SIZEOF_LONGDOUBLE_REPORTED <= 2 * ALIB_SIZEOF_INTEGER
315 ALIB_BOXING_VTABLE_DEFINE( long double, vt_long_double )
316#endif
317#if ALIB_FEAT_BOXING_BIJECTIVE_FLOATS
318 ALIB_BOXING_VTABLE_DEFINE( float , vt_float )
319#endif
320
321#if !ALIB_FEAT_BOXING_BIJECTIVE_CHARACTERS
322 ALIB_BOXING_VTABLE_DEFINE( wchar , vt_wchar )
323#else
324 ALIB_BOXING_VTABLE_DEFINE( char , vt_char )
325 ALIB_BOXING_VTABLE_DEFINE( wchar_t , vt_wchar_t )
326 ALIB_BOXING_VTABLE_DEFINE( char16_t , vt_char16_t )
327 ALIB_BOXING_VTABLE_DEFINE( char32_t , vt_char32_t )
328#endif
329
330DOX_MARKER([DOX_BOXING_OPTIMIZE_DEFINE_2])
331ALIB_BOXING_VTABLE_DEFINE_ARRAYTYPE( char, vt_arr_char )
332DOX_MARKER([DOX_BOXING_OPTIMIZE_DEFINE_2])
333ALIB_BOXING_VTABLE_DEFINE_ARRAYTYPE( wchar_t , vt_arr_wchar_t )
334ALIB_BOXING_VTABLE_DEFINE_ARRAYTYPE( char16_t , vt_arr_char16_t)
335ALIB_BOXING_VTABLE_DEFINE_ARRAYTYPE( char32_t , vt_arr_char32_t)
336
337// #################################################################################################
338// Static VTables for standard types
339// #################################################################################################
340ALIB_BOXING_VTABLE_DEFINE( std::type_info* , vt_std_type_info )
341
342// #################################################################################################
343// Static VTables for low-level ALib types
344// #################################################################################################
345// CodeMarker_CommonEnums
359ALIB_BOXING_VTABLE_DEFINE( alib::lang::Reach , vt_alib_Recursive )
370
#define ALIB_BOXING_VTABLE_DEFINE(TMapped, Identifier)
Definition vtable.inl:473
#define ALIB_API
Definition alib.hpp:639
#define IF_ALIB_STRINGS(...)
Definition alib.hpp:328
#define ALIB_BOXING_VTABLE_DEFINE_ARRAYTYPE(TMapped, Identifier)
Definition vtable.inl:483
#define ALIB_DBG(...)
Definition alib.hpp:390
#define IF_ALIB_MONOMEM(...)
Definition alib.hpp:312
FunctionTable DEFAULT_FUNCTIONS
The default box-functions set.
Definition vtable.cpp:112
AString DbgDumpDistribution(const THashtable &hashtable, bool detailedBucketList)
const alib::boxing::Box & Type
Inclusion
Denotes how members of a set something should be taken into account.
ValueReference
Denotes if a value is interpreted as an absolute or relative number.
Whitespaces
Denotes whether a string is trimmed or not.
CreateDefaults
Denotes whether default entities should be created or not.
Caching
Denotes if a cache mechanism is enabled or disabled.
Side
Denotes if something is left or right.
Propagation
Denotes whether a e.g a setting should be propagated.
Switch
Denotes if sth. is switched on or off.
Initialization
Used for example with constructors that allow to suppress initialization of members.
Safeness
Denotes whether something should be performed in a safe or unsafe fashion.
SortOrder
Denotes sort order.
Reach
Denotes the reach of something.
SourceData
Denotes if the source data should be moved or copied.
Timezone
Denotes whether a time value represents local time or UTC.
Case
Denotes upper and lower case character treatment.
Alignment
Denotes Alignments.
Timing
Denotes if asynchronous tasks become synchronized.
CreateIfNotExists
Denotes whether something should be created if it does not exist.
Phase
Denotes a phase, e.g.,of a transaction.
ContainerOp
Denotes standard container operations.
ALIB_API MonoAllocator GLOBAL_ALLOCATOR
Definition alib.cpp:69
containers::HashSet< TAllocator, T, THash, TEqual, THashCaching, TRecycling > HashSet
Type alias in namespace alib. See type definition alib::containers::HashSet.
monomem::TMonoAllocator< lang::HeapAllocator > MonoAllocator
containers::HashMap< TAllocator, TKey, TMapped, THash, TEqual, THashCaching, TRecycling > HashMap
Type alias in namespace alib.
static ALIB_API std::vector< std::pair< const std::type_info *, uinteger > > GetKnownFunctionTypes()
Definition vtable.cpp:201
static ALIB_API void DumpCustomFunctionHashMapMetrics(AString &target, bool detailedBucketList)
Definition vtable.cpp:261
static ALIB_API void getFunctionTypes(const detail::FunctionTable &input, std::vector< std::pair< const std::type_info *, uinteger > > &output)
Definition vtable.cpp:237
static ALIB_API std::vector< detail::VTable * > GetKnownVTables()
Definition vtable.cpp:175
uinteger DbgCntInvocationsFHashcode
Debug-compilation counter for the number of invocations.
Definition vtable.inl:43
uinteger DbgCntInvocationsFIsNotNull
Debug-compilation counter for the number of invocations.
Definition vtable.inl:44
FAppend< character, lang::HeapAllocator >::Signature fAppend
Entry for built-in function FAppend<character>.
Definition vtable.inl:40
FIsLess::Signature fIsLess
Entry for built-in function FIsLess.
Definition vtable.inl:34
ALIB_API void * getCustom(const std::type_info &rtti, bool isInvocation) const
Definition vtable.cpp:127
uinteger DbgCntInvocationsFIsLess
Debug-compilation counter for the number of invocations.
Definition vtable.inl:46
uinteger DbgCntInvocationsFClone
Debug-compilation counter for the number of invocations.
Definition vtable.inl:49
uinteger DbgCntInvocationsFAppend
Debug-compilation counter for the number of invocations.
Definition vtable.inl:52
uinteger DbgCntInvocationsFIsTrue
Debug-compilation counter for the number of invocations.
Definition vtable.inl:47
ALIB_API void setCustom(const std::type_info &rtti, void *implementation)
Definition vtable.cpp:143
uinteger DbgCntInvocationsFEquals
Debug-compilation counter for the number of invocations.
Definition vtable.inl:45
FClone::Signature fClone
Entry for built-in function FClone.
Definition vtable.inl:37
FHashcode::Signature fHashcode
Entry for built-in function FHashcode.
Definition vtable.inl:31
FIsTrue::Signature fIsTrue
Entry for built-in function FIsTrue.
Definition vtable.inl:35
static ALIB_API void Shutdown()
Needs to be called only in debug versions to shut down internal hashtables cleanly.
Definition vtable.cpp:115
FEquals::Signature fEquals
Entry for built-in function FEquals.
Definition vtable.inl:33
FIsNotNull::Signature fIsNotNull
Entry for built-in function FIsNotNull.
Definition vtable.inl:32