ALib C++ Framework
by
Library Version: 2511 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
localresourcepool.cpp
1//##################################################################################################
2// ALib C++ Framework
3//
4// Copyright 2013-2025 A-Worx GmbH, Germany
5// Published under 'Boost Software License' (a free software license, see LICENSE.txt)
6//##################################################################################################
7#include "alib_precompile.hpp"
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"
10#endif
11#if ALIB_C20_MODULES
12 module;
13#endif
14//========================================= Global Fragment ========================================
16#include <cstdarg>
17#if ALIB_DEBUG_RESOURCES
18# include <vector>
19# include <algorithm>
20#endif
21//============================================== Module ============================================
22#if ALIB_C20_MODULES
23 module ALib.Resources;
24 import ALib.Lang;
25# if ALIB_STRINGS
26 import ALib.Strings;
27# endif
28#if ALIB_DEBUG_RESOURCES
30#endif
31#else
32# include "ALib.Lang.H"
33# include "ALib.Strings.H"
34#if ALIB_DEBUG_RESOURCES
36#endif
37# include "ALib.Resources.H"
38#endif
39//========================================== Implementation ========================================
40namespace alib {
41
42/// This is the reference documentation module \alib_resources.<br>
43/// Extensive documentation for this namespace is provided with the
44/// #"alib_mod_resources;Programmer's Manual" of that module.
45namespace resources {
46
47#if ALIB_DEBUG_RESOURCES
49#endif
50
52 const NString& name,
53 const String& resource ) {
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 (*DbgResourceLoadObserver) << (it.second ? "Adding Resource: " : "Replacing Resource: " )
61 << category
62 << "/" << name << "=" << resource << std::endl;
63 }
64#endif
65
66 return !it.second;
67}
68
69void LocalResourcePool::BootstrapBulk( const nchar* category, ... ) {
70 // find / create category
71 detail::Key key {category, nullptr};
72
73 va_list args;
74 va_start(args, category);
75 for(;;) {
76 key.Name= NString( va_arg( args, const nchar* ) );
77 if( key.Name.IsNull() )
78 break;
79
80 String val = va_arg( args, const character* );
81 ALIB_ASSERT_ERROR( key.Name.IndexOf(' ') < 0, "RESOURCES",
82 "Resource key name contains spaces: {} / {} = val", category, key.Name, val )
83 ALIB_ASSERT_ERROR( key.Name.IndexOf(' ') <= 50, "RESOURCES",
84 "Resource key length > 50: {} / {} = val", category, key.Name, val )
85 #if ALIB_DEBUG_RESOURCES
87 (*DbgResourceLoadObserver) << "Bulk Resource: " << category
88 << "/" << key.Name << "=" << val << std::endl;
89 #endif
90
91 #if ALIB_DEBUG
92 auto it= data.Find(key);
93 if (it!=data.end())
94 ALIB_ERROR( "RESOURCES", "Replacing resource \"{}\" in category \"{}\".\n"
95 " Old value: \"{}\"\n"
96 " New value: \"{}\"",
97 key.Name, category, it->second, val )
98 #endif
99 #if !ALIB_DEBUG_RESOURCES
100 data.EmplaceOrAssign( key, val );
101 #else
102 data.EmplaceOrAssign( key, std::make_pair(val,0) );
103 #endif
104 }
105 va_end(args);
106}
107
108
109const String& LocalResourcePool::Get( const NString& category, const NString& name
110 ALIB_DBG(, bool dbgAssert ) ) {
111 // search main map
112 auto dataIt= data.Find( detail::Key { category, name } );
113 if( dataIt != data.end() ) {
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 "Missing resource \"{}\" in category: \"{}\"", name, category )
123 return NULL_STRING;
124
125}
126
127
128#if ALIB_DEBUG_RESOURCES
129
130std::vector<std::tuple<NString, NString, String, integer>>
132{
133 ALIB_WARNING( "STRINGS",
134 "ResourcePool::DbgGetList was not overridden by the ResourcePool type set. "
135 "Note that type built-in ALib type LocalResourcePool does provide an implementation." )
136
137 return std::vector<std::tuple<NString, NString, String, integer>>();
138}
139
140std::vector<std::pair<NString, integer>>
142{
143 ALIB_WARNING( "STRINGS",
144 "ResourcePool::DbgGetCategories was not overridden by the ResourcePool type set. "
145 "Note that type built-in ALib type LocalResourcePool does provide an implementation." )
146
147 return std::vector<std::pair<NString, integer>>();
148}
149
150std::vector<std::tuple<NString, NString, String, integer>>
152{
153 std::vector<std::tuple<NString, NString, String, integer>> result;
154
155 result.reserve( size_t( data.Size() ) );
156 for( auto& it : data )
157 {
158 result.emplace_back(
159 it.first.Category,
160 it.first.Name,
161 it.second.first,
162 it.second.second );
163 }
164
165 std::sort( result.begin(), result.end(),
166 [] (const std::tuple<NString, NString, String, integer>& a,
167 const std::tuple<NString, NString, String, integer>& b )
168 {
169
170 auto comp= std::get<0>(a).template CompareTo<CHK, lang::Case::Ignore>( std::get<0>(b) );
171 if( comp != 0 )
172 return comp < 0;
173
174 return std::get<1>(a).template CompareTo<CHK, lang::Case::Ignore>( std::get<1>(b) ) < 0;
175 }
176 );
177 return result;
178}
179
180std::vector<std::pair<NString, integer>>
182{
183 std::vector<std::pair<NString, integer>> result;
184
185 auto list= DbgGetList();
186 NString lastCat= nullptr;
187 for( auto& entry : list )
188 {
189 if( !lastCat.Equals( std::get<0>(entry) ) )
190 {
191 lastCat= std::get<0>(entry);
192 result.push_back( { std::get<0>(entry), 0 } );
193 }
194 ++result.back().second;
195 }
196
197 return result;
198}
199
200#endif // ALIB_DEBUG_RESOURCES
201
202}} // namespace [alib::resources]
#define ALIB_WARNING(domain,...)
Definition alib.inl:1141
#define ALIB_ERROR(domain,...)
Definition alib.inl:1140
#define ALIB_DBG(...)
Definition alib.inl:931
#define ALIB_ASSERT_ERROR(cond, domain,...)
Definition alib.inl:1144
virtual const String & Get(const NString &category, const NString &name, bool dbgAssert) override
virtual std::vector< std::tuple< NString, NString, String, integer > > DbgGetList() override
virtual void BootstrapBulk(const nchar *category,...) override
detail::StaticResourceMap data
A hash map used to store static resources.
virtual bool BootstrapAddOrReplace(const NString &category, const NString &name, const String &data) override
static std::ostream * DbgResourceLoadObserver
virtual std::vector< std::pair< NString, integer > > DbgGetCategories() override
virtual std::vector< std::pair< NString, integer > > DbgGetCategories()
virtual std::vector< std::tuple< NString, NString, String, integer > > DbgGetList()
integer IndexOf(TChar needle, integer startIdx=0) const
Definition string.inl:803
bool Equals(const TString< TChar > &rhs) const
Definition string.inl:519
constexpr bool IsNull() const
Definition string.inl:338
strings::TString< nchar > NString
Type alias in namespace alib.
Definition string.inl:2181
constexpr String NULL_STRING
A nulled string of the default character type.
Definition string.inl:2254
strings::TString< character > String
Type alias in namespace alib.
Definition string.inl:2172
characters::nchar nchar
Type alias in namespace alib.
characters::character character
Type alias in namespace alib.
Key type for hashing resource values.
NString Name
The resource name.