]> git.saurik.com Git - wxWidgets.git/blame - src/common/dynload.cpp
wxMotif for OS/2 adjustements. Source cleaning.
[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
1948bb32
VS
362
363
364#if WXWIN_COMPATIBILITY_2_2
365
366wxPluginLibrary *wxPluginManager::GetObjectFromHandle(wxDllType handle)
367{
368 for ( wxDLManifest::iterator i = ms_manifest->begin();
369 i != ms_manifest->end();
370 ++i )
371 {
372 wxPluginLibrary * const lib = i->second;
373
374 if ( lib->GetLibHandle() == handle )
375 return lib;
376 }
377
378 return NULL;
379}
380
4f89dbc4
RL
381// ---------------------------------------------------------------------------
382// wxDllLoader (all these methods are static)
383// ---------------------------------------------------------------------------
384
4f89dbc4 385
3d7fa4e6 386wxDllType wxDllLoader::LoadLibrary(const wxString &name, bool *success)
4f89dbc4 387{
d0fcccc4
VZ
388 wxPluginLibrary *p = wxPluginManager::LoadLibrary
389 (
390 name,
391 wxDL_DEFAULT | wxDL_VERBATIM | wxDL_NOSHARE
392 );
393
3d7fa4e6
VZ
394 if ( success )
395 *success = p != NULL;
396
d0fcccc4 397 return p ? p->GetLibHandle() : 0;
0b9ab0bd
RL
398}
399
4f89dbc4 400void wxDllLoader::UnloadLibrary(wxDllType handle)
0b9ab0bd 401{
4f89dbc4 402 wxPluginLibrary *p = wxPluginManager::GetObjectFromHandle(handle);
d0fcccc4
VZ
403
404 wxCHECK_RET( p, _T("Unloading a library not loaded with wxDllLoader?") );
405
4f89dbc4 406 p->UnrefLib();
0b9ab0bd
RL
407}
408
d0fcccc4
VZ
409void *
410wxDllLoader::GetSymbol(wxDllType dllHandle, const wxString &name, bool *success)
0b9ab0bd 411{
4f89dbc4 412 wxPluginLibrary *p = wxPluginManager::GetObjectFromHandle(dllHandle);
d0fcccc4
VZ
413
414 if ( !p )
415 {
416 wxFAIL_MSG( _T("Using a library not loaded with wxDllLoader?") );
417
418 if ( success )
68379eaf 419 *success = false;
d0fcccc4
VZ
420
421 return NULL;
422 }
423
4f89dbc4 424 return p->GetSymbol(name, success);
0b9ab0bd
RL
425}
426
1948bb32
VS
427
428// ---------------------------------------------------------------------------
429// Global variables
430// ---------------------------------------------------------------------------
431
432wxLibraries wxTheLibraries;
433
434// ============================================================================
435// implementation
436// ============================================================================
437
438// construct the full name from the base shared object name: adds a .dll
439// suffix under Windows or .so under Unix
440static wxString ConstructLibraryName(const wxString& basename)
441{
442 wxString fullname;
443 fullname << basename << wxDllLoader::GetDllExt();
444
445 return fullname;
446}
447
448// ---------------------------------------------------------------------------
449// wxLibrary (one instance per dynamic library)
450// ---------------------------------------------------------------------------
451
452wxLibrary::wxLibrary(wxDllType handle)
453{
454 typedef wxClassInfo *(*t_get_first)(void);
455 t_get_first get_first;
456
457 m_handle = handle;
458
459 // Some system may use a local heap for library.
460 get_first = (t_get_first)GetSymbol(_T("wxGetClassFirst"));
77ffb593 461 // It is a wxWidgets DLL.
1948bb32
VS
462 if (get_first)
463 PrepareClasses(get_first());
464}
465
466wxLibrary::~wxLibrary()
467{
468 if ( m_handle )
469 {
470 wxDllLoader::UnloadLibrary(m_handle);
471 }
472}
473
474wxObject *wxLibrary::CreateObject(const wxString& name)
475{
476 wxClassInfo *info = (wxClassInfo *)classTable.Get(name);
477
478 if (!info)
479 return NULL;
480
481 return info->CreateObject();
482}
483
484void wxLibrary::PrepareClasses(wxClassInfo *first)
485{
486 // Index all class infos by their class name
487 wxClassInfo *info = first;
488 while (info)
489 {
490 if (info->m_className)
491 classTable.Put(info->m_className, (wxObject *)info);
492 info = info->m_next;
493 }
494
d1d738f1 495#if !wxUSE_EXTENDED_RTTI
1948bb32
VS
496 // Set base pointers for each wxClassInfo
497 info = first;
498 while (info)
499 {
500 if (info->GetBaseClassName1())
501 info->m_baseInfo1 = (wxClassInfo *)classTable.Get(info->GetBaseClassName1());
502 if (info->GetBaseClassName2())
503 info->m_baseInfo2 = (wxClassInfo *)classTable.Get(info->GetBaseClassName2());
504 info = info->m_next;
505 }
1bc7510a 506#endif
1948bb32
VS
507}
508
509void *wxLibrary::GetSymbol(const wxString& symbname)
510{
511 return wxDllLoader::GetSymbol(m_handle, symbname);
512}
513
514
515// ---------------------------------------------------------------------------
516// wxLibraries (only one instance should normally exist)
517// ---------------------------------------------------------------------------
518
519wxLibraries::wxLibraries():m_loaded(wxKEY_STRING)
520{
521}
522
523wxLibraries::~wxLibraries()
524{
ec157c8f 525 wxNode *node = m_loaded.GetFirst();
1948bb32
VS
526
527 while (node) {
ec157c8f 528 wxLibrary *lib = (wxLibrary *)node->GetData();
1948bb32
VS
529 delete lib;
530
ec157c8f 531 node = node->GetNext();
1948bb32
VS
532 }
533}
534
535wxLibrary *wxLibraries::LoadLibrary(const wxString& name)
536{
537 wxLibrary *lib;
538 wxClassInfo *old_sm_first;
539 wxNode *node = m_loaded.Find(name.GetData());
540
541 if (node != NULL)
ec157c8f 542 return ((wxLibrary *)node->GetData());
1948bb32
VS
543
544 // If DLL shares data, this is necessary.
545 old_sm_first = wxClassInfo::sm_first;
546 wxClassInfo::sm_first = NULL;
547
548 wxString libname = ConstructLibraryName(name);
549
68379eaf 550 bool success = false;
1948bb32
VS
551 wxDllType handle = wxDllLoader::LoadLibrary(libname, &success);
552 if(success)
553 {
554 lib = new wxLibrary(handle);
555 wxClassInfo::sm_first = old_sm_first;
556
557 m_loaded.Append(name.GetData(), lib);
558 }
559 else
560 lib = NULL;
561 return lib;
562}
563
564wxObject *wxLibraries::CreateObject(const wxString& path)
565{
ec157c8f 566 wxNode *node = m_loaded.GetFirst();
1948bb32
VS
567 wxObject *obj;
568
569 while (node) {
ec157c8f 570 obj = ((wxLibrary *)node->GetData())->CreateObject(path);
1948bb32
VS
571 if (obj)
572 return obj;
573
ec157c8f 574 node = node->GetNext();
1948bb32
VS
575 }
576 return NULL;
577}
578
4f89dbc4
RL
579#endif // WXWIN_COMPATIBILITY_2_2
580
1948bb32 581
0b9ab0bd
RL
582#endif // wxUSE_DYNAMIC_LOADER
583