ALib C++ Library
Library Version: 2402 R1
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
9#if !defined(ALIB_DOX)
10#if !defined (HPP_ALIB_LANG_FORMAT_FORMATTER_PYTHONSTYLE)
12#endif
13
14#if !defined (_GLIBCXX_CMATH) && !defined (_CMATH_)
15# include <cmath>
16#endif
17#endif // !defined(ALIB_DOX)
18
19
20// For code compatibility with ALox Java/C++
21// We have to use underscore as the start of the name and for this have to disable a compiler
22// warning. But this is a local code (cpp file) anyhow.
23#if defined(__clang__)
24 #pragma clang diagnostic push
25 #pragma clang diagnostic ignored "-Wreserved-id-macro"
26#endif
27
28#if defined(__clang__)
29 #pragma clang diagnostic pop
30#endif
31
32using namespace alib::strings;
33
34namespace alib::lang::format {
35
37
38#if !defined(ALIB_DOX)
39Boxes& Formatter::Acquire( ALIB_DBG( const NCString& file, int line, const NCString& func ) )
40{
41 // call parent's implementation
42 #if ALIB_THREADS
44 , ThreadLock::Acquire(file,line,func); )
45
46 #else
48 # if ALIB_DEBUG
49 (void) file;
50 (void) line;
51 (void) func;
53 # endif
54 #endif
55
56 // if this is the first (recursive) lock, we reset
57 if( cntAcquirements == 1 )
58 reset();
59
60 if( Next )
61 Next->Acquire( ALIB_DBG( file, line, func ) );
62
63 boxes.clear();
64 return boxes;
65}
66#endif //!defined(ALIB_DOX)
67
68
69
71{
72 #if ALIB_THREADS
74 #else
76 #endif
77
78 if( Next )
79 Next->Release();
80}
81
82
84{
86
87 // initialize formatters
88 Formatter* formatter= this;
89 do
90 formatter->initializeFormat();
91 while( (formatter= formatter->Next.get()) != nullptr );
92
93 // loop over boxes
94 integer argIdx= 0;
95 while ( argIdx < args.Size() - 1 )
96 {
97 String formatString;
98 const Box& actual= args[static_cast<size_t>(argIdx++)];
99 if( actual.IsType<void>() )
100 continue;
101
102 // Either this is a string, or we convert the box to a string. This fetches anything
103 // that is string like, including string types that are encapsulated in BoxedAs, etc.
104 if( actual.IsArrayOf<character>() )
105 formatString= actual.Unbox<String>();
106 else
107 formatString= formatStringBuffer.Reset( actual );
108
109 if( formatString.IsEmpty() )
110 continue;
111
112 // try each formatter unless one succeeds
113 integer qtyConsume= 0;
114 formatter= this;
115 while( ( qtyConsume= formatter->format( target, formatString, args, static_cast<int>(argIdx ) ) )
116 == 0
117 && (formatter= formatter->Next.get()) != nullptr )
118 {}
119
120 // no formatter reacted?
121 if( qtyConsume == 0 )
122 // we just append the string...
123 target.template _<false>( formatString );
124 else
125 // ...otherwise arguments were consumed
126 argIdx+= static_cast<size_t>( qtyConsume );
127 }
128
129 // the last argument was not consumed?
130 if ( argIdx==args.Size() - 1 && !args.back().IsType<void>() )
131 target.template _<false>( args.back() );
132 return *this;
133}
134
135
136
138{
141
142 // if both sub-formatters are of std type, we do the same for them.
143 if( Next && reference.Next )
144 Next->CloneSettings( *reference.Next );
145}
146
147
148} // namespace [alib::lang::format]
bool IsType() const
bool IsArrayOf() const
Definition box.inl:724
const TUnboxable Unbox() const
integer Size() const
Definition boxes.inl:258
static ALIB_API SPFormatter defaultFormatter
virtual ALIB_API void CloneSettings(Formatter &reference)
ALIB_API Boxes & Acquire(const NCString &dbgFile, int dbgLine, const NCString &dbgFunc)
ALIB_API void Release()
defined(ALIB_DOX)
Definition formatter.cpp:70
std::shared_ptr< Formatter > Next
ALIB_API Formatter & formatLoop(AString &target, const Boxes &args)
Definition formatter.cpp:83
constexpr bool IsEmpty() const
Definition string.hpp:414
ALIB_API void Acquire(const NCString &dbgFile, int dbgLine, const NCString &dbgFunc)
ALIB_API void Release()
defined(ALIB_DOX)
#define ALIB_DBG_PREVENT_RECURSIVE_METHOD_CALLS
Definition owner.hpp:122
#define ALIB_DBG(...)
Definition alib.hpp:457
#define ALIB_REL_DBG(releaseCode,...)
Definition alib.hpp:459
platform_specific integer
Definition integers.hpp:50
void DbgCheckSingleThreaded()
Definition alib.cpp:226
std::shared_ptr< lang::format::Formatter > SPFormatter
characters::character character
Type alias in namespace alib.
void Set(TNumberFormat *other=nullptr)