]>
Commit | Line | Data |
---|---|---|
0b9ab0bd RL |
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> | |
65571936 | 10 | // Licence: wxWindows licence |
0b9ab0bd RL |
11 | ///////////////////////////////////////////////////////////////////////////// |
12 | ||
13 | #ifndef _WX_DYNAMICLOADER_H__ | |
14 | #define _WX_DYNAMICLOADER_H__ | |
15 | ||
0b9ab0bd RL |
16 | // ---------------------------------------------------------------------------- |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | #include "wx/defs.h" | |
21 | ||
22 | #if wxUSE_DYNAMIC_LOADER | |
23 | ||
1948bb32 | 24 | #include "wx/dynlib.h" |
2b5f62a0 | 25 | #include "wx/hashmap.h" |
0b9ab0bd RL |
26 | #include "wx/module.h" |
27 | ||
bddd7a8d | 28 | class WXDLLIMPEXP_BASE wxPluginLibrary; |
0b9ab0bd | 29 | |
0b9ab0bd | 30 | |
1948bb32 VS |
31 | WX_DECLARE_STRING_HASH_MAP_WITH_DECL(wxPluginLibrary *, wxDLManifest, |
32 | class WXDLLIMPEXP_BASE); | |
33 | typedef wxDLManifest wxDLImports; | |
0b9ab0bd RL |
34 | |
35 | // --------------------------------------------------------------------------- | |
4f89dbc4 | 36 | // wxPluginLibrary |
0b9ab0bd RL |
37 | // --------------------------------------------------------------------------- |
38 | ||
4f89dbc4 RL |
39 | // NOTE: Do not attempt to use a base class pointer to this class. |
40 | // wxDL is not virtual and we deliberately hide some of it's | |
41 | // methods here. | |
42 | // | |
43 | // Unless you know exacty why you need to, you probably shouldn't | |
44 | // instantiate this class directly anyway, use wxPluginManager | |
45 | // instead. | |
46 | ||
bddd7a8d | 47 | class WXDLLIMPEXP_BASE wxPluginLibrary : public wxDynamicLibrary |
0b9ab0bd RL |
48 | { |
49 | public: | |
50 | ||
dabd1377 | 51 | static wxDLImports* ms_classes; // Static hash of all imported classes. |
0b9ab0bd | 52 | |
23213f18 | 53 | wxPluginLibrary( const wxString &libname, int flags = wxDL_DEFAULT ); |
4f89dbc4 | 54 | ~wxPluginLibrary(); |
0b9ab0bd | 55 | |
d0fcccc4 | 56 | wxPluginLibrary *RefLib(); |
4f89dbc4 | 57 | bool UnrefLib(); |
7c1e2b44 RL |
58 | |
59 | // These two are called by the PluginSentinel on (PLUGGABLE) object | |
60 | // creation/destruction. There is usually no reason for the user to | |
61 | // call them directly. We have to separate this from the link count, | |
62 | // since the two are not interchangeable. | |
63 | ||
64 | // FIXME: for even better debugging PluginSentinel should register | |
65 | // the name of the class created too, then we can state | |
66 | // exactly which object was not destroyed which may be | |
67 | // difficult to find otherwise. Also this code should | |
68 | // probably only be active in DEBUG mode, but let's just | |
69 | // get it right first. | |
70 | ||
4f89dbc4 RL |
71 | void RefObj() { ++m_objcount; } |
72 | void UnrefObj() | |
7c1e2b44 RL |
73 | { |
74 | wxASSERT_MSG( m_objcount > 0, _T("Too many objects deleted??") ); | |
75 | --m_objcount; | |
76 | } | |
0b9ab0bd | 77 | |
4f89dbc4 | 78 | // Override/hide some base class methods |
0b9ab0bd | 79 | |
4f89dbc4 RL |
80 | bool IsLoaded() const { return m_linkcount > 0; } |
81 | void Unload() { UnrefLib(); } | |
0b9ab0bd RL |
82 | |
83 | private: | |
84 | ||
7c1e2b44 | 85 | wxClassInfo *m_before; // sm_first before loading this lib |
7c1e2b44 | 86 | wxClassInfo *m_after; // ..and after. |
0b9ab0bd | 87 | |
7c1e2b44 RL |
88 | size_t m_linkcount; // Ref count of library link calls |
89 | size_t m_objcount; // ..and (pluggable) object instantiations. | |
90 | wxModuleList m_wxmodules; // any wxModules that we initialised. | |
0b9ab0bd | 91 | |
2d4957f2 VS |
92 | void UpdateClasses(); // Update ms_classes |
93 | void RestoreClasses(); // Removes this library from ms_classes | |
7c1e2b44 RL |
94 | void RegisterModules(); // Init any wxModules in the lib. |
95 | void UnregisterModules(); // Cleanup any wxModules we installed. | |
0b9ab0bd | 96 | |
2b5f62a0 | 97 | DECLARE_NO_COPY_CLASS(wxPluginLibrary) |
0b9ab0bd RL |
98 | }; |
99 | ||
100 | ||
bddd7a8d | 101 | class WXDLLIMPEXP_BASE wxPluginManager |
0b9ab0bd RL |
102 | { |
103 | public: | |
104 | ||
105 | // Static accessors. | |
106 | ||
4f89dbc4 | 107 | static wxPluginLibrary *LoadLibrary( const wxString &libname, |
23213f18 | 108 | int flags = wxDL_DEFAULT ); |
4f89dbc4 RL |
109 | static bool UnloadLibrary(const wxString &libname); |
110 | ||
111 | // This is used by wxDllLoader. It's wrapped in the compatibility | |
112 | // macro because it's of arguable use outside of that. | |
113 | ||
114 | #if WXWIN_COMPATIBILITY_2_2 | |
2d67974d | 115 | wxDEPRECATED( static wxPluginLibrary *GetObjectFromHandle(wxDllType handle) ); |
4f89dbc4 | 116 | #endif |
0b9ab0bd RL |
117 | |
118 | // Instance methods. | |
119 | ||
6fb99eb3 | 120 | wxPluginManager() : m_entry(NULL) {} |
23213f18 | 121 | wxPluginManager(const wxString &libname, int flags = wxDL_DEFAULT) |
4f89dbc4 RL |
122 | { |
123 | Load(libname, flags); | |
124 | } | |
e5e2612a | 125 | ~wxPluginManager() { if ( IsLoaded() ) Unload(); } |
4f89dbc4 | 126 | |
23213f18 | 127 | bool Load(const wxString &libname, int flags = wxDL_DEFAULT); |
4f89dbc4 | 128 | void Unload(); |
0b9ab0bd RL |
129 | |
130 | bool IsLoaded() const { return m_entry && m_entry->IsLoaded(); } | |
131 | void *GetSymbol(const wxString &symbol, bool *success = 0) | |
132 | { | |
133 | return m_entry->GetSymbol( symbol, success ); | |
134 | } | |
135 | ||
dabd1377 JS |
136 | static void CreateManifest() { ms_manifest = new wxDLManifest(wxKEY_STRING); } |
137 | static void ClearManifest() { delete ms_manifest; ms_manifest = NULL; } | |
138 | ||
0b9ab0bd | 139 | private: |
2b5f62a0 VZ |
140 | // return the pointer to the entry for the library with given name in |
141 | // ms_manifest or NULL if none | |
142 | static wxPluginLibrary *FindByName(const wxString& name) | |
143 | { | |
144 | const wxDLManifest::iterator i = ms_manifest->find(name); | |
145 | ||
146 | return i == ms_manifest->end() ? NULL : i->second; | |
147 | } | |
0b9ab0bd | 148 | |
dabd1377 JS |
149 | static wxDLManifest* ms_manifest; // Static hash of loaded libs. |
150 | wxPluginLibrary* m_entry; // Cache our entry in the manifest. | |
0b9ab0bd RL |
151 | |
152 | // We could allow this class to be copied if we really | |
153 | // wanted to, but not without modification. | |
2b5f62a0 | 154 | DECLARE_NO_COPY_CLASS(wxPluginManager) |
0b9ab0bd RL |
155 | }; |
156 | ||
157 | ||
158 | #endif // wxUSE_DYNAMIC_LOADER | |
159 | #endif // _WX_DYNAMICLOADER_H__ | |
160 |