ML Reference
MeVis/Foundation/Sources/MLUtilities/mlUtilsLibraryInitMacros.h
Go to the documentation of this file.
00001 // **InsertLicense** code
00002 //-------------------------------------------------------------------------
00005 
00009 // This file defines ML_UTILS_INIT_LIBRARY(initMethod) macros.
00010 // See also mlRuntimeType.h, mlRuntime.h and mlRuntimeDict.h.
00011 //
00012 // Remark: These macros are inserted in the header- and source files
00013 // of the image processing modules (e.g., class Module)
00014 // or in in the library initialization.
00015 //
00016 //-------------------------------------------------------------------------
00017 #ifndef __mlUtilsLibraryInitMacros_H
00018 #define __mlUtilsLibraryInitMacros_H
00019 
00020 #ifdef _MSC_VER
00021 
00023 #pragma warning(disable : 4786 )
00024 
00025 // This stuff is needed only for windows DLL initialization.
00026 // It is borrowed form VC++ automatic DLL project creation.
00027 #if !defined(AFX_STDAFX_H__17533C48_9DB6_4FA8_AEC3_550E71AF7183__INCLUDED_)
00028 #define AFX_STDAFX_H__17533C48_9DB6_4FA8_AEC3_550E71AF7183__INCLUDED_
00029 
00030 #if _MSC_VER > 1000
00031 #pragma once
00032 #endif // _MSC_VER > 1000
00033 
00034 #ifndef WIN32_LEAN_AND_MEAN
00035 #define WIN32_LEAN_AND_MEAN   // Do not include rarely used parts of windows header
00036 #endif
00037 
00038 #ifndef ML_SUPPRESS_LIBRARY_INIT_MACROS_WINDOWS_H_INCLUDE
00039   #include "mlSystemWarningsDisable.h"
00040   #include <windows.h>
00041   #include "mlSystemWarningsRestore.h"
00042 #endif
00043 
00044 #endif // !defined(AFX_STDAFX_H__17533C48_9DB6_4FA8_AEC3_550E71AF7183__INCLUDED_)
00045 
00046 #endif // _MSC_VER
00047 
00048 
00049 
00050 // Include normal ML runtime system and error handling stuff.
00051 #ifndef __mlVersion_H
00052 #include "mlVersion.h"
00053 #endif
00054 #ifndef __mlErrorOutput_H
00055 #include "mlErrorOutput.h"
00056 #endif
00057 #ifndef __mlErrorOutputInfos_H
00058 #include "mlErrorOutputInfos.h"
00059 #endif
00060 #ifndef __mlRuntime_H
00061 #include "mlRuntime.h"
00062 #endif
00063 
00064 //--------------------------------------------------------------
00079 //--------------------------------------------------------------
00080 #ifdef WIN32
00081 
00082 // WIN32 case :
00083 // Macro to implement win32 DLL initialization function.
00084 // It is called after automatic instance initialization.
00085 #define ML_UTILS_INIT_LIBRARY_EXT_2(initMethod, NAMESP, LIB_TARGET)       \
00086   extern "C" {                                                            \
00087     extern int MLIsCPPAPILinkCompatible(int majorVersion,                 \
00088                                         int majorCAPIVersion,             \
00089                                         int verCPPAPI);                   \
00090   }                                                                       \
00091   BOOL APIENTRY DllMain( HANDLE /*hModule*/,                              \
00092                          DWORD  ul_reason_for_call,                       \
00093                          LPVOID /*lpReserved*/                            \
00094                        )                                                  \
00095   {                                                                       \
00096     ML_TRACE_IN( "DllMain" );                                             \
00097                                                                           \
00098     switch (ul_reason_for_call){                                          \
00099     case DLL_PROCESS_ATTACH:                                              \
00100       /* Set the name of the init method as library name.   */            \
00101       ML_UTILS_NAMESPACE::Runtime::setRecentlyLoadedDllName(#LIB_TARGET); \
00102                                                                           \
00103       /* Check whether the ML C++ API has a valid version number so */    \
00104       /* that the library can be linked safely.                     */    \
00105       MLCheckCPPAPILinkCompatibility(ML_MAJOR_VERSION,                    \
00106                                      ML_MAJOR_CAPI_VERSION,               \
00107                                      ML_CPPAPI_VERSION,                   \
00108                                      ML_CAPI_REVISION,                    \
00109                                      ML_VERSION_STRING,                   \
00110                                      #initMethod);                        \
00111                                                                           \
00112       /* Call the initialization method of the library even on load   */  \
00113       /* failures. That function will usually register runtime types. */  \
00114       /* It turned out that suppressing library init will cause more  */  \
00115       /* problems than trying it anyway.                              */  \
00116       NAMESP::initMethod();                                               \
00117     break;                                                                \
00118                                                                           \
00119     case DLL_THREAD_ATTACH:                                               \
00120     break;                                                                \
00121                                                                           \
00122     case DLL_THREAD_DETACH:                                               \
00123     break;                                                                \
00124                                                                           \
00125     case DLL_PROCESS_DETACH:                                              \
00126       MLUtilsDestroy();                                                   \
00127     break;                                                                \
00128     }                                                                     \
00129     return TRUE;                                                          \
00130   }
00131 
00132 #else
00133 
00134 // Unix case:
00135 // Macro to implement Unix shared object initialization function.
00136 // It is called after automatic instance initialization.
00137 #define ML_UTILS_INIT_LIBRARY_EXT_2(initMethod, NAMESP, LIB_TARGET)       \
00138   extern "C" {                                                            \
00139     extern int MLInitializeUtils();                                       \
00140     extern int MLIsCPPAPILinkCompatible(int majorVersion,                 \
00141                                         int majorCAPIVersion,             \
00142                                         int verCPPAPI);                   \
00143   }                                                                       \
00144   void initMethod##initDll() __attribute__ ((constructor));               \
00145   void initMethod##initDll()                                              \
00146   {                                                                       \
00147     ML_TRACE_IN( "initMethod##initDll()" );                               \
00148                                                                           \
00149     static bool initDone = false;                                         \
00150     if (!initDone) {                                                      \
00151       /* On Unix we must guarantee that ML is initialized, because  */    \
00152       /* otherwise global variables or singletons are still not     */    \
00153       /* valid for init class calls. We need to do that here        */    \
00154       /* because order of variable initialization and dll           */    \
00155       /* initialization differs on windows and Unix.                */    \
00156       MLInitializeUtils();                                                \
00157                                                                           \
00158       /* Set the name of the init method as library name.   */            \
00159       ML_UTILS_NAMESPACE::Runtime::setRecentlyLoadedDllName(#LIB_TARGET); \
00160                                                                           \
00161       /* Check whether the ML C++ API has a valid version number so */    \
00162       /* that the library can be linked safely.                     */    \
00163       MLCheckCPPAPILinkCompatibility(ML_MAJOR_VERSION,                    \
00164                                      ML_MAJOR_CAPI_VERSION,               \
00165                                      ML_CPPAPI_VERSION,                   \
00166                                      ML_CAPI_REVISION,                    \
00167                                      ML_VERSION_STRING,                   \
00168                                      #initMethod);                        \
00169                                                                           \
00170       /* Call the initialization method of the library even on load   */  \
00171       /* failures. That function will usually register runtime types. */  \
00172       /* It turned out that suppressing library init will cause more  */  \
00173       /* problems than trying it anyway.                              */  \
00174       NAMESP::initMethod();                                               \
00175       initDone = true;                                                    \
00176     }                                                                     \
00177   }
00178 
00179 #endif
00180 
00181 
00182 //--------------------------------------------------------------
00188 //--------------------------------------------------------------
00189 #define ML_UTILS_INIT_LIBRARY(initMethod) \
00190   _ML_UTILS_INIT_LIBRARY_EXT_HELPER(initMethod, ML_UTILS_NAMESPACE, MEVIS_TARGET)
00191 
00192 //--------------------------------------------------------------
00197 //--------------------------------------------------------------
00198 #define ML_UTILS_INIT_LIBRARY_EXT(initMethod, NAME_SP) \
00199   _ML_UTILS_INIT_LIBRARY_EXT_HELPER(initMethod, NAME_SP, MEVIS_TARGET)
00200 
00201 //--------------------------------------------------------------
00208 //--------------------------------------------------------------
00209 #define _ML_UTILS_INIT_LIBRARY_EXT_HELPER(initMethod, NAME_SP, LIB_TARGET) \
00210   ML_UTILS_INIT_LIBRARY_EXT_2(initMethod, NAME_SP, LIB_TARGET)
00211 
00212 
00213 #endif  // __mlUtilsLibraryInitMacros_H
00214 
00215