ALib C++ Framework
by
Library Version: 2605 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
localresourcepool.hpp
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/// Copyright 2013-2026 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 protected:
36 /// A hash map used to store static resources.
38
39 #if ALIB_DEBUG_RESOURCES
40 /// If set before bootstrapping (e.g., to <c>&std::cout</c>), then each found resource string
41 /// is written here. This is very useful to find errors in bulk resource strings, for example
42 /// a simple missing comma.
43 ///
44 /// \par Availability
45 /// Available only if the configuration macro #"ALIB_DEBUG_RESOURCES" is set.
46 ///
47 /// \see Manual section #"alib_resources_details_debug".
48 public: static std::ostream* DbgResourceLoadObserver;
49 #endif
50
51 //################################################################################################
52 // Constructor/Destructor
53 //################################################################################################
54 public:
55 /// Constructor.
57 : data( monomem::GLOBAL_ALLOCATOR ) {}
58
59 /// Destructor.
61 virtual
62 ~LocalResourcePool() override {}
63
64 /// Returns the internal table that maps a pair of category and name strings to the resource
65 /// string.
66 ///
67 /// Access to this map may be useful for two purposes:
68 /// 1. Debug inspection.
69 /// 2. Modification of the parameters of the hash table. (For example, the use of methods
70 /// #"HashTable::Reserve",
71 /// #"HashTable::BaseLoadFactor()",
72 /// #"HashTable::BaseLoadFactor(float)",
73 /// #"HashTable::MaxLoadFactor()"), or
74 /// #"HashTable::MaxLoadFactor(float)").
75 ///
76 /// Modifications on the returned object are allowed only while no threads have been started
77 /// by the software process (respectively no threads that use \alib ), which usually is true
78 /// during bootstrapping a process. Therefore , the prefix in this method's name.
79 ///
80 /// @return The internal hash map.
82
83
84 //################################################################################################
85 // ResourcePool Interface
86 //################################################################################################
87
88 /// Implements abstract method #"ResourcePool::BootstrapAddOrReplace;*".
89 /// \note
90 /// If the configuration macro #"ALIB_DEBUG_RESOURCES" is set on compilation and static field
91 /// #"LocalResourcePool::DbgResourceLoadObserver;*" is set before bootstrapping,
92 /// this method will write information about whether the given data is added or replaced
93 /// to the debug output stream.
94 ///
95 /// @param category Category string of the resource to add.
96 /// @param name Name string of the resource.
97 /// @param data The resource data.
98 /// @return \c true if the resource did exist and was replaced, \c false if it was an insertion.
100 virtual
101 bool BootstrapAddOrReplace( const NString& category,
102 const NString& name,
103 const String& data ) override;
104
105 /// Implements abstract method #"ResourcePool::BootstrapBulk;*".
106 ///
107 /// @param category The category of the resources given.
108 /// @param ... A list of pairs of <b>const nchar*</b> and <b>const character*</b>
109 /// keys and data, including a terminating \c nullptr value.
111 virtual
112 void BootstrapBulk( const nchar* category, ... ) override;
113
114
115#if DOXYGEN
116 //==============================================================================================
117 /// Implements abstract method #"ResourcePool::Get;*".
118 ///
119 /// @param category Category string of the resource.
120 /// @param name Name string of the resource
121 /// @param dbgAssert This parameter is available (and to be passed) only in debug mode.
122 /// If \c true, an #"alib_mod_assert;error is raised" if the resource
123 /// was not found.
124 /// @return The resource string, respectively a \e nulled string on failure.
125 //==============================================================================================
127 virtual
128 const String& Get( const NString& category, const NString& name, bool dbgAssert ) override;
129#else
131 virtual
132 const String& Get( const NString& category, const NString& name
133 ALIB_DBG(, bool dbgAssert) ) override;
134#endif
135
136 #if ALIB_DEBUG_RESOURCES
137 //==========================================================================================
138 /// Returns a vector of tuples for each resourced element. Each tuple contains:
139 /// 0. The category name
140 /// 1. The resource name
141 /// 2. The resource value
142 /// 3. The number of requests for the resource performed by using software.
143 ///
144 /// While being useful to generally inspect the resources, a high number of requests
145 /// might indicate a performance penalty. Mitigation can usually be performed in a very
146 /// simple fashion by "caching" a resource string in a local or global/static string
147 /// variable.
148 ///
149 /// \par Availability
150 /// Available only if the configuration macro #"ALIB_DEBUG_RESOURCES" is set.
151 ///
152 /// \attention
153 /// This function is implemented only with the default pool instance of type
154 /// #"LocalResourcePool" is used.
155 /// Otherwise, an \alib_warning is raised and an empty vector is returned.
156 ///
157 /// \see
158 /// Namespace function #"resources::DbgDump", method #".DbgGetCategories" and field
159 /// #".DbgResourceLoadObserver".
160 ///
161 /// @return The externalized resource string.
162 //==========================================================================================
164 virtual
165 std::vector<std::tuple<NString, NString, String, integer>>
166 DbgGetList() override;
167
168
169 //==========================================================================================
170 /// Implements abstract method #"ResourcePool::DbgGetCategories;*".
171 ///
172 /// \par Availability
173 /// Available only if the configuration macro #"ALIB_DEBUG_RESOURCES" is set.
174 ///
175 /// @return The externalized resource string.
176 //==========================================================================================
178 virtual
179 std::vector<std::pair<NString, integer>>
180 DbgGetCategories() override;
181 #endif
182}; // class LocalResourcePool
183
184} // namespace [alib::resources]
#define ALIB_DLL
#define ALIB_EXPORT
#define ALIB_DBG(...)
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.hpp:2174
strings::TString< character > String
Type alias in namespace #"%alib".
Definition string.hpp:2165
characters::nchar nchar
Type alias in namespace #"%alib".