]> git.saurik.com Git - wxWidgets.git/blob - include/wx/dynload.h
Make storing non-trivial data in wxThreadSpecificInfo possible.
[wxWidgets.git] / include / wx / dynload.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/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 // Copyright: (c) 2001 Ron Lee <ron@debian.org>
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_DYNAMICLOADER_H__
13 #define _WX_DYNAMICLOADER_H__
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 #include "wx/defs.h"
20
21 #if wxUSE_DYNAMIC_LOADER
22
23 #include "wx/dynlib.h"
24 #include "wx/hashmap.h"
25 #include "wx/module.h"
26
27 class WXDLLIMPEXP_FWD_BASE wxPluginLibrary;
28
29
30 WX_DECLARE_STRING_HASH_MAP_WITH_DECL(wxPluginLibrary *, wxDLManifest,
31 class WXDLLIMPEXP_BASE);
32 typedef wxDLManifest wxDLImports;
33
34 // ---------------------------------------------------------------------------
35 // wxPluginLibrary
36 // ---------------------------------------------------------------------------
37
38 // NOTE: Do not attempt to use a base class pointer to this class.
39 // wxDL is not virtual and we deliberately hide some of it's
40 // methods here.
41 //
42 // Unless you know exacty why you need to, you probably shouldn't
43 // instantiate this class directly anyway, use wxPluginManager
44 // instead.
45
46 class WXDLLIMPEXP_BASE wxPluginLibrary : public wxDynamicLibrary
47 {
48 public:
49
50 static wxDLImports* ms_classes; // Static hash of all imported classes.
51
52 wxPluginLibrary( const wxString &libname, int flags = wxDL_DEFAULT );
53 ~wxPluginLibrary();
54
55 wxPluginLibrary *RefLib();
56 bool UnrefLib();
57
58 // These two are called by the PluginSentinel on (PLUGGABLE) object
59 // creation/destruction. There is usually no reason for the user to
60 // call them directly. We have to separate this from the link count,
61 // since the two are not interchangeable.
62
63 // FIXME: for even better debugging PluginSentinel should register
64 // the name of the class created too, then we can state
65 // exactly which object was not destroyed which may be
66 // difficult to find otherwise. Also this code should
67 // probably only be active in DEBUG mode, but let's just
68 // get it right first.
69
70 void RefObj() { ++m_objcount; }
71 void UnrefObj()
72 {
73 wxASSERT_MSG( m_objcount > 0, wxT("Too many objects deleted??") );
74 --m_objcount;
75 }
76
77 // Override/hide some base class methods
78
79 bool IsLoaded() const { return m_linkcount > 0; }
80 void Unload() { UnrefLib(); }
81
82 private:
83
84 // These pointers may be NULL but if they are not, then m_ourLast follows
85 // m_ourFirst in the linked list, i.e. can be found by calling GetNext() a
86 // sufficient number of times.
87 const wxClassInfo *m_ourFirst; // first class info in this plugin
88 const wxClassInfo *m_ourLast; // ..and the last one
89
90 size_t m_linkcount; // Ref count of library link calls
91 size_t m_objcount; // ..and (pluggable) object instantiations.
92 wxModuleList m_wxmodules; // any wxModules that we initialised.
93
94 void UpdateClasses(); // Update ms_classes
95 void RestoreClasses(); // Removes this library from ms_classes
96 void RegisterModules(); // Init any wxModules in the lib.
97 void UnregisterModules(); // Cleanup any wxModules we installed.
98
99 wxDECLARE_NO_COPY_CLASS(wxPluginLibrary);
100 };
101
102
103 class WXDLLIMPEXP_BASE wxPluginManager
104 {
105 public:
106
107 // Static accessors.
108
109 static wxPluginLibrary *LoadLibrary( const wxString &libname,
110 int flags = wxDL_DEFAULT );
111 static bool UnloadLibrary(const wxString &libname);
112
113 // Instance methods.
114
115 wxPluginManager() : m_entry(NULL) {}
116 wxPluginManager(const wxString &libname, int flags = wxDL_DEFAULT)
117 {
118 Load(libname, flags);
119 }
120 ~wxPluginManager() { if ( IsLoaded() ) Unload(); }
121
122 bool Load(const wxString &libname, int flags = wxDL_DEFAULT);
123 void Unload();
124
125 bool IsLoaded() const { return m_entry && m_entry->IsLoaded(); }
126 void *GetSymbol(const wxString &symbol, bool *success = 0)
127 {
128 return m_entry->GetSymbol( symbol, success );
129 }
130
131 static void CreateManifest() { ms_manifest = new wxDLManifest(wxKEY_STRING); }
132 static void ClearManifest() { delete ms_manifest; ms_manifest = NULL; }
133
134 private:
135 // return the pointer to the entry for the library with given name in
136 // ms_manifest or NULL if none
137 static wxPluginLibrary *FindByName(const wxString& name)
138 {
139 const wxDLManifest::iterator i = ms_manifest->find(name);
140
141 return i == ms_manifest->end() ? NULL : i->second;
142 }
143
144 static wxDLManifest* ms_manifest; // Static hash of loaded libs.
145 wxPluginLibrary* m_entry; // Cache our entry in the manifest.
146
147 // We could allow this class to be copied if we really
148 // wanted to, but not without modification.
149 wxDECLARE_NO_COPY_CLASS(wxPluginManager);
150 };
151
152
153 #endif // wxUSE_DYNAMIC_LOADER
154 #endif // _WX_DYNAMICLOADER_H__
155