SharedLibrary.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. //
  2. // SharedLibrary.h
  3. //
  4. // $Id: //poco/1.4/Foundation/include/Poco/SharedLibrary.h#3 $
  5. //
  6. // Library: Foundation
  7. // Package: SharedLibrary
  8. // Module: SharedLibrary
  9. //
  10. // Definition of the SharedLibrary class.
  11. //
  12. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  13. // and Contributors.
  14. //
  15. // SPDX-License-Identifier: BSL-1.0
  16. //
  17. #ifndef Foundation_SharedLibrary_INCLUDED
  18. #define Foundation_SharedLibrary_INCLUDED
  19. #include "Poco/Foundation.h"
  20. #if defined(hpux) || defined(_hpux)
  21. #include "Poco/SharedLibrary_HPUX.h"
  22. #elif defined(POCO_VXWORKS)
  23. #include "Poco/SharedLibrary_VX.h"
  24. #elif defined(POCO_OS_FAMILY_UNIX)
  25. #include "Poco/SharedLibrary_UNIX.h"
  26. #elif defined(POCO_OS_FAMILY_WINDOWS) && defined(POCO_WIN32_UTF8)
  27. #include "Poco/SharedLibrary_WIN32U.h"
  28. #elif defined(POCO_OS_FAMILY_WINDOWS)
  29. #include "Poco/SharedLibrary_WIN32.h"
  30. #elif defined(POCO_OS_FAMILY_VMS)
  31. #include "Poco/SharedLibrary_VMS.h"
  32. #endif
  33. namespace Poco {
  34. class Foundation_API SharedLibrary: private SharedLibraryImpl
  35. /// The SharedLibrary class dynamically
  36. /// loads shared libraries at run-time.
  37. {
  38. public:
  39. enum Flags
  40. {
  41. SHLIB_GLOBAL = 1,
  42. /// On platforms that use dlopen(), use RTLD_GLOBAL. This is the default
  43. /// if no flags are given.
  44. ///
  45. /// This flag is ignored on platforms that do not use dlopen().
  46. SHLIB_LOCAL = 2
  47. /// On platforms that use dlopen(), use RTLD_LOCAL instead of RTLD_GLOBAL.
  48. ///
  49. /// Note that if this flag is specified, RTTI (including dynamic_cast and throw) will
  50. /// not work for types defined in the shared library with GCC and possibly other
  51. /// compilers as well. See http://gcc.gnu.org/faq.html#dso for more information.
  52. ///
  53. /// This flag is ignored on platforms that do not use dlopen().
  54. };
  55. SharedLibrary();
  56. /// Creates a SharedLibrary object.
  57. SharedLibrary(const std::string& path);
  58. /// Creates a SharedLibrary object and loads a library
  59. /// from the given path.
  60. SharedLibrary(const std::string& path, int flags);
  61. /// Creates a SharedLibrary object and loads a library
  62. /// from the given path, using the given flags.
  63. /// See the Flags enumeration for valid values.
  64. virtual ~SharedLibrary();
  65. /// Destroys the SharedLibrary. The actual library
  66. /// remains loaded.
  67. void load(const std::string& path);
  68. /// Loads a shared library from the given path.
  69. /// Throws a LibraryAlreadyLoadedException if
  70. /// a library has already been loaded.
  71. /// Throws a LibraryLoadException if the library
  72. /// cannot be loaded.
  73. void load(const std::string& path, int flags);
  74. /// Loads a shared library from the given path,
  75. /// using the given flags. See the Flags enumeration
  76. /// for valid values.
  77. /// Throws a LibraryAlreadyLoadedException if
  78. /// a library has already been loaded.
  79. /// Throws a LibraryLoadException if the library
  80. /// cannot be loaded.
  81. void unload();
  82. /// Unloads a shared library.
  83. bool isLoaded() const;
  84. /// Returns true iff a library has been loaded.
  85. bool hasSymbol(const std::string& name);
  86. /// Returns true iff the loaded library contains
  87. /// a symbol with the given name.
  88. void* getSymbol(const std::string& name);
  89. /// Returns the address of the symbol with
  90. /// the given name. For functions, this
  91. /// is the entry point of the function.
  92. /// Throws a NotFoundException if the symbol
  93. /// does not exist.
  94. const std::string& getPath() const;
  95. /// Returns the path of the library, as
  96. /// specified in a call to load() or the
  97. /// constructor.
  98. static std::string suffix();
  99. /// Returns the platform-specific filename suffix
  100. /// for shared libraries (including the period).
  101. /// In debug mode, the suffix also includes a
  102. /// "d" to specify the debug version of a library.
  103. private:
  104. SharedLibrary(const SharedLibrary&);
  105. SharedLibrary& operator = (const SharedLibrary&);
  106. };
  107. } // namespace Poco
  108. #endif // Foundation_SharedLibrary_INCLUDED