Medical Imaging Interaction Toolkit  2026.06.00
Medical Imaging Interaction Toolkit
usModuleInitialization.h
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #ifndef US_MODULE_NAME
14 #error Missing US_MODULE_NAME preprocessor define
15 #endif
16 
17 #ifndef USMODULEINITIALIZATION_H
18 #define USMODULEINITIALIZATION_H
19 
20 #include <usStaticInit_p.h>
21 
22 #include <usModuleRegistry.h>
23 #include <usModuleInfo.h>
24 #include <usModuleUtils_p.h>
25 
26 #include <cstring>
27 
28 
48 #define US_INITIALIZE_MODULE \
49 namespace us { \
50 namespace { \
51  \
52 /* Declare a file scoped ModuleInfo object */ \
53 US_GLOBAL_STATIC_WITH_ARGS(ModuleInfo, moduleInfo, (US_STR(US_MODULE_NAME))) \
54  \
55 /* This class is used to statically initialize the library within the C++ Micro services \
56  library. It looks up a library specific C-style function returning an instance \
57  of the ModuleActivator interface. */ \
58 class US_ABI_LOCAL US_CONCAT(ModuleInitializer_, US_MODULE_NAME) { \
59  \
60 public: \
61  \
62  US_CONCAT(ModuleInitializer_, US_MODULE_NAME)() \
63  { \
64  ModuleInfo*(*moduleInfoPtr)() = moduleInfo; \
65  void* moduleInfoSym = nullptr; \
66  std::memcpy(&moduleInfoSym, &moduleInfoPtr, sizeof(void*)); \
67  std::string location = ModuleUtils::GetLibraryPath(moduleInfoSym); \
68  moduleInfoPtr()->location = location; \
69  \
70  Register(); \
71  } \
72  \
73  static void Register() \
74  { \
75  ModuleRegistry::Register(moduleInfo()); \
76  } \
77  \
78  ~US_CONCAT(ModuleInitializer_, US_MODULE_NAME)() \
79  { \
80  ModuleRegistry::UnRegister(moduleInfo()); \
81  } \
82  \
83 }; \
84  \
85  \
86 US_DEFINE_MODULE_INITIALIZER \
87 } \
88  \
89 } \
90  \
91 /* A helper function which is called by the US_IMPORT_MODULE macro to initialize \
92  static modules */ \
93 extern "C" void US_ABI_LOCAL US_CONCAT(_us_import_module_initializer_, US_MODULE_NAME)() \
94 { \
95  static us::US_CONCAT(ModuleInitializer_, US_MODULE_NAME) US_CONCAT(_InitializeModule_, US_MODULE_NAME); \
96 }
97 
98 // Create a file-scoped static object for registering the module
99 // during static initialization of the shared library
100 #define US_DEFINE_MODULE_INITIALIZER \
101 static US_CONCAT(ModuleInitializer_, US_MODULE_NAME) US_CONCAT(_InitializeModule_, US_MODULE_NAME);
102 
103 // Static modules don't create a file-scoped static object for initialization
104 // (it would be discarded during static linking anyway). The initialization code
105 // is triggered by the US_IMPORT_MODULE macro instead.
106 #if defined(US_STATIC_MODULE)
107 #undef US_DEFINE_MODULE_INITIALIZER
108 #define US_DEFINE_MODULE_INITIALIZER
109 #endif
110 
111 #endif // USMODULEINITIALIZATION_H