1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
| 1| #ifndef DYNAMIC_LIBRARY_HANDLE_HPP
2| #define DYNAMIC_LIBRARY_HANDLE_HPP
3|
4|
5| #include <stdexcept> // for std::runtime_error
6| #include <string>
7|
8| #include <dlfcn.h>
9|
10| #include "dynamic_library_error.hpp"
11|
12|
13|
14| class DynamicLibraryHandle
15| {
16|
17|
18| //******************************* NESTED TYPES *******************************//
19|
20| public:
21| enum OpenMode {Lazy = RTLD_LAZY, Now = RTLD_NOW};
22|
23| enum Flag {Global = RTLD_GLOBAL,
24| Local = RTLD_LOCAL,
25| NoDelete = RTLD_NODELETE,
26| NoLoad = RTLD_NOLOAD,
27| DeepBind = RTLD_DEEPBIND};
28|
29| #ifdef _GNU_SOURCE
30| enum PseudoHandle {Default, Next};
31| #endif
32|
33|
34|
35| //************************** STATIC MEMBER VARIABLES *************************//
36|
37| #ifdef _GNU_SOURCE
38| private:
39| static
40| const DynamicLibraryHandle _default;
41|
42| static
43| const DynamicLibraryHandle _next;
44| #endif
45|
46|
47| //***************************** MEMBER VARIABLES *****************************//
48|
49| private:
50| void *_opaqueHandle;
51|
52| #ifdef _GNU_SOURCE
53| bool _pseudoHandle;
54| #endif
55|
56|
57|
58| //************************** STATIC MEMBER FUNCTIONS *************************//
59|
60| #ifdef _GNU_SOURCE
61| public:
62| template <typename SymbolType>
63| static
64| SymbolType searchSymbol(PseudoHandle __ph, const std::string& symbol)
65| throw(DynamicLibraryError);
66|
67| static
68| const DynamicLibraryHandle& pseudoHandle(PseudoHandle __ph) throw();
69| #endif
70|
71|
72| //******************************* CONSTRUCTORS *******************************//
73|
74| public:
75| DynamicLibraryHandle() throw() : _opaqueHandle()
76| #ifdef _GNU_SOURCE
77| , _pseudoHandle(false)
78| #endif
79| {}
80|
81| explicit inline
82| DynamicLibraryHandle(OpenMode mode, int flags = 0)
83| throw(DynamicLibraryError);
84|
85| inline
86| DynamicLibraryHandle(const std::string& filename,
87| OpenMode mode,
88| int flags = 0)
89| throw(DynamicLibraryError);
90|
91| private:
92| #ifdef _GNU_SOURCE
93| DynamicLibraryHandle(void *pseudoHandle) throw()
94| : _opaqueHandle(pseudoHandle), _pseudoHandle(true)
95| {}
96| #endif
97|
98| DynamicLibraryHandle(const DynamicLibraryHandle&);
99|
100|
101| //******************************** DESTRUCTOR ********************************//
102|
103| public:
104| ~DynamicLibraryHandle() throw();
105|
106|
107| //********************************** GETTERS *********************************//
108|
109| inline
110| bool isOpened() const throw();
111|
112| #ifdef _GNU_SOURCE
113| inline
114| bool isPseudoHandler() const throw();
115| #endif
116|
117|
118| //********************************* OPERATORS ********************************//
119|
120| private:
121| DynamicLibraryHandle& operator = (const DynamicLibraryHandle&);
122|
123|
124| //************************** OTHER MEMBER FUNCTIONS **************************//
125|
126| void open(const char *filename, int flags) throw(DynamicLibraryError);
127|
128| public:
129| inline
130| void open(OpenMode mode, int flags = 0) throw(DynamicLibraryError);
131|
132| inline
133| void open(const std::string& filename, OpenMode mode, int flags = 0)
134| throw(DynamicLibraryError);
135|
136| void close() throw(DynamicLibraryCloseError);
137|
138| template <typename SymbolType>
139| SymbolType searchSymbol(const std::string& symbol) const
140| throw(std::runtime_error, DynamicLibraryError);
141|
142| void swap(DynamicLibraryHandle& other) throw();
143|
144|
145| }; // class DynamicLibraryHandle
146|
147|
148|
149|
150|
151| //************************** STATIC MEMBER FUNCTIONS *************************//
152|
153|
154| #ifdef _GNU_SOURCE
155|
156| template <typename SymbolType>
157| SymbolType DynamicLibraryHandle::searchSymbol(PseudoHandle __ph, const std::string& symbol)
158| throw(DynamicLibraryError)
159| {
160| return pseudoHandle(__ph).searchSymbol<SymbolType>(symbol);
161| }
162|
163| #endif
164|
165|
166|
167| //******************************* CONSTRUCTORS *******************************//
168|
169|
170| inline
171| DynamicLibraryHandle::DynamicLibraryHandle(OpenMode mode, int flags) throw(DynamicLibraryError)
172| : _opaqueHandle()
173| #ifdef _GNU_SOURCE
174| , _pseudoHandle(false)
175| #endif
176| {
177| open(mode, flags);
178| }
179|
180|
181| inline
182| DynamicLibraryHandle::DynamicLibraryHandle(const std::string& filename, OpenMode mode, int flags)
183| throw(DynamicLibraryError)
184| : _opaqueHandle()
185| #ifdef _GNU_SOURCE
186| , _pseudoHandle(false)
187| #endif
188| {
189| open(filename, mode, flags);
190| }
191|
192|
193|
194| //********************************** GETTERS *********************************//
195|
196|
197| inline
198| bool DynamicLibraryHandle::isOpened() const throw()
199| {
200| return (_opaqueHandle != NULL);
201| }
202|
203|
204| #ifdef _GNU_SOURCE
205|
206| inline
207| bool DynamicLibraryHandle::isPseudoHandler() const throw()
208| {
209| return _pseudoHandle;
210| }
211|
212| #endif
213|
214|
215|
216| //************************** OTHER MEMBER FUNCTIONS **************************//
217|
218|
219| inline
220| void DynamicLibraryHandle::open(OpenMode mode, int flags) throw(DynamicLibraryError)
221| {
222| return open(NULL, mode | flags);
223| }
224|
225| inline
226| void DynamicLibraryHandle::open(const std::string& filename, OpenMode mode, int flags)
227| throw(DynamicLibraryError)
228| {
229| return open(filename.c_str(), mode | flags);
230| }
231|
232|
233| template <typename SymbolType>
234| SymbolType DynamicLibraryHandle::searchSymbol(const std::string& symbol) const
235| throw(std::runtime_error, DynamicLibraryError)
236| {
237| #ifdef _GNU_SOURCE
238| if (!_pseudoHandle) {
239| #endif
240|
241| if (!_opaqueHandle)
242| throw std::runtime_error("No loaded dynamic library.");
243|
244| #ifdef _GNU_SOURCE
245| }
246| #endif
247|
248| dlerror();
249|
250| SymbolType ret;
251| *reinterpret_cast<void **>( &ret ) = dlsym(_opaqueHandle, symbol.c_str());
252|
253| char *error = dlerror();
254| if (error)
255| throw DynamicLibraryError(error);
256|
257| return ret;
258| }
259|
260|
261|
262| //*************************** NON-MEMBER FUNCTIONS ***************************//
263|
264|
265| inline
266| void swap(DynamicLibraryHandle& x, DynamicLibraryHandle& y) throw()
267| {
268| return x.swap(y);
269| }
270|
271|
272|
273| #endif // DYNAMIC_LIBRARY_HANDLE_HPP |
Partager