ALib C++ Library
Library Version: 2511 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
localresourcepool.cpp
1//##################################################################################################
2// ALib C++ Library
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 "Symbol 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/// \ref 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#if ALIB_DEBUG_RESOURCES
83 (*DbgResourceLoadObserver) << "Bulk Resource: " << category
84 << "/" << key.Name << "=" << val << std::endl;
85#endif
86
87ALIB_DBG( auto result=)
88#if !ALIB_DEBUG_RESOURCES
89 data.EmplaceOrAssign( key, val );
90#else
91 data.EmplaceOrAssign( key, std::make_pair(val,0) );
92#endif
93
94ALIB_ASSERT_WARNING( result.second, "RESOURCES",
95 "Replacing resource with BootstrapBulk: {} / {} = val",
96 category, key.Name, val )
97 // \checkpromise: when typed ALib assertions and warnings are available, then
98 // raise a warning if the result of above EmplaceOrAssign is an assign, aka the
99 // bulk data existed already.
100 }
101 va_end(args);
102 }
103
104
105const String& LocalResourcePool::Get( const NString& category, const NString& name
106 ALIB_DBG(, bool dbgAssert ) ) {
107 // search main map
108 auto dataIt= data.Find( detail::Key { category, name } );
109 if( dataIt != data.end() ) {
110#if !ALIB_DEBUG_RESOURCES
111 return dataIt.Mapped();
112#else
113 dataIt.Mapped().second++;
114 return dataIt.Mapped().first;
115#endif
116 }
117 ALIB_ASSERT_ERROR( !dbgAssert, "RESOURCES",
118 "Unknown resource! Category: \"{}\", Name: \"{}\".", category, name )
119 return NULL_STRING;
120
121}
122
123
124#if ALIB_DEBUG_RESOURCES
125
126std::vector<std::tuple<NString, NString, String, integer>>
128{
129 ALIB_WARNING( "STRINGS",
130 "ResourcePool::DbgGetList was not overridden by the ResourcePool type set. "
131 "Note that type built-in ALib type LocalResourcePool does provide an implementation." )
132
133 return std::vector<std::tuple<NString, NString, String, integer>>();
134}
135
136std::vector<std::pair<NString, integer>>
138{
139 ALIB_WARNING( "STRINGS",
140 "ResourcePool::DbgGetCategories was not overridden by the ResourcePool type set. "
141 "Note that type built-in ALib type LocalResourcePool does provide an implementation." )
142
143 return std::vector<std::pair<NString, integer>>();
144}
145
146std::vector<std::tuple<NString, NString, String, integer>>
148{
149 std::vector<std::tuple<NString, NString, String, integer>> result;
150
151 result.reserve( size_t( data.Size() ) );
152 for( auto& it : data )
153 {
154 result.emplace_back(
155 it.first.Category,
156 it.first.Name,
157 it.second.first,
158 it.second.second );
159 }
160
161 std::sort( result.begin(), result.end(),
162 [] (const std::tuple<NString, NString, String, integer>& a,
163 const std::tuple<NString, NString, String, integer>& b )
164 {
165
166 auto comp= std::get<0>(a).template CompareTo<CHK, lang::Case::Ignore>( std::get<0>(b) );
167 if( comp != 0 )
168 return comp < 0;
169
170 return std::get<1>(a).template CompareTo<CHK, lang::Case::Ignore>( std::get<1>(b) ) < 0;
171 }
172 );
173 return result;
174}
175
176std::vector<std::pair<NString, integer>>
178{
179 std::vector<std::pair<NString, integer>> result;
180
181 auto list= DbgGetList();
182 NString lastCat= nullptr;
183 for( auto& entry : list )
184 {
185 if( !lastCat.Equals( std::get<0>(entry) ) )
186 {
187 lastCat= std::get<0>(entry);
188 result.push_back( { std::get<0>(entry), 0 } );
189 }
190 ++result.back().second;
191 }
192
193 return result;
194}
195
196#endif // ALIB_DEBUG_RESOURCES
197
198}} // namespace [alib::resources]
virtual ALIB_DLL const String & Get(const NString &category, const NString &name, bool dbgAssert) override
virtual ALIB_DLL std::vector< std::tuple< NString, NString, String, integer > > DbgGetList() override
virtual ALIB_DLL void BootstrapBulk(const nchar *category,...) override
detail::StaticResourceMap data
A hash map used to store static resources.
virtual ALIB_DLL bool BootstrapAddOrReplace(const NString &category, const NString &name, const String &data) override
static std::ostream * DbgResourceLoadObserver
virtual ALIB_DLL std::vector< std::pair< NString, integer > > DbgGetCategories() override
virtual ALIB_DLL std::vector< std::pair< NString, integer > > DbgGetCategories()
virtual ALIB_DLL std::vector< std::tuple< NString, NString, String, integer > > DbgGetList()
bool Equals(const TString< TChar > &rhs) const
Definition string.inl:531
#define ALIB_WARNING(domain,...)
Definition alib.inl:1063
#define ALIB_ASSERT_WARNING(cond, domain,...)
Definition alib.inl:1067
#define ALIB_DBG(...)
Definition alib.inl:853
#define ALIB_ASSERT_ERROR(cond, domain,...)
Definition alib.inl:1066
constexpr String NULL_STRING
A nulled string of the default character type.
Definition string.inl:2271
strings::TString< nchar > NString
Type alias in namespace alib.
Definition string.inl:2198
characters::nchar nchar
Type alias in namespace alib.
strings::TString< character > String
Type alias in namespace alib.
Definition string.inl:2189
characters::character character
Type alias in namespace alib.
Key type for hashing resource values.