ALib C++ Library
Library Version: 2402 R1
Documentation generated by doxygen
Loading...
Searching...
No Matches
configresourcepool.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_RESOURCES_CONFIG_RESOURCE_POOL)
12# endif
13
14# if !defined (HPP_ALIB_STRINGS_LOCALSTRING)
16# endif
17#endif // !defined(ALIB_DOX)
18
19#if !defined (_GLIBCXX_CSTDARG) && !defined (_CSTDARG_)
20# include <cstdarg>
21#endif
22
23namespace alib::lang::resources {
24
26: allocator( 8 * 1024 )
27, cache ( &allocator )
28, Config ( lang::CreateDefaults::No )
29{
31 Priorities::DefaultValues,
33}
34
39
40
41
43 const NString& name,
44 const String& resource )
45{
46 variable.Declare( category, name );
48 bool existed= variable.Priority() != Priorities::NONE;
49
50 variable.Declare( category, name );
51 variable.Add( resource );
53
54 return existed;
55}
56
57
58void ConfigResourcePool::BootstrapBulk( const nchar* category, ... )
59{
60 va_list args;
61 va_start(args, category);
62 for(;;)
63 {
64 NString name= va_arg( args, const nchar* );
65 if( name.IsNull() )
66 break;
67
68 variable.Declare( category, name );
69 variable.Add( String(va_arg( args, const character* )) );
71
72 // \checkpromise: when typed ALib assertions and warnings are available, then
73 // raise a warning if the result of above EmplaceOrAssign is an assign, aka the
74 // bulk data existed already.
75 }
76 va_end(args);
77}
78
79
80const String& ConfigResourcePool::Get( const NString& category, const NString& name ALIB_DBG(, bool dbgAssert ) )
81{
83
84 // pre-calculate hashCode
85 const detail::Key key { category, name };
86 auto hashCode= detail::Key::Hash()( key );
87
88 // search replacement map
89 auto replacementsIt= cache.Find( key, hashCode );
90 if( replacementsIt != cache.end() )
92 return replacementsIt.Mapped();
93#else
94 return replacementsIt.Mapped().first;
95#endif
96
97 // load variable
98 if( Config.Load( variable.Declare( category, name ) ) != Priorities::NONE )
99 {
100#if !ALIB_DEBUG_RESOURCES
101 return cache.InsertUnique( {key, variable.GetString() }, hashCode )
102 .Mapped();
103#else
104 return cache.InsertUnique( {key, { variable.GetString(), 0} }, hashCode )
105 .Mapped().first;
106#endif
107 }
108
109 ALIB_ASSERT_ERROR( !dbgAssert, "RESOURCES", "Unknown resource! Category: ", NString64(category),
110 ", Name: ", NString64(name) )
111 return NULL_STRING;
112}
113
114} // namespace [alib::lang::resources]
ALIB_API Priorities Load(Variable &variable)
ALIB_API Priorities Store(Variable &variable, const String &externalizedValue=nullptr)
ALIB_API void Add(const String &value)
Definition variable.cpp:223
ALIB_API Variable & Declare(const VariableDecl &declaration, const Box &replacements)
Definition variable.cpp:77
const String & GetString(int idx=0)
Definition variable.hpp:780
Priorities Priority() const
Definition variable.hpp:617
void InsertPlugin(TPlugin *plugin, TPriorities priority, lang::Responsibility responsibility=lang::Responsibility::KeepWithSender)
TPluginType * GetPluginTypeSafe(TPriorities priority)
virtual ALIB_API void BootstrapBulk(const nchar *category,...) override
bool BootstrapAddOrReplace(const NString &category, const NString &name, const String &data) override
NALIB_API const String & Get(const NString &category, const NString &name, bool dbgAssert) override
Iterator InsertUnique(const T &value)
Iterator Find(const KeyType &key)
ALIB_FORCE_INLINE T * Emplace(TArgs &&... args)
constexpr bool IsNull() const
Definition string.hpp:395
#define A_CHAR(STR)
#define ALIB_ASSERT_ERROR(cond,...)
Definition alib.hpp:984
#define ALIB_LOCK
#define ALIB_DBG(...)
Definition alib.hpp:457
#define ALIB_DEBUG_RESOURCES
Definition prepro.dox:44
@ KeepWithSender
Keeps responsibility, e.g. when passing an object.
static ALIB_FORCE_INLINE void Destruct(T *object)
NLocalString< 64 > NString64
Type alias name for TLocalString<nchar,64> .
characters::character character
Type alias in namespace alib.
strings::TString< character > String
Type alias in namespace alib.
characters::nchar nchar
Type alias in namespace alib.
String NULL_STRING
A global instance of a nulled string of standard character size.
Definition string.cpp:470