]> git.saurik.com Git - wxWidgets.git/blob - include/wx/dynload.h
Use theme and HTML Help headers from Wine (with permission)
[wxWidgets.git] / include / wx / dynload.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 // RCS-ID: $Id$
9 // Copyright: (c) 2001 Ron Lee <ron@debian.org>
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
12
13 #ifndef _WX_DYNAMICLOADER_H__
14 #define _WX_DYNAMICLOADER_H__
15
16 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
17 #pragma interface "dynload.h"
18 #endif
19
20 // ----------------------------------------------------------------------------
21 // headers
22 // ----------------------------------------------------------------------------
23
24 #include "wx/defs.h"
25
26 #if wxUSE_DYNAMIC_LOADER
27
28 #include "wx/dynlib.h"
29 #include "wx/hashmap.h"
30 #include "wx/module.h"
31
32 class WXDLLIMPEXP_BASE wxPluginLibrary;
33
34
35 WX_DECLARE_STRING_HASH_MAP_WITH_DECL(wxPluginLibrary *, wxDLManifest,
36 class WXDLLIMPEXP_BASE);
37 typedef wxDLManifest wxDLImports;
38
39 // ---------------------------------------------------------------------------
40 // wxPluginLibrary
41 // ---------------------------------------------------------------------------
42
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
45 // methods here.
46 //
47 // Unless you know exacty why you need to, you probably shouldn't
48 // instantiate this class directly anyway, use wxPluginManager
49 // instead.
50
51 class WXDLLIMPEXP_BASE wxPluginLibrary : public wxDynamicLibrary
52 {
53 public:
54
55 static wxDLImports* ms_classes; // Static hash of all imported classes.
56
57 wxPluginLibrary( const wxString &libname, int flags = wxDL_DEFAULT );
58 ~wxPluginLibrary();
59
60 wxPluginLibrary *RefLib();
61 bool UnrefLib();
62
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.
67
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.
74
75 void RefObj() { ++m_objcount; }
76 void UnrefObj()
77 {
78 wxASSERT_MSG( m_objcount > 0, _T("Too many objects deleted??") );
79 --m_objcount;
80 }
81
82 // Override/hide some base class methods
83
84 bool IsLoaded() const { return m_linkcount > 0; }
85 void Unload() { UnrefLib(); }
86
87 private:
88
89 wxClassInfo *m_before; // sm_first before loading this lib
90 wxClassInfo *m_after; // ..and after.
91
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.
95
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.
100
101 DECLARE_NO_COPY_CLASS(wxPluginLibrary)
102 };
103
104
105 class WXDLLIMPEXP_BASE wxPluginManager
106 {
107 public:
108
109 // Static accessors.
110
111 static wxPluginLibrary *LoadLibrary( const wxString &libname,
112 int flags = wxDL_DEFAULT );
113 static bool UnloadLibrary(const wxString &libname);
114
115 // This is used by wxDllLoader. It's wrapped in the compatibility
116 // macro because it's of arguable use outside of that.
117
118 #if WXWIN_COMPATIBILITY_2_2
119 wxDEPRECATED( static wxPluginLibrary *GetObjectFromHandle(wxDllType handle) );
120 #endif
121
122 // Instance methods.
123
124 wxPluginManager() : m_entry(NULL) {}
125 wxPluginManager(const wxString &libname, int flags = wxDL_DEFAULT)
126 {
127 Load(libname, flags);
128 }
129 ~wxPluginManager() { if ( IsLoaded() ) Unload(); }
130
131 bool Load(const wxString &libname, int flags = wxDL_DEFAULT);
132 void Unload();
133
134 bool IsLoaded() const { return m_entry && m_entry->IsLoaded(); }
135 void *GetSymbol(const wxString &symbol, bool *success = 0)
136 {
137 return m_entry->GetSymbol( symbol, success );
138 }
139
140 static void CreateManifest() { ms_manifest = new wxDLManifest(wxKEY_STRING); }
141 static void ClearManifest() { delete ms_manifest; ms_manifest = NULL; }
142
143 private:
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)
147 {
148 const wxDLManifest::iterator i = ms_manifest->find(name);
149
150 return i == ms_manifest->end() ? NULL : i->second;
151 }
152
153 static wxDLManifest* ms_manifest; // Static hash of loaded libs.
154 wxPluginLibrary* m_entry; // Cache our entry in the manifest.
155
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)
159 };
160
161
162 #endif // wxUSE_DYNAMIC_LOADER
163 #endif // _WX_DYNAMICLOADER_H__
164