]> git.saurik.com Git - wxWidgets.git/blame - src/common/dynlib.cpp
Added Latex style file
[wxWidgets.git] / src / common / dynlib.cpp
CommitLineData
7a4b9130
GL
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
77ff2d26 25#ifdef __UNIX__
7a4b9130
GL
26#include <dlfcn.h>
27#endif
28
123a7fdd
GL
29#ifdef __WINDOWS__
30#include <windows.h>
31#endif
32
7a4b9130
GL
33// ---------------------------------------------------------------------------
34// Global variables
35// ---------------------------------------------------------------------------
36
37wxLibraries wxTheLibraries;
38
39// ---------------------------------------------------------------------------
40// wxLibrary (one instance per dynamic library
41// ---------------------------------------------------------------------------
42
43wxLibrary::wxLibrary(void *handle)
44{
f4a8c29f
GL
45 typedef wxClassInfo **(*t_get_first)(void);
46 t_get_first get_first;
7a4b9130
GL
47
48 m_handle = handle;
f4a8c29f 49 m_destroy = TRUE;
7a4b9130 50
f4a8c29f
GL
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());
7a4b9130
GL
56}
57
58wxLibrary::~wxLibrary()
59{
f4a8c29f 60 if (m_handle && m_destroy) {
77ff2d26 61#ifdef __UNIX__
7a4b9130 62 dlclose(m_handle);
123a7fdd
GL
63#endif
64#ifdef __WINDOWS__
65 FreeLibrary((HMODULE)m_handle);
66#endif
7a4b9130
GL
67 }
68}
69
70wxObject *wxLibrary::CreateObject(const wxString& name)
71{
f4a8c29f
GL
72 wxClassInfo *info = (wxClassInfo *)classTable.Get(name);
73
74 if (!info)
75 return NULL;
76
77 return info->CreateObject();
78}
79
80void 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;
7a4b9130
GL
102}
103
104void *wxLibrary::GetSymbol(const wxString& symbname)
105{
77ff2d26 106#ifdef __UNIX__
123a7fdd 107 return dlsym(m_handle, WXSTRINGCAST symbname);
7a4b9130 108#endif
123a7fdd
GL
109#ifdef __WINDOWS__
110 return GetProcAddress(m_handle, WXSTRINGCAST symbname);
111#endif
112 return NULL;
7a4b9130
GL
113}
114
115// ---------------------------------------------------------------------------
116// wxLibraries (only one instance should normally exist)
117// ---------------------------------------------------------------------------
118
119wxLibraries::wxLibraries()
120{
121}
122
123wxLibraries::~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
135wxLibrary *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
f4a8c29f 144#if defined(__UNIX__)
1d44aaf8 145 lib_name.Prepend("./lib");
7a4b9130
GL
146 lib_name += ".so";
147
148 printf("lib_name = %s\n", WXSTRINGCAST lib_name);
149
123a7fdd 150 void *handle = dlopen(WXSTRINGCAST lib_name, RTLD_LAZY);
7a4b9130 151
123a7fdd
GL
152 if (!handle)
153 return NULL;
f4a8c29f 154#elif defined(__WINDOWS__)
7a4b9130
GL
155 lib_name += ".dll";
156
123a7fdd
GL
157 HMODULE handle = LoadLibrary(lib_name);
158 if (!handle)
159 return NULL;
f4a8c29f
GL
160#else
161 return NULL;
7a4b9130 162#endif
f4a8c29f 163
123a7fdd 164 lib = new wxLibrary((void *)handle);
7a4b9130
GL
165
166 m_loaded.Append(name.GetData(), lib);
167 return lib;
168}
169
170wxObject *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}