ALib C++ Library
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 \ref mainpage_license "Boost Software License".
7//==================================================================================================
9
10//==================================================================================================
11/// This class provides a very simple implementation of abstract interface class
12/// \alib{resources;ResourcePool}, which does \b not externalize resources.
13///
14/// A shared instance of this class is attached to each \alibmod, if the
15/// \ref alib_mod_bs "bootstrapping of ALib" is not customized.
16///
17/// With the use of type \alib{containers;HashTable} for its internal data management, this
18/// class uses \ref 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 \ref 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/// \ref 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 compiler-symbol \ref ALIB_DEBUG_RESOURCES is set.
47 ///
48 /// \see Manual section \ref 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 /// \alib{containers::HashTable;Reserve},
72 /// \alib{containers::HashTable;BaseLoadFactor} or
73 /// \alib{containers::HashTable;MaxLoadFactor}).
74 ///
75 /// Modifications on the returned object are allowed only while no threads have been started
76 /// by the software process (respectively no threads that use \alib ), which usually is true
77 /// during bootstrapping a process. Therefore , the prefix in this method's name.
78 ///
79 /// @return The internal hash map.
81
82
83 //################################################################################################
84 // ResourcePool Interface
85 //################################################################################################
86
87 /// Implements abstract method \alib{resources;ResourcePool::BootstrapAddOrReplace}.
88 /// \note
89 /// If the compiler-symbol \ref ALIB_DEBUG_RESOURCES is set on compilation and static field
90 /// \alib{resources;LocalResourcePool::DbgResourceLoadObserver} is set before bootstrapping,
91 /// this method will write information about whether the given data is added or replaced
92 /// to the debug output stream.
93 ///
94 /// @param category Category string of the resource to add.
95 /// @param name Name string of the resource.
96 /// @param data The resource data.
97 /// @return \c true if the resource did exist and was replaced, \c false if it was an insertion.
99 virtual
100 bool BootstrapAddOrReplace( const NString& category,
101 const NString& name,
102 const String& data ) override;
103
104 /// Implements abstract method \alib{resources;ResourcePool::BootstrapBulk}.
105 ///
106 /// @param category The category of the resources given.
107 /// @param ... A list of pairs of <b>const nchar*</b> and <b>const character*</b>
108 /// keys and data, including a terminating \c nullptr value.
110 virtual
111 void BootstrapBulk( const nchar* category, ... ) override;
112
113
114#if DOXYGEN
115 //==============================================================================================
116 /// Implements abstract method \alib{resources;ResourcePool::Get}.
117 ///
118 /// @param category Category string of the resource.
119 /// @param name Name string of the resource
120 /// @param dbgAssert This parameter is available (and to be passed) only in debug mode.
121 /// If \c true, an \ref alib_mod_assert "error is raised" if the resource
122 /// was not found.
123 /// @return The resource string, respectively a \e nulled string on failure.
124 //==============================================================================================
126 virtual
127 const String& Get( const NString& category, const NString& name, bool dbgAssert ) override;
128#else
130 virtual
131 const String& Get( const NString& category, const NString& name
132 ALIB_DBG(, bool dbgAssert) ) override;
133#endif
134
135 #if ALIB_DEBUG_RESOURCES
136 //==========================================================================================
137 /// Returns a vector of tuples for each resourced element. Each tuple contains:
138 /// 0. The category name
139 /// 1. The resource name
140 /// 2. The resource value
141 /// 3. The number of requests for the resource performed by using software.
142 ///
143 /// While being useful to generally inspect the resources, a high number of requests
144 /// might indicate a performance penalty. Mitigation can usually be performed in a very
145 /// simple fashion by "caching" a resource string in a local or global/static string
146 /// variable.
147 ///
148 /// \par Availability
149 /// Available only if the compiler-symbol \ref ALIB_DEBUG_RESOURCES is set.
150 ///
151 /// \attention
152 /// This function is implemented only with the default pool instance of type
153 /// \alib{resources;LocalResourcePool} is used.
154 /// Otherwise, an \alib_warning is raised and an empty vector is returned.
155 ///
156 /// \see
157 /// Methods #DbgGetCategories and #DbgDump.
158 ///
159 /// @return The externalized resource string.
160 //==========================================================================================
162 virtual
163 std::vector<std::tuple<NString, NString, String, integer>>
164 DbgGetList() override;
165
166
167 //==========================================================================================
168 /// Implements abstract method \alib{resources;ResourcePool::DbgGetCategories}.
169 ///
170 /// \par Availability
171 /// Available only if the compiler-symbol \ref ALIB_DEBUG_RESOURCES is set.
172 ///
173 /// @return The externalized resource string.
174 //==========================================================================================
176 virtual
177 std::vector<std::pair<NString, integer>>
178 DbgGetCategories() override;
179 #endif
180}; // class LocalResourcePool
181
182} // 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 & BootstrapGetInternalHashMap()
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
virtual ALIB_DLL ~LocalResourcePool() override
Destructor.
static std::ostream * DbgResourceLoadObserver
virtual ALIB_DLL std::vector< std::pair< NString, integer > > DbgGetCategories() override
#define ALIB_DLL
Definition alib.inl:503
#define ALIB_EXPORT
Definition alib.inl:497
#define ALIB_DBG(...)
Definition alib.inl:853
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:2198
characters::nchar nchar
Type alias in namespace alib.
strings::TString< character > String
Type alias in namespace alib.
Definition string.inl:2189