ALib C++ Framework
by
Library Version: 2605 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
localresourcepool.cpp
1namespace alib {
2
3/// This is the reference documentation module \alib_resources.<br>
4/// Extensive documentation for this namespace is provided with the
5/// #"alib_mod_resources;Programmer's Manual" of that module.
6namespace resources {
7
8#if ALIB_DEBUG_RESOURCES
10#endif
11
13 const NString& name,
14 const String& resource ) {
15
16#if !ALIB_DEBUG_RESOURCES
17 auto it= data.InsertOrAssign( detail::Key {category, name}, resource );
18#else
19 auto it= data.InsertOrAssign( detail::Key {category, name}, { resource, 0 } );
21 (*DbgResourceLoadObserver) << (it.second ? "Adding Resource: " : "Replacing Resource: " )
22 << category
23 << "/" << name << "=" << resource << std::endl;
24 }
25#endif
26
27 return !it.second;
28}
29
30void LocalResourcePool::BootstrapBulk( const nchar* category, ... ) {
31 // find / create category
32 detail::Key key {category, nullptr};
33
34 va_list args;
35 va_start(args, category);
36 for(;;) {
37 key.Name= NString( va_arg( args, const nchar* ) );
38 if( key.Name.IsNull() )
39 break;
40
41 String val = va_arg( args, const character* );
42 ALIB_ASSERT_ERROR( key.Name.IndexOf(' ') < 0, "RESOURCES",
43 "Resource key name contains spaces: {} / {} = val", category, key.Name, val )
44 ALIB_ASSERT_ERROR( key.Name.IndexOf(' ') <= 50, "RESOURCES",
45 "Resource key length > 50: {} / {} = val", category, key.Name, val )
46 #if ALIB_DEBUG_RESOURCES
48 (*DbgResourceLoadObserver) << "Bulk Resource: " << category
49 << "/" << key.Name << "=" << val << std::endl;
50 #endif
51
52 #if ALIB_DEBUG_RESOURCES
53 auto it= data.Find(key);
54 if (it!=data.end())
55 ALIB_MESSAGE( "RESOURCES", "Replacing resource \"{}\" in category \"{}\".\n"
56 " Old value: \"{}\"\n"
57 " New value: \"{}\"",
58 key.Name, category, it->second, val )
59 #endif
60 #if !ALIB_DEBUG_RESOURCES
61 data.EmplaceOrAssign( key, val );
62 #else
63 data.EmplaceOrAssign( key, std::make_pair(val,0) );
64 #endif
65 }
66 va_end(args);
67}
68
69
70const String& LocalResourcePool::Get( const NString& category, const NString& name
71 ALIB_DBG(, bool dbgAssert ) ) {
72 // search main map
73 auto dataIt= data.Find( detail::Key { category, name } );
74 if( dataIt != data.end() ) {
75#if !ALIB_DEBUG_RESOURCES
76 return dataIt.Mapped();
77#else
78 dataIt.Mapped().second++;
79 return dataIt.Mapped().first;
80#endif
81 }
82 ALIB_ASSERT_ERROR( !dbgAssert, "RESOURCES",
83 "Missing resource \"{}\" in category: \"{}\"", name, category )
84 return NULL_STRING;
85
86}
87
88
89#if ALIB_DEBUG_RESOURCES
90
91std::vector<std::tuple<NString, NString, String, integer>>
93 ALIB_WARNING( "STRINGS",
94 "ResourcePool::DbgGetList was not overridden by the ResourcePool type set. "
95 "Note that type built-in ALib type LocalResourcePool does provide an implementation." )
96
97 return std::vector<std::tuple<NString, NString, String, integer>>();
98}
99
100std::vector<std::pair<NString, integer>>
102 ALIB_WARNING( "STRINGS",
103 "ResourcePool::DbgGetCategories was not overridden by the ResourcePool type set. "
104 "Note that type built-in ALib type LocalResourcePool does provide an implementation." )
105
106 return std::vector<std::pair<NString, integer>>();
107}
108
109std::vector<std::tuple<NString, NString, String, integer>>
111 std::vector<std::tuple<NString, NString, String, integer>> result;
112
113 result.reserve( size_t( data.Size() ) );
114 for( auto& it : data ) {
115 result.emplace_back(
116 it.first.Category,
117 it.first.Name,
118 it.second.first,
119 it.second.second );
120 }
121
122 std::sort( result.begin(), result.end(),
123 [] (const std::tuple<NString, NString, String, integer>& a,
124 const std::tuple<NString, NString, String, integer>& b )
125 {
126
127 auto comp= std::get<0>(a).template CompareTo<CHK, lang::Case::Ignore>( std::get<0>(b) );
128 if( comp != 0 )
129 return comp < 0;
130
131 return std::get<1>(a).template CompareTo<CHK, lang::Case::Ignore>( std::get<1>(b) ) < 0;
132 }
133 );
134 return result;
135}
136
137std::vector<std::pair<NString, integer>>
139 std::vector<std::pair<NString, integer>> result;
140
141 auto list= DbgGetList();
142 NString lastCat= nullptr;
143 for( auto& entry : list ) {
144 if( !lastCat.Equals( std::get<0>(entry) ) ) {
145 lastCat= std::get<0>(entry);
146 result.push_back( { std::get<0>(entry), 0 } );
147 }
148 ++result.back().second;
149 }
150
151 return result;
152}
153
154#endif // ALIB_DEBUG_RESOURCES
155
156}} // namespace [alib::resources]
#define ALIB_MESSAGE(domain,...)
#define ALIB_WARNING(domain,...)
#define ALIB_DBG(...)
#define ALIB_ASSERT_ERROR(cond, domain,...)
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.hpp:799
bool Equals(const TString< TChar > &rhs) const
Definition string.hpp:515
constexpr bool IsNull() const
Definition string.hpp:334
Definition alox.cpp:14
strings::TString< nchar > NString
Type alias in namespace #"%alib".
Definition string.hpp:2174
constexpr String NULL_STRING
A nulled string of the default character type.
Definition string.hpp:2247
strings::TString< character > String
Type alias in namespace #"%alib".
Definition string.hpp:2165
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.