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