]>
git.saurik.com Git - wxWidgets.git/blob - src/common/dynload.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/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)
9 // Copyright: (c) 2001 Ron Lee <ron@debian.org>
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
13 // ----------------------------------------------------------------------------
15 // ----------------------------------------------------------------------------
17 #include "wx/wxprec.h"
23 #if wxUSE_DYNAMIC_LOADER
26 #include "wx/msw/private.h"
34 #include "wx/module.h"
37 #include "wx/strconv.h"
39 #include "wx/dynload.h"
42 // ---------------------------------------------------------------------------
44 // ---------------------------------------------------------------------------
47 wxDLImports
* wxPluginLibrary::ms_classes
= NULL
;
49 class wxPluginLibraryModule
: public wxModule
52 wxPluginLibraryModule() { }
54 // TODO: create ms_classes on demand, why always preallocate it?
57 wxPluginLibrary::ms_classes
= new wxDLImports
;
58 wxPluginManager::CreateManifest();
64 wxDELETE(wxPluginLibrary::ms_classes
);
65 wxPluginManager::ClearManifest();
69 DECLARE_DYNAMIC_CLASS(wxPluginLibraryModule
)
72 IMPLEMENT_DYNAMIC_CLASS(wxPluginLibraryModule
, wxModule
)
75 wxPluginLibrary::wxPluginLibrary(const wxString
&libname
, int flags
)
79 m_before
= wxClassInfo::GetFirst();
80 Load( libname
, flags
);
81 m_after
= wxClassInfo::GetFirst();
90 // Flag us for deletion
95 wxPluginLibrary::~wxPluginLibrary()
104 wxPluginLibrary
*wxPluginLibrary::RefLib()
106 wxCHECK_MSG( m_linkcount
> 0, NULL
,
107 wxT("Library had been already deleted!") );
113 bool wxPluginLibrary::UnrefLib()
115 wxASSERT_MSG( m_objcount
== 0,
116 wxT("Library unloaded before all objects were destroyed") );
118 if ( m_linkcount
== 0 || --m_linkcount
== 0 )
127 // ------------------------
129 // ------------------------
131 void wxPluginLibrary::UpdateClasses()
133 for (const wxClassInfo
*info
= m_after
; info
!= m_before
; info
= info
->GetNext())
135 if( info
->GetClassName() )
137 // Hash all the class names into a local table too so
138 // we can quickly find the entry they correspond to.
139 (*ms_classes
)[info
->GetClassName()] = this;
144 void wxPluginLibrary::RestoreClasses()
146 // Check if there is a need to restore classes.
150 for(const wxClassInfo
*info
= m_after
; info
!= m_before
; info
= info
->GetNext())
152 ms_classes
->erase(ms_classes
->find(info
->GetClassName()));
156 void wxPluginLibrary::RegisterModules()
158 // Plugin libraries might have wxModules, Register and initialise them if
161 // Note that these classes are NOT included in the reference counting since
162 // it's implicit that they will be unloaded if and when the last handle to
163 // the library is. We do have to keep a copy of the module's pointer
164 // though, as there is currently no way to Unregister it without it.
166 wxASSERT_MSG( m_linkcount
== 1,
167 wxT("RegisterModules should only be called for the first load") );
169 for ( const wxClassInfo
*info
= m_after
; info
!= m_before
; info
= info
->GetNext())
171 if( info
->IsKindOf(CLASSINFO(wxModule
)) )
173 wxModule
*m
= wxDynamicCast(info
->CreateObject(), wxModule
);
175 wxASSERT_MSG( m
, wxT("wxDynamicCast of wxModule failed") );
177 m_wxmodules
.push_back(m
);
178 wxModule::RegisterModule(m
);
182 // FIXME: Likewise this is (well was) very similar to InitializeModules()
184 for ( wxModuleList::iterator it
= m_wxmodules
.begin();
185 it
!= m_wxmodules
.end();
190 wxLogDebug(wxT("wxModule::Init() failed for wxPluginLibrary"));
192 // XXX: Watch this, a different hash implementation might break it,
193 // a good hash implementation would let us fix it though.
195 // The name of the game is to remove any uninitialised modules and
196 // let the dtor Exit the rest on shutdown, (which we'll initiate
199 wxModuleList::iterator oldNode
= m_wxmodules
.end();
202 if( oldNode
!= m_wxmodules
.end() )
203 m_wxmodules
.erase(oldNode
);
204 wxModule::UnregisterModule( *it
);
206 } while( it
!= m_wxmodules
.end() );
208 --m_linkcount
; // Flag us for deletion
214 void wxPluginLibrary::UnregisterModules()
216 wxModuleList::iterator it
;
218 for ( it
= m_wxmodules
.begin(); it
!= m_wxmodules
.end(); ++it
)
221 for ( it
= m_wxmodules
.begin(); it
!= m_wxmodules
.end(); ++it
)
222 wxModule::UnregisterModule( *it
);
224 // NB: content of the list was deleted by UnregisterModule calls above:
229 // ---------------------------------------------------------------------------
231 // ---------------------------------------------------------------------------
233 wxDLManifest
* wxPluginManager::ms_manifest
= NULL
;
235 // ------------------------
237 // ------------------------
240 wxPluginManager::LoadLibrary(const wxString
&libname
, int flags
)
242 wxString
realname(libname
);
244 if( !(flags
& wxDL_VERBATIM
) )
245 realname
+= wxDynamicLibrary::GetDllExt();
247 wxPluginLibrary
*entry
;
249 if ( flags
& wxDL_NOSHARE
)
255 entry
= FindByName(realname
);
260 wxLogTrace(wxT("dll"),
261 wxT("LoadLibrary(%s): already loaded."), realname
.c_str());
267 entry
= new wxPluginLibrary( libname
, flags
);
269 if ( entry
->IsLoaded() )
271 (*ms_manifest
)[realname
] = entry
;
273 wxLogTrace(wxT("dll"),
274 wxT("LoadLibrary(%s): loaded ok."), realname
.c_str());
279 wxLogTrace(wxT("dll"),
280 wxT("LoadLibrary(%s): failed to load."), realname
.c_str());
282 // we have created entry just above
283 if ( !entry
->UnrefLib() )
285 // ... so UnrefLib() is supposed to delete it
286 wxFAIL_MSG( wxT("Currently linked library is not loaded?") );
296 bool wxPluginManager::UnloadLibrary(const wxString
& libname
)
298 wxString realname
= libname
;
300 wxPluginLibrary
*entry
= FindByName(realname
);
304 realname
+= wxDynamicLibrary::GetDllExt();
306 entry
= FindByName(realname
);
311 wxLogDebug(wxT("Attempt to unload library '%s' which is not loaded."),
317 wxLogTrace(wxT("dll"), wxT("UnloadLibrary(%s)"), realname
.c_str());
319 if ( !entry
->UnrefLib() )
321 // not really unloaded yet
325 ms_manifest
->erase(ms_manifest
->find(realname
));
330 // ------------------------
331 // Class implementation
332 // ------------------------
334 bool wxPluginManager::Load(const wxString
&libname
, int flags
)
336 m_entry
= wxPluginManager::LoadLibrary(libname
, flags
);
341 void wxPluginManager::Unload()
343 wxCHECK_RET( m_entry
, wxT("unloading an invalid wxPluginManager?") );
345 for ( wxDLManifest::iterator i
= ms_manifest
->begin();
346 i
!= ms_manifest
->end();
349 if ( i
->second
== m_entry
)
351 ms_manifest
->erase(i
);
361 #endif // wxUSE_DYNAMIC_LOADER