ALib C++ Library
Library Version: 2510 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
56#if !ALIB_DEBUG_RESOURCES
57 auto it= data.InsertOrAssign( detail::Key {category, name}, resource );
58#else
59 auto it= data.InsertOrAssign( detail::Key {category, name}, { resource, 0 } );
61 {
62 (*DbgResourceLoadObserver) << (it.second ? "Adding Resource: " : "Replacing Resource: " )
63 << category
64 << "/" << name << "=" << resource << std::endl;
65 }
66#endif
67
68 return !it.second;
69}
70
71void LocalResourcePool::BootstrapBulk( const nchar* category, ... )
72{
73 // find / create category
74 detail::Key key {category, nullptr};
75
76 va_list args;
77 va_start(args, category);
78 for(;;)
79 {
80 key.Name= NString( va_arg( args, const nchar* ) );
81 if( key.Name.IsNull() )
82 break;
83
84 String val = va_arg( args, const character* );
85#if ALIB_DEBUG_RESOURCES
87 (*DbgResourceLoadObserver) << "Bulk Resource: " << category
88 << "/" << key.Name << "=" << val << std::endl;
89#endif
90
91ALIB_DBG( auto result=)
92#if !ALIB_DEBUG_RESOURCES
93 data.EmplaceOrAssign( key, val );
94#else
95 data.EmplaceOrAssign( key, std::make_pair(val,0) );
96#endif
97
98ALIB_ASSERT_WARNING( result.second, "RESOURCES",
99 "Replacing resource with BootstrapBulk: {} / {} = val",
100 category, key.Name, val )
101 // \checkpromise: when typed ALib assertions and warnings are available, then
102 // raise a warning if the result of above EmplaceOrAssign is an assign, aka the
103 // bulk data existed already.
104 }
105 va_end(args);
106 }
107
108
109const String& LocalResourcePool::Get( const NString& category, const NString& name ALIB_DBG(, bool dbgAssert ) )
110{
111 // search main map
112 auto dataIt= data.Find( detail::Key { category, name } );
113 if( dataIt != data.end() )
114 {
115#if !ALIB_DEBUG_RESOURCES
116 return dataIt.Mapped();
117#else
118 dataIt.Mapped().second++;
119 return dataIt.Mapped().first;
120#endif
121 }
122 ALIB_ASSERT_ERROR( !dbgAssert, "RESOURCES",
123 "Unknown resource! Category: \"{}\", Name: \"{}\".", category, name )
124 return NULL_STRING;
125
126}
127
128
129#if ALIB_DEBUG_RESOURCES
130
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
141std::vector<std::pair<NString, integer>>
143{
144 ALIB_WARNING( "STRINGS",
145 "ResourcePool::DbgGetCategories was not overridden by the ResourcePool type set. "
146 "Note that type built-in ALib type LocalResourcePool does provide an implementation." )
147
148 return std::vector<std::pair<NString, integer>>();
149}
150
151std::vector<std::tuple<NString, NString, String, integer>>
153{
154 std::vector<std::tuple<NString, NString, String, integer>> result;
155
156 result.reserve( size_t( data.Size() ) );
157 for( auto& it : data )
158 {
159 result.emplace_back(
160 it.first.Category,
161 it.first.Name,
162 it.second.first,
163 it.second.second );
164 }
165
166 std::sort( result.begin(), result.end(),
167 [] (const std::tuple<NString, NString, String, integer>& a,
168 const std::tuple<NString, NString, String, integer>& b )
169 {
170
171 auto comp= std::get<0>(a).template CompareTo<CHK, lang::Case::Ignore>( std::get<0>(b) );
172 if( comp != 0 )
173 return comp < 0;
174
175 return std::get<1>(a).template CompareTo<CHK, lang::Case::Ignore>( std::get<1>(b) ) < 0;
176 }
177 );
178 return result;
179}
180
181std::vector<std::pair<NString, integer>>
183{
184 std::vector<std::pair<NString, integer>> result;
185
186 auto list= DbgGetList();
187 NString lastCat= nullptr;
188 for( auto& entry : list )
189 {
190 if( !lastCat.Equals( std::get<0>(entry) ) )
191 {
192 lastCat= std::get<0>(entry);
193 result.push_back( { std::get<0>(entry), 0 } );
194 }
195 ++result.back().second;
196 }
197
198 return result;
199}
200
201#endif // ALIB_DEBUG_RESOURCES
202
203}} // namespace [alib::resources]
204
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:541
#define ALIB_WARNING(domain,...)
Definition alib.inl:1046
#define ALIB_ASSERT_WARNING(cond, domain,...)
Definition alib.inl:1050
#define ALIB_DBG(...)
Definition alib.inl:836
#define ALIB_ASSERT_ERROR(cond, domain,...)
Definition alib.inl:1049
constexpr String NULL_STRING
A nulled string of the default character type.
Definition string.inl:2463
strings::TString< nchar > NString
Type alias in namespace alib.
Definition string.inl:2390
characters::nchar nchar
Type alias in namespace alib.
strings::TString< character > String
Type alias in namespace alib.
Definition string.inl:2381
characters::character character
Type alias in namespace alib.
Key type for hashing resource values.