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