DynamicLoader.hxx.in 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*============================================================================
  2. KWSys - Kitware System Library
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #ifndef @KWSYS_NAMESPACE@_DynamicLoader_hxx
  11. #define @KWSYS_NAMESPACE@_DynamicLoader_hxx
  12. #include <@KWSYS_NAMESPACE@/Configure.h>
  13. #include <@KWSYS_NAMESPACE@/stl/string>
  14. #if defined(__hpux)
  15. #include <dl.h>
  16. #elif defined(_WIN32) && !defined(__CYGWIN__)
  17. #include <windows.h>
  18. #elif defined(__APPLE__)
  19. #include <AvailabilityMacros.h>
  20. #if MAC_OS_X_VERSION_MAX_ALLOWED < 1030
  21. #include <mach-o/dyld.h>
  22. #endif
  23. #elif defined(__BEOS__)
  24. #include <be/kernel/image.h>
  25. #endif
  26. /* Define these macros temporarily to keep the code readable. */
  27. #if !defined (KWSYS_NAMESPACE) && !@KWSYS_NAMESPACE@_NAME_IS_KWSYS
  28. # define kwsys_stl @KWSYS_NAMESPACE@_stl
  29. #endif
  30. namespace @KWSYS_NAMESPACE@
  31. {
  32. /** \class DynamicLoader
  33. * \brief Portable loading of dynamic libraries or dll's.
  34. *
  35. * DynamicLoader provides a portable interface to loading dynamic
  36. * libraries or dll's into a process.
  37. *
  38. * Directory currently works with Windows, Apple, HP-UX and Unix (POSIX)
  39. * operating systems
  40. *
  41. * \warning dlopen on *nix system works the following way:
  42. * If filename contains a slash ("/"), then it is interpreted as a (relative
  43. * or absolute) pathname. Otherwise, the dynamic linker searches for the
  44. * library as follows : see ld.so(8) for further details):
  45. * Whereas this distinction does not exist on Win32. Therefore ideally you
  46. * should be doing full path to garantee to have a consistent way of dealing
  47. * with dynamic loading of shared library.
  48. *
  49. * \warning the Cygwin implementation do not use the Win32 HMODULE. Put extra
  50. * condition so that we can include the correct declaration (POSIX)
  51. */
  52. class @KWSYS_NAMESPACE@_EXPORT DynamicLoader
  53. {
  54. public:
  55. // Ugly stuff for library handles
  56. // They are different on several different OS's
  57. #if defined(__hpux)
  58. typedef shl_t LibraryHandle;
  59. #elif defined(_WIN32) && !defined(__CYGWIN__)
  60. typedef HMODULE LibraryHandle;
  61. #elif defined(__APPLE__)
  62. #if MAC_OS_X_VERSION_MAX_ALLOWED < 1030
  63. typedef NSModule LibraryHandle;
  64. #else
  65. typedef void* LibraryHandle;
  66. #endif
  67. #elif defined(__BEOS__)
  68. typedef image_id LibraryHandle;
  69. #else // POSIX
  70. typedef void* LibraryHandle;
  71. #endif
  72. // Return type from DynamicLoader::GetSymbolAddress.
  73. typedef void (*SymbolPointer)();
  74. /** Load a dynamic library into the current process.
  75. * The returned LibraryHandle can be used to access the symbols in the
  76. * library. */
  77. static LibraryHandle OpenLibrary(const kwsys_stl::string&);
  78. /** Attempt to detach a dynamic library from the
  79. * process. A value of true is returned if it is sucessful. */
  80. static int CloseLibrary(LibraryHandle);
  81. /** Find the address of the symbol in the given library. */
  82. static SymbolPointer GetSymbolAddress(LibraryHandle, const kwsys_stl::string&);
  83. /** Return the default module prefix for the current platform. */
  84. static const char* LibPrefix() { return "@KWSYS_DynamicLoader_PREFIX@"; }
  85. /** Return the default module suffix for the current platform. */
  86. static const char* LibExtension() { return "@KWSYS_DynamicLoader_SUFFIX@"; }
  87. /** Return the last error produced from a calls made on this class. */
  88. static const char* LastError();
  89. }; // End Class: DynamicLoader
  90. } // namespace @KWSYS_NAMESPACE@
  91. /* Undefine temporary macros. */
  92. #if !defined (KWSYS_NAMESPACE) && !@KWSYS_NAMESPACE@_NAME_IS_KWSYS
  93. # undef kwsys_stl
  94. #endif
  95. #endif