ALib C++ Library
Library Version: 2402 R1
Documentation generated by doxygen
Loading...
Searching...
No Matches
dbgboxing.hpp
Go to the documentation of this file.
1/** ************************************************************************************************
2 * \file
3 * This header file is part of module \alib_boxing of the \aliblong.
4 *
5 * \emoji :copyright: 2013-2024 A-Worx GmbH, Germany.
6 * Published under \ref mainpage_license "Boost Software License".
7 **************************************************************************************************/
8#ifndef HPP_ALIB_BOXING_DBGBOXING
9#define HPP_ALIB_BOXING_DBGBOXING 1
10
11
12#ifndef HPP_ALIB_BOXING_BOXING
13# include "alib/boxing/boxing.hpp"
14#endif
15
16#if ALIB_DEBUG_BOXING
17
18#if ALIB_CAMP && !defined(HPP_ALIB_LANG_FORMAT_FORMATTER)
20#endif
21
22#if !defined (_GLIBCXX_TUPLE) && !defined(_TUPLE_)
23# include <tuple>
24#endif
25
26namespace alib { namespace boxing {
27
28
29#if ALIB_CAMP
30namespace detail {
31/** ************************************************************************************************
32 * Template class implementing a monotonically allocated vector of variadic tuples whose first
33 * element is of type \alib{strings;TString<TChar>;String}.
34 *
35 * When new tuples are added with method #Add, then memory for copying the provided string is
36 * allocated using an internal field of type \alib{monomem;MonoAllocator}.
37 * This allows to add single strings, which are allocated in bigger memory chunks.
38 *
39 * Standard vector operations, including insertions and deletions are still allowed.
40 * In other words, it is allowed to store string objects which are pointing to string data that
41 * is \b not allocated in the internal chunks.
42 *
43 * The associated values are especially useful when the vector is sorted (e.g. using
44 * \c std::sort), because such associations will be kept intact and allow referencing back
45 * to whatever the strings represents.
46 *
47 * \par Availability
48 * This struct is only available if compilation symbol \ref ALIB_DEBUG_BOXING is set and
49 * furthermore, module \alib_basecamp is included in the \alibdist.
50 **************************************************************************************************/
51template< typename... TAssociatedTypes >
52struct DbgStringTable : public std::vector<std::tuple<String, TAssociatedTypes...>>
53{
54 /** Shortcut to the <c>std::tuple</c>-type that instantiations of this template class store. */
55 using ElementType= std::tuple<String, TAssociatedTypes...>;
56
57 /** Shortcut to the base type of this type. */
58 using Base = std::vector<ElementType>;
59
60 /** Allocator for making copies of inserted strings */
62
63 /**
64 * Constructor.
65 * @param monoAllocator A mono allocator used to create copies of the inserted strings
66 */
67 DbgStringTable( MonoAllocator& monoAllocator )
68 : allocator(monoAllocator)
69 {}
70
71 /**
72 * Adds a tuple. The contents of the string of the tuple (its first element) is copied
73 * from the given string \p{src}. The memory for this string is allocated using
74 * the allocator provided in the constructor.<br>
75 * The other members of the tuple added are forwarded from variadic parameter block \p{args}.
76 *
77 * @param src The string to copy into the first member of the tuple.
78 * @param args Variadic arguments to fill the rest of the inserted tuple.
79 *
80 * @return A reference to the string that was added.
81 */
82 template <typename... TArgs>
83 String& Add( const String& src, TArgs&&... args )
84 {
85 // on errors, see note above!
86 Base::emplace_back( allocator.EmplaceString( src ), std::forward<TArgs>(args)... );
87 return std::get<0>( std::vector<std::tuple<String, TAssociatedTypes...>>::back() );
88 }
89};
90} // namespace detail
91
92#endif // ALIB_CAMP
93
94
95
96/** ************************************************************************************************
97 * Provides low- and high-level debug methods for \alib_boxing_nl.<br>
98 *
99 * This type is not constructible, as all method are static.
100 *
101 * To shorten custom type names, custom namespaces might be added to static vector
102 * #RemovableNamespaces prior to invoking a method.
103 *
104 * \note
105 * While some of the methods are written with a non-capital letter (which usually indicates
106 * that they are \c protected or \c private), those are still declared \c public. These methods
107 * are "inner" methods used by the similar named capitalized methods.
108 * Their use is recommended, when writing custom "composite" debug-functions, as the various
109 * output parameters might be reused with subsequent invocations.
110 *
111 * \par Availability
112 * This struct is only available if compilation symbol \ref ALIB_DEBUG_BOXING is set.
113 *
114 * \par
115 * The following methods are available independent from the \alibdist.
116 * - #GetKnownVTables
117 * - #GetKnownFunctionTypes
118 * - #GetSpecificFunctionTypes
119 * - #getFunctionTypes
120 * \par
121 * The following methods become available if module \alib_strings is included in the \alibdist:
122 * - #TypeName
123 * - #typeName
124 * - #removeNamespaces
125 *
126 * \par
127 * All other methods become available if module \alib_basecamp is included in the \alibdist.
128 *
129 * \see
130 * Chapter \ref alib_boxing_more_debug of the
131 * \ref alib_mod_boxing "Programmer's Manual" of module \alib_boxing_nl.
132 **************************************************************************************************/
134{
135 /** ********************************************************************************************
136 * Not constructible
137 **********************************************************************************************/
138 DbgBoxing() = delete;
139
140 // #############################################################################################
141 // Function Lists
142 // #############################################################################################
143
144 /** ********************************************************************************************
145 * Returns all \e vtable singletons that have been created during program execution so far.
146 * One \e vtable is created for each mapped type used.
147 *
148 * \see
149 * The result can be conveniently passed to #DumpVTables.
150 * @return A vector of pointers to objects of type \alib{boxing;detail::VTable}.
151 **********************************************************************************************/
152 ALIB_API static
153 std::vector<detail::VTable*>
155
156 /** ********************************************************************************************
157 * Returns a vector of \c std::type_info objects, representing all function types that either
158 * a default or a type specific implementation has been registered for.<br>
159 * A second value in the vector provides the number of invocations of a default implementation.
160 * If such is not available, this number is set to <c>std::numeric_limits<uinteger>::max()</c>.
161 *
162 * The list includes the built-in function types.
163 *
164 * \see
165 * The result can be conveniently passed to #DumpFunctions.
166 * @return A vector of pairs of type information structs and usage numbers.
167 **********************************************************************************************/
168 ALIB_API static
169 std::vector<std::pair<const std::type_info*,uinteger>>
171
172 /** ********************************************************************************************
173 * Collects all function declarator types of the given box-function table.
174 *
175 * \see
176 * The result can be conveniently passed to #DumpFunctions.
177 *
178 * @param input The function table to use.
179 * @param output The result vector to fill.
180 **********************************************************************************************/
181 ALIB_API static
182 void getFunctionTypes( const detail::FunctionTable& input,
183 std::vector<std::pair<const std::type_info*,uinteger>>& output );
184
185 /** ********************************************************************************************
186 * Collects all function declarator types, with type-specific implementations.
187 * Parameter \p{vtable} might for example be retrieved from a box instance with
188 * \alib{boxing;Box::DbgGetVTable}.
189 *
190 * \see
191 * The result can be conveniently passed to #DumpFunctions.
192 * @param vtable The vtable to get all function implementations for.
193 * @return A vector of type information structs.
194 **********************************************************************************************/
195 static
196 std::vector<std::pair<const std::type_info*,uinteger>>
198 {
199 std::vector<std::pair<const std::type_info*,uinteger>> result;
200 getFunctionTypes( vtable->Functions, result );
201 return result;
202 }
203
204 /** ********************************************************************************************
205 * Convenience function: invokes #GetSpecificFunctionTypes(const detail::VTable*) with
206 * the \e vtable of the box created or passed with the invocation.
207 *
208 * \see
209 * The result can be conveniently passed to #DumpFunctions.
210 *
211 * @param box A box that the vtable is used of.
212 * @return A vector of type information structs.
213 **********************************************************************************************/
214 static
215 std::vector<std::pair<const std::type_info*,uinteger>>
217 {
219 }
220
221#if ALIB_STRINGS
222 // #############################################################################################
223 // Type Name
224 // #############################################################################################
225 /** ********************************************************************************************
226 * Writes the (demangled) mapped type that the given \p{vtable} represents.
227 *
228 * \par Availability
229 * This method is included only if module \alib_strings is included in the \alibdist.
230 *
231 * @param vtable The \e vtable to get the mapped type name for.
232 * @return A string containing the requested information.
233 **********************************************************************************************/
234 static
236 {
237 AString result;
238 typeName( vtable, result );
239 return result;
240 }
241
242 /** ********************************************************************************************
243 * Convenience function: invokes #TypeName(const detail::VTable*) with
244 * the \e vtable of the box created or passed with the invocation.
245 *
246 * \par Availability
247 * This method is included only if module \alib_strings is included in the \alibdist.
248 *
249 * @param box The box to get the mapped type name for.
250 * @return A string containing the requested information.
251 **********************************************************************************************/
252 static
253 AString TypeName ( const Box& box )
254 {
255 AString result;
256 typeName( box.DbgGetVTable(), result );
257 return result;
258 }
259
260
261 /** ********************************************************************************************
262 * Implementation of #TypeName.
263 *
264 * \par Availability
265 * This method is included only if module \alib_strings is included in the \alibdist.
266 *
267 * @param vtable The vtable.
268 * @param[out] result The target string to write the type information to.
269 **********************************************************************************************/
270 ALIB_API static
271 void typeName ( const detail::VTable* vtable, AString& result );
272
273#endif // ALIB_STRINGS
274
275#if ALIB_CAMP
276 // #############################################################################################
277 // Type Info
278 // #############################################################################################
279 /** ********************************************************************************************
280 * Returns human readable information about the type \p{TBoxable} in respect to how boxing
281 * of objects of this type is performed.
282 *
283 * The implementation of this method creates a default value of \p{TBoxable}, boxes this
284 * and returns the result of #TypeInfo(const Box&,const String&,const String&).
285 * Hence, for types that are not default constructible, inner function \b %TypeInfo
286 * has to be used with an accordingly boxed "sample" of type \p{TBoxable}
287 *
288 * \note
289 * Does not work for type \c void*, which is not customizable anyhow
290 * (always boxed as \c void*).
291 *
292 *
293 * \par Availability
294 * This method is included only if module \alib_strings is included in the \alibdist.
295 *
296 * @tparam TBoxable The boxable type to investigate in.
297 * @param headline The headline to write.
298 * @param indent Spaces to write prior to each entry.
299 * @return A string containing the requested information.
300 **********************************************************************************************/
301 template<typename TBoxable>
302 static
303 AString TypeInfo( const String& headline = A_CHAR( "Boxing Information For Boxable Type: ") ,
304 const String& indent = A_CHAR( " " ) )
305
306 {
307 return DbgBoxing::TypeInfo<TBoxable>( TBoxable(), headline, indent );
308 }
309
310 /** ********************************************************************************************
311 * Collects necessary information from template type \p{TBoxable} and invokes
312 * #typeInfo.<br>
313 * See method #TypeInfo.
314 *
315 * \par Availability
316 * This method is included only if module \alib_strings is included in the \alibdist.
317 *
318 * @tparam TBoxable The boxable type to investigate in.
319 * @param box A box containing a "sample" of \p{TBoxable.}. If a different box is given
320 * this method has undefined behavior
321 * @param headline The headline to write.
322 * @param indent Spaces to write prior to each entry.
323 * @return A string containing the requested information.
324 **********************************************************************************************/
325 template<typename TBoxable>
326 static
327 AString TypeInfo( const Box& box,
328 const String& headline= A_CHAR("Boxing Information For Boxable Type: "),
329 const String& indent = A_CHAR(" " ) )
330 {
331 AString target;
332 using TSrc = ATMP_RCV(TBoxable);
333 using TPlain= ATMP_RCVP(TBoxable);
334
335 lang::DbgTypeDemangler srcRTTIDmgld( typeid(TSrc) );
336
337 // repeat twice to get auto-tabs adjusted
339 for( int theSakeOfAutoTabs= 0 ; theSakeOfAutoTabs < 2 ; ++theSakeOfAutoTabs )
340 {
341 target.Reset() << headline << srcRTTIDmgld.Get() << NewLine();
342 removeNamespaces( target, headline.Length() );
343
344 if( ATMP_EQ(TSrc, std::nullptr_t ) )
345 {
346 target << indent << "'std::nullptr_t' sets a box to void state" << NewLine();
347 return target;
348 }
349
350 typeInfo( target,
351 box.DbgGetVTable(),
352 indent,
353 ATMP_IS_PTR(TSrc),
356 sizeof(boxing::Placeholder) <= sizeof(ATMP_RCVP(TPlain)),
357 std::is_copy_constructible<ATMP_RCVP(TPlain)>::value,
358 std::is_trivially_destructible<ATMP_RCVP(TPlain)>::value,
359 boxing::TT_IsUnboxable <TSrc >::value );
360
361 }
362 formatter->Release();
363 return target;
364 }
365
366 /** ********************************************************************************************
367 * See method #TypeInfo, which invokes this method using template parameter \p{TBoxable}
368 *
369 * \par Availability
370 * This method is included only if module \alib_strings is included in the \alibdist.
371 *
372 * @param[out] target The target string.
373 * @param vtable The vtable of the mapped type.
374 * @param indent Empty or a string of spaces that are alwys preprended
375 * prior to each entry.
376 * @param srcIsPointer Denotes if \p{TBoxable} is a pointer type.
377 * @param isValueTypeCustomized Denotes if the value type of \p{TBoxable} is customized.
378 * @param isPointerTypeCustomized Denotes if the pointer type of \p{TBoxable} is
379 * customized.
380 * @param fitsToPlaceholder Denotes if the value type of \p{TBoxable} would fit to
381 * placeholder.
382 * @param copyConstructible Denotes if the value type of \p{TBoxable} is
383 * copy constructible.
384 * @param triviallyDestructible Denotes if the value type of \p{TBoxable} is
385 * trivially destructible.
386 * @param isUnboxable Denotes if \p{TBoxable} is unboxable.
387 **********************************************************************************************/
388 ALIB_API static
389 void typeInfo( AString& target,
390 const detail::VTable* vtable,
391 const String& indent,
392 bool srcIsPointer,
393 bool isValueTypeCustomized,
394 bool isPointerTypeCustomized,
395 bool fitsToPlaceholder,
396 bool copyConstructible,
397 bool triviallyDestructible,
398 bool isUnboxable );
399
400 // #############################################################################################
401 // Dump
402 // #############################################################################################
403 /** ********************************************************************************************
404 * Takes a vector of pairs of \c std::type_info pointers and a usage counter, and returns an
405 * \b AString with a sorted list of type names including counter information.
406 *
407 * \par Availability
408 * This method is included only if module \alib_basecamp is included in the \alibdist.
409 *
410 * @param input A list of types.
411 * @param headline The headline to write.
412 * @param indent Spaces to write prior to each entry.
413 * @return A list of demangled type names.
414 **********************************************************************************************/
415 ALIB_API static
417 const std::vector<std::pair<const std::type_info*,uinteger>>& input,
418 const String& headline = EmptyString(),
419 const String& indent = EmptyString() );
420
421 /** ********************************************************************************************
422 * Helper for (bigger part of) #DumpFunctions.
423 *
424 * \par Availability
425 * This method is included only if module \alib_basecamp is included in the \alibdist.
426 *
427 * @param input A list of types and usage numbers.
428 * @param[out] output A string to write to.
429 * @param headline The headline to write.
430 * @param indent Spaces to write prior to each entry.
431 * @param tmpStrings A buffer needed for internal use.
432 **********************************************************************************************/
433 ALIB_API static
434 void dumpFunctions (
435 const std::vector<std::pair<const std::type_info*,uinteger>>& input,
436 AString& output,
437 const String& headline,
438 const String& indent,
440
441
442 /** ********************************************************************************************
443 * Lists all mapped types with either static or dynamic \e vtables.
444 * Along with each type, its default function implementations are given.
445 *
446 * \par Availability
447 * This method is included only if module \alib_basecamp is included in the \alibdist.
448 *
449 * @param staticVtables If \c true, only types with static vtables are listed.
450 * Otherwise only those with dynamic vtables.
451 * @param includeFunctions If \c true for each type, the list of specialized functions are
452 * listed.
453 * @return A string containing the dump.
454 **********************************************************************************************/
455 ALIB_API static
456 AString DumpVTables( bool staticVtables,
457 bool includeFunctions= false );
458
459 /** ********************************************************************************************
460 * Internally used by overloaded #DumpVTables methods.
461 *
462 * \par Availability
463 * This method is included only if module \alib_basecamp is included in the \alibdist.
464 *
465 * @param[out] target The target string to write to.
466 * @param[out] vtableNames Output parameter that receives the strings.
467 * @param staticVtables If \c true, only types with static vtables are listed.
468 * Otherwise only those with dynamic vtables.
469 * @param includeFunctions If \c true, For each \e vtable the list of specialized
470 * function is written.
471 **********************************************************************************************/
472 ALIB_API static
473 void dumpVTables( AString& target,
475 bool staticVtables,
476 bool includeFunctions );
477
478#if ALIB_DEBUG_MONOMEM
479 /** ********************************************************************************************
480 * Invokes \alib{monomem;DbgDumpDistribution} on the internal hash table used to register
481 * and fetch implementations of custom box-functions.
482 *
483 * \par Availability
484 * This method is included only if module \alib_basecamp is included in the \alibdist
485 * and compilation symbol \ref ALIB_DEBUG_MONOMEM is given.
486 *
487 * @param[out] target The target string to write to.
488 * @param detailedBucketList If \c true is given, for each bucket a line with its size
489 * value and a "size bar" is written.
490 **********************************************************************************************/
491 ALIB_API static
492 void DumpCustomFunctionHashMapMetrics( AString& target, bool detailedBucketList );
493#endif
494
495 /** ********************************************************************************************
496 * First, lists all mapped types with static, then those with dynamic \e vtables.
497 * Along with each type, its default function implementations are given.
498 *
499 * \par Availability
500 * This method is included only if module \alib_basecamp is included in the \alibdist.
501 *
502 * @return A string containing the dump.
503 **********************************************************************************************/
504 ALIB_API static
505 AString DumpAll ();
506
507#endif // ALIB_CAMP
508
509 // #############################################################################################
510 // Tools
511 // #############################################################################################
512#if ALIB_STRINGS
513 /** ********************************************************************************************
514 * Removes namespaces in the given \p{string}. The function is used with all methods that
515 * create string values containing type names.
516 * Note that custom namespaces might be added to static field #RemovableNamespaces
517 * prior to invoking any method.
518 *
519 * @param string The string to remove the namespace from.
520 * @param startIndex The index within the string to start searching for removable namespaces.
521 * @return A string containing the requested information.
522 **********************************************************************************************/
523 ALIB_API static
524 AString& removeNamespaces( AString& string, integer startIndex );
525
526 /**
527 * See method #removeNamespaces. Pre-initialized with <b>"alib::"</b>.
528 */
529 ALIB_API static
530 std::vector<String> RemovableNamespaces;
531#endif // ALIB_STRINGS
532
533}; // class DbgBoxingDump
534
535
536} // namespace alib[::boxing]
537
538/// Type alias in namespace \b alib.
540
541} // namespace [alib]
542
543#endif // ALIB_DEBUG_BOXING
544#endif // HPP_ALIB_BOXING_DBGBOXING
const detail::VTable * DbgGetVTable() const
Definition box.inl:509
ALIB_API const char * Get()
static SPFormatter AcquireDefault(const NCString &dbgFile, int dbgLine, const NCString &dbgFunc)
strings::TString< TChar > EmplaceString(const strings::TString< TChar > &src)
constexpr integer Length() const
Definition string.hpp:357
#define A_CHAR(STR)
#define ATMP_RCV( T)
Definition tmp.hpp:40
#define ALIB_API
Definition alib.hpp:538
#define ATMP_RCVP( T)
Definition tmp.hpp:43
#define ATMP_EQ( T, TEqual)
Definition tmp.hpp:32
#define ATMP_IS_PTR(T)
Definition tmp.hpp:27
#define ALIB_CALLER_PRUNED
Definition alib.hpp:845
Definition alib.cpp:57
constexpr CString NewLine()
Definition cstring.hpp:528
constexpr CString EmptyString()
Definition cstring.hpp:502
std::shared_ptr< lang::format::Formatter > SPFormatter
strings::TString< character > String
Type alias in namespace alib.
lang::integer integer
Type alias in namespace alib.
Definition integers.hpp:286
static ALIB_API void dumpVTables(AString &target, detail::DbgStringTable< const detail::VTable * > &vtableNames, bool staticVtables, bool includeFunctions)
static ALIB_API AString & removeNamespaces(AString &string, integer startIndex)
static ALIB_API std::vector< std::pair< const std::type_info *, uinteger > > GetKnownFunctionTypes()
Definition vtable.cpp:206
static AString TypeName(const detail::VTable *vtable)
static std::vector< std::pair< const std::type_info *, uinteger > > GetSpecificFunctionTypes(const detail::VTable *vtable)
static ALIB_API void DumpCustomFunctionHashMapMetrics(AString &target, bool detailedBucketList)
Definition vtable.cpp:266
static ALIB_API void typeInfo(AString &target, const detail::VTable *vtable, const String &indent, bool srcIsPointer, bool isValueTypeCustomized, bool isPointerTypeCustomized, bool fitsToPlaceholder, bool copyConstructible, bool triviallyDestructible, bool isUnboxable)
static ALIB_API alib::AString DumpFunctions(const std::vector< std::pair< const std::type_info *, uinteger > > &input, const String &headline=EmptyString(), const String &indent=EmptyString())
static ALIB_API void dumpFunctions(const std::vector< std::pair< const std::type_info *, uinteger > > &input, AString &output, const String &headline, const String &indent, detail::DbgStringTable< uinteger > &tmpStrings)
static ALIB_API void getFunctionTypes(const detail::FunctionTable &input, std::vector< std::pair< const std::type_info *, uinteger > > &output)
Definition vtable.cpp:242
static ALIB_API std::vector< detail::VTable * > GetKnownVTables()
Definition vtable.cpp:180
static AString TypeInfo(const String &headline=A_CHAR("Boxing Information For Boxable Type: "), const String &indent=A_CHAR(" "))
static std::vector< std::pair< const std::type_info *, uinteger > > GetSpecificFunctionTypes(const Box &box)
static ALIB_API void typeName(const detail::VTable *vtable, AString &result)
static ALIB_API AString DumpAll()
static AString TypeName(const Box &box)
static ALIB_API std::vector< String > RemovableNamespaces
static ALIB_API AString DumpVTables(bool staticVtables, bool includeFunctions=false)
static AString TypeInfo(const Box &box, const String &headline=A_CHAR("Boxing Information For Boxable Type: "), const String &indent=A_CHAR(" "))
std::vector< ElementType > Base
Definition dbgboxing.hpp:58
DbgStringTable(MonoAllocator &monoAllocator)
Definition dbgboxing.hpp:67
String & Add(const String &src, TArgs &&... args)
Definition dbgboxing.hpp:83
std::tuple< String, TAssociatedTypes... > ElementType
Definition dbgboxing.hpp:55