added wxDynamicLibrary as suggested by Vadim
[wxWidgets.git] / include / wx / dynlib.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: dynlib.cpp
3 // Purpose: Dynamic library management
4 // Author: Guilhem Lavaux
5 // Modified by:
6 // Created: 20/07/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Guilhem Lavaux
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_DYNLIB_H__
13 #define _WX_DYNLIB_H__
14
15 #ifdef __GNUG__
16 # pragma interface
17 #endif
18
19 #include "wx/setup.h"
20
21 #if wxUSE_DYNLIB_CLASS
22
23 #include "wx/string.h"
24 #include "wx/list.h"
25 #include "wx/hash.h"
26
27 // this is normally done by configure, but I leave it here for now...
28 #if defined(__UNIX__) && !(defined(HAVE_DLOPEN) || defined(HAVE_SHL_LOAD))
29 # if defined(__LINUX__) || defined(__SOLARIS__) || defined(__SUNOS__) || defined(__FREEBSD__)
30 # define HAVE_DLOPEN
31 # elif defined(__HPUX__)
32 # define HAVE_SHL_LOAD
33 # endif // Unix flavour
34 #endif // !Unix or already have some HAVE_xxx defined
35
36 // Note: WXPM/EMX has to be tested first, since we want to use
37 // native version, even if configure detected presence of DLOPEN.
38 #if defined(__WXPM__) || defined(__EMX__)
39 # define INCL_DOS
40 # include <os2.h>
41 typedef HMODULE wxDllType;
42 #elif defined(HAVE_DLOPEN)
43 # include <dlfcn.h>
44 typedef void *wxDllType;
45 #elif defined(HAVE_SHL_LOAD)
46 # include <dl.h>
47 typedef shl_t wxDllType;
48 #elif defined(__WINDOWS__)
49 # include <windows.h> // needed to get HMODULE
50 typedef HMODULE wxDllType;
51 #elif defined(__APPLE__) && defined(__UNIX__)
52 typedef void *wxDllType;
53 #elif defined(__WXMAC__)
54 typedef CFragConnectionID wxDllType;
55 #else
56 # error "wxLibrary can't be compiled on this platform, sorry."
57 #endif // OS
58
59 // LoadLibrary is defined in windows.h as LoadLibraryA, but wxDllLoader method
60 // should be called LoadLibrary, not LoadLibraryA or LoadLibraryW!
61 #if defined(__WIN32__) && defined(LoadLibrary)
62 # include "wx/msw/winundef.h"
63 #endif
64
65 // ----------------------------------------------------------------------------
66 // wxDllLoader
67 // ----------------------------------------------------------------------------
68
69 /** wxDllLoader is a class providing an interface similar to unix's
70 dlopen(). It is used by the wxLibrary framework and manages the
71 actual loading of DLLs and the resolving of symbols in them.
72 There are no instances of this class, it simply serves as a
73 namespace for its static member functions.
74 */
75 class WXDLLEXPORT wxDllLoader
76 {
77 public:
78 /** This function loads a shared library into memory, with libname
79 being the basename of the library, without the filename
80 extension. No initialisation of the library will be done.
81 @param libname Name of the shared object to load.
82 @param success Must point to a bool variable which will be set to TRUE or FALSE.
83 @return A handle to the loaded DLL. Use success parameter to test if it is valid.
84 */
85 static wxDllType LoadLibrary(const wxString & libname, bool *success = NULL);
86 /** This function unloads the shared library. */
87 static void UnloadLibrary(wxDllType dll);
88 /** This function returns a valid handle for the main program
89 itself. */
90 static wxDllType GetProgramHandle(void);
91 /** This function resolves a symbol in a loaded DLL, such as a
92 variable or function name.
93 @param dllHandle Handle of the DLL, as returned by LoadDll().
94 @param name Name of the symbol.
95 @return A pointer to the symbol.
96 */
97 static void * GetSymbol(wxDllType dllHandle, const wxString &name);
98
99 // return the standard DLL extension (with leading dot) for this platform
100 static wxString GetDllExt();
101
102 private:
103 /// forbid construction of objects
104 wxDllLoader();
105 };
106
107 // ----------------------------------------------------------------------------
108 // wxDynamicLibrary - friendly interface to wxDllLoader
109 // ----------------------------------------------------------------------------
110
111 class wxDynamicLibrary
112 {
113 public:
114 wxDynamicLibrary() { m_library = 0; }
115 wxDynamicLibrary(const wxString& name) { Load(name); }
116
117 bool IsLoaded() const { return m_library != 0; }
118
119 bool Load(const wxString& name)
120 {
121 m_library = wxDllLoader::LoadLibrary(name);
122
123 return IsLoaded();
124 }
125
126 void Unload()
127 {
128 if ( IsLoaded() )
129 wxDllLoader::UnloadLibrary(m_library)
130 }
131
132 void *GetSymbol(const wxString& name) const
133 {
134 wxCHECK_MSG( IsLoaded(), NULL,
135 _T("can't load symbol from unloaded library") );
136
137 return wxDllLoader::GetSymbol(m_library, name);
138 }
139
140 ~wxDynamicLibrary() { Unload(); }
141
142 private:
143 wxDllType m_library;
144 };
145
146
147 // ----------------------------------------------------------------------------
148 // wxLibrary
149 // ----------------------------------------------------------------------------
150
151 class WXDLLEXPORT wxLibrary : public wxObject
152 {
153 public:
154 wxHashTable classTable;
155
156 public:
157 wxLibrary(wxDllType handle);
158 ~wxLibrary();
159
160 // Get a symbol from the dynamic library
161 void *GetSymbol(const wxString& symbname);
162
163 // Create the object whose classname is "name"
164 wxObject *CreateObject(const wxString& name);
165
166 protected:
167 void PrepareClasses(wxClassInfo *first);
168
169 wxDllType m_handle;
170 };
171
172
173
174 // ----------------------------------------------------------------------------
175 // wxLibraries
176 // ----------------------------------------------------------------------------
177
178 class WXDLLEXPORT wxLibraries
179 {
180 public:
181 wxLibraries();
182 ~wxLibraries();
183
184 // caller is responsible for deleting the returned pointer if !NULL
185 wxLibrary *LoadLibrary(const wxString& basename);
186
187 wxObject *CreateObject(const wxString& name);
188
189 protected:
190 wxList m_loaded;
191 };
192
193 // ----------------------------------------------------------------------------
194 // Global variables
195 // ----------------------------------------------------------------------------
196
197 extern wxLibraries wxTheLibraries;
198
199 // ----------------------------------------------------------------------------
200 // Interesting defines
201 // ----------------------------------------------------------------------------
202
203 #define WXDLL_ENTRY_FUNCTION() \
204 extern "C" wxClassInfo *wxGetClassFirst(); \
205 wxClassInfo *wxGetClassFirst() { \
206 return wxClassInfo::GetFirst(); \
207 }
208
209 #endif // wxUSE_DYNLIB_CLASS
210
211 #endif // _WX_DYNLIB_H__