]> git.saurik.com Git - wxWidgets.git/blob - src/common/dynlib.cpp
* wxCreateDynamicObject() uses an hashtable now
[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 wxClassInfo **(*t_get_first)(void);
46 t_get_first get_first;
47
48 m_handle = handle;
49 m_destroy = TRUE;
50
51 // Some system may use a local heap for library.
52 get_first = (t_get_first)GetSymbol("wxGetClassFirst");
53 // It is a wxWindows DLL.
54 if (get_first)
55 PrepareClasses(get_first());
56 }
57
58 wxLibrary::~wxLibrary()
59 {
60 if (m_handle && m_destroy) {
61 #ifdef __UNIX__
62 dlclose(m_handle);
63 #endif
64 #ifdef __WINDOWS__
65 FreeLibrary((HMODULE)m_handle);
66 #endif
67 }
68 }
69
70 wxObject *wxLibrary::CreateObject(const wxString& name)
71 {
72 wxClassInfo *info = (wxClassInfo *)classTable.Get(name);
73
74 if (!info)
75 return NULL;
76
77 return info->CreateObject();
78 }
79
80 void wxLibrary::PrepareClasses(wxClassInfo **first)
81 {
82 // Index all class infos by their class name
83 wxClassInfo *info = *first;
84 while (info)
85 {
86 if (info->className)
87 classTable.Put(info->className, (wxObject *)info);
88 info = info->next;
89 }
90
91 // Set base pointers for each wxClassInfo
92 info = *first;
93 while (info)
94 {
95 if (info->GetBaseClassName1())
96 info->baseInfo1 = (wxClassInfo *)classTable.Get(info->GetBaseClassName1());
97 if (info->GetBaseClassName2())
98 info->baseInfo2 = (wxClassInfo *)classTable.Get(info->GetBaseClassName2());
99 info = info->next;
100 }
101 *first = NULL;
102 }
103
104 void *wxLibrary::GetSymbol(const wxString& symbname)
105 {
106 #ifdef __UNIX__
107 return dlsym(m_handle, WXSTRINGCAST symbname);
108 #endif
109 #ifdef __WINDOWS__
110 return GetProcAddress(m_handle, WXSTRINGCAST symbname);
111 #endif
112 return NULL;
113 }
114
115 // ---------------------------------------------------------------------------
116 // wxLibraries (only one instance should normally exist)
117 // ---------------------------------------------------------------------------
118
119 wxLibraries::wxLibraries()
120 {
121 }
122
123 wxLibraries::~wxLibraries()
124 {
125 wxNode *node = m_loaded.First();
126
127 while (node) {
128 wxLibrary *lib = (wxLibrary *)node->Data();
129 delete lib;
130
131 node = node->Next();
132 }
133 }
134
135 wxLibrary *wxLibraries::LoadLibrary(const wxString& name)
136 {
137 wxString lib_name = name;
138 wxNode *node;
139 wxLibrary *lib;
140
141 if ( (node = m_loaded.Find(name.GetData())) )
142 return ((wxLibrary *)node->Data());
143
144 #if defined(__UNIX__)
145 lib_name.Prepend("./lib");
146 lib_name += ".so";
147
148 printf("lib_name = %s\n", WXSTRINGCAST lib_name);
149
150 void *handle = dlopen(WXSTRINGCAST lib_name, RTLD_LAZY);
151
152 if (!handle)
153 return NULL;
154 #elif defined(__WINDOWS__)
155 lib_name += ".dll";
156
157 HMODULE handle = LoadLibrary(lib_name);
158 if (!handle)
159 return NULL;
160 #else
161 return NULL;
162 #endif
163
164 lib = new wxLibrary((void *)handle);
165
166 m_loaded.Append(name.GetData(), lib);
167 return lib;
168 }
169
170 wxObject *wxLibraries::CreateObject(const wxString& path)
171 {
172 wxNode *node = m_loaded.First();
173 wxObject *obj;
174
175 while (node) {
176 obj = ((wxLibrary *)node->Data())->CreateObject(path);
177 if (obj)
178 return obj;
179
180 node = node->Next();
181 }
182 return NULL;
183 }