ALib C++ Library
Library Version: 2402 R1
Documentation generated by doxygen
Loading...
Searching...
No Matches
localresourcepool.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_LOCALRESOURCEPOOL)
12# endif
13#endif // !defined(ALIB_DOX)
14
15#if !defined (_GLIBCXX_CSTDARG) && !defined (_CSTDARG_)
16# include <cstdarg>
17#endif
18
19#if ALIB_DEBUG_RESOURCES && !defined (_GLIBCXX_ALGORITHM) && !defined(_ALGORITHM_)
20# include <algorithm>
21#endif
22
23#if ALIB_CAMP
24# if !defined (HPP_ALIB_LANG_FORMAT_FORMATTER_PYTHONSTYLE)
26# endif
27# if !defined (HPP_ALIB_STRINGS_UTIL_TOKENIZER)
29# endif
30#endif
31
32#if ALIB_DEBUG_RESOURCES && !defined (HPP_ALIB_COMPATIBILITY_STD_STRINGS_IOSTREAM)
34#endif
35namespace alib::lang {
36
37/**
38 * This is the reference documentation of sub-namespace \b resources of module \alib_basecamp.
39 *
40 * Extensive documentation for this namespace is provided with chapter
41 * \ref alib_basecamp_resources "3. Namespace alib::lang::resources" of the
42 * Programmer's Manual of that module..
43 */
44namespace resources {
45
46#if ALIB_DEBUG_RESOURCES
48#endif
49
51 const NString& name,
52 const String& resource )
53{
54
55#if !ALIB_DEBUG_RESOURCES
56 auto it= data.InsertOrAssign( detail::Key {category, name}, resource );
57#else
58 auto it= data.InsertOrAssign( detail::Key {category, name}, { resource, 0 } );
60 {
61 (*DbgResourceLoadObserver) << (it.second ? "Adding Resource: " : "Replacing Resource: " )
62 << category
63 << "/" << name << "=" << resource << std::endl;
64 }
65#endif
66
67 return !it.second;
68}
69
70void LocalResourcePool::BootstrapBulk( const nchar* category, ... )
71{
72 // find / create category
73 detail::Key key {category, nullptr};
74
75 va_list args;
76 va_start(args, category);
77 for(;;)
78 {
79 key.Name= NString( va_arg( args, const nchar* ) );
80 if( key.Name.IsNull() )
81 break;
82
83 String val = va_arg( args, const character* );
84#if ALIB_DEBUG_RESOURCES
86 (*DbgResourceLoadObserver) << "Bulk Resource: " << category
87 << "/" << key.Name << "=" << val << std::endl;
88#endif
89
90ALIB_DBG( auto result=)
91#if !ALIB_DEBUG_RESOURCES
92 data.EmplaceOrAssign( key, val );
93#else
94 data.EmplaceOrAssign( key, std::make_pair(val,0) );
95#endif
96
97ALIB_ASSERT_WARNING( result.second, "RESOURCES",
98 NString4K () << "Replacing resource with BootstrapBulk: "
99 << category << "/" << key.Name << "=" << val )
100 // \checkpromise: when typed ALib assertions and warnings are available, then
101 // raise a warning if the result of above EmplaceOrAssign is an assign, aka the
102 // bulk data existed already.
103 }
104 va_end(args);
105 }
106
107
108const String& LocalResourcePool::Get( const NString& category, const NString& name ALIB_DBG(, bool dbgAssert ) )
109{
110 // search main map
111 auto dataIt= data.Find( detail::Key { category, name } );
112 if( dataIt != data.end() )
113 {
114#if !ALIB_DEBUG_RESOURCES
115 return dataIt.Mapped();
116#else
117 dataIt.Mapped().second++;
118 return dataIt.Mapped().first;
119#endif
120 }
121 ALIB_ASSERT_ERROR( !dbgAssert, "RESOURCES",
122 NString1K() << "Unknown resource! Category: \"" << category
123 << "\", Name: \"" << name
124 << "\"." )
125 return NULL_STRING;
126
127}
128
129
130#if ALIB_DEBUG_RESOURCES
131std::vector<std::tuple<NString, NString, String, integer>>
133{
134 ALIB_WARNING( "STRINGS",
135 "ResourcePool::DbgGetList was not overridden by the ResourcePool type set. "
136 "Note that type built-in ALib type LocalResourcePool does provide an implementation." )
137
138 return std::vector<std::tuple<NString, NString, String, integer>>();
139}
140
141
142std::vector<std::pair<NString, integer>>
144{
145 ALIB_WARNING( "STRINGS",
146 "ResourcePool::DbgGetCategories was not overridden by the ResourcePool type set. "
147 "Note that type built-in ALib type LocalResourcePool does provide an implementation." )
148
149 return std::vector<std::pair<NString, integer>>();
150}
151
152std::vector<std::tuple<NString, NString, String, integer>>
154{
155 std::vector<std::tuple<NString, NString, String, integer>> result;
156
157 result.reserve( static_cast<size_t>( data.Size() ) );
158 for( auto& it : data )
159 {
160 result.emplace_back(
161 it.first.Category,
162 it.first.Name,
163 it.second.first,
164 it.second.second );
165 }
166
167 std::sort( result.begin(), result.end(),
168 [] (const std::tuple<NString, NString, String, integer>& a,
169 const std::tuple<NString, NString, String, integer>& b )
170 {
171
172 auto comp= std::get<0>(a).template CompareTo<true, Case::Ignore>( std::get<0>(b) );
173 if( comp != 0 )
174 return comp < 0;
175
176 return std::get<1>(a).template CompareTo<true, Case::Ignore>( std::get<1>(b) ) < 0;
177 }
178 );
179 return result;
180}
181
182std::vector<std::pair<NString, integer>>
184{
185 std::vector<std::pair<NString, integer>> result;
186
187 auto list= DbgGetList();
188 NString lastCat= nullptr;
189 for( auto& entry : list )
190 {
191 if( !lastCat.Equals( std::get<0>(entry) ) )
192 {
193 lastCat= std::get<0>(entry);
194 result.push_back( { std::get<0>(entry), 0 } );
195 }
196 ++result.back().second;
197 }
198
199 return result;
200}
201
202
203
204#if ALIB_CAMP
205
206AString ResourcePool::DbgDump( std::vector<std::tuple<NString, NString, String, integer>>& list,
207 const NString& catFilter, const String& format )
208{
209 AString result;
210 NString actCategory( nullptr );
212 for( auto it : list )
213 {
214 if( catFilter.IsNotEmpty() )
215 {
216 TokenizerN cats( catFilter, ',');
217 bool found= false;
218 while( cats.HasNext() )
219 found|= cats.Next().Trim().Equals<true, Case::Ignore>(std::get<0>(it) );
220 if( !found )
221 continue;
222 }
223
224 if( actCategory != std::get<0>(it) )
225 {
226 actCategory= std::get<0>(it);
227 result << NewLine()
228 << '[' << actCategory << ']' << NewLine();
229 }
230
231 formatter->Format( result, format, std::get<0>(it), std::get<1>(it),
232 std::get<2>(it), std::get<3>(it) );
233 }
234 formatter->Release();
235
236 return result;
237}
238#endif // ALIB_CAMP
239#endif // ALIB_DEBUG_RESOURCES
240
241}} // namespace [alib::lang::resources]
static SPFormatter AcquireDefault(const NCString &dbgFile, int dbgLine, const NCString &dbgFunc)
virtual ALIB_API std::vector< std::tuple< NString, NString, String, integer > > DbgGetList() override
virtual ALIB_API void BootstrapBulk(const nchar *category,...) override
virtual ALIB_API bool BootstrapAddOrReplace(const NString &category, const NString &name, const String &data) override
virtual NALIB_API const String & Get(const NString &category, const NString &name, bool dbgAssert) override
virtual ALIB_API std::vector< std::pair< NString, integer > > DbgGetCategories() override
virtual ALIB_API std::vector< std::tuple< NString, NString, String, integer > > DbgGetList()
static ALIB_API AString DbgDump(std::vector< std::tuple< NString, NString, String, integer > > &list, const NString &catFilter=nullptr, const String &format=A_CHAR("({3:}) {1}={2!TAB20!ESC<!Q}\n"))
virtual ALIB_API std::vector< std::pair< NString, integer > > DbgGetCategories()
InsertOrAssign(const KeyType &key, const MappedType &mapped)
std::pair< Iterator, bool > EmplaceOrAssign(const KeyType &key, TArgs &&... args)
Iterator Find(const KeyType &key)
constexpr bool IsNotEmpty() const
Definition string.hpp:420
bool Equals(const TString< TChar > &rhs) const
Definition string.hpp:573
ALIB_API TSubstring< TChar > & Next(lang::Whitespaces trimming=lang::Whitespaces::Trim, TChar newDelim='\0')
Definition tokenizer.cpp:18
#define ALIB_WARNING(...)
Definition alib.hpp:981
#define ALIB_ASSERT_ERROR(cond,...)
Definition alib.hpp:984
#define ALIB_ASSERT_WARNING(cond,...)
Definition alib.hpp:985
#define ALIB_DBG(...)
Definition alib.hpp:457
#define ALIB_CALLER_PRUNED
Definition alib.hpp:845
constexpr CString NewLine()
Definition cstring.hpp:528
strings::TString< nchar > NString
Type alias in namespace alib.
NLocalString< 1024 > NString1K
Type alias name for TLocalString<nchar,1024> .
characters::character character
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