ALib C++ Library
Library Version: 2402 R1
Documentation generated by doxygen
Loading...
Searching...
No Matches
variable.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_CONFIG_VARIABLE)
12#endif
13
14#if !defined (HPP_ALIB_CONFIG_CONFIGURATION)
16#endif
17
18#if !defined (HPP_ALIB_ENUMS_RECORDPARSER)
20#endif
21#endif // !defined(ALIB_DOX)
22
23namespace alib { namespace config {
24
26{
27 auto& self= Self();
28 String128 oldName;
29 String128 oldCategory;
30 if( nameAndCategory == lang::CurrentData::Keep )
31 {
32 oldName << self.Name;
33 oldCategory << self.Category;
34 }
35
36 SelfContained::Reset( &fields );
37 self.Config = nullptr;
38 self.Priority = Priorities::NONE;
39 self.Delim = '\0';
40 self.FmtHints = FormatHints::None;
41
42 self.Category =
43 self.Name =
44 self.Comments =
45 self.Fullname = EmptyString();
46 self.DefaultValue = NullString();
47
48 // our vector needs to be constructed in place! This is needed to make it "forget"
49 // its internal data field.
50 new (&self.values) std::vector<String,StdContMA<String>>( StdContMA<String>(Allocator()) );
51
52 if( nameAndCategory == lang::CurrentData::Keep )
53 {
54 self.Name = Allocator().EmplaceString( oldName );
55 self.Category = Allocator().EmplaceString( oldCategory );
56 }
57
58 return *this;
59}
60
62{
63 auto& self = Self();
64 auto& allocator= Allocator();
65
66 Reset();
67
68 self.Category= allocator.EmplaceString( variable.Category() );
69 self.Name = allocator.EmplaceString( variable.Name () );
70 self.Comments= allocator.EmplaceString( variable.Comments() );
71 self.Delim = variable.Delim () ;
72 self.Fullname= EmptyString();
73 return *this;
74}
75
76
77Variable& Variable::Declare( const VariableDecl& declaration, const Box& replacements )
78{
79 auto& self = Self();
80 auto& allocator= Allocator();
81 Reset();
82
83 self.Delim= declaration.Delim;
84 self.FmtHints= declaration.FmtHints;
85 self.FormatAttrAlignment= allocator.EmplaceString( declaration.FormatAttrAlignment );
86
87 // set Category, Name, Comment
88 String128 bufCategory; ALIB_DBG( bufCategory .DbgDisableBufferReplacementWarning() );
89 String128 bufName; ALIB_DBG( bufName .DbgDisableBufferReplacementWarning() );
90 String1K bufComments; ALIB_DBG( bufComments .DbgDisableBufferReplacementWarning() );
91 String128 bufDefaultValue; ALIB_DBG( bufDefaultValue.DbgDisableBufferReplacementWarning() );
92
93 bufCategory << declaration.Category;
94 bufName << declaration.EnumElementName;
95 bufComments << declaration.Comments;
96 if ( declaration.DefaultValue.IsNotNull() )
97 bufDefaultValue << declaration.DefaultValue;
98
99 // replace strings
100 if( !replacements.IsType<void>() )
101 {
102 String128 replace;
103
105 const Box* replacementPtr;
106 integer qtyReplacements;
107 if ( replacements.IsType<Boxes*>() )
108 {
109 const Boxes* boxes= replacements.Unbox<Boxes*>();
110 replacementPtr = boxes->data();
111 qtyReplacements= boxes->Size();
112 }
113 else if ( replacements.IsArrayOf<Box>() )
114 {
115 replacementPtr = replacements.UnboxArray<Box>();
116 qtyReplacements= replacements.UnboxLength();
117 }
118 else
119 {
120 replacementPtr = &replacements;
121 qtyReplacements= 1;
122 }
123
124 for ( integer replCnt= 0; replCnt< qtyReplacements ; ++replCnt )
125 if ( !replacementPtr->IsType<void>() )
126 {
127 String64 search("%"); search._( replCnt + 1 );
128 replace.Reset( *( replacementPtr + replCnt) );
129 bufCategory .SearchAndReplace( search, replace );
130 bufName .SearchAndReplace( search, replace );
131 bufComments .SearchAndReplace( search, replace );
132 bufDefaultValue.SearchAndReplace( search, replace );
133 }
135 }
136
137 ALIB_ASSERT_WARNING( bufName.IsNotEmpty(), "CONFIG", "Empty variable name given" )
138
139 self.Category = allocator.EmplaceString( bufCategory );
140 self.Name = allocator.EmplaceString( bufName );
141 self.Comments = allocator.EmplaceString( bufComments );
142
143 if ( declaration.DefaultValue.IsNotNull() )
144 self.DefaultValue = allocator.EmplaceString( bufDefaultValue );
145 else
146 self.DefaultValue = NullString();
147
148 return *this;
149}
150
151Variable& Variable::Declare( const String& category, const String& name, character delim ,
152 const String& comments )
153{
154 auto& self = Self();
155 auto& allocator= Allocator();
156
157 Reset();
158
159 self.Category = allocator.EmplaceString( category );
160 self.Name = allocator.EmplaceString( name );
161 self.Comments = allocator.EmplaceString( comments );
162 self.Delim = delim;
163
164 return *this;
165}
166
168 const ComplementString& name,
169 complementChar delim ,
170 const ComplementString& comments )
171{
172 auto& self = Self();
173 auto& allocator= Allocator();
174
175 String2K charWidthConverter;
176 charWidthConverter.DbgDisableBufferReplacementWarning();
177
178 Reset();
179
180 self.Category = allocator.EmplaceString( charWidthConverter.Reset(category) );
181 self.Name = allocator.EmplaceString( charWidthConverter.Reset(name ) );
182 self.Comments = allocator.EmplaceString( charWidthConverter.Reset(comments) );
183 self.Delim = static_cast<character>( delim );
184
185 return *this;
186}
187
188void Variable::ReplaceValue( int idx, const String& replacement )
189{
190 auto& self= Self();
191 if( idx < 0 || idx >= Size() )
192 {
193 ALIB_WARNING( "CONFIG", "Index out of range: ", idx )
194 return;
195 }
196
197 self.values[static_cast<size_t>(idx)]= Allocator().EmplaceString( replacement );
198}
199
200void Variable::ReplaceValue( int idx, Variable& replVariable )
201{
202 auto& self = Self();
203 auto& allocator= Allocator();
204 if( idx < 0 || idx >= Size() )
205 {
206 ALIB_WARNING( "CONFIG", "Index out of range: ", idx )
207 return;
208 }
209
210 integer replSize= replVariable.Size();
211 if( replSize == 0 )
212 {
213 self.values.erase( self.values.begin() + idx );
214 return;
215 }
216 self.values.reserve( static_cast<size_t>( Size() + replSize - 1 ) );
217 self.values[static_cast<size_t>(idx)]= allocator.EmplaceString( replVariable.GetString() );
218 for( int i= 1 ; i < replSize; ++i )
219 self.values.insert( self.values.begin() + idx + i, allocator.EmplaceString( replVariable.GetString(i) ) );
220
221}
222
223void Variable::Add( const String& value )
224{
225 Self().values.push_back(Allocator().EmplaceString( value ) );
226}
227
228
229void Variable::Add( int64_t value )
230{
231 String128 converter(value);
232 Self().values.push_back( Allocator().EmplaceString( converter ) );
233}
234
235void Variable::Add( double value )
236{
237 String128 converter(value);
238 Self().values.push_back( Allocator().EmplaceString( converter ) );
239}
240
241
242bool Variable::IsTrue (int idx) { return idx < Size() ? Self().Config->IsTrue( GetString (idx) )
243 : false; }
244integer Variable::GetInteger(int idx) { return idx < Size() ? static_cast<integer>( GetString(idx).ParseInt())
245 : 0; }
246double Variable::GetFloat (int idx) { return idx < Size() ? GetString(idx).ParseFloat( Self().Config ? &Self().Config->NumberFormat : &NumberFormat::Global )
247 : 0.0; }
248
249
250bool Variable::GetAttribute( const String& attrName, Substring& result, character attrDelim )
251{
252 for ( int i= 0; i< Size(); ++i )
253 {
254 result= GetString( i );
256 && result.ConsumeChar <lang::Case::Ignore, lang::Whitespaces::Trim>( attrDelim ) )
257 {
258 result.Trim();
259 return true;
260 }
261 }
262 result= NullString();
263 return false;
264}
265
267{
268 auto& self = Self();
269 auto& allocator= Allocator();
270 if( self.Fullname.IsEmpty() )
271 {
272 String256 bufFullname; ALIB_DBG( bufFullname.DbgDisableBufferReplacementWarning() );
273 if( self.Category.IsNotEmpty() )
274 bufFullname << self.Category << '_';
275 bufFullname << self.Name;
276
277 self.Fullname= allocator.EmplaceString( bufFullname );
278 }
279
280 return self.Fullname;
281}
282
283
284DOX_MARKER([DOX_ALIB_ENUMS_RECORD_PARSER])
285void VariableDecl::Parse()
286{
288 enums::EnumRecordParser::Get( ERSerializable::EnumElementName ); // field from parent class
289 ERSerializable::MinimumRecognitionLength= 0; // omit reading, but fix to zero
291 enums::EnumRecordParser::Get( FormatAttrAlignment );
292 enums::EnumRecordParser::Get( FmtHints , true ); // last field!
293}
294DOX_MARKER([DOX_ALIB_ENUMS_RECORD_PARSER])
295
296}} // namespace [alib::config]
TElementType * UnboxArray() const
Definition box.inl:948
integer UnboxLength() const
Definition box.inl:979
bool IsType() const
bool IsArrayOf() const
Definition box.inl:724
const TUnboxable Unbox() const
integer Size() const
Definition boxes.inl:258
ALIB_API bool IsTrue(int idx=0)
Definition variable.cpp:242
integer Size() const
Definition variable.hpp:708
ALIB_API bool GetAttribute(const String &attrName, Substring &result, character attrDelim=A_CHAR('='))
Definition variable.cpp:250
ALIB_API const String & Fullname()
Definition variable.cpp:266
ALIB_API void Add(const String &value)
Definition variable.cpp:223
ALIB_API void ReplaceValue(int idx, const String &replacement)
Definition variable.cpp:188
character Delim() const
Definition variable.hpp:506
ALIB_API Variable & Declare(const VariableDecl &declaration, const Box &replacements)
Definition variable.cpp:77
ALIB_API double GetFloat(int idx=0)
Definition variable.cpp:246
const String & Comments() const
Definition variable.hpp:558
const String & Name() const
Definition variable.hpp:496
const String & Category() const
Definition variable.hpp:487
const String & GetString(int idx=0)
Definition variable.hpp:780
ALIB_API Variable & Reset(lang::CurrentData nameAndCategory=lang::CurrentData::Clear)
Definition variable.cpp:25
ALIB_API integer GetInteger(int idx=0)
Definition variable.cpp:244
strings::TString< TChar > EmplaceString(const strings::TString< TChar > &src)
TAString & _(const TString< TChar > &src, integer regionStart, integer regionLength=MAX_LEN)
Definition astring.hpp:1056
void DbgDisableBufferReplacementWarning()
Definition astring.hpp:353
ALIB_API integer SearchAndReplace(TChar needle, TChar replacement, integer startIdx=0)
Definition astring.cpp:330
constexpr bool IsNotEmpty() const
Definition string.hpp:420
constexpr bool IsNotNull() const
Definition string.hpp:402
ALIB_API double ParseFloat(integer startIdx=0, TNumberFormat< TChar > *numberFormat=nullptr, integer *newIdx=nullptr) const
TSubstring & Trim(const TCString< TChar > &whiteSpaces=TT_StringConstants< TChar >::DefaultWhitespaces())
bool ConsumeString(const TString< TChar > &consumable)
#define ALIB_WARNING(...)
Definition alib.hpp:981
#define ALIB_WARNINGS_RESTORE
Definition alib.hpp:715
#define ALIB_WARNINGS_ALLOW_UNSAFE_BUFFER_USAGE
Definition alib.hpp:644
#define ALIB_ASSERT_WARNING(cond,...)
Definition alib.hpp:985
#define ALIB_DBG(...)
Definition alib.hpp:457
@ Trim
Trim whitespaces away.
@ Keep
Chooses not no clear existing data.
Definition alib.cpp:57
characters::complementChar complementChar
Type alias in namespace alib.
constexpr CString EmptyString()
Definition cstring.hpp:502
constexpr String NullString()
Definition string.hpp:2498
characters::character character
Type alias in namespace alib.
lang::integer integer
Type alias in namespace alib.
Definition integers.hpp:286
static ALIB_API void Get(String &result, bool isLastField=false)