dynload.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/dynload.h
  3. // Purpose: Dynamic loading framework
  4. // Author: Ron Lee, David Falkinder, Vadim Zeitlin and a cast of 1000's
  5. // (derived in part from dynlib.cpp (c) 1998 Guilhem Lavaux)
  6. // Modified by:
  7. // Created: 03/12/01
  8. // Copyright: (c) 2001 Ron Lee <ron@debian.org>
  9. // Licence: wxWindows licence
  10. /////////////////////////////////////////////////////////////////////////////
  11. #ifndef _WX_DYNAMICLOADER_H__
  12. #define _WX_DYNAMICLOADER_H__
  13. // ----------------------------------------------------------------------------
  14. // headers
  15. // ----------------------------------------------------------------------------
  16. #include "wx/defs.h"
  17. #if wxUSE_DYNAMIC_LOADER
  18. #include "wx/dynlib.h"
  19. #include "wx/hashmap.h"
  20. #include "wx/module.h"
  21. class WXDLLIMPEXP_FWD_BASE wxPluginLibrary;
  22. WX_DECLARE_STRING_HASH_MAP_WITH_DECL(wxPluginLibrary *, wxDLManifest,
  23. class WXDLLIMPEXP_BASE);
  24. typedef wxDLManifest wxDLImports;
  25. // ---------------------------------------------------------------------------
  26. // wxPluginLibrary
  27. // ---------------------------------------------------------------------------
  28. // NOTE: Do not attempt to use a base class pointer to this class.
  29. // wxDL is not virtual and we deliberately hide some of it's
  30. // methods here.
  31. //
  32. // Unless you know exacty why you need to, you probably shouldn't
  33. // instantiate this class directly anyway, use wxPluginManager
  34. // instead.
  35. class WXDLLIMPEXP_BASE wxPluginLibrary : public wxDynamicLibrary
  36. {
  37. public:
  38. static wxDLImports* ms_classes; // Static hash of all imported classes.
  39. wxPluginLibrary( const wxString &libname, int flags = wxDL_DEFAULT );
  40. ~wxPluginLibrary();
  41. wxPluginLibrary *RefLib();
  42. bool UnrefLib();
  43. // These two are called by the PluginSentinel on (PLUGGABLE) object
  44. // creation/destruction. There is usually no reason for the user to
  45. // call them directly. We have to separate this from the link count,
  46. // since the two are not interchangeable.
  47. // FIXME: for even better debugging PluginSentinel should register
  48. // the name of the class created too, then we can state
  49. // exactly which object was not destroyed which may be
  50. // difficult to find otherwise. Also this code should
  51. // probably only be active in DEBUG mode, but let's just
  52. // get it right first.
  53. void RefObj() { ++m_objcount; }
  54. void UnrefObj()
  55. {
  56. wxASSERT_MSG( m_objcount > 0, wxT("Too many objects deleted??") );
  57. --m_objcount;
  58. }
  59. // Override/hide some base class methods
  60. bool IsLoaded() const { return m_linkcount > 0; }
  61. void Unload() { UnrefLib(); }
  62. private:
  63. // These pointers may be NULL but if they are not, then m_ourLast follows
  64. // m_ourFirst in the linked list, i.e. can be found by calling GetNext() a
  65. // sufficient number of times.
  66. const wxClassInfo *m_ourFirst; // first class info in this plugin
  67. const wxClassInfo *m_ourLast; // ..and the last one
  68. size_t m_linkcount; // Ref count of library link calls
  69. size_t m_objcount; // ..and (pluggable) object instantiations.
  70. wxModuleList m_wxmodules; // any wxModules that we initialised.
  71. void UpdateClasses(); // Update ms_classes
  72. void RestoreClasses(); // Removes this library from ms_classes
  73. void RegisterModules(); // Init any wxModules in the lib.
  74. void UnregisterModules(); // Cleanup any wxModules we installed.
  75. wxDECLARE_NO_COPY_CLASS(wxPluginLibrary);
  76. };
  77. class WXDLLIMPEXP_BASE wxPluginManager
  78. {
  79. public:
  80. // Static accessors.
  81. static wxPluginLibrary *LoadLibrary( const wxString &libname,
  82. int flags = wxDL_DEFAULT );
  83. static bool UnloadLibrary(const wxString &libname);
  84. // Instance methods.
  85. wxPluginManager() : m_entry(NULL) {}
  86. wxPluginManager(const wxString &libname, int flags = wxDL_DEFAULT)
  87. {
  88. Load(libname, flags);
  89. }
  90. ~wxPluginManager() { if ( IsLoaded() ) Unload(); }
  91. bool Load(const wxString &libname, int flags = wxDL_DEFAULT);
  92. void Unload();
  93. bool IsLoaded() const { return m_entry && m_entry->IsLoaded(); }
  94. void *GetSymbol(const wxString &symbol, bool *success = 0)
  95. {
  96. return m_entry->GetSymbol( symbol, success );
  97. }
  98. static void CreateManifest() { ms_manifest = new wxDLManifest(wxKEY_STRING); }
  99. static void ClearManifest() { delete ms_manifest; ms_manifest = NULL; }
  100. private:
  101. // return the pointer to the entry for the library with given name in
  102. // ms_manifest or NULL if none
  103. static wxPluginLibrary *FindByName(const wxString& name)
  104. {
  105. const wxDLManifest::iterator i = ms_manifest->find(name);
  106. return i == ms_manifest->end() ? NULL : i->second;
  107. }
  108. static wxDLManifest* ms_manifest; // Static hash of loaded libs.
  109. wxPluginLibrary* m_entry; // Cache our entry in the manifest.
  110. // We could allow this class to be copied if we really
  111. // wanted to, but not without modification.
  112. wxDECLARE_NO_COPY_CLASS(wxPluginManager);
  113. };
  114. #endif // wxUSE_DYNAMIC_LOADER
  115. #endif // _WX_DYNAMICLOADER_H__