Medical Imaging Interaction Toolkit  2024.06.99-60d9b802
Medical Imaging Interaction Toolkit
The Module Context

In the context of the C++ Micro Services library, we will call all supported "shared library" types (DLL, DSO, DyLib, etc.) uniformly a module. A module accesses the C++ Micro Services API via a ModuleContext object, which is specific to each module.

Creating a ModuleContext

To create a ModuleContext object for a specific library, you have two options. If your project uses CMake as the build system, use the supplied usFunctionGenerateModuleInit CMake function to automatically create a source file and add it to your module's sources:

set(module_srcs )
usFunctionGenerateModuleInit(module_srcs)
add_library(mylib ${module_srcs})
set_property(TARGET ${mylib} APPEND PROPERTY COMPILE_DEFINITIONS US_MODULE_NAME=mylib)

You also need to specify a unique module name by using the US_MODULE_NAME compile definition as shown in the last line. The module name must be a valid C identifier and in the case of executables is required to be defined to main.

If you do not use CMake, you have to add a call to the macro #US_INITIALIZE_MODULE in one of the source files of your module:

Getting a ModuleContext

To retrieve the module specific ModuleContext object from anywhere in your module, use the GetModuleContext function:

#include <usModule.h>
void RetrieveModuleContext()
{
Module* module = context->GetModule();
std::cout << "Module name: " << module->GetName() << " [id: " << module->GetModuleId() << "]\n";
}

Please note that trying to use GetModuleContext without proper initialization code in the using shared library while either lead to compile or rumtime errors.

usModule.h
US_INITIALIZE_MODULE
#define US_INITIALIZE_MODULE
Creates initialization code for a module.
Definition: usModuleInitialization.h:57
ModuleContext
Definition: usModuleContext.h:91
us::GetModuleContext
static ModuleContext * GetModuleContext()
Returns the module context of the calling module.
Definition: usGetModuleContext.h:50
US_USE_NAMESPACE
#define US_USE_NAMESPACE
Definition: usGlobalConfig.h:75
usModuleInitialization.h
ModuleContext::GetModule
Module * GetModule() const
usModuleContext.h
usGetModuleContext.h