1 /////////////////////////////////////////////////////////////////////////////
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)
9 // Copyright: (c) 2001 Ron Lee <ron@debian.org>
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
13 #ifndef _WX_DYNAMICLOADER_H__
14 #define _WX_DYNAMICLOADER_H__
16 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
17 #pragma interface "dynload.h"
20 // ----------------------------------------------------------------------------
22 // ----------------------------------------------------------------------------
26 #if wxUSE_DYNAMIC_LOADER
28 #include "wx/dynlib.h"
29 #include "wx/hashmap.h"
30 #include "wx/module.h"
32 class WXDLLIMPEXP_BASE wxPluginLibrary
;
35 WX_DECLARE_STRING_HASH_MAP_WITH_DECL(wxPluginLibrary
*, wxDLManifest
,
36 class WXDLLIMPEXP_BASE
);
37 typedef wxDLManifest wxDLImports
;
39 // ---------------------------------------------------------------------------
41 // ---------------------------------------------------------------------------
43 // NOTE: Do not attempt to use a base class pointer to this class.
44 // wxDL is not virtual and we deliberately hide some of it's
47 // Unless you know exacty why you need to, you probably shouldn't
48 // instantiate this class directly anyway, use wxPluginManager
51 class WXDLLIMPEXP_BASE wxPluginLibrary
: public wxDynamicLibrary
55 static wxDLImports
* ms_classes
; // Static hash of all imported classes.
57 wxPluginLibrary( const wxString
&libname
, int flags
= wxDL_DEFAULT
);
60 wxPluginLibrary
*RefLib();
63 // These two are called by the PluginSentinel on (PLUGGABLE) object
64 // creation/destruction. There is usually no reason for the user to
65 // call them directly. We have to separate this from the link count,
66 // since the two are not interchangeable.
68 // FIXME: for even better debugging PluginSentinel should register
69 // the name of the class created too, then we can state
70 // exactly which object was not destroyed which may be
71 // difficult to find otherwise. Also this code should
72 // probably only be active in DEBUG mode, but let's just
73 // get it right first.
75 void RefObj() { ++m_objcount
; }
78 wxASSERT_MSG( m_objcount
> 0, _T("Too many objects deleted??") );
82 // Override/hide some base class methods
84 bool IsLoaded() const { return m_linkcount
> 0; }
85 void Unload() { UnrefLib(); }
89 wxClassInfo
*m_before
; // sm_first before loading this lib
90 wxClassInfo
*m_after
; // ..and after.
92 size_t m_linkcount
; // Ref count of library link calls
93 size_t m_objcount
; // ..and (pluggable) object instantiations.
94 wxModuleList m_wxmodules
; // any wxModules that we initialised.
96 void UpdateClasses(); // Update ms_classes
97 void RestoreClasses(); // Removes this library from ms_classes
98 void RegisterModules(); // Init any wxModules in the lib.
99 void UnregisterModules(); // Cleanup any wxModules we installed.
101 DECLARE_NO_COPY_CLASS(wxPluginLibrary
)
105 class WXDLLIMPEXP_BASE wxPluginManager
111 static wxPluginLibrary
*LoadLibrary( const wxString
&libname
,
112 int flags
= wxDL_DEFAULT
);
113 static bool UnloadLibrary(const wxString
&libname
);
115 // This is used by wxDllLoader. It's wrapped in the compatibility
116 // macro because it's of arguable use outside of that.
118 #if WXWIN_COMPATIBILITY_2_2
119 static wxPluginLibrary
*GetObjectFromHandle(wxDllType handle
);
124 wxPluginManager() : m_entry(NULL
) {}
125 wxPluginManager(const wxString
&libname
, int flags
= wxDL_DEFAULT
)
127 Load(libname
, flags
);
129 ~wxPluginManager() { if ( IsLoaded() ) Unload(); }
131 bool Load(const wxString
&libname
, int flags
= wxDL_DEFAULT
);
134 bool IsLoaded() const { return m_entry
&& m_entry
->IsLoaded(); }
135 void *GetSymbol(const wxString
&symbol
, bool *success
= 0)
137 return m_entry
->GetSymbol( symbol
, success
);
140 static void CreateManifest() { ms_manifest
= new wxDLManifest(wxKEY_STRING
); }
141 static void ClearManifest() { delete ms_manifest
; ms_manifest
= NULL
; }
144 // return the pointer to the entry for the library with given name in
145 // ms_manifest or NULL if none
146 static wxPluginLibrary
*FindByName(const wxString
& name
)
148 const wxDLManifest::iterator i
= ms_manifest
->find(name
);
150 return i
== ms_manifest
->end() ? NULL
: i
->second
;
153 static wxDLManifest
* ms_manifest
; // Static hash of loaded libs.
154 wxPluginLibrary
* m_entry
; // Cache our entry in the manifest.
156 // We could allow this class to be copied if we really
157 // wanted to, but not without modification.
158 DECLARE_NO_COPY_CLASS(wxPluginManager
)
162 #endif // wxUSE_DYNAMIC_LOADER
163 #endif // _WX_DYNAMICLOADER_H__