ALib C++ Library
Library Version: 2402 R1
Documentation generated by doxygen
Loading...
Searching...
No Matches
camp.hpp
Go to the documentation of this file.
1/** ************************************************************************************************
2 * \file
3 * This header file is part of module \alib_basecamp of the \aliblong.
4 *
5 * \emoji :copyright: 2013-2024 A-Worx GmbH, Germany.
6 * Published under \ref mainpage_license "Boost Software License".
7 **************************************************************************************************/
8#ifndef HPP_ALIB_LANG_CAMP
9#define HPP_ALIB_LANG_CAMP 1
10
11#if !defined(HPP_ALIB_STRINGS_CSTRING)
13#endif
15
16
17// forwards
18namespace alib::lang::resources { class ResourcePool; }
19namespace alib::config { class Configuration; }
20namespace alib::lang { class Camp; }
21
22namespace alib {
23
24/** Initialization levels usable with \ref alib_manual_bootstrapping_camps "Bootstrapping ALib Camps". */
26{
27 PrepareResources = 1, ///< Creates field \alib{lang;Camp::resourcePool} and loads its
28 ///< camp-specific default values.
29 PrepareConfig = 2, ///< Initializes up to the creation of a field \alib{lang::Camp;config}.
30 Final = 3, ///< The final initialization phase.
31};
32
33/** Termination levels usable with \ref alib_manual_bootstrapping_camps "Bootstrapping ALib Camps". */
35{
36 Announce = 1, ///< Signals termination. Keeps resources, config, etc.
37 ///< intact. Usable to write configuration data, stop
38 ///< background threads, etc.
39 Destruct = 2, ///< The main phase of termination that destructs everything.
40};
41
42// forwards
43ALIB_API void Bootstrap(BootstrapPhases, lang::Camp*, int, int, TCompilationFlags);
44ALIB_API void Shutdown( ShutdownPhases , lang::Camp*);
45
46
47namespace lang {
48
49/** ************************************************************************************************
50 * This class is used by \alib to
51 *
52 * - manage library bootstrap and termination,
53 * - provide \ref alib_basecamp_resources "resources" and to
54 * - provide Configuration data (e.g. configuration files, command line parameters, etc.).
55 *
56 * Typically, every "bigger" and "higher level" \alibmod disposes about a type derived from this
57 * class. The singleton instances of these types are collected in namespace \ref alib and are named
58 * in capital letters, for example \ref alib::ALOX, \ref alib::EXPRESSIONS, etc.
59 *
60 * Types found in an \alibmod_nl, will use methods \alib{lang::Camp;GetResourcePool} and
61 * \alib{lang::Camp;GetConfig} of that singleton to access resources and to read and write
62 * configuration data.
63 * With that, it has to be kept in mind that these objects most probably are shared with other
64 * camps. As a consequence, clear and understandable resource and variable categories and names
65 * are to be used, to avoid conflicting entries.
66 *
67 * The \ref alib_manual provides several chapters that explain how
68 * \ref alib_manual_bootstrapping "4. Bootstrapping And Shutting Down ALib" is performed with the
69 * help of this class.<br>
70 * Furthermore, source code sample is given with the
71 * \ref alib_cli_tut "tutorial of ALib Module CLI".
72 *
73 * Often, types that comprise a "camp singleton" (as the derived types shown in the inheritance
74 * diagram above) incorporate some further functionality specific to the field of application that
75 * a "module" respectively "camp" covers.
76 *
77 * # Reference Documentation #
78 **************************************************************************************************/
79class Camp
80{
81 protected:
82 #if !defined(ALIB_DOX)
83 friend void alib::Bootstrap( BootstrapPhases, Camp*, int, int, TCompilationFlags );
84 friend void alib::Shutdown( ShutdownPhases , Camp*);
85 #endif
86
87 /** State of initialization, used to avoid double initialization as well as initialization
88 * "gaps". */
90
91 /**
92 * Pointer to the resource pool.
93 * An instance is created and assigned with the invocation of one of the #Bootstrap methods.
94 * This instance is then passed to all dependent camps (recursively) and this way
95 * shared. Dependent libraries that are to an own dedicated instance, have to be initialized
96 * explicitly prior to initializing this \alibcamp.
97 */
99
100 /**
101 * If a default instance of type \alib{lang::resources;LocalResourcePool} has been created
102 * when this camp was initialized, this flag evaluates to \c true and the instance stored
103 * in #resourcePool will be deleted with destruction of this object.
104 */
105 bool isResourceOwner = false;
106
107
108 #if ALIB_CONFIGURATION
109 /**
110 * Pointer to the configuration instance used to load variable configuration data.
111 * An instance is created and assigned with the invocation of one of the #Bootstrap methods.
112 * This instance is then passed to all dependent libraries (recursively) and this way
113 * shared. Dependent libraries that are to an own dedicated instance, have to be
114 * initialized explicitly prior to initializing this \alibcamp.
115 *
116 * Access to the field is provided with method #GetConfig.
117 *
118 * \note
119 * This field is available only when \alib_config is included in the \alibdist.
120 */
122
123 /**
124 * If a configuration instance has been created when this \alibcamp was initialized, this
125 * flag evaluates to \c true and the instance stored in #config will be deleted with
126 * destruction of this object.
127 * \note
128 * This field is available only when \alib_config is included in the \alibdist.
129 */
130 bool isConfigOwner = false;
131 #endif // ALIB_CONFIGURATION
132
133 // ##############################################################################################
134 // Public fields
135 // ##############################################################################################
136 public:
137 /**
138 * The name of the resource category of externalized string resources defined and used by
139 * this \alibcamp.<br>
140 * Special camp \alib_basecamp uses "ALIB". Other \alibcamps_nl use a short unique name.
141 */
143
144 // #############################################################################################
145 // Constructor/Destructor
146 // #############################################################################################
147 /** ********************************************************************************************
148 * Constructor.
149 * @param resourceCategory Value for field #ResourceCategory.
150 **********************************************************************************************/
151 Camp( const NCString& resourceCategory )
152 : ResourceCategory(resourceCategory)
153 {}
154
155 /** Deleted copy constructor. */
156 Camp( const Camp& ) = delete;
157
158 /** Deleted move constructor. */
159 Camp( Camp&& ) = delete;
160
161 /** Deleted copy assignment. */
162 void operator=( const Camp& ) = delete;
163
164 /** Deleted move assignment. */
165 void operator=( Camp&& ) = delete;
166
167
168 /** ********************************************************************************************
169 * Virtual destructor to satisfy C++ abstract type rules.
170 **********************************************************************************************/
171 virtual ~Camp()
172 {
174 "CAMPS",
175 "Destructing a non-terminated camp. "
176 "Camp Resource category: ", ResourceCategory )
177 }
178
179 // ###############################################################################################
180 // Bootstrap/Shutdown()
181 // ###############################################################################################
182 public:
183
184 /** ********************************************************************************************
185 * Tests if this \alibcamp was completely initialized.
186 * @return
187 * \c true if the initialization state is either \alib{BootstrapPhases::Final}
188 * or \alib{ShutdownPhases::Announce}, \c false otherwise.
189 **********************************************************************************************/
195
196 // #############################################################################################
197 // Other public interface methods
198 // #############################################################################################
199 #if ALIB_CONFIGURATION
200 /** ****************************************************************************************
201 * Sets the configuration object of this \alibcamp.
202 *
203 * \attention
204 * This method must be invoked before function
205 * \ref alib::Bootstrap( BootstrapPhases, lang::Camp*, int, int, TCompilationFlags ) "Bootstrap"
206 * is invoked with parameter \p{targetPhase} set equal or higher than
207 * \alib{BootstrapPhases::PrepareConfig},
208 * because in this phase, internally a configuration is created if none is set.
209 *
210 * \attention
211 * Function \b Bootstrap will distribute the given instance to each lower level
212 * \alibcamp that has not received a different object.
213 *
214 * \see
215 * Chapter \ref alib_manual_bootstrapping of the Programmer's Manual of \alib.
216 *
217 * @param pConfig The external configuration object to use.
218 ******************************************************************************************/
220 {
221 ALIB_ASSERT_WARNING( config == nullptr, "CAMPS",
222 "This camp already has a configuration object set.\n"
223 "A custom configuration must be set prior to camp initialization.")
224 config= pConfig;
225 }
226
227 /** ****************************************************************************************
228 * Returns a reference to the configuration object.
229 * @return The configuration object set with bootstrapping.
230 ******************************************************************************************/
232 {
233 return *config;
234 }
235 #endif
236
237 /** ********************************************************************************************
238 * Sets the resource pool of this camp.
239 *
240 * \attention
241 * This method must be invoked before method #Bootstrap,
242 * because in the first phase \alib{BootstrapPhases::PrepareConfig}, internally a
243 * pool of type \alib{lang::resources;LocalResourcePool} is created if none is set.
244 *
245 * \attention
246 * Method #Bootstrap will distribute the given instance to each lower level
247 * camp that has not received a different object.
248 *
249 * \see
250 * Chapter \ref alib_manual_bootstrapping of the Programmer's Manual of \alib.
251 *
252 * @param pool The external resource pool to use.
253 **********************************************************************************************/
255 {
256 ALIB_ASSERT_WARNING( bootstrapState == 0, "CAMPS",
257 "This camp already has a resource pool object set.\n"
258 "A custom resource pool must be set prior to camp initialization.")
259 resourcePool= pool;
260 }
261
262 /** ********************************************************************************************
263 * Returns a reference to the resource pool.
264 * @return The resource pool set with bootstrapping.
265 **********************************************************************************************/
270
271 /** ********************************************************************************************
272 * Shortcut method that invokes \alib{lang::resources;ResourcePool::Bootstrap} on field
273 * #resourcePool providing field #ResourceCategory as parameter.
274 * \note This method is defined only after inclusion of header file
275 * \alibheader{lang/basecamp/camp_inlines.hpp}.
276 * @param name The resource name.
277 * @param data The resource data string.
278 **********************************************************************************************/
279 inline
280 void BootstrapResource( const NString& name, const String& data );
281
282 /** ********************************************************************************************
283 * Shortcut method that invokes \alib{lang::resources;ResourcePool::Get} on field
284 * #resourcePool providing field #ResourceCategory as parameter.
285 *
286 * With debug builds, this method asserts that a resource was found. If this is not
287 * wanted, use #TryResource.
288 *
289 * \note This method is defined only after inclusion of header file
290 * \alibheader{lang/basecamp/camp_inlines.hpp}.
291 * @param name The resource name.
292 * @return The resource string, respectively a \e nulled string on failure.
293 **********************************************************************************************/
294 inline
295 const String& GetResource( const NString& name );
296
297 /** ********************************************************************************************
298 * Shortcut method that invokes \alib{lang::resources;ResourcePool::Get} on field
299 * #resourcePool providing field #ResourceCategory as parameter.
300 *
301 * \note
302 * Usually, it is recommended to use #GetResource, which asserts with debug builds
303 * if a resource was not found.
304 *
305 * \note This method is defined only after inclusion of header file
306 * \alibheader{lang/basecamp/camp_inlines.hpp}.
307 * @param name The resource name.
308 * @return The resource string, respectively a \e nulled string on failure.
309 **********************************************************************************************/
310 inline
311 const String& TryResource( const NString& name );
312
313
314 // ###############################################################################################
315 // protected abstract methods
316 // ###############################################################################################
317 protected:
318 /** ********************************************************************************************
319 * Abstract method which is invoked during bootstrapping by function<br>
320 * \doxlinkproblem{namespacealib.html;ac33999bb9be31380c6ae139210d23083;alib::Bootstrap(BootstrapPhases targetPhase; lang::Camp* targetCamp, int,int,TCompilationFlags)}.
321
322 * for each \p{phase}, and each camp found in list \alib{Camps}.
323 *
324 * For details on bootstrapping \alibcamps, see the detailed explanations in
325 * chapter \ref alib_manual_bootstrapping "4.Bootstrapping And Shutting Down ALib" of
326 * the \ref alib_manual.
327 *
328 * @param phase The initialization phase to perform.
329 **********************************************************************************************/
330 virtual void bootstrap( BootstrapPhases phase ) = 0;
331
332 /** ********************************************************************************************
333 * Abstract method which is invoked during bootstrapping by function<br>
334 * \doxlinkproblem{namespacealib.html;acd654b95c6e1833f4f04d0fc19e4ce36;alib::Shutdown(ShutdownPhases targetPhase; lang::Camp* targetCamp)}
335
336 * for each \p{phase}, and each camp found in list \alib{Camps}.
337 *
338 * For details on bootstrapping \alibcamps, see the detailed explanations in
339 * chapter \ref alib_manual_bootstrapping "4. Bootstrapping And Shutting Down ALib" of
340 * the \ref alib_manual.
341 * @param phase The termination level to perform.
342 **********************************************************************************************/
343 virtual void shutdown( ShutdownPhases phase ) = 0;
344
345};// class Camp
346
347} // namespace alib[::lang]
348
349} // namespace [alib]
350
351#endif // HPP_ALIB_LANG_CAMP
int bootstrapState
Definition camp.hpp:89
void BootstrapSetResourcePool(lang::resources::ResourcePool *pool)
Definition camp.hpp:254
bool isResourceOwner
Definition camp.hpp:105
bool IsBootstrapped()
Definition camp.hpp:190
void operator=(Camp &&)=delete
resources::ResourcePool & GetResourcePool()
Definition camp.hpp:266
bool isConfigOwner
Definition camp.hpp:130
void operator=(const Camp &)=delete
void BootstrapResource(const NString &name, const String &data)
void BootstrapSetConfig(config::Configuration *pConfig)
Definition camp.hpp:219
virtual void shutdown(ShutdownPhases phase)=0
const String & TryResource(const NString &name)
NCString ResourceCategory
Definition camp.hpp:142
lang::resources::ResourcePool * resourcePool
Definition camp.hpp:98
Camp(const NCString &resourceCategory)
Definition camp.hpp:151
virtual void bootstrap(BootstrapPhases phase)=0
config::Configuration & GetConfig()
Definition camp.hpp:231
Camp(Camp &&)=delete
config::Configuration * config
Definition camp.hpp:121
const String & GetResource(const NString &name)
Camp(const Camp &)=delete
virtual ~Camp()
Definition camp.hpp:171
#define ALIB_ASSERT_MODULE(modulename)
Definition alib.hpp:190
#define ALIB_API
Definition alib.hpp:538
#define ALIB_ASSERT_WARNING(cond,...)
Definition alib.hpp:985
Definition alib.cpp:57
ShutdownPhases
Definition camp.hpp:35
@ Destruct
The main phase of termination that destructs everything.
BootstrapPhases
Definition camp.hpp:26
@ PrepareConfig
Initializes up to the creation of a field config .
@ Final
The final initialization phase.
config::Configuration Configuration
Type alias in namespace alib.
ALIB_WARNINGS_RESTORE void Bootstrap(int alibVersion, int alibRevision, TCompilationFlags compilationFlags)
Definition alib.cpp:72
lang::resources::ResourcePool ResourcePool
Type alias in namespace alib.
void Shutdown()
Definition alib.cpp:94