ALib C++ Framework
by
Library Version: 2511 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
localresourcepool.inl
Go to the documentation of this file.
1//==================================================================================================
2/// \file
3/// This header-file is part of module \alib_resources of the \aliblong.
4///
5/// \emoji :copyright: 2013-2025 A-Worx GmbH, Germany.
6/// Published under #"mainpage_license".
7//==================================================================================================
9
10//==================================================================================================
11/// This class provides a very simple implementation of abstract interface class
12/// #"ResourcePool", which does \b not externalize resources.
13///
14/// A shared instance of this class is attached to each \alibmod, if the
15/// #"alib_mod_bs;bootstrapping of ALib" is not customized.
16///
17/// With the use of type #"HashTable" for its internal data management, this
18/// class uses #"alib_mods_contmono;monotonically growing memory" taken from the global
19/// allocator.
20///
21/// Only pointers to the given resources (as well as their associated category and name strings)
22/// are stored, hence all string argument's of this class's methods have to be static data.
23///
24/// For debug or optimization purposes, method #"BootstrapGetInternalHashMap" is given that allows
25/// fine-tune the performance parameters or to inspect the data. For the latter, in
26/// debug-compilations, consider also methods #"DbgGetList" and #"DbgGetCategories".
27///
28/// \see
29/// Please consult the #"alib_mod_resources;Programmer's Manual" of module \alib_resources_nl
30/// for details on the concept of resources.
31/// Some details on this specific type are given in chapter
32/// #"alib_resources_interface_default".
33//==================================================================================================
35{
36 protected:
37 /// A hash map used to store static resources.
39
40 #if ALIB_DEBUG_RESOURCES
41 /// If set before bootstrapping (e.g., to <c>&std::cout</c>), then each found resource string
42 /// is written here. This is very useful to find errors in bulk resource strings, for example
43 /// a simple missing comma.
44 ///
45 /// \par Availability
46 /// Available only if the configuration macro #"ALIB_DEBUG_RESOURCES" is set.
47 ///
48 /// \see Manual section #"alib_resources_details_debug".
49 public: static std::ostream* DbgResourceLoadObserver;
50 #endif
51
52 //################################################################################################
53 // Constructor/Destructor
54 //################################################################################################
55 public:
56 /// Constructor.
58 : data( monomem::GLOBAL_ALLOCATOR ) {}
59
60 /// Destructor.
62 virtual
63 ~LocalResourcePool() override {}
64
65 /// Returns the internal table that maps a pair of category and name strings to the resource
66 /// string.
67 ///
68 /// Access to this map may be useful for two purposes:
69 /// 1. Debug inspection.
70 /// 2. Modification of the parameters of the hash table. (For example, the use of methods
71 /// #"HashTable::Reserve",
72 /// #"HashTable::BaseLoadFactor()",
73 /// #"HashTable::BaseLoadFactor(float)",
74 /// #"HashTable::MaxLoadFactor()"), or
75 /// #"HashTable::MaxLoadFactor(float)").
76 ///
77 /// Modifications on the returned object are allowed only while no threads have been started
78 /// by the software process (respectively no threads that use \alib ), which usually is true
79 /// during bootstrapping a process. Therefore , the prefix in this method's name.
80 ///
81 /// @return The internal hash map.
83
84
85 //################################################################################################
86 // ResourcePool Interface
87 //################################################################################################
88
89 /// Implements abstract method #"ResourcePool::BootstrapAddOrReplace;*".
90 /// \note
91 /// If the configuration macro #"ALIB_DEBUG_RESOURCES" is set on compilation and static field
92 /// #"LocalResourcePool::DbgResourceLoadObserver;*" is set before bootstrapping,
93 /// this method will write information about whether the given data is added or replaced
94 /// to the debug output stream.
95 ///
96 /// @param category Category string of the resource to add.
97 /// @param name Name string of the resource.
98 /// @param data The resource data.
99 /// @return \c true if the resource did exist and was replaced, \c false if it was an insertion.
101 virtual
102 bool BootstrapAddOrReplace( const NString& category,
103 const NString& name,
104 const String& data ) override;
105
106 /// Implements abstract method #"ResourcePool::BootstrapBulk;*".
107 ///
108 /// @param category The category of the resources given.
109 /// @param ... A list of pairs of <b>const nchar*</b> and <b>const character*</b>
110 /// keys and data, including a terminating \c nullptr value.
112 virtual
113 void BootstrapBulk( const nchar* category, ... ) override;
114
115
116#if DOXYGEN
117 //==============================================================================================
118 /// Implements abstract method #"ResourcePool::Get;*".
119 ///
120 /// @param category Category string of the resource.
121 /// @param name Name string of the resource
122 /// @param dbgAssert This parameter is available (and to be passed) only in debug mode.
123 /// If \c true, an #"alib_mod_assert;error is raised" if the resource
124 /// was not found.
125 /// @return The resource string, respectively a \e nulled string on failure.
126 //==============================================================================================
128 virtual
129 const String& Get( const NString& category, const NString& name, bool dbgAssert ) override;
130#else
132 virtual
133 const String& Get( const NString& category, const NString& name
134 ALIB_DBG(, bool dbgAssert) ) override;
135#endif
136
137 #if ALIB_DEBUG_RESOURCES
138 //==========================================================================================
139 /// Returns a vector of tuples for each resourced element. Each tuple contains:
140 /// 0. The category name
141 /// 1. The resource name
142 /// 2. The resource value
143 /// 3. The number of requests for the resource performed by using software.
144 ///
145 /// While being useful to generally inspect the resources, a high number of requests
146 /// might indicate a performance penalty. Mitigation can usually be performed in a very
147 /// simple fashion by "caching" a resource string in a local or global/static string
148 /// variable.
149 ///
150 /// \par Availability
151 /// Available only if the configuration macro #"ALIB_DEBUG_RESOURCES" is set.
152 ///
153 /// \attention
154 /// This function is implemented only with the default pool instance of type
155 /// #"LocalResourcePool" is used.
156 /// Otherwise, an \alib_warning is raised and an empty vector is returned.
157 ///
158 /// \see
159 /// Namespace function #"resources::DbgDump", method #".DbgGetCategories" and field
160 /// #".DbgResourceLoadObserver".
161 ///
162 /// @return The externalized resource string.
163 //==========================================================================================
165 virtual
166 std::vector<std::tuple<NString, NString, String, integer>>
167 DbgGetList() override;
168
169
170 //==========================================================================================
171 /// Implements abstract method #"ResourcePool::DbgGetCategories;*".
172 ///
173 /// \par Availability
174 /// Available only if the configuration macro #"ALIB_DEBUG_RESOURCES" is set.
175 ///
176 /// @return The externalized resource string.
177 //==========================================================================================
179 virtual
180 std::vector<std::pair<NString, integer>>
181 DbgGetCategories() override;
182 #endif
183}; // class LocalResourcePool
184
185} // namespace [alib::resources]
#define ALIB_DLL
Definition alib.inl:573
#define ALIB_EXPORT
Definition alib.inl:562
#define ALIB_DBG(...)
Definition alib.inl:931
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 & BootstrapGetInternalHashMap()
detail::StaticResourceMap data
A hash map used to store static resources.
virtual bool BootstrapAddOrReplace(const NString &category, const NString &name, const String &data) override
virtual ~LocalResourcePool() override
Destructor.
static std::ostream * DbgResourceLoadObserver
virtual std::vector< std::pair< NString, integer > > DbgGetCategories() override
HashMap< MonoAllocator, Key, std::pair< String, integer >, Key::Hash, Key::EqualTo, lang::Caching::Enabled, Recycling::None > StaticResourceMap
strings::TString< nchar > NString
Type alias in namespace alib.
Definition string.inl:2181
strings::TString< character > String
Type alias in namespace alib.
Definition string.inl:2172
characters::nchar nchar
Type alias in namespace alib.