ALib C++ Library
Library Version: 2402 R1
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 sub-namespace #alib::lang::resources of module \alib_basecamp of
4 * the \aliblong.
5 *
6 * \emoji :copyright: 2013-2024 A-Worx GmbH, Germany.
7 * Published under \ref mainpage_license "Boost Software License".
8 **************************************************************************************************/
9#ifndef HPP_ALIB_LANG_RESOURCES_LOCALRESOURCEPOOL
10#define HPP_ALIB_LANG_RESOURCES_LOCALRESOURCEPOOL 1
11
12#if !defined (HPP_ALIB_LANG_RESOURCES_RESOURCES)
14#endif
15#if !defined (HPP_ALIB_LANG_RESOURCES_DETAIL_RESOURCEMAP)
16# include "alib/lang/resources/detail/resourcemap.hpp"
17#endif
18#if !defined (HPP_ALIB_MONOMEM_HASHMAP)
20#endif
21
22
23namespace alib::lang::resources {
24
25/** ************************************************************************************************
26 * This class provides a very simple implementation of abstract interface class
27 * \alib{lang::resources;ResourcePool}, which does \b not externalize resources.
28 *
29 * A shared instance of this class is attached to each \alibmod, if the
30 * \ref alib_manual_bootstrapping "bootstrapping of ALib" is not customized.
31 *
32 * With the use of type \alib{monomem;HashTable} for its internal data management, this class uses
33 * \ref alib_mod_monomem "monotonically growing memory" taken from the global allocator.
34 *
35 * Only pointers to the given resources (as well as their associated category and name strings)
36 * are stored, hence all string argument's of this class's methods have to be static data.
37 *
38 * For debug or optimization purposes, method #BootstrapGetInternalHashMap is given that allows
39 * fine-tune the performance parameters or to inspect the data. For the latter, in
40 * debug-compilations, consider also methods #DbgGetList and #DbgGetCategories.
41 *
42 * \see
43 * Please consult chapter \ref alib_basecamp_resources "3. Namespace alib::lang::resources" of
44 * the Programmer's Manual of module \alib_basecamp for details on the concept of resources.
45 * Some details on this specific type are given in chapter
46 * \ref alib_basecamp_resources_interface_default.
47 *
48 **************************************************************************************************/
50{
51 protected:
52 /** A hash map used to store static resources. */
54
55 #if ALIB_DEBUG_RESOURCES
56 /** If set prior to bootstrapping (e.g. to <c>&std::cout</c>), then each found resource string
57 * is written here. This is very useful to find errors in bulk resource strings, for example
58 * a simple missing comma.
59 *
60 * \par Availability
61 * Available only if compiler symbol \ref ALIB_DEBUG_RESOURCES is set.
62 *
63 * \see Manual section \ref alib_basecamp_resources_details_debug.
64 */
65 public: static std::ostream* DbgResourceLoadObserver;
66 #endif
67
68 // #############################################################################################
69 // Constructor/Destructor
70 // #############################################################################################
71 public:
72 /** Constructor. */
74 : data( &monomem::GlobalAllocator )
75 {}
76
77 /** Destructor. */
79 virtual
81 {}
82
83 /**
84 * Returns the internal table that maps a pair of category and name strings to the resource
85 * string.
86 *
87 * Access to this map may be useful for two purposes:
88 * 1. Debug inspection.
89 * 2. Modification of the parameters of the hash table. (For example, the use of methods
90 * \alib{monomem::HashTable;Reserve}, \alib{monomem::HashTable;BaseLoadFactor} or
91 * \alib{monomem::HashTable;MaxLoadFactor}).
92 *
93 * Modifications on the returned object are allowed only while no threads have been started
94 * by the software process (respectively no threads that use \alib ), which usually is true
95 * during bootstrapping a software. Therefore the prefix in this method's name.
96 *
97 * @return The internal hash map.
98 */
103
104
105 // #############################################################################################
106 // ResourcePool Interface
107 // #############################################################################################
108
109 /** ********************************************************************************************
110 * Implements abstract method \alib{lang::resources;ResourcePool::BootstrapAddOrReplace}.
111 * \note
112 * If compiler symbol \ref ALIB_DEBUG_RESOURCES is set on compilation and static field
113 * \alib{lang::resources;LocalResourcePool::DbgResourceLoadObserver} is set prior to bootstrapping,
114 * this method will write information about whether the given data is added or replaced
115 * to the debug output stream.
116 *
117 * @param category Category string of the resource to add.
118 * @param name Name string of the resource.
119 * @param data The resource data.
120 * @return \c true if the resource did exist and was replaced, \c false if it was an insertion.
121 **********************************************************************************************/
123 virtual
124 bool BootstrapAddOrReplace( const NString& category,
125 const NString& name,
126 const String& data ) override;
127
128 /** ********************************************************************************************
129 * Implements abstract method \alib{lang::resources;ResourcePool::BootstrapBulk}.
130 *
131 * @param category The category of the resources given.
132 * @param ... A list of pairs of <b>const nchar*</b> and <b>const character*</b>
133 * keys and data, including a terminating \c nullptr value.
134 **********************************************************************************************/
136 virtual
137 void BootstrapBulk( const nchar* category, ... ) override;
138
139
140#if defined(ALIB_DOX)
141 /** ********************************************************************************************
142 * Implements abstract method \alib{lang::resources;ResourcePool::Get}.
143 *
144 * @param category Category string of the resource.
145 * @param name Name string of the resource
146 * @param dbgAssert This parameter is available only in debug mode. If \c true, an assertion
147 * is raised if the resource was not found.
148 * @return The resource string, respectively a \e nulled string on failure.
149 **********************************************************************************************/
150 NALIB_API
151 virtual
152 const String& Get( const NString& category, const NString& name, bool dbgAssert ) override;
153#else
155 virtual
156 const String& Get( const NString& category, const NString& name
157 ALIB_DBG(, bool dbgAssert) ) override;
158#endif
159
160 #if ALIB_DEBUG_RESOURCES
161 /** ****************************************************************************************
162 * Implements abstract method \alib{lang::resources;ResourcePool::DbgGetList}.
163 *
164 * \par Availability
165 * Available only if compiler symbol \ref ALIB_DEBUG_RESOURCES is set.
166 *
167 * @return The externalized resource string.
168 ******************************************************************************************/
170 virtual
171 std::vector<std::tuple<NString, NString, String, integer>>
172 DbgGetList() override;
173
174 /** ****************************************************************************************
175 * Implements abstract method \alib{lang::resources;ResourcePool::DbgGetCategories}.
176 *
177 * \par Availability
178 * Available only if compiler symbol \ref ALIB_DEBUG_RESOURCES is set.
179 *
180 * @return The externalized resource string.
181 ******************************************************************************************/
183 virtual
184 std::vector<std::pair<NString, integer>>
185 DbgGetCategories() override;
186 #endif
187
188
189}; // class LocalResourcePool
190
191} // namespace [alib::lang::resources]
192
193
194#endif // HPP_ALIB_LANG_RESOURCES_LOCALRESOURCEPOOL
virtual ALIB_API std::vector< std::tuple< NString, NString, String, integer > > DbgGetList() override
virtual ALIB_API void BootstrapBulk(const nchar *category,...) override
detail::StaticResourceMap & BootstrapGetInternalHashMap()
virtual ALIB_API ~LocalResourcePool() override
virtual ALIB_API bool BootstrapAddOrReplace(const NString &category, const NString &name, const String &data) override
virtual ALIB_API std::vector< std::pair< NString, integer > > DbgGetCategories() override
#define ALIB_API
Definition alib.hpp:538
#define ALIB_DBG(...)
Definition alib.hpp:457
@ Get
Denotes to search data.
characters::nchar nchar
Type alias in namespace alib.