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