8#if !defined(ALIB_C20_MODULES) || ((ALIB_C20_MODULES != 0) && (ALIB_C20_MODULES != 1))
9# error "Configuration MACRO ALIB_C20_MODULES has to be given to the compiler as either 0 or 1"
19 module ALib.Variables;
30 if( substitutionVariableStart.
IsEmpty() )
37 int maxReplacements = 50;
41 integer repStart= orig.
IndexOf( substitutionVariableStart, searchStartIdx );
44 buf << orig.
Substring( searchStartIdx, repStart - searchStartIdx );
45 searchStartIdx = repStart;
46 integer varStart= repStart + substitutionVariableStart.
Length();
51 if ( substitutionVariableEnd.
IsEmpty() ) {
56 varLen= idx - varStart;
62 ALIB_WARNING(
"VARIABLES",
"End of substitution variable not found "
63 "(while start was found). Variable name: ",
Name(namebuf) )
67 varLen= idx - varStart;
68 searchStartIdx= idx + substitutionVariableEnd.
Length();
74 buf << substitutionVariableStart;
85 if( replVar.
Try(replVarName) )
86 replVar.
Export( buf, escaper );
93 replVar.
Export( buf, escaper );
99 while( --maxReplacements );
101 if( maxReplacements < 50) {
111 auto it= Tree<Configuration>().types.Find(typeName);
113 "No Meta-Handler found for given variable type \"{}\".\n"
114 "Probably the type was not registered during bootstrap.\n"
115 "Use the macro ALIB_VARIABLES_REGISTER_TYPE in bootstrap phase "
116 "'PrepareConfig' to register your custom types.", typeName)
117 auto* meta= Cursor::Value().meta= *it;
120 Cursor::Value().data =
reinterpret_cast<detail::VDATA*
>(Tree<Configuration>().Pool().Alloc( meta->size(),
alignof(
detail::VDATA)));
121 meta->construct(Cursor::Value().data, Tree<Configuration>().Pool );
122 Cursor::Value().priority= Priority::NONE;
137 for (
int i = 0; i < Tree<Configuration>().CountPlugins(); ++i) {
138 auto& plugin= *Tree<Configuration>().GetPlugin(i);
139 auto plPrio= plugin.GetPriority();
140 if( Cursor::Value().priority <= plPrio && plugin.Get(varName, buf) ) {
142 Cursor::Value().priority= plPrio;
143 Cursor::Value().meta->imPort( Cursor::Value().data,
GetConfiguration(), plugin.GetEscaper(),
144 substitute(buf, substBuf, &plugin.GetEscaper()) );
148 auto cursor= Tree<Configuration>().Root();
149 if( cursor.GoToChild(
A_CHAR(
"$PRESETS"))
150 && cursor.GoTo(varName).IsEmpty()
151 && cursor->meta !=
nullptr
152 && cursor->priority > Value().priority )
155 "Internal error. This must never happen. ")
157 auto* escaper= cursor->declaration ?
reinterpret_cast<const StringEscaper*
>( cursor->declaration )
159 Cursor::Value().priority= cursor->priority;
161 Cursor::Value().meta->imPort( Cursor::Value().data,
169 if( Cursor::Value().priority <= Priority::DefaultValues && defaultValue.
IsNotEmpty() ) {
172 Cursor::Value().priority= Priority::DefaultValues;
173 Cursor::Value().meta->imPort( Cursor::Value().data,
GetConfiguration(), escaper,
174 substitute( defaultValue, substBuf, &escaper) );
187 const String& defaultValue ) {
189 "Invalid Variable. Not associated with a Configuration. Probably a default constructed "
190 "instance.\nCopy or move a valid Variable object before usage.")
193 "Variable name with placeholder(s) given: ", name )
197 if( GoToCreatedPathIfNotExistent(name) == 0
198 && Cursor::Value().meta !=
nullptr )
201 auto it= Tree<Configuration>().types.Find(typeName);
202 if( it == Tree<Configuration>().types.end() ) {
204 "No Meta-Handler found for given variable type \"{}\".\n"
205 "Probably the type was not registered during bootstrap.\n"
206 "Use the macro ALIB_VARIABLES_REGISTER_TYPE in bootstrap phase "
207 "'PrepareConfig' to register your custom types.", typeName )
209 if( *it != Cursor::Value().meta ) {
211 "Variable \"{}\" redeclared with a different typename.\n"
212 "Previous typename: ", Cursor::Value().meta->typeName(), typeName )
219 Cursor::Value().declaration=
nullptr;
220 create( typeName, defaultValue );
226 "Invalid Variable. Not associated with a Configuration.\n"
227 "Probably a default constructed instance.\n"
228 "Copy or move a valid Variable object before usage.")
231 "Variable descriptor \"{}\" with unset placeholders given.", decl->
Name() )
235 if( 0 == GoToCreatedPathIfNotExistent( decl->
Name() )) {
238 "Variable \"{}\" redeclared with different declaration record pointer.\n"
239 "Declaration records should be singletons and their life-time needs to survive\n"
240 "that of the variable. New record will be ignored.", decl->
Name() )
242 auto it= Tree<Configuration>().types.Find(decl->
typeName);
243 if( it == Tree<Configuration>().types.end() ) {
245 "No Meta-Handler found for given variable type \"{}\".\n"
246 "Probably the type was not registered during bootstrap.\n"
247 "Use the macro ALIB_VARIABLES_REGISTER_TYPE in bootstrap phase "
248 "'PrepareConfig' to register your custom types.",
GetDeclaration()->typeName)
255 Cursor::Value().declaration= decl;
261 if( Cursor::Value().priority > requestedPriority )
263 auto prevPriority= Cursor::Value().priority;
264 Cursor::Value().priority= requestedPriority ;
278 Cursor::node=
nullptr;
283 "Invalid Variable. Not associated with a Configuration. Probably a default constructed "
284 "instance.\nCopy or move a valid Variable object before usage.")
287 if( Cursor::GoToRoot().GoTo( name ).IsEmpty()
292 auto cursor= Tree<Configuration>().Root();
293 if( cursor.GoToChild(
A_CHAR(
"$PRESETS")) && cursor.GoTo(name).IsEmpty() ) {
295 "Internal error. This must never happen.")
297 if( Cursor::Value().priority < cursor->priority) {
299 auto* escaper= cursor->declaration ?
reinterpret_cast<const StringEscaper*
>( cursor->declaration )
302 Cursor::Value().priority= cursor->priority;
303 Cursor::Value().meta->imPort( Cursor::Value().data,
317 || Cursor::Value().declaration == decl,
"CONFIG/VARDECL",
318 "Variable \"{}\" redeclared with different declaration record pointer.\n"
319 "Declaration records should be singletons and their life-time needs to survive\n"
320 "that of the variable. New record will be ignored.", decl->
Name() )
322 if( Cursor::Value().declaration==
nullptr)
323 Cursor::Value().declaration= decl;
332 "Tried to import nulled string for variable \"{}\"",
this )
#define ALIB_WARNING(domain,...)
#define ALIB_ASSERT_WARNING(cond, domain,...)
#define ALIB_ERROR(domain,...)
#define ALIB_ASSERT_ERROR(cond, domain,...)
constexpr integer Length() const
constexpr bool IsEmpty() const
integer IndexOf(TChar needle, integer startIdx=0) const
constexpr bool IsNotNull() const
constexpr bool IsNotEmpty() const
integer IndexOfAny(const TString &needles, integer startIdx=0) const
TString< TChar > Substring(integer regionStart, integer regionLength=MAX_LEN) const
String SubstitutionVariableStart
CString SubstitutionVariableDelimiters
String SubstitutionVariableEnd
const String & DefaultValue() const
const String & TypeName() const
const String & Name() const
bool Define(Priority requestedPriority=Priority::Standard)
void Import(const String &src, Priority priority, const StringEscaper *escaper=nullptr)
AString & Export(AString &dest, const StringEscaper *escaper=nullptr) const
AString & Name(AString &target) const
void create(const String &typeName, const String &defaultValue)
Priority GetPriority() const
bool Try(const String &name)
Variable & Declare(const String &name, const String &typeName, const String &defaultValue=NULL_STRING)
const Declaration * GetDeclaration() const
Configuration & GetConfiguration() const
const String & substitute(const String &importString, AString &buf, const StringEscaper *escaper)
@ Include
Chooses inclusion.
lang::integer integer
Type alias in namespace alib.
strings::util::StringEscaper StringEscaper
Type alias in namespace alib.
strings::TString< character > String
Type alias in namespace alib.
strings::TSubstring< character > Substring
Type alias in namespace alib.
LocalString< 256 > String256
Type alias name for #"TLocalString;TLocalString<character,256>".
strings::TAString< character, lang::HeapAllocator > AString
Type alias in namespace alib.
LocalString< 512 > String512
Type alias name for #"TLocalString;TLocalString<character,512>".
@ Definition
A variable was defined or re-defined with the same or a higher priority.
@ Creation
A variable was declared for the first time.