Medical Imaging Interaction Toolkit  2024.12.99-0da743f6
Medical Imaging Interaction Toolkit
usModuleInitialization.h
Go to the documentation of this file.
1 /*============================================================================
2 
3  Library: CppMicroServices
4 
5  Copyright (c) German Cancer Research Center (DKFZ)
6  All rights reserved.
7 
8  Licensed under the Apache License, Version 2.0 (the "License");
9  you may not use this file except in compliance with the License.
10  You may obtain a copy of the License at
11 
12  https://www.apache.org/licenses/LICENSE-2.0
13 
14  Unless required by applicable law or agreed to in writing, software
15  distributed under the License is distributed on an "AS IS" BASIS,
16  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  See the License for the specific language governing permissions and
18  limitations under the License.
19 
20 ============================================================================*/
21 
22 #ifndef US_MODULE_NAME
23 #error Missing US_MODULE_NAME preprocessor define
24 #endif
25 
26 #ifndef USMODULEINITIALIZATION_H
27 #define USMODULEINITIALIZATION_H
28 
29 #include <usStaticInit_p.h>
30 
31 #include <usModuleRegistry.h>
32 #include <usModuleInfo.h>
33 #include <usModuleUtils_p.h>
34 
35 #include <cstring>
36 
37 
57 #define US_INITIALIZE_MODULE \
58 US_BEGIN_NAMESPACE \
59 namespace { \
60  \
61 /* Declare a file scoped ModuleInfo object */ \
62 US_GLOBAL_STATIC_WITH_ARGS(ModuleInfo, moduleInfo, (US_STR(US_MODULE_NAME))) \
63  \
64 /* This class is used to statically initialize the library within the C++ Micro services \
65  library. It looks up a library specific C-style function returning an instance \
66  of the ModuleActivator interface. */ \
67 class US_ABI_LOCAL US_CONCAT(ModuleInitializer_, US_MODULE_NAME) { \
68  \
69 public: \
70  \
71  US_CONCAT(ModuleInitializer_, US_MODULE_NAME)() \
72  { \
73  ModuleInfo*(*moduleInfoPtr)() = moduleInfo; \
74  void* moduleInfoSym = nullptr; \
75  std::memcpy(&moduleInfoSym, &moduleInfoPtr, sizeof(void*)); \
76  std::string location = ModuleUtils::GetLibraryPath(moduleInfoSym); \
77  moduleInfoPtr()->location = location; \
78  \
79  Register(); \
80  } \
81  \
82  static void Register() \
83  { \
84  ModuleRegistry::Register(moduleInfo()); \
85  } \
86  \
87  ~US_CONCAT(ModuleInitializer_, US_MODULE_NAME)() \
88  { \
89  ModuleRegistry::UnRegister(moduleInfo()); \
90  } \
91  \
92 }; \
93  \
94  \
95 US_DEFINE_MODULE_INITIALIZER \
96 } \
97  \
98 US_END_NAMESPACE \
99  \
100 /* A helper function which is called by the US_IMPORT_MODULE macro to initialize \
101  static modules */ \
102 extern "C" void US_ABI_LOCAL US_CONCAT(_us_import_module_initializer_, US_MODULE_NAME)() \
103 { \
104  static US_PREPEND_NAMESPACE(US_CONCAT(ModuleInitializer_, US_MODULE_NAME)) US_CONCAT(_InitializeModule_, US_MODULE_NAME); \
105 }
106 
107 // Create a file-scoped static object for registering the module
108 // during static initialization of the shared library
109 #define US_DEFINE_MODULE_INITIALIZER \
110 static US_CONCAT(ModuleInitializer_, US_MODULE_NAME) US_CONCAT(_InitializeModule_, US_MODULE_NAME);
111 
112 // Static modules don't create a file-scoped static object for initialization
113 // (it would be discarded during static linking anyway). The initialization code
114 // is triggered by the US_IMPORT_MODULE macro instead.
115 #if defined(US_STATIC_MODULE)
116 #undef US_DEFINE_MODULE_INITIALIZER
117 #define US_DEFINE_MODULE_INITIALIZER
118 #endif
119 
120 #endif // USMODULEINITIALIZATION_H
usModuleRegistry.h
usModuleInfo.h