ALib C++ Library
Library Version: 2412 R0
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 !DOXYGEN
11#endif // !DOXYGEN
12
13#include <cstdarg>
14#if ALIB_DEBUG_RESOURCES
15# include <algorithm>
16#endif
17#if ALIB_CAMP
20#endif
21#if ALIB_DEBUG_RESOURCES
23#endif
24namespace alib::lang {
25
26/// This is the reference documentation of sub-namespace \b resources of module \alib_basecamp.
27///
28/// Extensive documentation for this namespace is provided with chapter
29/// \ref alib_basecamp_resources "3. Namespace alib::lang::resources" of the
30/// Programmer's Manual of that module..
31namespace resources {
32
33#if ALIB_DEBUG_RESOURCES
35#endif
36
38 const NString& name,
39 const String& resource )
40{
41
42#if !ALIB_DEBUG_RESOURCES
43 auto it= data.InsertOrAssign( detail::Key {category, name}, resource );
44#else
45 auto it= data.InsertOrAssign( detail::Key {category, name}, { resource, 0 } );
47 {
48 (*DbgResourceLoadObserver) << (it.second ? "Adding Resource: " : "Replacing Resource: " )
49 << category
50 << "/" << name << "=" << resource << std::endl;
51 }
52#endif
53
54 return !it.second;
55}
56
57void LocalResourcePool::BootstrapBulk( const nchar* category, ... )
58{
59 // find / create category
60 detail::Key key {category, nullptr};
61
62 va_list args;
63 va_start(args, category);
64 for(;;)
65 {
66 key.Name= NString( va_arg( args, const nchar* ) );
67 if( key.Name.IsNull() )
68 break;
69
70 String val = va_arg( args, const character* );
71#if ALIB_DEBUG_RESOURCES
73 (*DbgResourceLoadObserver) << "Bulk Resource: " << category
74 << "/" << key.Name << "=" << val << std::endl;
75#endif
76
77ALIB_DBG( auto result=)
78#if !ALIB_DEBUG_RESOURCES
79 data.EmplaceOrAssign( key, val );
80#else
81 data.EmplaceOrAssign( key, std::make_pair(val,0) );
82#endif
83
84ALIB_ASSERT_WARNING( result.second, "RESOURCES",
85 NString4K () << "Replacing resource with BootstrapBulk: "
86 << category << "/" << key.Name << "=" << val )
87 // \checkpromise: when typed ALib assertions and warnings are available, then
88 // raise a warning if the result of above EmplaceOrAssign is an assign, aka the
89 // bulk data existed already.
90 }
91 va_end(args);
92 }
93
94
95const String& LocalResourcePool::Get( const NString& category, const NString& name ALIB_DBG(, bool dbgAssert ) )
96{
97 // search main map
98 auto dataIt= data.Find( detail::Key { category, name } );
99 if( dataIt != data.end() )
100 {
101#if !ALIB_DEBUG_RESOURCES
102 return dataIt.Mapped();
103#else
104 dataIt.Mapped().second++;
105 return dataIt.Mapped().first;
106#endif
107 }
108 ALIB_ASSERT_ERROR( !dbgAssert, "RESOURCES",
109 NString1K() << "Unknown resource! Category: \"" << category
110 << "\", Name: \"" << name
111 << "\"." )
112 return NULL_STRING;
113
114}
115
116
117#if ALIB_DEBUG_RESOURCES
118std::vector<std::tuple<NString, NString, String, integer>>
120{
121 ALIB_WARNING( "STRINGS",
122 "ResourcePool::DbgGetList was not overridden by the ResourcePool type set. "
123 "Note that type built-in ALib type LocalResourcePool does provide an implementation." )
124
125 return std::vector<std::tuple<NString, NString, String, integer>>();
126}
127
128
129std::vector<std::pair<NString, integer>>
131{
132 ALIB_WARNING( "STRINGS",
133 "ResourcePool::DbgGetCategories was not overridden by the ResourcePool type set. "
134 "Note that type built-in ALib type LocalResourcePool does provide an implementation." )
135
136 return std::vector<std::pair<NString, integer>>();
137}
138
139std::vector<std::tuple<NString, NString, String, integer>>
141{
142 std::vector<std::tuple<NString, NString, String, integer>> result;
143
144 result.reserve( size_t( data.Size() ) );
145 for( auto& it : data )
146 {
147 result.emplace_back(
148 it.first.Category,
149 it.first.Name,
150 it.second.first,
151 it.second.second );
152 }
153
154 std::sort( result.begin(), result.end(),
155 [] (const std::tuple<NString, NString, String, integer>& a,
156 const std::tuple<NString, NString, String, integer>& b )
157 {
158
159 auto comp= std::get<0>(a).template CompareTo<CHK, Case::Ignore>( std::get<0>(b) );
160 if( comp != 0 )
161 return comp < 0;
162
163 return std::get<1>(a).template CompareTo<CHK, Case::Ignore>( std::get<1>(b) ) < 0;
164 }
165 );
166 return result;
167}
168
169std::vector<std::pair<NString, integer>>
171{
172 std::vector<std::pair<NString, integer>> result;
173
174 auto list= DbgGetList();
175 NString lastCat= nullptr;
176 for( auto& entry : list )
177 {
178 if( !lastCat.Equals( std::get<0>(entry) ) )
179 {
180 lastCat= std::get<0>(entry);
181 result.push_back( { std::get<0>(entry), 0 } );
182 }
183 ++result.back().second;
184 }
185
186 return result;
187}
188
189#if ALIB_CAMP
191AString ResourcePool::DbgDump( std::vector<std::tuple<NString, NString, String, integer>>& list,
192 const NString& catFilter, const String& format )
193{
194 AString result;
195 NString actCategory( nullptr );
197 auto& formatter= Formatter::Default;
198 for( auto it : list )
199 {
200 if( catFilter.IsNotEmpty() )
201 {
202 TokenizerN cats( catFilter, ',');
203 bool found= false;
204 while( cats.HasNext() )
205 found|= cats.Next().Trim().Equals<CHK, Case::Ignore>(std::get<0>(it) );
206 if( !found )
207 continue;
208 }
209
210 if( actCategory != std::get<0>(it) )
211 {
212 actCategory= std::get<0>(it);
213 result << NEW_LINE
214 << '[' << actCategory << ']' << NEW_LINE;
215 }
216
217 formatter->Format( result, format, std::get<0>(it), std::get<1>(it),
218 std::get<2>(it), std::get<3>(it) );
219 }
220
221 return result;
222}
224#endif // ALIB_CAMP
225
226#endif // ALIB_DEBUG_RESOURCES
227
228}} // namespace [alib::lang::resources]
229
InsertOrAssign(const KeyType &key, const MappedType &mapped)
integer Size() const noexcept
std::pair< Iterator, bool > EmplaceOrAssign(const KeyType &key, TArgs &&... args)
Iterator Find(const KeyType &key)
static ALIB_API threads::RecursiveLock DefaultLock
static ALIB_API SPFormatter Default
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
detail::StaticResourceMap data
A hash map used to store static resources.
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()
constexpr bool IsNotEmpty() const
Definition string.hpp:389
bool Equals(const TString< TChar > &rhs) const
Definition string.hpp:580
ALIB_API TSubstring< TChar > & Next(lang::Whitespaces trimming=lang::Whitespaces::Trim, TChar newDelim='\0')
Definition tokenizer.cpp:16
#define ALIB_WARNING(...)
Definition alib.hpp:1268
#define ALIB_LOCK_RECURSIVE_WITH(lock)
Definition owner.hpp:457
#define ALIB_ASSERT_ERROR(cond,...)
Definition alib.hpp:1271
#define ALIB_ASSERT_WARNING(cond,...)
Definition alib.hpp:1272
#define ALIB_DBG(...)
Definition alib.hpp:390
strings::TString< nchar > NString
Type alias in namespace alib.
constexpr CString NEW_LINE
A zero-terminated string containing the new-line character sequence.
Definition cstring.hpp:580
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.
constexpr String NULL_STRING
A nulled string of the default character type.
Definition string.hpp:2549
See sibling type NC.
Definition alib.hpp:1097
Key type for hashing resource values.