]> git.saurik.com Git - wxWidgets.git/blame - src/common/dynload.cpp
Unix compilation fixes after last commit
[wxWidgets.git] / src / common / dynload.cpp
CommitLineData
0b9ab0bd
RL
1/////////////////////////////////////////////////////////////////////////////
2// Name: dynload.cpp
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
0b9ab0bd
RL
13// ----------------------------------------------------------------------------
14// headers
15// ----------------------------------------------------------------------------
16
17#include "wx/wxprec.h"
18
19#ifdef __BORLANDC__
409bd320 20 #pragma hdrstop
0b9ab0bd
RL
21#endif
22
23#if wxUSE_DYNAMIC_LOADER
24
25#ifdef __WINDOWS__
409bd320 26 #include "wx/msw/private.h"
0b9ab0bd
RL
27#endif
28
29#ifndef WX_PRECOMP
409bd320
VZ
30 #include "wx/log.h"
31 #include "wx/intl.h"
2b5f62a0 32 #include "wx/hash.h"
076fdb21 33 #include "wx/utils.h"
0b9ab0bd
RL
34#endif
35
401eb3de 36#include "wx/strconv.h"
0b9ab0bd 37
409bd320 38#include "wx/dynload.h"
dabd1377 39#include "wx/module.h"
0b9ab0bd 40
0b9ab0bd
RL
41
42// ---------------------------------------------------------------------------
4f89dbc4 43// wxPluginLibrary
0b9ab0bd
RL
44// ---------------------------------------------------------------------------
45
46
dabd1377
JS
47wxDLImports* wxPluginLibrary::ms_classes = NULL;
48
49class wxPluginLibraryModule : public wxModule
50{
51public:
d0fcccc4
VZ
52 wxPluginLibraryModule() { }
53
54 // TODO: create ms_classes on demand, why always preallocate it?
55 virtual bool OnInit()
56 {
b2bb920f 57 wxPluginLibrary::ms_classes = new wxDLImports;
d0fcccc4 58 wxPluginManager::CreateManifest();
68379eaf 59 return true;
d0fcccc4
VZ
60 }
61
62 virtual void OnExit()
63 {
64 delete wxPluginLibrary::ms_classes;
65 wxPluginLibrary::ms_classes = NULL;
66 wxPluginManager::ClearManifest();
67 }
dabd1377
JS
68
69private:
70 DECLARE_DYNAMIC_CLASS(wxPluginLibraryModule )
71};
72
73IMPLEMENT_DYNAMIC_CLASS(wxPluginLibraryModule, wxModule)
74
0b9ab0bd 75
23213f18 76wxPluginLibrary::wxPluginLibrary(const wxString &libname, int flags)
4f89dbc4 77 : m_linkcount(1)
7c1e2b44 78 , m_objcount(0)
0b9ab0bd 79{
4f89dbc4
RL
80 m_before = wxClassInfo::sm_first;
81 Load( libname, flags );
82 m_after = wxClassInfo::sm_first;
83
0b9ab0bd
RL
84 if( m_handle != 0 )
85 {
2d4957f2 86 UpdateClasses();
0b9ab0bd
RL
87 RegisterModules();
88 }
89 else
d0fcccc4
VZ
90 {
91 // Flag us for deletion
92 --m_linkcount;
93 }
0b9ab0bd
RL
94}
95
4f89dbc4 96wxPluginLibrary::~wxPluginLibrary()
0b9ab0bd 97{
01610529
RL
98 if( m_handle != 0 )
99 {
100 UnregisterModules();
2d4957f2 101 RestoreClasses();
01610529 102 }
0b9ab0bd
RL
103}
104
d0fcccc4
VZ
105wxPluginLibrary *wxPluginLibrary::RefLib()
106{
107 wxCHECK_MSG( m_linkcount > 0, NULL,
108 _T("Library had been already deleted!") );
109
110 ++m_linkcount;
111 return this;
112}
113
4f89dbc4 114bool wxPluginLibrary::UnrefLib()
7c1e2b44 115{
d0fcccc4
VZ
116 wxASSERT_MSG( m_objcount == 0,
117 _T("Library unloaded before all objects were destroyed") );
118
ebf0caa1 119 if ( m_linkcount == 0 || --m_linkcount == 0 )
7c1e2b44
RL
120 {
121 delete this;
68379eaf 122 return true;
7c1e2b44 123 }
d0fcccc4 124
68379eaf 125 return false;
7c1e2b44 126}
0b9ab0bd
RL
127
128// ------------------------
129// Private methods
130// ------------------------
131
2d4957f2
VS
132void wxPluginLibrary::UpdateClasses()
133{
134 for (wxClassInfo *info = m_after; info != m_before; info = info->m_next)
135 {
b77a9389 136 if( info->GetClassName() )
2d4957f2
VS
137 {
138 // Hash all the class names into a local table too so
139 // we can quickly find the entry they correspond to.
b77a9389 140 (*ms_classes)[info->GetClassName()] = this;
2d4957f2
VS
141 }
142 }
143}
144
145void wxPluginLibrary::RestoreClasses()
146{
ebf0caa1
VS
147 // Check if there is a need to restore classes.
148 if (!ms_classes)
149 return;
150
2d4957f2
VS
151 for(wxClassInfo *info = m_after; info != m_before; info = info->m_next)
152 {
b77a9389 153 ms_classes->erase(ms_classes->find(info->GetClassName()));
2d4957f2
VS
154 }
155}
156
4f89dbc4 157void wxPluginLibrary::RegisterModules()
0b9ab0bd
RL
158{
159 // Plugin libraries might have wxModules, Register and initialise them if
160 // they do.
161 //
162 // Note that these classes are NOT included in the reference counting since
163 // it's implicit that they will be unloaded if and when the last handle to
164 // the library is. We do have to keep a copy of the module's pointer
165 // though, as there is currently no way to Unregister it without it.
166
7c1e2b44 167 wxASSERT_MSG( m_linkcount == 1,
0b9ab0bd
RL
168 _T("RegisterModules should only be called for the first load") );
169
6eccc0c4 170 for ( wxClassInfo *info = m_after; info != m_before; info = info->m_next)
0b9ab0bd
RL
171 {
172 if( info->IsKindOf(CLASSINFO(wxModule)) )
173 {
174 wxModule *m = wxDynamicCast(info->CreateObject(), wxModule);
175
176 wxASSERT_MSG( m, _T("wxDynamicCast of wxModule failed") );
177
df5168c4 178 m_wxmodules.push_back(m);
0b9ab0bd
RL
179 wxModule::RegisterModule(m);
180 }
181 }
182
183 // FIXME: Likewise this is (well was) very similar to InitializeModules()
184
df5168c4
MB
185 for ( wxModuleList::iterator it = m_wxmodules.begin();
186 it != m_wxmodules.end();
187 ++it)
0b9ab0bd 188 {
df5168c4 189 if( !(*it)->Init() )
0b9ab0bd 190 {
4f89dbc4 191 wxLogDebug(_T("wxModule::Init() failed for wxPluginLibrary"));
0b9ab0bd
RL
192
193 // XXX: Watch this, a different hash implementation might break it,
194 // a good hash implementation would let us fix it though.
195
196 // The name of the game is to remove any uninitialised modules and
197 // let the dtor Exit the rest on shutdown, (which we'll initiate
198 // shortly).
199
df5168c4 200 wxModuleList::iterator oldNode = m_wxmodules.end();
0b9ab0bd 201 do {
df5168c4
MB
202 ++it;
203 if( oldNode != m_wxmodules.end() )
204 m_wxmodules.erase(oldNode);
205 wxModule::UnregisterModule( *it );
206 oldNode = it;
207 } while( it != m_wxmodules.end() );
0b9ab0bd 208
7c1e2b44 209 --m_linkcount; // Flag us for deletion
0b9ab0bd
RL
210 break;
211 }
212 }
213}
214
4f89dbc4 215void wxPluginLibrary::UnregisterModules()
0b9ab0bd 216{
df5168c4 217 wxModuleList::iterator it;
0b9ab0bd 218
df5168c4
MB
219 for ( it = m_wxmodules.begin(); it != m_wxmodules.end(); ++it )
220 (*it)->Exit();
0b9ab0bd 221
df5168c4
MB
222 for ( it = m_wxmodules.begin(); it != m_wxmodules.end(); ++it )
223 wxModule::UnregisterModule( *it );
0b9ab0bd 224
5385fa5b
VS
225 // NB: content of the list was deleted by UnregisterModule calls above:
226 m_wxmodules.clear();
0b9ab0bd
RL
227}
228
229
230// ---------------------------------------------------------------------------
d0fcccc4 231// wxPluginManager
0b9ab0bd
RL
232// ---------------------------------------------------------------------------
233
dabd1377 234wxDLManifest* wxPluginManager::ms_manifest = NULL;
0b9ab0bd
RL
235
236// ------------------------
237// Static accessors
238// ------------------------
239
d0fcccc4
VZ
240wxPluginLibrary *
241wxPluginManager::LoadLibrary(const wxString &libname, int flags)
0b9ab0bd 242{
4f89dbc4 243 wxString realname(libname);
0b9ab0bd 244
4f89dbc4
RL
245 if( !(flags & wxDL_VERBATIM) )
246 realname += wxDynamicLibrary::GetDllExt();
247
d0fcccc4
VZ
248 wxPluginLibrary *entry;
249
250 if ( flags & wxDL_NOSHARE )
251 {
252 entry = NULL;
253 }
254 else
255 {
2b5f62a0 256 entry = FindByName(realname);
d0fcccc4 257 }
4f89dbc4 258
d0fcccc4 259 if ( entry )
0b9ab0bd 260 {
d0fcccc4
VZ
261 wxLogTrace(_T("dll"),
262 _T("LoadLibrary(%s): already loaded."), realname.c_str());
263
7c1e2b44 264 entry->RefLib();
0b9ab0bd
RL
265 }
266 else
267 {
4f89dbc4 268 entry = new wxPluginLibrary( libname, flags );
0b9ab0bd 269
d0fcccc4 270 if ( entry->IsLoaded() )
0b9ab0bd 271 {
2b5f62a0 272 (*ms_manifest)[realname] = entry;
d0fcccc4
VZ
273
274 wxLogTrace(_T("dll"),
275 _T("LoadLibrary(%s): loaded ok."), realname.c_str());
276
0b9ab0bd
RL
277 }
278 else
279 {
d0fcccc4
VZ
280 wxLogTrace(_T("dll"),
281 _T("LoadLibrary(%s): failed to load."), realname.c_str());
282
283 // we have created entry just above
284 if ( !entry->UnrefLib() )
285 {
286 // ... so UnrefLib() is supposed to delete it
287 wxFAIL_MSG( _T("Currently linked library is not loaded?") );
288 }
289
290 entry = NULL;
0b9ab0bd
RL
291 }
292 }
d0fcccc4 293
0b9ab0bd
RL
294 return entry;
295}
296
d0fcccc4 297bool wxPluginManager::UnloadLibrary(const wxString& libname)
0b9ab0bd 298{
d0fcccc4 299 wxString realname = libname;
4f89dbc4 300
2b5f62a0 301 wxPluginLibrary *entry = FindByName(realname);
0b9ab0bd 302
d0fcccc4
VZ
303 if ( !entry )
304 {
305 realname += wxDynamicLibrary::GetDllExt();
0b9ab0bd 306
2b5f62a0 307 entry = FindByName(realname);
d0fcccc4
VZ
308 }
309
310 if ( !entry )
311 {
312 wxLogDebug(_T("Attempt to unload library '%s' which is not loaded."),
313 libname.c_str());
314
68379eaf 315 return false;
d0fcccc4
VZ
316 }
317
318 wxLogTrace(_T("dll"), _T("UnloadLibrary(%s)"), realname.c_str());
319
320 if ( !entry->UnrefLib() )
321 {
322 // not really unloaded yet
68379eaf 323 return false;
d0fcccc4
VZ
324 }
325
2b5f62a0 326 ms_manifest->erase(ms_manifest->find(realname));
d0fcccc4 327
68379eaf 328 return true;
4f89dbc4
RL
329}
330
0b9ab0bd
RL
331// ------------------------
332// Class implementation
333// ------------------------
334
23213f18 335bool wxPluginManager::Load(const wxString &libname, int flags)
0b9ab0bd 336{
4f89dbc4 337 m_entry = wxPluginManager::LoadLibrary(libname, flags);
2b5f62a0 338
4f89dbc4 339 return IsLoaded();
0b9ab0bd
RL
340}
341
4f89dbc4 342void wxPluginManager::Unload()
0b9ab0bd 343{
2b5f62a0 344 wxCHECK_RET( m_entry, _T("unloading an invalid wxPluginManager?") );
0b9ab0bd 345
2b5f62a0
VZ
346 for ( wxDLManifest::iterator i = ms_manifest->begin();
347 i != ms_manifest->end();
348 ++i )
4f89dbc4 349 {
2b5f62a0
VZ
350 if ( i->second == m_entry )
351 {
352 ms_manifest->erase(i);
46113053 353 break;
2b5f62a0 354 }
4f89dbc4 355 }
2b5f62a0
VZ
356
357 m_entry->UnrefLib();
358
359 m_entry = NULL;
0b9ab0bd
RL
360}
361
0b9ab0bd
RL
362#endif // wxUSE_DYNAMIC_LOADER
363