ALib C++ Library
Library Version: 2402 R1
Documentation generated by doxygen
Loading...
Searching...
No Matches
bootstrap.cpp
1// #################################################################################################
2// ALib C++ Library
3//
4// Copyright 2013-2024 A-Worx GmbH, Germany
5// Published under 'Boost Software License' (a free software license, see LICENSE.txt)
6// #################################################################################################
8
9#if !defined(ALIB_DOX)
10# if !defined (HPP_ALIB)
11# include "alib/alib.hpp"
12# endif
13
14# if !defined (HPP_ALIB_LANG_BASECAMP)
16# endif
17
18# if ALIB_BOXING && !defined (HPP_ALIB_BOXING_BOXING)
19# include "alib/boxing/boxing.hpp"
20# endif
21
22# if ALIB_CONFIGURATION
23# if !defined (HPP_ALIB_CONFIG_CONFI)
24# include "alib/config/config.hpp"
25# endif
26# if !defined (HPP_ALIB_CONFIG_CONFIGURATION)
28# endif
29# endif
30
31# if !defined (HPP_ALIB_LANG_RESOURCES_LOCALRESOURCEPOOL)
33# endif
34
35# if ALIB_TIME
36# if !defined (HPP_ALIB_TIME_TIME)
37# include "alib/time/time.hpp"
38# endif
39# endif
40
41# if ALIB_THREADS && !defined (HPP_ALIB_THREADS_THREAD)
43# endif
44
45
46# if ALIB_ALOX
47# if !defined (HPP_ALIB_ALOXMODULE)
49# endif
50# endif
51# if ALIB_CLI
52# if !defined (HPP_ALIB_CLI_CLI)
53# include "alib/cli/cli.hpp"
54# endif
55# endif
56# if ALIB_EXPRESSIONS
57# if !defined (HPP_ALIB_EXPRESSIONS_EXPRESSIONS)
59# endif
60# endif
61# if ALIB_FILES
62# if !defined (HPP_ALIB_FILES_CAMP)
64# endif
65# endif
66# if !defined (HPP_ALIB_LANG_CAMP_BOOTSTRAP)
68# endif
69
70# if !defined (HPP_ALIB_LANG_CAMP_INLINES)
72# endif
73#endif // !defined(ALIB_DOX)
74
75//#include <iostream>
76
77using namespace alib::lang;
78
79namespace alib {
80
82
84{
85 ALIB_ASSERT_ERROR( Camps.IsEmpty(), "CAMPS", "List Camps already set." )
86
87 ALIB_IF_CAMP( Camps.PushBack( &BASECAMP ); )
88 ALIB_IF_CONFIGURATION( Camps.PushBack( &CONFIG ); )
89 ALIB_IF_ALOX( Camps.PushBack( &ALOX ); )
90 ALIB_IF_CLI( Camps.PushBack( &CLI ); )
92 ALIB_IF_FILES( Camps.PushBack( &FILES ); )
93}
94
95void Bootstrap( BootstrapPhases targetPhase,
96 Camp* targetCamp,
97 int alibVersion, int alibRevision, TCompilationFlags compilationFlags )
98{
99 // verify ALib
100 AssertALibVersionAndFlags( alibVersion, alibRevision, compilationFlags );
101
102 // if not customized, create default module list
103 if( Camps.IsEmpty() )
105
106 if( targetCamp == nullptr )
107 targetCamp= Camps.Back();
108
109 //std::cout << "Camp::Bootstrap called on: '" << this->ResourceCategory << "', target phase: " << int(targetPhase) << std::endl;
110
111 // Initialize non-camp modules once
113 {
119 }
120
121 // find target camp in the list of camps
122 auto targetCampIt= Camps.rbegin();
123 while(targetCampIt != Camps.rend() &&
124 *targetCampIt != targetCamp )
125 ++targetCampIt;
126
127 ALIB_ASSERT_ERROR(targetCampIt != Camps.rend(), "CAMPS",
128 "Target camp given to function alib::Bootstrap() is not included in list alib::Camps.\n"
129 "Resource category of the target camp: ", targetCamp->ResourceCategory )
130
131
132 // loop over all phases
133 for ( int phaseIntegral= targetCamp->bootstrapState + 1 ;
134 phaseIntegral <= UnderlyingIntegral( targetPhase ) ;
135 ++phaseIntegral )
136 {
137 BootstrapPhases actualPhase = BootstrapPhases( phaseIntegral );
138
139 // phase 1: distribute a resource pool
140 if ( actualPhase == BootstrapPhases::PrepareResources )
141 {
142 // create a resource pool?
143 if ( targetCamp->resourcePool == nullptr )
144 {
145 targetCamp->isResourceOwner = true;
148 targetCamp->resourcePool= pool;
149
150 // \releasetask{update resource numbers numbers}
151 integer expectedSize= 97 // ALIB distribution resources
152 ALIB_IF_ALOX ( + 42 )
154 ALIB_IF_CLI ( + 17 )
155 ALIB_IF_EXPRESSIONS ( + 256 )
156 ALIB_IF_FILES ( + 43 ) ;
157
158 auto& hashMap= pool->BootstrapGetInternalHashMap();
159 hashMap.BaseLoadFactor( 2.0 );
160 hashMap.MaxLoadFactor ( 5.0 );
161 hashMap.Reserve ( expectedSize, ValueReference::Absolute );
162 }
163
164
165 // loop in reverse order over modules, start with this module
166 auto* actPool = targetCamp->resourcePool;
167 for(auto campIt= targetCampIt ; campIt != Camps.rend() ; ++campIt )
168 {
169
170 // if a different resources object is set, then use that one from now on
171 if((*campIt)->resourcePool != nullptr && (*campIt)->resourcePool != actPool)
172 {
173 actPool= (*campIt)->resourcePool;
174 continue;
175 }
176
177 (*campIt)->resourcePool= actPool;
178
179 } // resources distribution loop
180 }
181
182 // phase 2: create and distribute a configuration
183 #if ALIB_CONFIGURATION
184 else if ( actualPhase == BootstrapPhases::PrepareConfig)
185 {
186 // create a configuration?
187 if ( targetCamp->config == nullptr )
188 {
189 targetCamp->isConfigOwner= true;
191
192 targetCamp->config = alloc.Emplace<Configuration>( CreateDefaults::No );
193
194 targetCamp->config->InsertPlugin( alloc.Emplace<InMemoryPlugin>( CONFIG.GetResource( "CfgPlgDef" ) ),
195 Priorities::DefaultValues, Responsibility::KeepWithSender );
196
197 targetCamp->config->InsertPlugin( alloc.Emplace<config::Environment>(),
198 Priorities::Environment, Responsibility::KeepWithSender );
199
200 targetCamp->config->InsertPlugin( alloc.Emplace<config::CLIArgs>(),
201 Priorities::CLI, Responsibility::KeepWithSender );
202
203 targetCamp->config->InsertPlugin( alloc.Emplace<InMemoryPlugin>( CONFIG.GetResource( "CfgPlgPro" ) ),
204 Priorities::ProtectedValues, Responsibility::KeepWithSender );
205
207
208 if ( ArgC > 0 )
209 {
210 if ( ArgVN != nullptr )
211 targetCamp->config->SetCommandLineArgs( ArgC, ArgVN );
212 else
213 targetCamp->config->SetCommandLineArgs( ArgC, ArgVW );
214 }
215 }
216
217 // loop in reverse order over modules, start with this module
218 auto* actConfig = targetCamp->config;
219 for(auto module= targetCampIt ; module != Camps.rend() ; ++module )
220 {
221 // if a different resources object is set, then use that one from now on
222 if( (*module)->config != nullptr && (*module)->config != actConfig)
223 actConfig= (*module)->config;
224 else
225 (*module)->config= actConfig;
226
227 } // resources distribution loop
228 }
229 #endif
230
231 // initialize modules on this phase
232 ALIB_DBG( bool foundThisModuleInList = false; )
233 for ( auto* camp : Camps )
234 {
235 if(camp->bootstrapState >= UnderlyingIntegral(actualPhase ) )
236 continue;
237
238 //std::cout << "Camp::Bootstrap '" << module->ResourceCategory << "', phase: " << int(actualPhase) << std::endl;
239
240 // propagate resource pool and config, if sub-module does not provide an own instance
241 if ( actualPhase == BootstrapPhases::PrepareResources &&
242 camp->resourcePool == nullptr )
243 {
244 camp->resourcePool = targetCamp->resourcePool;
245 }
246 #if ALIB_CONFIGURATION
247 else if ( actualPhase == BootstrapPhases::PrepareConfig &&
248 camp->config == nullptr )
249 {
250 camp->config = targetCamp->config;
251 }
252 #endif
253
254 // bootstrap module
255 ALIB_ASSERT_ERROR( camp->bootstrapState == phaseIntegral - 1,
256 "With this invocation of Bootstrap() a camp skips a bootstrap phase \n"
257 "Resource category of the target camp: ", camp->ResourceCategory )
258 camp->bootstrap(actualPhase );
259
260 camp->bootstrapState= UnderlyingIntegral(actualPhase );
261
262 // stop if this is us
263 if (camp == targetCamp )
264 {
265 ALIB_DBG( foundThisModuleInList = true );
266 break;
267 }
268 }
269 ALIB_ASSERT_ERROR( foundThisModuleInList, "CAMPS",
270 "The target camp of function Bootstrap is not included in list alib::Camps "
271 "or was already bootstrapped for this phase!\n"
272 "Resource category of the target camp: ", targetCamp->ResourceCategory )
273
274 }
275}
276
277void Shutdown( ShutdownPhases targetPhase,
278 Camp* targetCamp )
279{
280 if( targetCamp == nullptr )
281 targetCamp= Camps.Front();
282
283 //std::cout << "Camp::Shutdown called on'" << targetCamp->ResourceCategory << "', target phase: " << int(targetPhase) << std::endl;
284
285 // find target camp in the list of camps
286 auto targetCampIt= Camps.begin();
287 while( targetCampIt != Camps.end()
288 && *targetCampIt != targetCamp )
289 ++targetCampIt;
290 ALIB_ASSERT_ERROR(targetCampIt != Camps.end(), "CAMPS",
291 "Target camp given to function alib::Shutdown() is not included in list alib::Camps.\n"
292 "Resource category of the target camp: ", targetCamp->ResourceCategory )
293
294
295 ALIB_DBG( bool foundThisModuleInList= false );
296 // loop over all (both) termination levels
297 for( auto phaseIntegral= UnderlyingIntegral( ShutdownPhases::Announce )
298 ; phaseIntegral<= UnderlyingIntegral( targetPhase )
299 ; ++phaseIntegral )
300 {
301 ShutdownPhases actualPhase = ShutdownPhases( phaseIntegral );
302
303 // shutdown in reverse order
304 for(auto campIt= Camps.rbegin() ; campIt != Camps.rend() ; ++campIt )
305 {
306 ALIB_ASSERT_ERROR( ( *campIt )->bootstrapState < 0
307 || ( *campIt )->bootstrapState == UnderlyingIntegral(BootstrapPhases::Final ),
308 "CAMPS", "Trying to terminate a not (fully) initialized module. "
309 "Module Name (resource category): ", targetCamp->ResourceCategory )
310
311 // shutdown module
312 if (( *campIt )->bootstrapState > -UnderlyingIntegral(actualPhase) )
313 {
314 //std::cout << "Camp::Shutdown '" << (*campIt)->ResourceCategory << "', phase: " << int(actualPhase) << std::endl;
315
316 ALIB_ASSERT_ERROR( ( ( *campIt )->bootstrapState == 3 && phaseIntegral == 1 )
317 || ( ( *campIt )->bootstrapState == -1 && phaseIntegral == 2 ),
318 "CAMPS", "With this invocation of Bootstrap(), a camp skips a bootstrap phase \n"
319 "Resource category of the target camp: ", ( *campIt )->ResourceCategory )
320
321
322 ( *campIt )->shutdown(actualPhase );
323 ( *campIt )->bootstrapState = -UnderlyingIntegral(actualPhase );
324
325 ALIB_DBG( if(( *campIt ) == targetCamp )
326 foundThisModuleInList= true; )
327 }
328
329 if(( *campIt ) == targetCamp )
330 break;
331 }
332 }
333 ALIB_ASSERT_ERROR( foundThisModuleInList, "CAMPS",
334 "The target camp of function Shutdown is not included in list alib::Camps "
335 "or was already shutdown for this phase!\n"
336 "Resource category of the target camp: ", targetCamp->ResourceCategory )
337
338
339 // delete resources/config
340 if( targetPhase == ShutdownPhases::Destruct)
341 for(auto campIt= Camps.rbegin() ; campIt != Camps.rend() ; ++campIt )
342 {
343 if ( ( *campIt )->isResourceOwner ) monomem::Destruct(( *campIt )->resourcePool );
344 #if ALIB_CONFIGURATION
345 if ( ( *campIt )->isConfigOwner )
346 {
347 Configuration* config= (*campIt)->config;
348 auto *im = config->GetPluginTypeSafe<InMemoryPlugin>( Priorities::DefaultValues );
349 if ( im ) { config->RemovePlugin( Priorities::DefaultValues );
350 monomem::Destruct( im ); }
351
352 auto *e = config->GetPluginTypeSafe<config::Environment>( Priorities::Environment );
353 if ( e ) { config->RemovePlugin( Priorities::Environment );
354 monomem::Destruct( e ); }
355
356 auto *cli = config->GetPluginTypeSafe<config::CLIArgs>( Priorities::CLI );
357 if ( cli ){ config->RemovePlugin( Priorities::CLI );
358 monomem::Destruct( cli ); }
359
360 im = config->GetPluginTypeSafe<InMemoryPlugin>( Priorities::ProtectedValues );
361 if ( im ) { config->RemovePlugin( Priorities::ProtectedValues );
362 monomem::Destruct( im ); }
363
364 monomem::Destruct( config );
365 }
366 #endif
367
368 if(( *campIt ) == targetCamp )
369 break;
370 }
371
372 if( targetPhase == ShutdownPhases::Destruct )
373 {
377 }
378}
379
380} // namespace [alib]
void SetCommandLineArgs(int argc=0, const nchar **argv=nullptr)
int bootstrapState
Definition camp.hpp:89
bool isResourceOwner
Definition camp.hpp:105
bool isConfigOwner
Definition camp.hpp:130
NCString ResourceCategory
Definition camp.hpp:142
lang::resources::ResourcePool * resourcePool
Definition camp.hpp:98
config::Configuration * config
Definition camp.hpp:121
const String & GetResource(const NString &name)
void InsertPlugin(TPlugin *plugin, TPriorities priority, lang::Responsibility responsibility=lang::Responsibility::KeepWithSender)
bool RemovePlugin(TPlugin *plugIn)
TPluginType * GetPluginTypeSafe(TPriorities priority)
detail::StaticResourceMap & BootstrapGetInternalHashMap()
void BaseLoadFactor(float newBaseLoadFactor)
ALIB_FORCE_INLINE T * Emplace(TArgs &&... args)
#define ALIB_IF_CONFIGURATION(...)
Definition alib.hpp:239
#define ALIB_IF_TIME(...)
Definition alib.hpp:311
#define ALIB_IF_BOXING(...)
Definition alib.hpp:215
#define ALIB_IF_EXPRESSIONS(...)
Definition alib.hpp:255
#define ALIB_IF_CAMP(...)
Definition alib.hpp:295
#define ALIB_IF_SINGLETONS(...)
Definition alib.hpp:279
#define ALIB_IF_THREADS(...)
Definition alib.hpp:303
#define ALIB_IF_ALOX(...)
Definition alib.hpp:199
#define ALIB_IF_ENUMS(...)
Definition alib.hpp:247
#define ALIB_IF_FILES(...)
Definition alib.hpp:263
#define ALIB_ASSERT_ERROR(cond,...)
Definition alib.hpp:984
#define ALIB_DBG(...)
Definition alib.hpp:457
#define ALIB_CALLER_PRUNED
Definition alib.hpp:845
#define ALIB_IF_CLI(...)
Definition alib.hpp:231
void Bootstrap()
Definition boxing.cpp:732
ALIB_API void Bootstrap()
static ALIB_FORCE_INLINE void Destruct(T *object)
void ReleaseGlobalAllocator()
MonoAllocator GlobalAllocator(8 *1024)
MonoAllocator & AcquireGlobalAllocator(const NCString &dbgFile, int dbgLine, const NCString &dbgFunc)
void Bootstrap()
Definition thread.cpp:140
void Shutdown()
Definition thread.cpp:179
void Bootstrap()
Definition time.cpp:50
void Shutdown()
Definition time.cpp:63
Definition alib.cpp:57
files::Files FILES
Definition filescamp.cpp:30
ShutdownPhases
Definition camp.hpp:35
@ Destruct
The main phase of termination that destructs everything.
cli::Cli CLI
Definition cli.cpp:30
ALIB_WARNINGS_ALLOW_UNSAFE_BUFFER_USAGE void AssertALibVersionAndFlags(int alibVersion, int alibRevision, TCompilationFlags compilationFlags)
Definition alib.cpp:140
const char ** ArgVN
Definition alib.cpp:60
ALIB_API List< lang::Camp * > Camps
lang::basecamp::BaseCamp BASECAMP
Definition basecamp.cpp:136
int ArgC
Definition alib.cpp:59
expressions::Expressions EXPRESSIONS
BootstrapPhases
Definition camp.hpp:26
@ PrepareConfig
Initializes up to the creation of a field config .
@ Final
The final initialization phase.
lox::ALox ALOX
bool NonCampModulesInitialized
Definition alib.cpp:63
const wchar_t ** ArgVW
Definition alib.cpp:61
void BootstrapAddDefaultCamps()
Definition bootstrap.cpp:83
ALIB_WARNINGS_RESTORE void Bootstrap(int alibVersion, int alibRevision, TCompilationFlags compilationFlags)
Definition alib.cpp:72
void Shutdown()
Definition alib.cpp:94
config::Config CONFIG
Definition config.cpp:40
lang::integer integer
Type alias in namespace alib.
Definition integers.hpp:286