ALib C++ Library
Library Version: 2312 R0
Documentation generated by doxygen
module.cpp
1 // #################################################################################################
2 // ALib C++ Library
3 //
4 // Copyright 2013-2023 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_FS_MODULES_MODULE)
12 # endif
13 # if !defined(HPP_ALIB_FS_MODULES_DISTRIBUTION)
15 # endif
16 
17 # if ALIB_BOXING && !defined (HPP_ALIB_BOXING_BOXING)
18 # include "alib/boxing/boxing.hpp"
19 # endif
20 
21 # if ALIB_CONFIGURATION
22 # if !defined (HPP_ALIB_CONFIG_CONFI)
23 # include "alib/config/config.hpp"
24 # endif
25 # if !defined (HPP_ALIB_CONFIG_CONFIGURATION)
27 # endif
28 # endif
29 
30 # if !defined (HPP_ALIB_ENUMS_ITERABLE)
31 # include "alib/enums/iterable.hpp"
32 # endif
33 
34 # if !defined (HPP_ALIB_RESOURCES_LOCALRESOURCEPOOL)
36 # endif
37 
38 # if ALIB_TIME
39 # if !defined (HPP_ALIB_TIME_TIME)
40 # include "alib/time/time.hpp"
41 # endif
42 # endif
43 
44 # if ALIB_THREADS && !defined (HPP_ALIB_THREADS_THREAD)
45 # include "alib/threads/thread.hpp"
46 # endif
47 
48 
49 # if !defined (_GLIBCXX_IOSTREAM) && !defined (_IOSTREAM_ )
50 # include <iostream>
51 # endif
52 # if !defined (_GLIBCXX_IOMANIP) && !defined (_IOMANIP_ )
53 # include <iomanip>
54 # endif
55 #endif // !defined(ALIB_DOX)
56 
57 
58 
59 namespace aworx { namespace lib {
60 
61 // static fields
63 
64 bool Module::Bootstrap( BootstrapPhases targetPhase, int argc, const char** argvN, const wchar_t** argvW )
65 {
66  // if not customized, create default module list
67  if( ALIB.Modules.IsEmpty() )
69 
70  // Initialize "micro" modules once
72  {
77  }
78 
79  // args given but not first phase?
80  ALIB_ASSERT_WARNING( argc == 0 || bootstrapState == 0, "MODULES",
81  "Command line args ignored. "
82  "Accepted only with initialization level 'PrepareResources'." )
83 
84  if( bootstrapState >= UnderlyingIntegral( targetPhase ) )
85  {
86  ALIB_WARNING( "MODULE", "Given initialization level already performed on module. "
87  "Module Name (resource category): ", ResourceCategory )
88  return false;
89  }
90 
91  // find this module in the list of modules
92  auto thisModuleIt= ALIB.Modules.rbegin();
93  while( thisModuleIt != ALIB.Modules.rend() &&
94  *thisModuleIt != this )
95  ++thisModuleIt;
96 
97  ALIB_ASSERT_ERROR( thisModuleIt != ALIB.Modules.rend(), "MODULES",
98  "Trying to bootstrap a module that is not included in list ALibDistribution::Modules.\n"
99  "Resource category of the module: ", (*thisModuleIt)->ResourceCategory )
100 
101 
102  // loop over all phases
103  for ( int phaseIntegral= bootstrapState + 1 ;
104  phaseIntegral <= UnderlyingIntegral( targetPhase ) ;
105  ++phaseIntegral )
106  {
107  BootstrapPhases actualPhase = BootstrapPhases( phaseIntegral );
108 
109  // phase 1: distribute a resource pool
110  if ( actualPhase == BootstrapPhases::PrepareResources )
111  {
112  // create a resource pool?
113  if ( resourcePool == nullptr )
114  {
115  isResourceOwner = true;
117  resourcePool= pool;
118 
119  // \releasetask{update resource numbers numbers}
120  integer expectedSize= 33 // ALIB distribution resources
121  ALIB_IF_ALOX ( + 42 )
122  ALIB_IF_CONFIGURATION ( + 9 )
123  ALIB_IF_CLI ( + 17 )
124  ALIB_IF_EXPRESSIONS ( + 256 )
125  ALIB_IF_RESULTS ( + 5 )
126  ALIB_IF_TEXT ( + 47 )
127  ALIB_IF_SYSTEM ( + 11 ) ;
128  auto& hashMap= pool->BootstrapGetInternalHashMap();
129  hashMap.BaseLoadFactor( 2.0 );
130  hashMap.MaxLoadFactor ( 5.0 );
131  hashMap.Reserve ( expectedSize, ValueReference::Absolute );
132  }
133 
134 
135  // loop in reverse order over modules, start with this module
136  auto* actPool = resourcePool;
137  for( auto moduleIt= thisModuleIt ; moduleIt != ALIB.Modules.rend() ; ++moduleIt )
138  {
139  // if a different resources object is set, then use that one from now on
140  if( (*moduleIt)->resourcePool != nullptr && (*moduleIt)->resourcePool != actPool)
141  {
142  actPool= (*moduleIt)->resourcePool;
143  continue;
144  }
145 
146  (*moduleIt)->resourcePool= actPool;
147 
148  } // resources distribution loop
149  }
150 
151  // phase 2: create and distribute a configuration
152  #if !ALIB_CONFIGURATION
153  (void) argc; (void) argvN; (void) argvW;
154  #else
155  else if ( actualPhase == BootstrapPhases::PrepareConfig)
156  {
157  // create a configuration?
158  if ( config == nullptr )
159  {
160  isConfigOwner = true;
162 
163  config = alloc.Emplace<Configuration>( CreateDefaults::No );
164 
166  alloc.Emplace<InMemoryPlugin>( CONFIG.GetResource( "CfgPlgDef" ) ),
167  Priorities::DefaultValues, Responsibility::KeepWithSender );
168 
169  config->InsertPlugin( alloc.Emplace<lib::config::Environment>(),
171 
172  config->InsertPlugin( alloc.Emplace<lib::config::CLIArgs>(),
173  Priorities::CLI, Responsibility::KeepWithSender );
174 
176  alloc.Emplace<InMemoryPlugin>( CONFIG.GetResource( "CfgPlgPro" ) ),
177  Priorities::ProtectedValues, Responsibility::KeepWithSender );
178 
180 
181  if ( argc > 0 )
182  {
183  if ( argvN != nullptr )
184  config->SetCommandLineArgs( argc, argvN );
185  else
186  config->SetCommandLineArgs( argc, argvW );
187  }
188  }
189 
190  // loop in reverse order over modules, start with this module
191  auto* actConfig = config;
192  for( auto module= thisModuleIt ; module != ALIB.Modules.rend() ; ++module )
193  {
194  // if a different resources object is set, then use that one from now on
195  if( (*module)->config != nullptr && (*module)->config != actConfig)
196  {
197  actConfig= (*module)->config;
198  continue;
199  }
200 
201  (*module)->config= actConfig;
202 
203  } // resources distribution loop
204  }
205  #endif
206 
207  // initialize modules on this phase
208  ALIB_DBG( bool foundThisModuleInList = false; )
209  for ( auto* module : ALIB.Modules )
210  {
211  if( module->bootstrapState >= UnderlyingIntegral( actualPhase ) )
212  continue;
213 
214 
215  // propagate resource pool and config, if sub-module does not provide an own instance
216  if ( actualPhase == BootstrapPhases::PrepareResources &&
217  module->resourcePool == nullptr )
218  {
219  module->resourcePool = resourcePool;
220  }
221  #if ALIB_CONFIGURATION
222  else if ( actualPhase == BootstrapPhases::PrepareConfig &&
223  module->config == nullptr )
224  {
225  module->config = config;
226  }
227  #endif
228 
229  // bootstrap module
230  module->bootstrap( actualPhase, argc, argvN, argvW );
231 
232  module->bootstrapState= UnderlyingIntegral( actualPhase );
233 
234  // stop if this is us
235  if ( module == this )
236  {
237  ALIB_DBG( foundThisModuleInList = true );
238  break;
239  }
240  }
241  ALIB_ASSERT_ERROR( foundThisModuleInList, "MODULES",
242  "The module that method Bootstrap was invoked on is not included in "
243  "list Module::BootstrapModules." )
244  }
245 
246  return true;
247 }
248 
249 void Module::Shutdown( ShutdownPhases targetPhase )
250 {
251 
252  // check and set ourselves initialized
253  if( bootstrapState <= - UnderlyingIntegral( targetPhase ) )
254  {
255  ALIB_WARNING( "MODULE", "Termination level already performed." )
256  return;
257  }
258 
259 
260  // loop over all (both) termination levels
261  for( auto phaseIntegral= UnderlyingIntegral( ShutdownPhases::Announce )
262  ; phaseIntegral<= UnderlyingIntegral( targetPhase )
263  ; ++phaseIntegral )
264  {
265 
266  if( bootstrapState <= - phaseIntegral )
267  continue;
268 
269  ShutdownPhases actualPhase = ShutdownPhases( phaseIntegral );
270 
271  // shutdown in reverse order
272  bool foundThisModule= false;
273  for( auto module= ALIB.Modules.rbegin()
274  ; module != ALIB.Modules.rend()
275  ; ++module )
276  {
277  // not us (yet)
278  if ( !foundThisModule )
279  {
280  if ( *module != this )
281  continue;
282  foundThisModule = true;
283  }
284 
286  ( *module )->bootstrapState == UnderlyingIntegral( BootstrapPhases::Final )
287  || ( *module )->bootstrapState < 0, "MODULES",
288  "Trying to terminate a not (fully) initialized module. "
289  "Module Name (resource category): ", ResourceCategory )
290 
291  // shutdown module
292  if ( ( *module )->bootstrapState > -UnderlyingIntegral( actualPhase ) )
293  {
294  ( *module )->shutdown( actualPhase );
295  ( *module )->bootstrapState = -UnderlyingIntegral( actualPhase );
296  }
297  else
298  break; // if already done on this phase, modules following are likewise done
299 
300  } // module loop
301 
302  if( actualPhase == ShutdownPhases::Destruct )
303  {
304  #if ALIB_CONFIGURATION
306  if ( isConfigOwner )
307  {
308  auto *im = config->GetPluginTypeSafe<InMemoryPlugin>( Priorities::DefaultValues );
309  if ( im )
310  {
311  config->RemovePlugin( Priorities::DefaultValues );
312  monomem::Destruct( im );
313  }
314 
315 
318  if ( e )
319  {
321  monomem::Destruct( e );
322  }
323 
324 
325  auto *cli = config->GetPluginTypeSafe<lib::config::CLIArgs>( Priorities::CLI );
326  if ( cli )
327  {
328  config->RemovePlugin( Priorities::CLI );
329  monomem::Destruct( cli );
330  }
331 
332 
333  im = config->GetPluginTypeSafe<InMemoryPlugin>( Priorities::ProtectedValues );
334  if ( im )
335  {
336  config->RemovePlugin( Priorities::ProtectedValues );
337  monomem::Destruct( im );
338  }
339 
341  }
342  #endif
343 
347  }
348  }
349 }
350 
351 
352 
353 
354 bool Module::VerifyCompilationFlags( uint64_t flags )
355 {
356  // verify the flags
357  if ( flags == CompilationFlags )
358  return true;
359 
360  // dump out the flags
361  std::cout << std::left << std::setw(30) << "Symbol" << '|' << std::setw(5) << " Lib" <<'|' << " Comp. Unit" << std::endl;
362 
363  for( auto& p : CompilationFlagMeanings )
364  {
365  std::cout << std::setw(30) << p.first << '|' << std::setw(5) << (CompilationFlags & p.second ? " On" : " Off")
366  << "|" << std::setw(5) << (flags & p.second ? " On" : " Off")
367  << std::endl;
368  }
369 
370  return false;
371 }
372 
373 
374 
375 
376 }} // namespace [aworx::lib]
aworx::lib::time::Shutdown
void Shutdown()
Definition: time.cpp:63
aworx::lib::enums::UnderlyingIntegral
constexpr std::underlying_type< TEnum >::type UnderlyingIntegral(TEnum element) noexcept(true)
aworx::lib::monomem::MonoAllocator::Emplace
ALIB_FORCE_INLINE T * Emplace(TArgs &&... args)
Definition: monoallocator.hpp:504
ALIB_IF_THREADS
#define ALIB_IF_THREADS(...)
Definition: modules.hpp:364
localresourcepool.hpp
aworx::lib::Module::ShutdownPhases::Destruct
The main phase of termination that destructs everything.
aworx::lib::Module::resourcePool
resources::ResourcePool * resourcePool
Definition: module.hpp:114
aworx::lib::integer
platform_specific integer
Definition: integers.hpp:49
ALIB_WARNING
#define ALIB_WARNING(...)
Definition: tools.hpp:182
config.hpp
aworx::lib::Module::ShutdownPhases
ShutdownPhases
Definition: module.hpp:88
aworx::lib::ALibDistribution::BootstrapFillDefaultModuleList
ALIB_API void BootstrapFillDefaultModuleList()
Definition: distribution.cpp:191
aworx::lib::Responsibility::KeepWithSender
Keeps responsibility, e.g. when passing an object.
configuration.hpp
ALIB_IF_CLI
#define ALIB_IF_CLI(...)
Definition: modules.hpp:276
aworx::lib::Module::BootstrapPhases
BootstrapPhases
Definition: module.hpp:78
aworx::lib::Module::bootstrapState
int bootstrapState
Definition: module.hpp:102
aworx::lib::monomem::GlobalAllocator
MonoAllocator GlobalAllocator(8 *1024)
Definition: monoallocator.hpp:710
ALIB_IF_ALOX
#define ALIB_IF_ALOX(...)
Definition: modules.hpp:252
aworx::lib::Module::BootstrapPhases::PrepareResources
aworx::lib::monomem::ReleaseGlobalAllocator
void ReleaseGlobalAllocator()
aworx::lib::CreateDefaults::No
Do not create.
aworx::lib::Module::ResourceCategory
NCString ResourceCategory
Definition: module.hpp:172
aworx::lib::time::Bootstrap
void Bootstrap()
Definition: time.cpp:50
aworx::lib::config::InMemoryPlugin
Definition: inmemoryplugin.hpp:47
aworx::lib::singletons::Shutdown
void Shutdown()
Definition: singleton.cpp:168
alib_precompile.hpp
aworx::lib::Module::isConfigOwner
bool isConfigOwner
Definition: module.hpp:145
aworx::lib::Module::config
config::Configuration * config
Definition: module.hpp:138
aworx::lib::Module::Bootstrap
ALIB_API bool Bootstrap(BootstrapPhases targetPhase=BootstrapPhases::Final, int argc=0, const char **argvN=nullptr, const wchar_t **argvW=nullptr)
Definition: module.cpp:64
ALIB_IF_EXPRESSIONS
#define ALIB_IF_EXPRESSIONS(...)
Definition: modules.hpp:300
aworx::lib::Module::Shutdown
ALIB_API void Shutdown(ShutdownPhases targetPhase=ShutdownPhases::Destruct)
Definition: module.cpp:249
aworx::lib::detail::PluginContainer::RemovePlugin
bool RemovePlugin(TPlugin *plugIn)
Definition: lib/fs_plugins/plugins.hpp:162
thread.hpp
iterable.hpp
aworx::lib::threads::Bootstrap
void Bootstrap()
Definition: thread.cpp:140
ALIB_IF_SINGLETONS
#define ALIB_IF_SINGLETONS(...)
Definition: modules.hpp:332
aworx::lib::monomem::AcquireGlobalAllocator
MonoAllocator & AcquireGlobalAllocator(const NCString &dbgFile, int dbgLine, const NCString &dbgFunc)
aworx
Definition: alox/alox.hpp:40
aworx::lib::detail::PluginContainer::GetPluginTypeSafe
TPluginType * GetPluginTypeSafe(TPriorities priority)
Definition: lib/fs_plugins/plugins.hpp:289
ALIB_IF_TIME
#define ALIB_IF_TIME(...)
Definition: modules.hpp:372
ALIB_DBG
#define ALIB_DBG(...)
Definition: compilers.hpp:44
ALIB_IF_TEXT
#define ALIB_IF_TEXT(...)
Definition: modules.hpp:356
aworx::ALIB
lib::ALibDistribution ALIB
Definition: distribution.cpp:125
aworx::lib::Module::BootstrapPhases::Final
The final initialization phase.
aworx::lib::resources::LocalResourcePool::BootstrapGetInternalHashMap
detail::StaticResourceMap & BootstrapGetInternalHashMap()
Definition: localresourcepool.hpp:96
module.hpp
ALIB_CALLER_PRUNED
#define ALIB_CALLER_PRUNED
Definition: tools.hpp:71
ALIB_IF_RESULTS
#define ALIB_IF_RESULTS(...)
Definition: modules.hpp:316
aworx::lib::detail::PluginContainer::InsertPlugin
void InsertPlugin(TPlugin *plugin, TPriorities priority, Responsibility responsibility=Responsibility::KeepWithSender)
Definition: lib/fs_plugins/plugins.hpp:125
ALIB_ASSERT_ERROR
#define ALIB_ASSERT_ERROR(cond,...)
Definition: tools.hpp:185
aworx::lib::Module::VerifyCompilationFlags
ALIB_API bool VerifyCompilationFlags(uint64_t compilationFlags)
Definition: module.cpp:354
ALIB_IF_CONFIGURATION
#define ALIB_IF_CONFIGURATION(...)
Definition: modules.hpp:284
aworx::lib::config::Configuration
Definition: configuration.hpp:62
ALIB_ASSERT_WARNING
#define ALIB_ASSERT_WARNING(cond,...)
Definition: tools.hpp:186
aworx::lib::config::CLIArgs
Definition: config/plugins.hpp:320
aworx::lib::CONFIG
Config CONFIG
Definition: config.cpp:40
aworx::lib::Module::CompilationFlagMeanings
std::vector< std::pair< const nchar *, uint64_t > > CompilationFlagMeanings
Definition: module.hpp:188
boxing.hpp
aworx::lib::ValueReference::Absolute
Referring to an absolute value.
aworx::lib::monomem::Destruct
static ALIB_FORCE_INLINE void Destruct(T *object)
Definition: monoallocator.hpp:659
distribution.hpp
aworx::lib::Module::microModulesInitialized
static bool microModulesInitialized
Definition: module.hpp:105
aworx::lib::monomem::HashTable::BaseLoadFactor
void BaseLoadFactor(float newBaseLoadFactor)
Definition: hashtable.hpp:854
aworx::lib::Module::BootstrapPhases::PrepareConfig
ALIB_IF_BOXING
#define ALIB_IF_BOXING(...)
Definition: modules.hpp:260
aworx::lib::Module::ShutdownPhases::Announce
time.hpp
aworx::lib::config::Configuration::SetCommandLineArgs
void SetCommandLineArgs(int argc=0, const nchar **argv=nullptr)
Definition: configuration.hpp:155
aworx::lib::config::Environment
Definition: config/plugins.hpp:453
aworx::lib::boxing::Bootstrap
void Bootstrap()
Definition: boxing.cpp:732
aworx::lib::resources::LocalResourcePool
Definition: localresourcepool.hpp:47
aworx::lib::Module::isResourceOwner
bool isResourceOwner
Definition: module.hpp:121
aworx::lib::Module::CompilationFlags
const uint64_t CompilationFlags
Definition: module.hpp:180
aworx::lib::config::Priorities::Environment
aworx::lib::threads::Shutdown
void Shutdown()
Definition: thread.cpp:179
aworx::lib::ALibDistribution::Modules
List< Module * > Modules
Definition: lib/fs_modules/distribution.hpp:199
ALIB_IF_SYSTEM
#define ALIB_IF_SYSTEM(...)
Definition: modules.hpp:348