SharedLibrary.h 3.8 KB

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