ALib C++ Library
Library Version: 2511 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
formatter.cpp
1//##################################################################################################
2// ALib C++ Library
3//
4// Copyright 2013-2025 A-Worx GmbH, Germany
5// Published under 'Boost Software License' (a free software license, see LICENSE.txt)
6//##################################################################################################
7#include "alib_precompile.hpp"
8#if !defined(ALIB_C20_MODULES) || ((ALIB_C20_MODULES != 0) && (ALIB_C20_MODULES != 1))
9# error "Symbol ALIB_C20_MODULES has to be given to the compiler as either 0 or 1"
10#endif
11#if ALIB_C20_MODULES
12 module;
13#endif
14//========================================= Global Fragment ========================================
15#include <cmath>
17//============================================== Module ============================================
18#if ALIB_C20_MODULES
19 module ALib.Format;
20 import ALib.Lang;
21 import ALib.Strings;
22 import ALib.Boxing;
23#else
24# include "ALib.Lang.H"
25# include "ALib.Strings.H"
26# include "ALib.Boxing.H"
27# include "ALib.Format.H"
28#endif
29//========================================== Implementation ========================================
31
32using namespace alib::strings;
33
34/// This is the reference documentation module \alib_format.<br>
35/// Extensive documentation for this namespace is provided with the
36/// \ref alib_mod_format "Programmer's Manual" of that module.
37namespace alib::format {
38
40#if !ALIB_SINGLE_THREADED
41 threads::RecursiveLock Formatter::DefaultLock;
42#endif
43
44
45
46#if !DOXYGEN
47
48template<>
51
52 // initialize formatters
53 Formatter* formatter= this;
54 do
55 formatter->initializeFormat();
56 while( (formatter= formatter->Next.Get()) != nullptr );
57
58 // loop over boxes
59 integer argIdx= 0;
60 while ( argIdx < args.Size() - 1 ) {
61 String formatString;
62 const Box& actual= args[size_t(argIdx++)];
63 if( actual.IsType<void>() )
64 continue;
65
66 // Either this is a string, or we convert the box to a string. This fetches anything
67 // that is string like, including string-types that are encapsulated in BoxedAs, etc.
68 if( actual.IsArrayOf<character>() )
69 formatString= actual.Unbox<String>();
70 else
71 formatString= formatStringBuffer.Reset( actual );
72
73 if( formatString.IsEmpty() )
74 continue;
75
76 // try each formatter unless one succeeds
77 integer qtyConsume= 0;
78 formatter= this;
79 while( ( qtyConsume= formatter->format( target, formatString, args, int(argIdx ) ) )
80 == 0
81 && (formatter= formatter->Next.Get()) != nullptr )
82 {}
83
84 // no formatter reacted?
85 if( qtyConsume == 0 )
86 // we just append the string...
87 target.template _<NC>( formatString );
88 else
89 // ...otherwise arguments were consumed
90 argIdx+= size_t( qtyConsume );
91 }
92
93 // the last argument was not consumed?
94 if ( argIdx==args.Size() - 1 && !args.back().template IsType<void>() )
95 target.template _<NC>( args.back() );
96 return *this;
97}
98#endif // !DOXYGEN
99
100
101
103 DefaultNumberFormat .Set( &reference.DefaultNumberFormat );
105
106 // if both sub-formatters are of std type, we do the same for them.
107 if( Next && reference.Next )
108 Next->CloneSettings( *reference.Next );
109}
110
111#if !DOXYGEN
112template<>
114 boxes.clear();
115 boxes.Add( args );
116 formatLoop( target, boxes );
117 return *this;
118}
119
120template<>
122 boxes.clear();
123 boxes.Add( args );
124 formatLoop( target, boxes );
125 return *this;
126}
127#endif // !DOXYGEN
128
129} // namespace [alib::format]
bool IsArrayOf() const
Definition box.inl:557
bool IsType() const
TValue Unbox() const
Definition box.inl:595
TBoxes & Add()
Definition boxes.inl:55
integer Size() const
Definition boxes.inl:126
NumberFormat DefaultNumberFormat
Definition formatter.inl:96
static ALIB_DLL threads::RecursiveLock DefaultLock
virtual ALIB_DLL void CloneSettings(Formatter &reference)
NumberFormat AlternativeNumberFormat
Formatter & formatLoop(AString &target, const boxing::TBoxes< TAllocator > &args)
static ALIB_DLL SPFormatter Default
SharedPtr< Formatter > Next
Formatter()
Default Constructor.
AString formatStringBuffer
A buffer used for conversion of the next argument if it is not of a string-type.
Definition formatter.inl:77
constexpr bool IsEmpty() const
Definition string.inl:365
#define ALIB_BOXING_VTABLE_DEFINE(TMapped, Identifier)
#define ALIB_DCS
Definition alib.inl:1392
#define ALIB_DBG_PREVENT_RECURSIVE_METHOD_CALLS
Definition alib.inl:1366
strings::TAString< character, lang::HeapAllocator > AString
Type alias in namespace alib.
lang::integer integer
Type alias in namespace alib.
Definition integers.inl:149
format::Formatter Formatter
Type alias in namespace alib.
boxing::Box Box
Type alias in namespace alib.
Definition box.inl:1149
boxing::TBoxes< MonoAllocator > BoxesMA
Type alias in namespace alib.
Definition boxes.inl:193
containers::SharedPtr< format::Formatter > SPFormatter
Definition formatter.inl:42
strings::TString< character > String
Type alias in namespace alib.
Definition string.inl:2189
characters::character character
Type alias in namespace alib.