DynamicLoader.hxx.in 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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.hxx>
  13. #include <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. namespace @KWSYS_NAMESPACE@
  27. {
  28. /** \class DynamicLoader
  29. * \brief Portable loading of dynamic libraries or dll's.
  30. *
  31. * DynamicLoader provides a portable interface to loading dynamic
  32. * libraries or dll's into a process.
  33. *
  34. * Directory currently works with Windows, Apple, HP-UX and Unix (POSIX)
  35. * operating systems
  36. *
  37. * \warning dlopen on *nix system works the following way:
  38. * If filename contains a slash ("/"), then it is interpreted as a (relative
  39. * or absolute) pathname. Otherwise, the dynamic linker searches for the
  40. * library as follows : see ld.so(8) for further details):
  41. * Whereas this distinction does not exist on Win32. Therefore ideally you
  42. * should be doing full path to garantee to have a consistent way of dealing
  43. * with dynamic loading of shared library.
  44. *
  45. * \warning the Cygwin implementation do not use the Win32 HMODULE. Put extra
  46. * condition so that we can include the correct declaration (POSIX)
  47. */
  48. class @KWSYS_NAMESPACE@_EXPORT DynamicLoader
  49. {
  50. public:
  51. // Ugly stuff for library handles
  52. // They are different on several different OS's
  53. #if defined(__hpux)
  54. typedef shl_t LibraryHandle;
  55. #elif defined(_WIN32) && !defined(__CYGWIN__)
  56. typedef HMODULE LibraryHandle;
  57. #elif defined(__APPLE__)
  58. #if MAC_OS_X_VERSION_MAX_ALLOWED < 1030
  59. typedef NSModule LibraryHandle;
  60. #else
  61. typedef void* LibraryHandle;
  62. #endif
  63. #elif defined(__BEOS__)
  64. typedef image_id LibraryHandle;
  65. #else // POSIX
  66. typedef void* LibraryHandle;
  67. #endif
  68. // Return type from DynamicLoader::GetSymbolAddress.
  69. typedef void (*SymbolPointer)();
  70. /** Load a dynamic library into the current process.
  71. * The returned LibraryHandle can be used to access the symbols in the
  72. * library. */
  73. static LibraryHandle OpenLibrary(const std::string&);
  74. /** Attempt to detach a dynamic library from the
  75. * process. A value of true is returned if it is sucessful. */
  76. static int CloseLibrary(LibraryHandle);
  77. /** Find the address of the symbol in the given library. */
  78. static SymbolPointer GetSymbolAddress(LibraryHandle, const std::string&);
  79. /** Return the default module prefix for the current platform. */
  80. static const char* LibPrefix() { return "@KWSYS_DynamicLoader_PREFIX@"; }
  81. /** Return the default module suffix for the current platform. */
  82. static const char* LibExtension() { return "@KWSYS_DynamicLoader_SUFFIX@"; }
  83. /** Return the last error produced from a calls made on this class. */
  84. static const char* LastError();
  85. }; // End Class: DynamicLoader
  86. } // namespace @KWSYS_NAMESPACE@
  87. #endif