]>
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> | |
371a5b4e | 10 | // Licence: wxWindows licence |
0b9ab0bd RL |
11 | ///////////////////////////////////////////////////////////////////////////// |
12 | ||
13 | #ifndef _WX_DYNAMICLOADER_H__ | |
14 | #define _WX_DYNAMICLOADER_H__ | |
15 | ||
12028905 | 16 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
0b9ab0bd RL |
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 | ||
1948bb32 | 28 | #include "wx/dynlib.h" |
2b5f62a0 | 29 | #include "wx/hashmap.h" |
0b9ab0bd RL |
30 | #include "wx/module.h" |
31 | ||
bddd7a8d | 32 | class WXDLLIMPEXP_BASE wxPluginLibrary; |
0b9ab0bd | 33 | |
0b9ab0bd | 34 | |
1948bb32 VS |
35 | WX_DECLARE_STRING_HASH_MAP_WITH_DECL(wxPluginLibrary *, wxDLManifest, |
36 | class WXDLLIMPEXP_BASE); | |
37 | typedef wxDLManifest wxDLImports; | |
0b9ab0bd RL |
38 | |
39 | // --------------------------------------------------------------------------- | |
4f89dbc4 | 40 | // wxPluginLibrary |
0b9ab0bd RL |
41 | // --------------------------------------------------------------------------- |
42 | ||
4f89dbc4 RL |
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 | ||
bddd7a8d | 51 | class WXDLLIMPEXP_BASE wxPluginLibrary : public wxDynamicLibrary |
0b9ab0bd RL |
52 | { |
53 | public: | |
54 | ||
dabd1377 | 55 | static wxDLImports* ms_classes; // Static hash of all imported classes. |
0b9ab0bd | 56 | |
23213f18 | 57 | wxPluginLibrary( const wxString &libname, int flags = wxDL_DEFAULT ); |
4f89dbc4 | 58 | ~wxPluginLibrary(); |
0b9ab0bd | 59 | |
d0fcccc4 | 60 | wxPluginLibrary *RefLib(); |
4f89dbc4 | 61 | bool UnrefLib(); |
7c1e2b44 RL |
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 | ||
4f89dbc4 RL |
75 | void RefObj() { ++m_objcount; } |
76 | void UnrefObj() | |
7c1e2b44 RL |
77 | { |
78 | wxASSERT_MSG( m_objcount > 0, _T("Too many objects deleted??") ); | |
79 | --m_objcount; | |
80 | } | |
0b9ab0bd | 81 | |
4f89dbc4 | 82 | // Override/hide some base class methods |
0b9ab0bd | 83 | |
4f89dbc4 RL |
84 | bool IsLoaded() const { return m_linkcount > 0; } |
85 | void Unload() { UnrefLib(); } | |
0b9ab0bd RL |
86 | |
87 | private: | |
88 | ||
7c1e2b44 | 89 | wxClassInfo *m_before; // sm_first before loading this lib |
7c1e2b44 | 90 | wxClassInfo *m_after; // ..and after. |
0b9ab0bd | 91 | |
7c1e2b44 RL |
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. | |
0b9ab0bd | 95 | |
7c1e2b44 RL |
96 | void RegisterModules(); // Init any wxModules in the lib. |
97 | void UnregisterModules(); // Cleanup any wxModules we installed. | |
0b9ab0bd | 98 | |
2b5f62a0 | 99 | DECLARE_NO_COPY_CLASS(wxPluginLibrary) |
0b9ab0bd RL |
100 | }; |
101 | ||
102 | ||
bddd7a8d | 103 | class WXDLLIMPEXP_BASE wxPluginManager |
0b9ab0bd RL |
104 | { |
105 | public: | |
106 | ||
107 | // Static accessors. | |
108 | ||
4f89dbc4 | 109 | static wxPluginLibrary *LoadLibrary( const wxString &libname, |
23213f18 | 110 | int flags = wxDL_DEFAULT ); |
4f89dbc4 RL |
111 | static bool UnloadLibrary(const wxString &libname); |
112 | ||
113 | // This is used by wxDllLoader. It's wrapped in the compatibility | |
114 | // macro because it's of arguable use outside of that. | |
115 | ||
116 | #if WXWIN_COMPATIBILITY_2_2 | |
117 | static wxPluginLibrary *GetObjectFromHandle(wxDllType handle); | |
118 | #endif | |
0b9ab0bd RL |
119 | |
120 | // Instance methods. | |
121 | ||
2b5f62a0 | 122 | wxPluginManager() : m_entry(NULL) {}; |
23213f18 | 123 | wxPluginManager(const wxString &libname, int flags = wxDL_DEFAULT) |
4f89dbc4 RL |
124 | { |
125 | Load(libname, flags); | |
126 | } | |
127 | ~wxPluginManager() { Unload(); } | |
128 | ||
23213f18 | 129 | bool Load(const wxString &libname, int flags = wxDL_DEFAULT); |
4f89dbc4 | 130 | void Unload(); |
0b9ab0bd RL |
131 | |
132 | bool IsLoaded() const { return m_entry && m_entry->IsLoaded(); } | |
133 | void *GetSymbol(const wxString &symbol, bool *success = 0) | |
134 | { | |
135 | return m_entry->GetSymbol( symbol, success ); | |
136 | } | |
137 | ||
dabd1377 JS |
138 | static void CreateManifest() { ms_manifest = new wxDLManifest(wxKEY_STRING); } |
139 | static void ClearManifest() { delete ms_manifest; ms_manifest = NULL; } | |
140 | ||
0b9ab0bd | 141 | private: |
2b5f62a0 VZ |
142 | // return the pointer to the entry for the library with given name in |
143 | // ms_manifest or NULL if none | |
144 | static wxPluginLibrary *FindByName(const wxString& name) | |
145 | { | |
146 | const wxDLManifest::iterator i = ms_manifest->find(name); | |
147 | ||
148 | return i == ms_manifest->end() ? NULL : i->second; | |
149 | } | |
0b9ab0bd | 150 | |
dabd1377 JS |
151 | static wxDLManifest* ms_manifest; // Static hash of loaded libs. |
152 | wxPluginLibrary* m_entry; // Cache our entry in the manifest. | |
0b9ab0bd RL |
153 | |
154 | // We could allow this class to be copied if we really | |
155 | // wanted to, but not without modification. | |
2b5f62a0 | 156 | DECLARE_NO_COPY_CLASS(wxPluginManager) |
0b9ab0bd RL |
157 | }; |
158 | ||
159 | ||
160 | #endif // wxUSE_DYNAMIC_LOADER | |
161 | #endif // _WX_DYNAMICLOADER_H__ | |
162 |