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 stuff is needed only for windows DLL initialization.
32 // It is borrowed form 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 #if _MSC_VER > 1000
37 #pragma once
38 #endif // _MSC_VER > 1000
39 
40 #ifndef WIN32_LEAN_AND_MEAN
41 #define WIN32_LEAN_AND_MEAN // Do not include rarely used parts of windows header
42 #endif
43 
44 #ifndef ML_SUPPRESS_LIBRARY_INIT_MACROS_WINDOWS_H_INCLUDE
45  #include <ThirdPartyWarningsDisable.h>
46  #include <windows.h>
47  #include <ThirdPartyWarningsRestore.h>
48 #endif
49 
50 #endif // !defined(AFX_STDAFX_H__17533C48_9DB6_4FA8_AEC3_550E71AF7183__INCLUDED_)
51 
52 #endif // _MSC_VER
53 
54 
55 
56 // Include normal ML runtime system and error handling stuff.
57 #include "mlVersion.h"
58 #include "mlErrorOutput.h"
59 #include "mlErrorOutputInfos.h"
60 #include "mlRuntime.h"
61 
62 //--------------------------------------------------------------
77 //--------------------------------------------------------------
78 #ifdef WIN32
79 
80 // WIN32 case :
81 // Macro to implement win32 DLL initialization function.
82 // It is called after automatic instance initialization.
83 #define ML_UTILS_INIT_LIBRARY_EXT_3(initMethod, NAMESP, LIB_TARGET) \
84  extern "C" { \
85  extern int MLIsCPPAPILinkCompatible(int majorVersion, \
86  int majorCAPIVersion, \
87  int verCPPAPI); \
88  } \
89  BOOL APIENTRY DllMain( HANDLE /*hModule*/, \
90  DWORD ul_reason_for_call, \
91  LPVOID /*lpReserved*/ \
92  ) \
93  { \
94  \
95  switch (ul_reason_for_call){ \
96  case DLL_PROCESS_ATTACH: \
97  /* Set the name of the init method as library name. */ \
98  ML_UTILS_NAMESPACE::Runtime::setRecentlyLoadedDllName(#LIB_TARGET); \
99  \
100  /* Check whether the ML C++ API has a valid version number so */ \
101  /* that the library can be linked safely. */ \
102  MLCheckCPPAPILinkCompatibility(ML_MAJOR_VERSION, \
103  ML_MAJOR_CAPI_VERSION, \
104  ML_CPPAPI_VERSION, \
105  ML_CAPI_REVISION, \
106  ML_VERSION_STRING, \
107  #initMethod); \
108  \
109  /* Call the initialization method of the library even on load */ \
110  /* failures. That function will usually register runtime types. */ \
111  /* It turned out that suppressing library init will cause more */ \
112  /* problems than trying it anyway. */ \
113  NAMESP::initMethod(); \
114  break; \
115  \
116  case DLL_THREAD_ATTACH: \
117  break; \
118  \
119  case DLL_THREAD_DETACH: \
120  break; \
121  \
122  case DLL_PROCESS_DETACH: \
123  MLUtilsDestroy(); \
124  break; \
125  } \
126  return TRUE; \
127  }
128 
129 #else
130 
131 // Unix case:
132 // Macro to implement Unix shared object initialization function.
133 // It is called after automatic instance initialization.
134 #define ML_UTILS_INIT_LIBRARY_EXT_3(initMethod, NAMESP, LIB_TARGET) \
135  extern "C" { \
136  extern int MLInitializeUtils(); \
137  extern int MLIsCPPAPILinkCompatible(int majorVersion, \
138  int majorCAPIVersion, \
139  int verCPPAPI); \
140  } \
141  void initMethod##initDll() __attribute__ ((constructor)); \
142  void initMethod##initDll() \
143  { \
144  \
145  static bool initDone = false; \
146  if (!initDone) { \
147  /* On Unix we must guarantee that ML is initialized, because */ \
148  /* otherwise global variables or singletons are still not */ \
149  /* valid for init class calls. We need to do that here */ \
150  /* because order of variable initialization and dll */ \
151  /* initialization differs on windows and Unix. */ \
152  MLInitializeUtils(); \
153  \
154  /* Set the name of the init method as library name. */ \
155  ML_UTILS_NAMESPACE::Runtime::setRecentlyLoadedDllName(#LIB_TARGET); \
156  \
157  /* Check whether the ML C++ API has a valid version number so */ \
158  /* that the library can be linked safely. */ \
159  MLCheckCPPAPILinkCompatibility(ML_MAJOR_VERSION, \
160  ML_MAJOR_CAPI_VERSION, \
161  ML_CPPAPI_VERSION, \
162  ML_CAPI_REVISION, \
163  ML_VERSION_STRING, \
164  #initMethod); \
165  \
166  /* Call the initialization method of the library even on load */ \
167  /* failures. That function will usually register runtime types. */ \
168  /* It turned out that suppressing library init will cause more */ \
169  /* problems than trying it anyway. */ \
170  NAMESP::initMethod(); \
171  initDone = true; \
172  } \
173  }
174 
175 #endif
176 
177 
178 //--------------------------------------------------------------
184 //--------------------------------------------------------------
185 #define ML_UTILS_INIT_LIBRARY(initMethod) \
186  _ML_UTILS_INIT_LIBRARY_EXT_HELPER(initMethod, ML_UTILS_NAMESPACE, MEVIS_TARGET)
187 
188 //--------------------------------------------------------------
193 //--------------------------------------------------------------
194 #define ML_UTILS_INIT_LIBRARY_EXT(initMethod, NAME_SP) \
195  _ML_UTILS_INIT_LIBRARY_EXT_HELPER(initMethod, NAME_SP, MEVIS_TARGET)
196 
197 //--------------------------------------------------------------
204 //--------------------------------------------------------------
205 #define _ML_UTILS_INIT_LIBRARY_EXT_HELPER(initMethod, NAME_SP, LIB_TARGET) \
206  ML_UTILS_INIT_LIBRARY_EXT_2(initMethod, NAME_SP, LIB_TARGET)
207 
208 //--------------------------------------------------------------
211 //--------------------------------------------------------------
212 #ifndef ML_STATIC_BUILD
213 #define ML_UTILS_INIT_LIBRARY_EXT_2(initMethod, NAME_SP, LIB_TARGET) \
214  ML_UTILS_INIT_LIBRARY_EXT_3(initMethod, NAME_SP, LIB_TARGET)
215 #else
216 #define ML_UTILS_INIT_LIBRARY_EXT_2(initMethod, NAME_SP, LIB_TARGET)
217 #endif
218 
219 #endif // __mlUtilsLibraryInitMacros_H