ALib C++ Library
Library Version: 2412 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
formatter.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#if !DOXYGEN
10# include <cmath>
11#endif // !DOXYGEN
12
13
14// For code compatibility with ALox Java/C++
15// We have to use underscore as the start of the name and for this have to disable a compiler
16// warning. But this is a local code (cpp file) anyhow.
17#if defined(__clang__)
18 #pragma clang diagnostic push
19 #pragma clang diagnostic ignored "-Wreserved-id-macro"
20#endif
21
22#if defined(__clang__)
23 #pragma clang diagnostic pop
24#endif
25
26using namespace alib::strings;
27
28namespace alib::lang::format {
29
31#if ALIB_THREADS
33#endif
34
35
36
37#if !DOXYGEN
38
39template<>
40Formatter& Formatter::formatLoop( AString& target, const BoxesMA& args )
43
44 // initialize formatters
45 Formatter* formatter= this;
46 do
47 formatter->initializeFormat();
48 while( (formatter= formatter->Next.Get()) != nullptr );
49
50 // loop over boxes
51 integer argIdx= 0;
52 while ( argIdx < args.Size() - 1 )
53 {
54 String formatString;
55 const Box& actual= args[size_t(argIdx++)];
56 if( actual.IsType<void>() )
57 continue;
58
59 // Either this is a string, or we convert the box to a string. This fetches anything
60 // that is string like, including string-types that are encapsulated in BoxedAs, etc.
61 if( actual.IsArrayOf<character>() )
62 formatString= actual.Unbox<String>();
63 else
64 formatString= formatStringBuffer.Reset( actual );
65
66 if( formatString.IsEmpty() )
67 continue;
68
69 // try each formatter unless one succeeds
70 integer qtyConsume= 0;
71 formatter= this;
72 while( ( qtyConsume= formatter->format( target, formatString, args, static_cast<int>(argIdx ) ) )
73 == 0
74 && (formatter= formatter->Next.Get()) != nullptr )
75 {}
76
77 // no formatter reacted?
78 if( qtyConsume == 0 )
79 // we just append the string...
80 target.template _<NC>( formatString );
81 else
82 // ...otherwise arguments were consumed
83 argIdx+= size_t( qtyConsume );
84 }
85
86 // the last argument was not consumed?
87 if ( argIdx==args.Size() - 1 && !args.back().template IsType<void>() )
88 target.template _<NC>( args.back() );
89 return *this;
90}
91#endif // !DOXYGEN
92
93
94
96{
99
100 // if both sub-formatters are of std type, we do the same for them.
101 if( Next && reference.Next )
102 Next->CloneSettings( *reference.Next );
103}
104
105#if !DOXYGEN
106template<>
108{
109 boxes.clear();
110 boxes.Add( args );
111 formatLoop( target, boxes );
112 return *this;
113}
114
115template<>
117{
118 boxes.clear();
119 boxes.Add( args );
120 formatLoop( target, boxes );
121 return *this;
122}
123#endif // !DOXYGEN
124
125} // namespace [alib::lang::format]
126
127
bool IsType() const
bool IsArrayOf() const
Definition box.inl:725
const TUnboxable Unbox() const
integer Size() const
Definition boxes.inl:176
TBoxes & Add()
Definition boxes.inl:74
static ALIB_API threads::RecursiveLock DefaultLock
AString formatStringBuffer
A buffer used for conversion of the next argument if it is not of a string-type.
Definition formatter.hpp:64
static ALIB_API SPFormatter Default
virtual ALIB_API void CloneSettings(Formatter &reference)
Definition formatter.cpp:95
Formatter & formatLoop(AString &target, const boxing::TBoxes< TAllocator > &args)
SharedPtr< Formatter > Next
constexpr bool IsEmpty() const
Definition string.hpp:383
#define ALIB_DCS
#define ALIB_DBG_PREVENT_RECURSIVE_METHOD_CALLS
Definition owner.hpp:483
characters::character character
Type alias in namespace alib.
lang::integer integer
Type alias in namespace alib.
Definition integers.hpp:273
void Set(TNumberFormat *other=nullptr)