]> git.saurik.com Git - wxWidgets.git/blob - src/common/dynlib.cpp
Removed redundant makefiles and AIAI icons. Changed dynamic sample source name from
[wxWidgets.git] / src / common / dynlib.cpp
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 #ifdef __GNUG__
13 #pragma implementation "dynlib.h"
14 #endif
15
16 #include <wx/dynlib.h>
17 #include <wx/filefn.h>
18 #include <wx/list.h>
19 #include <wx/string.h>
20
21 // ---------------------------------------------------------------------------
22 // System dependent include
23 // ---------------------------------------------------------------------------
24
25 #ifdef __UNIX__
26 #include <dlfcn.h>
27 #endif
28
29 #ifdef __WINDOWS__
30 #include <windows.h>
31 #endif
32
33 // ---------------------------------------------------------------------------
34 // Global variables
35 // ---------------------------------------------------------------------------
36
37 wxLibraries wxTheLibraries;
38
39 // ---------------------------------------------------------------------------
40 // wxLibrary (one instance per dynamic library
41 // ---------------------------------------------------------------------------
42
43 wxLibrary::wxLibrary(void *handle)
44 {
45 typedef wxClassLibrary *(*t_get_list)(void);
46 t_get_list get_list;
47
48 m_handle = handle;
49
50 get_list = (t_get_list)GetSymbol("GetClassList");
51 m_liblist = (*get_list)();
52 }
53
54 wxLibrary::~wxLibrary()
55 {
56 if (m_handle) {
57 typedef void (*t_free_list)(wxClassLibrary *);
58 t_free_list free_list;
59
60 free_list = (t_free_list) GetSymbol("FreeClassList");
61 if (free_list != NULL)
62 free_list(m_liblist);
63 else
64 delete m_liblist;
65
66 #ifdef __UNIX__
67 dlclose(m_handle);
68 #endif
69 #ifdef __WINDOWS__
70 FreeLibrary((HMODULE)m_handle);
71 #endif
72 }
73 }
74
75 wxObject *wxLibrary::CreateObject(const wxString& name)
76 {
77 return m_liblist->CreateObject(name);
78 }
79
80 void *wxLibrary::GetSymbol(const wxString& symbname)
81 {
82 #ifdef __UNIX__
83 return dlsym(m_handle, WXSTRINGCAST symbname);
84 #endif
85 #ifdef __WINDOWS__
86 return GetProcAddress(m_handle, WXSTRINGCAST symbname);
87 #endif
88 return NULL;
89 }
90
91 // ---------------------------------------------------------------------------
92 // wxLibraries (only one instance should normally exist)
93 // ---------------------------------------------------------------------------
94
95 wxLibraries::wxLibraries()
96 {
97 }
98
99 wxLibraries::~wxLibraries()
100 {
101 wxNode *node = m_loaded.First();
102
103 while (node) {
104 wxLibrary *lib = (wxLibrary *)node->Data();
105 delete lib;
106
107 node = node->Next();
108 }
109 }
110
111 wxLibrary *wxLibraries::LoadLibrary(const wxString& name)
112 {
113 wxString lib_name = name;
114 wxNode *node;
115 wxLibrary *lib;
116
117 if ( (node = m_loaded.Find(name.GetData())) )
118 return ((wxLibrary *)node->Data());
119
120 #ifdef __UNIX__
121 lib_name.Prepend("lib");
122 lib_name += ".so";
123
124 printf("lib_name = %s\n", WXSTRINGCAST lib_name);
125
126 void *handle = dlopen(WXSTRINGCAST lib_name, RTLD_LAZY);
127
128 if (!handle)
129 return NULL;
130 #endif
131 #ifdef __WINDOWS__
132 lib_name += ".dll";
133
134 HMODULE handle = LoadLibrary(lib_name);
135 if (!handle)
136 return NULL;
137 #endif
138 lib = new wxLibrary((void *)handle);
139
140 m_loaded.Append(name.GetData(), lib);
141 return lib;
142 }
143
144 wxObject *wxLibraries::CreateObject(const wxString& path)
145 {
146 wxNode *node = m_loaded.First();
147 wxObject *obj;
148
149 while (node) {
150 obj = ((wxLibrary *)node->Data())->CreateObject(path);
151 if (obj)
152 return obj;
153
154 node = node->Next();
155 }
156 return NULL;
157 }
158
159 // ---------------------------------------------------------------------------
160 // wxClassLibrary (this class is used to access the internal class)
161 // ---------------------------------------------------------------------------
162
163 wxClassLibrary::wxClassLibrary(void)
164 {
165 }
166
167 wxClassLibrary::~wxClassLibrary(void)
168 {
169 size_t i;
170
171 for (i=0;i<m_list.Count();i++)
172 delete (m_list[i]);
173 }
174
175 void wxClassLibrary::RegisterClass(wxClassInfo *class_info,
176 const wxString& path)
177 {
178 wxClassLibInfo *info = new wxClassLibInfo;
179
180 info->class_info = class_info;
181 info->path = path;
182 m_list.Add(info);
183 }
184
185 void wxClassLibrary::UnregisterClass(wxClassInfo *class_info)
186 {
187 size_t i = 0;
188
189 while (i < m_list.Count()) {
190 if (m_list[i]->class_info == class_info) {
191 delete (m_list[i]);
192 m_list.Remove(i);
193 return;
194 }
195 i++;
196 }
197 }
198
199 bool wxClassLibrary::CreateObjects(const wxString& path,
200 wxArrayClassInfo& objs)
201 {
202 wxClassLibInfo *info;
203 size_t i = 0;
204
205 while (i < m_list.Count()) {
206 info = m_list[i];
207 if (wxMatchWild(path, info->path))
208 objs.Add(info->class_info);
209 i++;
210 }
211 return (i > 0);
212 }
213
214 bool wxClassLibrary::FetchInfos(const wxString& path,
215 wxArrayClassLibInfo& infos)
216 {
217 wxClassLibInfo *info;
218 size_t i = 0;
219
220 while (i < m_list.Count()) {
221 info = m_list[i];
222 if (wxMatchWild(path, info->path)) {
223 wxClassLibInfo *inf = new wxClassLibInfo;
224 *inf = *info;
225 infos.Add(inf);
226 }
227 i++;
228 }
229 return (i > 0);
230 }
231
232 wxObject *wxClassLibrary::CreateObject(const wxString& path)
233 {
234 wxClassLibInfo *info;
235 size_t i = 0;
236
237 while (i < m_list.Count()) {
238 info = m_list[i];
239 if (wxMatchWild(path, info->path))
240 return info->class_info->CreateObject();
241 i++;
242 }
243 return NULL;
244 }