DynamicLibraryManager.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #ifndef CPPUNIT_PLUGIN_DYNAMICLIBRARYMANAGER_H
  2. #define CPPUNIT_PLUGIN_DYNAMICLIBRARYMANAGER_H
  3. #include <cppunit/Portability.h>
  4. #include <string>
  5. #if !defined(CPPUNIT_NO_TESTPLUGIN)
  6. CPPUNIT_NS_BEGIN
  7. /*! \brief Manages dynamic libraries.
  8. *
  9. * The Dynamic Library Manager provides a platform independent way to work with
  10. * dynamic library. It load a specific dynamic library, and can returns specific
  11. * symbol exported by the dynamic library.
  12. *
  13. * If an error occurs, a DynamicLibraryManagerException is thrown.
  14. *
  15. * \internal Implementation of the OS independent methods is in
  16. * DynamicLibraryManager.cpp.
  17. *
  18. * \internal Porting to a new platform:
  19. * - Adds platform detection in config/SelectDllLoader.h. Should define a specific
  20. * macro for that platform of the form: CPPUNIT_HAVE_XYZ_DLL_LOADER, where
  21. * XYZ is the platform.
  22. * - Makes a copy of UnixDynamicLibraryManager.cpp and named it after the platform.
  23. * - Updated the 'guard' in your file (CPPUNIT_HAVE_XYZ_DLL_LOADER) so that it is
  24. * only processed if the matching platform has been detected.
  25. * - Change the implementation of methods doLoadLibrary(), doReleaseLibrary(),
  26. * doFindSymbol() in your copy. Those methods usually maps directly to OS calls.
  27. * - Adds the file to the project.
  28. */
  29. class DynamicLibraryManager
  30. {
  31. public:
  32. typedef void *Symbol;
  33. typedef void *LibraryHandle;
  34. /*! \brief Loads the specified library.
  35. * \param libraryFileName Name of the library to load.
  36. * \exception DynamicLibraryManagerException if a failure occurs while loading
  37. * the library (fail to found or load the library).
  38. */
  39. DynamicLibraryManager( const std::string &libraryFileName );
  40. /// Releases the loaded library..
  41. ~DynamicLibraryManager();
  42. /*! \brief Returns a pointer on the specified symbol exported by the library.
  43. * \param symbol Name of the symbol exported by the library.
  44. * \return Pointer on the symbol. Should be casted to the actual type. Never \c NULL.
  45. * \exception DynamicLibraryManagerException if the symbol is not found.
  46. */
  47. Symbol findSymbol( const std::string &symbol );
  48. private:
  49. /*! Loads the specified library.
  50. * \param libraryName Name of the library to load.
  51. * \exception DynamicLibraryManagerException if a failure occurs while loading
  52. * the library (fail to found or load the library).
  53. */
  54. void loadLibrary( const std::string &libraryName );
  55. /*! Releases the loaded library.
  56. *
  57. * \warning Must NOT throw any exceptions (called from destructor).
  58. */
  59. void releaseLibrary();
  60. /*! Loads the specified library.
  61. *
  62. * May throw any exceptions (indicates failure).
  63. * \param libraryName Name of the library to load.
  64. * \return Handle of the loaded library. \c NULL indicates failure.
  65. */
  66. LibraryHandle doLoadLibrary( const std::string &libraryName );
  67. /*! Releases the loaded library.
  68. *
  69. * The handle of the library to free is in \c m_libraryHandle. It is never
  70. * \c NULL.
  71. * \warning Must NOT throw any exceptions (called from destructor).
  72. */
  73. void doReleaseLibrary();
  74. /*! Returns a pointer on the specified symbol exported by the library.
  75. *
  76. * May throw any exceptions (indicates failure).
  77. * \param symbol Name of the symbol exported by the library.
  78. * \return Pointer on the symbol. \c NULL indicates failure.
  79. */
  80. Symbol doFindSymbol( const std::string &symbol );
  81. /*! Returns detailed information about doLoadLibrary() failure.
  82. *
  83. * Called just after a failed call to doLoadLibrary() to get extra
  84. * error information.
  85. *
  86. * \return Detailed information about the failure of the call to
  87. * doLoadLibrary() that just failed.
  88. */
  89. std::string getLastErrorDetail() const;
  90. /// Prevents the use of the copy constructor.
  91. DynamicLibraryManager( const DynamicLibraryManager &copy );
  92. /// Prevents the use of the copy operator.
  93. void operator =( const DynamicLibraryManager &copy );
  94. private:
  95. LibraryHandle m_libraryHandle;
  96. std::string m_libraryName;
  97. };
  98. CPPUNIT_NS_END
  99. #endif // !defined(CPPUNIT_NO_TESTPLUGIN)
  100. #endif // CPPUNIT_PLUGIN_DYNAMICLIBRARYMANAGER_H