MeVisLab Toolbox Reference
mlUtilsLibraryInitMacros.h
Go to the documentation of this file.
1/*************************************************************************************
2**
3** Copyright 2007, MeVis Medical Solutions AG
4**
5** The user may use this file in accordance with the license agreement provided with
6** the Software or, alternatively, in accordance with the terms contained in a
7** written agreement between the user and MeVis Medical Solutions AG.
8**
9** For further information use the contact form at https://www.mevislab.de/contact
10**
11**************************************************************************************/
12
13#ifndef ML_UTILS_LIBRARY_INIT_MACROS_H
14#define ML_UTILS_LIBRARY_INIT_MACROS_H
15
18
19// This file defines ML_UTILS_INIT_LIBRARY(initMethod) macros.
20// See also mlRuntimeType.h, mlRuntime.h, and mlRuntimeDict.h.
21//
22// Remark: These macros are inserted in the header- and source files
23// of the image processing modules (e.g., class Module)
24// or in in the library initialization.
25
26#ifdef _MSC_VER
27
29#pragma warning(disable : 4786 )
30
31// This code is needed only for Windows DLL initialization.
32// It is borrowed from VC++ automatic DLL project creation.
33#if !defined(AFX_STDAFX_H__17533C48_9DB6_4FA8_AEC3_550E71AF7183__INCLUDED_)
34#define AFX_STDAFX_H__17533C48_9DB6_4FA8_AEC3_550E71AF7183__INCLUDED_
35
36#pragma once
37
38#ifndef WIN32_LEAN_AND_MEAN
39#define WIN32_LEAN_AND_MEAN // Do not include rarely used parts of Windows header
40#endif
41
42#ifndef ML_SUPPRESS_LIBRARY_INIT_MACROS_WINDOWS_H_INCLUDE
43 #include <ThirdPartyWarningsDisable.h>
44 #include <windows.h>
45 #include <ThirdPartyWarningsRestore.h>
46#endif
47
48#endif // !defined(AFX_STDAFX_H__17533C48_9DB6_4FA8_AEC3_550E71AF7183__INCLUDED_)
49
50#endif // _MSC_VER
51
52
53
54// Include typical ML runtime system and error handling code.
55#include "mlVersion.h"
56#include "mlErrorOutput.h"
57#include "mlErrorOutputInfos.h"
58#include "mlRuntime.h"
59
60//--------------------------------------------------------------
75//--------------------------------------------------------------
76#ifdef WIN32
77
78// WIN32 case:
79// Macro to implement win32 DLL initialization function.
80// It is called after automatic instance initialization.
81#define ML_UTILS_INIT_LIBRARY_EXT_3(initMethod, NAMESP, LIB_TARGET) \
82 extern "C" { \
83 extern int MLIsCPPAPILinkCompatible(int majorVersion, \
84 int majorCAPIVersion, \
85 int verCPPAPI); \
86 } \
87 BOOL APIENTRY DllMain( HANDLE /*hModule*/, \
88 DWORD ul_reason_for_call, \
89 LPVOID /*lpReserved*/ \
90 ) \
91 { \
92 \
93 switch (ul_reason_for_call){ \
94 case DLL_PROCESS_ATTACH: \
95 /* Set the name of the init method as library name. */ \
96 ML_UTILS_NAMESPACE::Runtime::setRecentlyLoadedDllName(#LIB_TARGET); \
97 \
98 /* Check whether the ML C++ API has a valid version number so */ \
99 /* that the library can be linked safely. */ \
100 MLCheckCPPAPILinkCompatibility(ML_MAJOR_VERSION, \
101 ML_MAJOR_CAPI_VERSION, \
102 ML_CPPAPI_VERSION, \
103 ML_CAPI_REVISION, \
104 ML_VERSION_STRING, \
105 #initMethod); \
106 \
107 /* Call the initialization method of the library even on load */ \
108 /* failures. That function will usually register runtime types. */ \
109 /* It turned out that suppressing library init will cause more */ \
110 /* problems than trying it anyway. */ \
111 NAMESP::initMethod(); \
112 break; \
113 \
114 case DLL_THREAD_ATTACH: \
115 break; \
116 \
117 case DLL_THREAD_DETACH: \
118 break; \
119 \
120 case DLL_PROCESS_DETACH: \
121 MLUtilsDestroy(); \
122 break; \
123 } \
124 return TRUE; \
125 }
126
127#else
128
129// Unix case:
130// Macro to implement Unix shared object initialization function.
131// It is called after automatic instance initialization.
132#define ML_UTILS_INIT_LIBRARY_EXT_3(initMethod, NAMESP, LIB_TARGET) \
133 extern "C" { \
134 extern int MLInitializeUtils(); \
135 extern int MLIsCPPAPILinkCompatible(int majorVersion, \
136 int majorCAPIVersion, \
137 int verCPPAPI); \
138 } \
139 void initMethod##initDll() __attribute__ ((constructor)); \
140 void initMethod##initDll() \
141 { \
142 \
143 static bool initDone = false; \
144 if (!initDone) { \
145 /* On Unix, we must guarantee that ML is initialized, because */ \
146 /* otherwise, global variables or singletons are still not */ \
147 /* valid for init class calls. We need to do that here */ \
148 /* because order of variable initialization and DLL */ \
149 /* initialization differs on Windows and Unix. */ \
150 MLInitializeUtils(); \
151 \
152 /* Set the name of the init method as library name. */ \
153 ML_UTILS_NAMESPACE::Runtime::setRecentlyLoadedDllName(#LIB_TARGET); \
154 \
155 /* Check whether the ML C++ API has a valid version number so */ \
156 /* that the library can be linked safely. */ \
157 MLCheckCPPAPILinkCompatibility(ML_MAJOR_VERSION, \
158 ML_MAJOR_CAPI_VERSION, \
159 ML_CPPAPI_VERSION, \
160 ML_CAPI_REVISION, \
161 ML_VERSION_STRING, \
162 #initMethod); \
163 \
164 /* Call the initialization method of the library even on load */ \
165 /* failures. That function will usually register runtime types. */ \
166 /* It turned out that suppressing library init will cause more */ \
167 /* problems than trying it anyway. */ \
168 NAMESP::initMethod(); \
169 initDone = true; \
170 } \
171 }
172
173#endif
174
175
176//--------------------------------------------------------------
182//--------------------------------------------------------------
183#define ML_UTILS_INIT_LIBRARY(initMethod) \
184 _ML_UTILS_INIT_LIBRARY_EXT_HELPER(initMethod, ML_UTILS_NAMESPACE, MEVIS_TARGET)
185
186//--------------------------------------------------------------
191//--------------------------------------------------------------
192#define ML_UTILS_INIT_LIBRARY_EXT(initMethod, NAME_SP) \
193 _ML_UTILS_INIT_LIBRARY_EXT_HELPER(initMethod, NAME_SP, MEVIS_TARGET)
194
195//--------------------------------------------------------------
202//--------------------------------------------------------------
203#define _ML_UTILS_INIT_LIBRARY_EXT_HELPER(initMethod, NAME_SP, LIB_TARGET) \
204 ML_UTILS_INIT_LIBRARY_EXT_2(initMethod, NAME_SP, LIB_TARGET)
205
206//--------------------------------------------------------------
209//--------------------------------------------------------------
210#ifndef ML_STATIC_BUILD
211#define ML_UTILS_INIT_LIBRARY_EXT_2(initMethod, NAME_SP, LIB_TARGET) \
212 ML_UTILS_INIT_LIBRARY_EXT_3(initMethod, NAME_SP, LIB_TARGET)
213#else
214#define ML_UTILS_INIT_LIBRARY_EXT_2(initMethod, NAME_SP, LIB_TARGET)
215#endif
216
217#endif // __mlUtilsLibraryInitMacros_H