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