ALib C++ Library
Library Version: 2511 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
camp.inl
Go to the documentation of this file.
1//==================================================================================================
2/// \file
3/// This header-file is part of module \alib_camp of the \aliblong.
4///
5/// \emoji :copyright: 2013-2025 A-Worx GmbH, Germany.
6/// Published under \ref mainpage_license "Boost Software License".
7//==================================================================================================
8ALIB_EXPORT namespace alib {
9
10//==================================================================================================
11/// Initialization levels usable with
12/// \ref alib_mod_bs_camps "Bootstrapping ALib Camps".
13//==================================================================================================
15{
16 /// Field \alib{camp;Camp::resourcePool} is created when this phase is called for a camp.
17 /// Camps are requested to feed in their resources now. When this is done, all entities that
18 /// rely exclusively on resources are to be initialized. For example, all resourced enumerations
19 /// of a camp should be parsed in this phase.
21
22 /// Field \alib{camp;Camp::config} is created when this phase is called for a camp.
23 /// Camps are requested to
24 /// \alib{variables;Configuration::RegisterType;register application-specific variable types} in
25 /// this phase. Furthermore modifications on the configuration object itself might be performed,
26 /// for example, \alib{variables;ConfigurationPlugin;custom plugins} might be added to a
27 /// configuration or configurable options of default plug-ins can be set.<br>
28 /// Finally, this is the right phase to
29 /// \alib{variables;Configuration::PreloadVariables;declare variables} which have a resourced
30 /// variable declaration, or likewise perform 'hard-coded' variable declarations including
31 /// their default values.
33
34 /// The final initialization phase. Here, camps are initializing their custom setup.
35 Final = 3,
36};
37
38//==================================================================================================
39/// Termination levels usable with \ref alib_mod_bs_camps "Bootstrapping ALib Camps".
40//==================================================================================================
42{
43 Announce = 1, ///< Signals termination. Keeps resources, config, etc.
44 ///< intact. Usable to write configuration data, stop
45 ///< background threads, etc.
46 Destruct = 2, ///< The main phase of termination that destructs everything.
47};
48
49namespace camp {
50
51//==================================================================================================
52/// This class is used by \alib to
53///
54/// - manage library bootstrap and termination,
55/// - provide \ref alib_mod_resources "resources" and to
56/// - provide Configuration data (e.g., configuration files, command line parameters, etc.).
57///
58/// Typically, every "bigger" and "higher level" \alibmod disposes of a type derived from this
59/// class. The singleton instances of these types are collected in namespace \ref alib and are named
60/// in capital letters, for example, \ref alib::ALOX, \ref alib::EXPRESSIONS, etc.
61///
62/// Types found in an \alibmod_nl, will use methods \alib{camp::Camp;GetResourcePool} and
63/// \alib{camp::Camp;GetConfig} of that singleton to access resources and to read and write
64/// configuration data.
65/// With that, it has to be kept in mind that these objects most probably are shared with other
66/// camps. As a consequence, clear and understandable resource and variable categories and names
67/// are to be used, to avoid conflicting entries.
68///
69/// The \ref alib_mod_bs "Programmer's Manual" of module \alib_bootstrap_nl explains how
70/// bootstrapping and shutting down \alib is performed with the help of this class.<br>
71/// Furthermore, source code sample is given with the
72/// \ref alib_cli_tut "tutorial of ALib Module CLI".
73///
74/// Often, types that comprise a "camp singleton" (as the derived types shown in the inheritance
75/// diagram above) incorporate some further functionality specific to the field of application that
76/// a "module" respectively "camp" covers.
77///
78/// # Reference Documentation #
79//==================================================================================================
80class Camp
81{
82 public:
83 /// Type definition used for sharing resource-pool instances. With default-bootstrapping,
84 /// an instance of type \alib{resources;LocalResourcePool} is used here, which is
85 /// created with allocator instance \alib{monomem;GLOBAL_ALLOCATOR}.
87
88 protected:
89 /// State of initialization, used to avoid double initialization as well as initialization
90 /// "gaps".
92
93 /// Shared pointer to the resource pool.
94 /// @see Function #BootstrapSetResourcePool.
96
97 /// Pointer to the configuration instance used to load variable configuration data.
98 /// An instance is created and assigned with the invocation of one of the #Bootstrap methods.
99 /// This instance is then passed to all dependent libraries (recursively) and this way
100 /// shared. Dependent libraries that are to an own dedicated instance, have to be
101 /// initialized explicitly before initializing this \alibcamp.
102 ///
103 /// Access to the field is provided with method #GetConfig.
105
106 //###############################################################################################
107 // Public fields
108 //###############################################################################################
109 public:
110 /// The name of the resource category of externalized string resources defined and used by
111 /// this \alibcamp.<br>
112 /// Special camp \alib{BASECAMP} uses "ALIB". Other \alibcamps_nl use a short unique name.
114
115 //################################################################################################
116 // Constructor/Destructor
117 //################################################################################################
118 /// Constructor.
119 /// @param resourceCategory Value for field #ResourceCategory.
120 Camp( const NCString& resourceCategory )
121 : ResourceCategory(resourceCategory) {}
122
123 /// Deleted copy constructor.
124 Camp( const Camp& ) =delete;
125
126 /// Deleted move constructor.
127 Camp( Camp&& ) =delete;
128
129 /// Deleted copy assignment.
130 void operator =( const Camp& ) =delete;
131
132 /// Deleted move assignment.
133 void operator =( Camp&& ) =delete;
134
135
136 /// Virtual destructor to satisfy C++ abstract type rules.
137 virtual ~Camp() {
139 || bootstrapState == 0, "CAMPS",
140 "Destructing a non-terminated camp. Camp Resource category is: \"{}\"", ResourceCategory )
141 }
142
143 //################################################################################################
144 // Bootstrap/Shutdown()
145 //################################################################################################
146 public:
147
148 /// Abstract method which is invoked during bootstrapping by function<br>
149 /// \doxlinkproblem{namespacealib.html;a78bb34888e5142adb87e265e23ee3c2e;alib::Bootstrap(BootstrapPhases targetPhase, camp::Camp* targetCamp, int,int,TCompilationFlags)}
150 /// for each \p{phase}, and each camp found in list \alib{CAMPS}.
151 /// Implementations of this function receive the phase to perform with #GetBootstrapState.
152 ///
153 /// This method is not to be called from outside, but is internally invoked by function
154 /// \ref alib::Bootstrap.
155 ///
156 /// For details on bootstrapping\alibcamps, see the detailed explanations in the
157 /// \ref alib_mod_bs "Programmer's Manual" of module \alib_bootstrap_nl.
158 virtual void Bootstrap() =0;
159
160 /// Abstract method which is invoked during bootstrapping by function<br>
161 /// \doxlinkproblem{namespacealib.html;a4374717714582ef1a859b773367ec5d9;alib::Shutdown(ShutdownPhases targetPhase, camp::Camp* targetCamp)}
162 /// for each \p{phase}, and each camp found in list \alib{CAMPS}.
163 ///
164 /// This method is not to be called from outside, but is internally invoked by function
165 /// \ref alib::Bootstrap.
166 ///
167 /// For details on bootstrapping \alibcamps, see the detailed explanations in the
168 /// \ref alib_mod_bs "Programmer's Manual" of module \alib_bootstrap_nl.
169 /// @param phase The termination level to perform.
170 virtual void Shutdown( ShutdownPhases phase ) =0;
171
172 /// Returns the bootstrap state of this camp.
173 /// @return The internal state flag.
175
176 /// Sets the (next) bootstrap phase to perform.
177 /// @param phase The upcomming phase.
179
180 /// Tests if this \alibcamp was completely initialized.
181 /// @return \c true if the initialization state is either \alib{BootstrapPhases::Final}
182 /// or \alib{ShutdownPhases::Announce}, \c false otherwise.
188
189 //################################################################################################
190 // Other public interface methods
191 //################################################################################################
192 /// Sets the configuration instance of this camp.
193 ///
194 /// \attention
195 /// Note the documentation of sibling method #BootstrapSetResourcePool.
196 /// Here, the very same rules apply in respect to the distribution of configuration instances
197 /// between different camps and in respect to the customization of bootstrapping.
198 ///
199 /// @see Chapter \ref alib_mod_bs_customize of the Programmer's Manual of module
200 /// \alib_bootstrap.
201 /// @param pConfig The configuration to use.
203 ALIB_ASSERT_WARNING( (config == nullptr) != (pConfig == nullptr), "CAMPS",
204 "Double setting or removing a camp's configuration instance.\n"
205 "Note: A custom configuration must be set before camp initialization." )
206 config= pConfig;
207 }
208
209 /// Returns a reference (!) to the configuration object.
210 /// @return The configuration object set with bootstrapping.
212
213 /// Sets the resource pool of this camp.
214 /// By default, this function is called with \p{pResourcePool} holding an instance of type
215 /// \alib{resources;LocalResourcePool} when \alib is bootstrapped.
216 ///
217 /// In case a custom resource pool type (and/or instance) should be used with this camp,
218 /// a custom shared pointer has to be created and the custom pool has to be inserted
219 /// (potentially using method \alib{containers;SharedPtr::InsertDerived}).
220 /// Then this method has to be called on the camp prior to the invocation of
221 /// #alib::Bootstrap.
222 ///
223 /// Note that the Function \b %alib::Bootstrap will distribute the given instance to each
224 /// lower-level camp that has not received a different object. If this should be avoided
225 /// (to separate the resources of this camp from lower level camps), a
226 /// \ref alib_mod_bs_customize "further customized bootstrap strategy" has to be implemented.
227 /// @param pResourcePool The resource pool to use.
228 void BootstrapSetResourcePool( const SPResourcePool& pResourcePool ) {
229 ALIB_ASSERT_WARNING( (resourcePool == nullptr) != (pResourcePool == nullptr), "CAMPS",
230 "Double setting or removing a camp's resource pool instance.\n"
231 "Note: A custom resource pool must be set before camp initialization." )
232
233 resourcePool = pResourcePool;
234 }
235
236 /// Returns a reference (!) to the \b SharedPtr holding the resource pool.
237 /// @return The resource pool set with bootstrapping.
239
240 /// Shortcut method that invokes \alib{resources;ResourcePool::Bootstrap} on field
241 /// #resourcePool providing field #ResourceCategory as parameter.
242 /// @param name The resource name.
243 /// @param data The resource data string.
244 inline
245 void BootstrapResource( const NString& name, const String& data ) {
246 resourcePool->Bootstrap( ResourceCategory, name, data );
247 }
248
249 /// Shortcut method that invokes \alib{resources;ResourcePool::Get} on field
250 /// #resourcePool providing field #ResourceCategory as parameter.
251 ///
252 /// With debug-builds, this method asserts that a resource was found. If this is not
253 /// wanted, use #TryResource.
254 ///
255 /// \note This method is defined only after inclusion of header-file
256 /// \implude{Camp}.
257 /// @param name The resource name.
258 /// @return The resource string, respectively a \e nulled string on failure.
259 inline
260 const String& GetResource( const NString& name ) {
261 return resourcePool->Get( ResourceCategory, name ALIB_DBG(, true) );
262 }
263
264 /// Shortcut method that invokes \alib{resources;ResourcePool::Get} on field
265 /// #resourcePool providing field #ResourceCategory as parameter.
266 ///
267 /// \note
268 /// Usually, it is recommended to use #GetResource, which asserts with debug-builds
269 /// if a resource was not found.
270 ///
271 /// \note This method is defined only after inclusion of header-file
272 /// \implude{Camp}.
273 /// @param name The resource name.
274 /// @return The resource string, respectively a \e nulled string on failure.
275 inline
276 const String& TryResource( const NString& name ) {
277 return resourcePool->Get( ResourceCategory, name ALIB_DBG(, false) );
278 }
279
280
281};// class Camp
282}} // namespace [alib::camp]
283
284
285//==================================================================================================
286//=== enumrecords::Bootstrap() functions with Camps
287//==================================================================================================
289
290/// This method can be used if a set of enum records is resourced using an \alib
291/// \alib{camp;Camp}'s resource instance.
292///
293/// Invokes #Bootstrap(ResourcePool&, const NString&, const NString&, character, character)
294/// accepting a \alib{camp;Camp} and using its \alib{resources;ResourcePool} and
295/// field \alib{camp;Camp::ResourceCategory}.
296///
297/// \note
298/// This is the preferred overload taken with \alib to load built-in enum records.
299/// The only exception is the use of overload #Bootstrap(character, character)
300/// for enum record types that require a specialization of
301/// \alib{resources;ResourcedTraits} to perform "recursive" acquisition of other
302/// resources defined by fields of the records.
303///
304/// \par Availability
305/// This namespace function is available only if the module \alib_camp is included in
306/// the \alibbuild.
307///
308/// \see Chapters \ref alib_enums_records_resourced_tresourced and
309/// \ref alib_enums_records_resourced_from_modules for more information.
310///
311/// @tparam TEnum The enumeration type to load resourced records for.
312/// @param camp The module to use the resource pool and category name from.
313/// @param name The resource name of the externalized name. In the case that a
314/// resource with that name does not exist, it is tried to load
315/// a resource with index number \c 0 appended to this name, aiming to
316/// parse a single record. On success, the index is incremented until
317/// no consecutive resource is found.
318/// @param innerDelim The delimiter used for separating the fields of a record.
319/// Defaults to <c>','</c>.
320/// @param outerDelim The character delimiting enum records.
321/// Defaults to <c>','</c>.
322template<typename TEnum>
325 const NString& name,
326 character innerDelim= ',' ,
327 character outerDelim= ',' )
328{ Bootstrap<TEnum>(*camp.GetResourcePool(), camp.ResourceCategory, name, innerDelim, outerDelim); }
329
330} // namespace [alib::enumrecords::bootstrap]
331
332//==================================================================================================
333//=== Configuration variable construction with Camps
334//==================================================================================================
337/// Creates an undeclared configuration variable attached to an \alibcamp.
338/// \note While all other overloads of this shortcut-function lock the configuration instance of
339/// the given \p{camp}, with this method it this is not needed and thus not done.
340///
341/// @param camp An singleton which supplies the \b Configuration instance.
342/// @return A variable using the \alib{variables;Configuration} of the given \p{camp}.
343inline
345 return variables::Variable(camp.GetConfig()->Root());
346}
347
348/// Constructs and declares a variable, without using or allocating a declaration struct.
349/// \note This shortcut-function locks the configuration instance of the given \p{camp} for
350/// declaring the variable. Thus, it must not be locked before the call.
351/// @param camp An \alibcamp An \alibcamp singleton which supplies the
352/// \alib{variables;Configuration} instance.
353/// @param name The name of the variable.
354/// @param typeName The type of the variable.
355/// @param defaultValue An optional default value. Defaults to \e nulled string.
356/// @return The variable created.
357inline
359 const String& name, const String& typeName,
360 const String& defaultValue= NULL_STRING ) {
361 ALIB_LOCK_WITH(camp.GetConfig())
362 auto var= CampVariable(camp);
363 var.Declare( name, typeName, defaultValue );
364 return var;
365}
366
367/// Constructs and declares a variable.
368/// \note This shortcut-function locks the configuration instance of the given \p{camp} for
369/// declaring the variable. Thus, it must not be locked before the call.
370/// @param camp An \alibcamp singleton which supplies the \alib{variables;Configuration} instance.
371/// @param decl The declaration to use.
372/// @return The variable created.
373inline
375 ALIB_LOCK_WITH(camp.GetConfig())
376 auto var= CampVariable(camp);
377 var.Declare( decl );
378 return var;
379}
380
381/// Constructs and declares a variable.
382/// \note This shortcut-function locks the configuration instance of the given \p{camp} for
383/// declaring the variable. Thus, it must not be locked before the call.
384/// @tparam TEnum The type of parameter \p{Enum}
385/// @param camp An \alibcamp singleton which supplies the \alib{variables;Configuration} instance.
386/// @param Enum Element of an enum type that represents resourced configuration variables.
387/// @return The variable created.
388template<typename TEnum>
390 ALIB_LOCK_WITH(camp.GetConfig())
391 auto var= CampVariable(camp);
392 var.Declare( Declaration::Get( Enum ) );
393 return var;
394}
395
396/// Constructs and declares a variable.
397/// \note This shortcut-function locks the configuration instance of the given \p{camp} for
398/// declaring the variable. Thus, it must not be locked before the call.
399/// \attention This function might allocate a new record in the mono allocator of \p{cfg}.
400/// To avoid memory drain, consult the documentation of method
401/// \alib{variables;Configuration::StoreDeclaration}.,
402/// @tparam TEnum The type of parameter \p{Enum}
403/// @param camp An \alibcamp singleton which supplies the \alib{variables;Configuration}
404/// instance.
405/// @param Enum Element of an enum type that represents resourced configuration variables.
406/// @param replacements Replacement values.
407/// @return The variable created.
408template<typename TEnum>
409requires std::is_enum_v<TEnum>
411 ALIB_LOCK_WITH(camp.GetConfig())
412 auto var= CampVariable(camp);
413 var.Declare( camp.GetConfig()->StoreDeclaration( Declaration::Get( Enum ), replacements ) );
414 return var;
415}
416} // namespace [alib::camp]
417#include "ALib.Lang.CIMethods.H"
NCString ResourceCategory
Definition camp.inl:113
BootstrapPhases GetBootstrapState()
Definition camp.inl:174
SharedConfiguration config
Definition camp.inl:104
void BootstrapResource(const NString &name, const String &data)
Definition camp.inl:245
void operator=(const Camp &)=delete
Deleted copy assignment.
Camp(const Camp &)=delete
Deleted copy constructor.
void BootstrapSetPhase(BootstrapPhases phase)
Definition camp.inl:178
SharedConfiguration & GetConfig()
Definition camp.inl:211
virtual ~Camp()
Virtual destructor to satisfy C++ abstract type rules.
Definition camp.inl:137
virtual void Shutdown(ShutdownPhases phase)=0
Camp(Camp &&)=delete
Deleted move constructor.
virtual void Bootstrap()=0
int bootstrapState
Definition camp.inl:91
const String & TryResource(const NString &name)
Definition camp.inl:276
SharedPtr< resources::ResourcePool, MonoAllocator > SPResourcePool
Definition camp.inl:86
void BootstrapSetConfig(const SharedConfiguration &pConfig)
Definition camp.inl:202
void BootstrapSetResourcePool(const SPResourcePool &pResourcePool)
Definition camp.inl:228
bool IsBootstrapped()
Definition camp.inl:183
Camp(const NCString &resourceCategory)
Definition camp.inl:120
SPResourcePool resourcePool
Definition camp.inl:95
SPResourcePool & GetResourcePool()
Definition camp.inl:238
const String & GetResource(const NString &name)
Definition camp.inl:260
static const Declaration * Get(TEnum element)
#define ALIB_ASSERT_WARNING(cond, domain,...)
Definition alib.inl:1067
#define ALIB_EXPORT
Definition alib.inl:497
#define ALIB_DBG(...)
Definition alib.inl:853
#define ALIB_LOCK_WITH(lock)
Definition alib.inl:1339
void Bootstrap(camp::Camp &camp, const NString &name, character innerDelim=',', character outerDelim=',')
Definition camp.inl:324
variables::Variable CampVariable(camp::Camp &camp)
Definition camp.inl:344
constexpr String NULL_STRING
A nulled string of the default character type.
Definition string.inl:2271
BootstrapPhases
Definition camp.inl:15
@ Final
The final initialization phase. Here, camps are initializing their custom setup.
Definition camp.inl:35
ShutdownPhases
Termination levels usable with Bootstrapping ALib Camps.
Definition camp.inl:42
@ Destruct
The main phase of termination that destructs everything.
Definition camp.inl:46
strings::TString< nchar > NString
Type alias in namespace alib.
Definition string.inl:2198
variables::TSharedConfiguration< SharedLock > SharedConfiguration
Type alias in namespace alib.
strings::TCString< nchar > NCString
Type alias in namespace alib.
Definition cstring.inl:484
boxing::Enum Enum
Type alias in namespace alib.
Definition enum.inl:192
boxing::Box Box
Type alias in namespace alib.
Definition box.inl:1149
strings::TString< character > String
Type alias in namespace alib.
Definition string.inl:2189
characters::character character
Type alias in namespace alib.
containers::SharedPtr< T, TAllocator > SharedPtr
Type alias in namespace alib.