]> git.saurik.com Git - wxWidgets.git/blob - src/common/dynload.cpp
set background style to wxBG_STYLE_COLOUR in SetBackgroundColour()
[wxWidgets.git] / src / common / dynload.cpp
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>
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
12
13 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
14 #pragma implementation "dynload.h"
15 #endif
16
17 // ----------------------------------------------------------------------------
18 // headers
19 // ----------------------------------------------------------------------------
20
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_DYNAMIC_LOADER
28
29 #ifdef __WINDOWS__
30 #include "wx/msw/private.h"
31 #endif
32
33 #ifndef WX_PRECOMP
34 #include "wx/log.h"
35 #include "wx/intl.h"
36 #include "wx/hash.h"
37 #include "wx/utils.h"
38 #endif
39
40 #include "wx/strconv.h"
41
42 #include "wx/dynload.h"
43 #include "wx/module.h"
44
45
46 // ---------------------------------------------------------------------------
47 // wxPluginLibrary
48 // ---------------------------------------------------------------------------
49
50
51 wxDLImports* wxPluginLibrary::ms_classes = NULL;
52
53 class wxPluginLibraryModule : public wxModule
54 {
55 public:
56 wxPluginLibraryModule() { }
57
58 // TODO: create ms_classes on demand, why always preallocate it?
59 virtual bool OnInit()
60 {
61 wxPluginLibrary::ms_classes = new wxDLImports;
62 wxPluginManager::CreateManifest();
63 return true;
64 }
65
66 virtual void OnExit()
67 {
68 delete wxPluginLibrary::ms_classes;
69 wxPluginLibrary::ms_classes = NULL;
70 wxPluginManager::ClearManifest();
71 }
72
73 private:
74 DECLARE_DYNAMIC_CLASS(wxPluginLibraryModule )
75 };
76
77 IMPLEMENT_DYNAMIC_CLASS(wxPluginLibraryModule, wxModule)
78
79
80 wxPluginLibrary::wxPluginLibrary(const wxString &libname, int flags)
81 : m_linkcount(1)
82 , m_objcount(0)
83 {
84 m_before = wxClassInfo::sm_first;
85 Load( libname, flags );
86 m_after = wxClassInfo::sm_first;
87
88 if( m_handle != 0 )
89 {
90 UpdateClasses();
91 RegisterModules();
92 }
93 else
94 {
95 // Flag us for deletion
96 --m_linkcount;
97 }
98 }
99
100 wxPluginLibrary::~wxPluginLibrary()
101 {
102 if( m_handle != 0 )
103 {
104 UnregisterModules();
105 RestoreClasses();
106 }
107 }
108
109 wxPluginLibrary *wxPluginLibrary::RefLib()
110 {
111 wxCHECK_MSG( m_linkcount > 0, NULL,
112 _T("Library had been already deleted!") );
113
114 ++m_linkcount;
115 return this;
116 }
117
118 bool wxPluginLibrary::UnrefLib()
119 {
120 wxASSERT_MSG( m_objcount == 0,
121 _T("Library unloaded before all objects were destroyed") );
122
123 if ( m_linkcount == 0 || --m_linkcount == 0 )
124 {
125 delete this;
126 return true;
127 }
128
129 return false;
130 }
131
132 // ------------------------
133 // Private methods
134 // ------------------------
135
136 void wxPluginLibrary::UpdateClasses()
137 {
138 for (wxClassInfo *info = m_after; info != m_before; info = info->m_next)
139 {
140 if( info->m_className )
141 {
142 // Hash all the class names into a local table too so
143 // we can quickly find the entry they correspond to.
144 (*ms_classes)[info->m_className] = this;
145 }
146 }
147 }
148
149 void wxPluginLibrary::RestoreClasses()
150 {
151 // Check if there is a need to restore classes.
152 if (!ms_classes)
153 return;
154
155 for(wxClassInfo *info = m_after; info != m_before; info = info->m_next)
156 {
157 ms_classes->erase(ms_classes->find(info->m_className));
158 }
159 }
160
161 void wxPluginLibrary::RegisterModules()
162 {
163 // Plugin libraries might have wxModules, Register and initialise them if
164 // they do.
165 //
166 // Note that these classes are NOT included in the reference counting since
167 // it's implicit that they will be unloaded if and when the last handle to
168 // the library is. We do have to keep a copy of the module's pointer
169 // though, as there is currently no way to Unregister it without it.
170
171 wxASSERT_MSG( m_linkcount == 1,
172 _T("RegisterModules should only be called for the first load") );
173
174 for ( wxClassInfo *info = m_after; info != m_before; info = info->m_next)
175 {
176 if( info->IsKindOf(CLASSINFO(wxModule)) )
177 {
178 wxModule *m = wxDynamicCast(info->CreateObject(), wxModule);
179
180 wxASSERT_MSG( m, _T("wxDynamicCast of wxModule failed") );
181
182 m_wxmodules.push_back(m);
183 wxModule::RegisterModule(m);
184 }
185 }
186
187 // FIXME: Likewise this is (well was) very similar to InitializeModules()
188
189 for ( wxModuleList::iterator it = m_wxmodules.begin();
190 it != m_wxmodules.end();
191 ++it)
192 {
193 if( !(*it)->Init() )
194 {
195 wxLogDebug(_T("wxModule::Init() failed for wxPluginLibrary"));
196
197 // XXX: Watch this, a different hash implementation might break it,
198 // a good hash implementation would let us fix it though.
199
200 // The name of the game is to remove any uninitialised modules and
201 // let the dtor Exit the rest on shutdown, (which we'll initiate
202 // shortly).
203
204 wxModuleList::iterator oldNode = m_wxmodules.end();
205 do {
206 ++it;
207 if( oldNode != m_wxmodules.end() )
208 m_wxmodules.erase(oldNode);
209 wxModule::UnregisterModule( *it );
210 oldNode = it;
211 } while( it != m_wxmodules.end() );
212
213 --m_linkcount; // Flag us for deletion
214 break;
215 }
216 }
217 }
218
219 void wxPluginLibrary::UnregisterModules()
220 {
221 wxModuleList::iterator it;
222
223 for ( it = m_wxmodules.begin(); it != m_wxmodules.end(); ++it )
224 (*it)->Exit();
225
226 for ( it = m_wxmodules.begin(); it != m_wxmodules.end(); ++it )
227 wxModule::UnregisterModule( *it );
228
229 // NB: content of the list was deleted by UnregisterModule calls above:
230 m_wxmodules.clear();
231 }
232
233
234 // ---------------------------------------------------------------------------
235 // wxPluginManager
236 // ---------------------------------------------------------------------------
237
238 wxDLManifest* wxPluginManager::ms_manifest = NULL;
239
240 // ------------------------
241 // Static accessors
242 // ------------------------
243
244 wxPluginLibrary *
245 wxPluginManager::LoadLibrary(const wxString &libname, int flags)
246 {
247 wxString realname(libname);
248
249 if( !(flags & wxDL_VERBATIM) )
250 realname += wxDynamicLibrary::GetDllExt();
251
252 wxPluginLibrary *entry;
253
254 if ( flags & wxDL_NOSHARE )
255 {
256 entry = NULL;
257 }
258 else
259 {
260 entry = FindByName(realname);
261 }
262
263 if ( entry )
264 {
265 wxLogTrace(_T("dll"),
266 _T("LoadLibrary(%s): already loaded."), realname.c_str());
267
268 entry->RefLib();
269 }
270 else
271 {
272 entry = new wxPluginLibrary( libname, flags );
273
274 if ( entry->IsLoaded() )
275 {
276 (*ms_manifest)[realname] = entry;
277
278 wxLogTrace(_T("dll"),
279 _T("LoadLibrary(%s): loaded ok."), realname.c_str());
280
281 }
282 else
283 {
284 wxLogTrace(_T("dll"),
285 _T("LoadLibrary(%s): failed to load."), realname.c_str());
286
287 // we have created entry just above
288 if ( !entry->UnrefLib() )
289 {
290 // ... so UnrefLib() is supposed to delete it
291 wxFAIL_MSG( _T("Currently linked library is not loaded?") );
292 }
293
294 entry = NULL;
295 }
296 }
297
298 return entry;
299 }
300
301 bool wxPluginManager::UnloadLibrary(const wxString& libname)
302 {
303 wxString realname = libname;
304
305 wxPluginLibrary *entry = FindByName(realname);
306
307 if ( !entry )
308 {
309 realname += wxDynamicLibrary::GetDllExt();
310
311 entry = FindByName(realname);
312 }
313
314 if ( !entry )
315 {
316 wxLogDebug(_T("Attempt to unload library '%s' which is not loaded."),
317 libname.c_str());
318
319 return false;
320 }
321
322 wxLogTrace(_T("dll"), _T("UnloadLibrary(%s)"), realname.c_str());
323
324 if ( !entry->UnrefLib() )
325 {
326 // not really unloaded yet
327 return false;
328 }
329
330 ms_manifest->erase(ms_manifest->find(realname));
331
332 return true;
333 }
334
335 // ------------------------
336 // Class implementation
337 // ------------------------
338
339 bool wxPluginManager::Load(const wxString &libname, int flags)
340 {
341 m_entry = wxPluginManager::LoadLibrary(libname, flags);
342
343 return IsLoaded();
344 }
345
346 void wxPluginManager::Unload()
347 {
348 wxCHECK_RET( m_entry, _T("unloading an invalid wxPluginManager?") );
349
350 for ( wxDLManifest::iterator i = ms_manifest->begin();
351 i != ms_manifest->end();
352 ++i )
353 {
354 if ( i->second == m_entry )
355 {
356 ms_manifest->erase(i);
357 break;
358 }
359 }
360
361 m_entry->UnrefLib();
362
363 m_entry = NULL;
364 }
365
366
367
368 #if WXWIN_COMPATIBILITY_2_2
369
370 wxPluginLibrary *wxPluginManager::GetObjectFromHandle(wxDllType handle)
371 {
372 for ( wxDLManifest::iterator i = ms_manifest->begin();
373 i != ms_manifest->end();
374 ++i )
375 {
376 wxPluginLibrary * const lib = i->second;
377
378 if ( lib->GetLibHandle() == handle )
379 return lib;
380 }
381
382 return NULL;
383 }
384
385 // ---------------------------------------------------------------------------
386 // wxDllLoader (all these methods are static)
387 // ---------------------------------------------------------------------------
388
389
390 wxDllType wxDllLoader::LoadLibrary(const wxString &name, bool *success)
391 {
392 wxPluginLibrary *p = wxPluginManager::LoadLibrary
393 (
394 name,
395 wxDL_DEFAULT | wxDL_VERBATIM | wxDL_NOSHARE
396 );
397
398 if ( success )
399 *success = p != NULL;
400
401 return p ? p->GetLibHandle() : 0;
402 }
403
404 void wxDllLoader::UnloadLibrary(wxDllType handle)
405 {
406 wxPluginLibrary *p = wxPluginManager::GetObjectFromHandle(handle);
407
408 wxCHECK_RET( p, _T("Unloading a library not loaded with wxDllLoader?") );
409
410 p->UnrefLib();
411 }
412
413 void *
414 wxDllLoader::GetSymbol(wxDllType dllHandle, const wxString &name, bool *success)
415 {
416 wxPluginLibrary *p = wxPluginManager::GetObjectFromHandle(dllHandle);
417
418 if ( !p )
419 {
420 wxFAIL_MSG( _T("Using a library not loaded with wxDllLoader?") );
421
422 if ( success )
423 *success = false;
424
425 return NULL;
426 }
427
428 return p->GetSymbol(name, success);
429 }
430
431
432 // ---------------------------------------------------------------------------
433 // Global variables
434 // ---------------------------------------------------------------------------
435
436 wxLibraries wxTheLibraries;
437
438 // ============================================================================
439 // implementation
440 // ============================================================================
441
442 // construct the full name from the base shared object name: adds a .dll
443 // suffix under Windows or .so under Unix
444 static wxString ConstructLibraryName(const wxString& basename)
445 {
446 wxString fullname;
447 fullname << basename << wxDllLoader::GetDllExt();
448
449 return fullname;
450 }
451
452 // ---------------------------------------------------------------------------
453 // wxLibrary (one instance per dynamic library)
454 // ---------------------------------------------------------------------------
455
456 wxLibrary::wxLibrary(wxDllType handle)
457 {
458 typedef wxClassInfo *(*t_get_first)(void);
459 t_get_first get_first;
460
461 m_handle = handle;
462
463 // Some system may use a local heap for library.
464 get_first = (t_get_first)GetSymbol(_T("wxGetClassFirst"));
465 // It is a wxWidgets DLL.
466 if (get_first)
467 PrepareClasses(get_first());
468 }
469
470 wxLibrary::~wxLibrary()
471 {
472 if ( m_handle )
473 {
474 wxDllLoader::UnloadLibrary(m_handle);
475 }
476 }
477
478 wxObject *wxLibrary::CreateObject(const wxString& name)
479 {
480 wxClassInfo *info = (wxClassInfo *)classTable.Get(name);
481
482 if (!info)
483 return NULL;
484
485 return info->CreateObject();
486 }
487
488 void wxLibrary::PrepareClasses(wxClassInfo *first)
489 {
490 // Index all class infos by their class name
491 wxClassInfo *info = first;
492 while (info)
493 {
494 if (info->m_className)
495 classTable.Put(info->m_className, (wxObject *)info);
496 info = info->m_next;
497 }
498
499 #if !wxUSE_EXTENDED_RTTI
500 // Set base pointers for each wxClassInfo
501 info = first;
502 while (info)
503 {
504 if (info->GetBaseClassName1())
505 info->m_baseInfo1 = (wxClassInfo *)classTable.Get(info->GetBaseClassName1());
506 if (info->GetBaseClassName2())
507 info->m_baseInfo2 = (wxClassInfo *)classTable.Get(info->GetBaseClassName2());
508 info = info->m_next;
509 }
510 #endif
511 }
512
513 void *wxLibrary::GetSymbol(const wxString& symbname)
514 {
515 return wxDllLoader::GetSymbol(m_handle, symbname);
516 }
517
518
519 // ---------------------------------------------------------------------------
520 // wxLibraries (only one instance should normally exist)
521 // ---------------------------------------------------------------------------
522
523 wxLibraries::wxLibraries():m_loaded(wxKEY_STRING)
524 {
525 }
526
527 wxLibraries::~wxLibraries()
528 {
529 wxNode *node = m_loaded.First();
530
531 while (node) {
532 wxLibrary *lib = (wxLibrary *)node->Data();
533 delete lib;
534
535 node = node->Next();
536 }
537 }
538
539 wxLibrary *wxLibraries::LoadLibrary(const wxString& name)
540 {
541 wxLibrary *lib;
542 wxClassInfo *old_sm_first;
543 wxNode *node = m_loaded.Find(name.GetData());
544
545 if (node != NULL)
546 return ((wxLibrary *)node->Data());
547
548 // If DLL shares data, this is necessary.
549 old_sm_first = wxClassInfo::sm_first;
550 wxClassInfo::sm_first = NULL;
551
552 wxString libname = ConstructLibraryName(name);
553
554 bool success = false;
555 wxDllType handle = wxDllLoader::LoadLibrary(libname, &success);
556 if(success)
557 {
558 lib = new wxLibrary(handle);
559 wxClassInfo::sm_first = old_sm_first;
560
561 m_loaded.Append(name.GetData(), lib);
562 }
563 else
564 lib = NULL;
565 return lib;
566 }
567
568 wxObject *wxLibraries::CreateObject(const wxString& path)
569 {
570 wxNode *node = m_loaded.First();
571 wxObject *obj;
572
573 while (node) {
574 obj = ((wxLibrary *)node->Data())->CreateObject(path);
575 if (obj)
576 return obj;
577
578 node = node->Next();
579 }
580 return NULL;
581 }
582
583 #endif // WXWIN_COMPATIBILITY_2_2
584
585
586 #endif // wxUSE_DYNAMIC_LOADER
587