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