Mercurial > code
comparison C++/modules/Dynlib/Dynlib.h @ 334:0b576ee64d45
* Create brand new hierarchy
* Rename DynLib to Dynlib
* Remove some warnings
author | David Demelier <markand@malikania.fr> |
---|---|
date | Sun, 08 Mar 2015 14:26:33 +0100 |
parents | C++/DynLib.h@4e17193db141 |
children | d5ec1174b707 |
comparison
equal
deleted
inserted
replaced
333:412ca7a5e1ea | 334:0b576ee64d45 |
---|---|
1 /* | |
2 * DynLib.h -- portable shared library loader | |
3 * | |
4 * Copyright (c) 2013, 2014 David Demelier <markand@malikania.fr> | |
5 * | |
6 * Permission to use, copy, modify, and/or distribute this software for any | |
7 * purpose with or without fee is hereby granted, provided that the above | |
8 * copyright notice and this permission notice appear in all copies. | |
9 * | |
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
17 */ | |
18 | |
19 #ifndef _DYNLIB_H_ | |
20 #define _DYNLIB_H_ | |
21 | |
22 #include <string> | |
23 | |
24 #if defined(_WIN32) | |
25 # include <Windows.h> | |
26 # define DYNLIB_EXPORT __declspec(dllexport) | |
27 #else | |
28 # define DYNLIB_EXPORT | |
29 #endif | |
30 | |
31 /** | |
32 * @class Dynlib | |
33 * @brief Load a dynamic module | |
34 * | |
35 * This class is a portable wrapper to load shared libraries on | |
36 * supported systems. | |
37 */ | |
38 class Dynlib { | |
39 public: | |
40 #if defined(_WIN32) | |
41 using Handle = HMODULE; | |
42 using Sym = FARPROC; | |
43 #else | |
44 using Handle = void *; | |
45 using Sym = void *; | |
46 #endif | |
47 | |
48 enum Policy { | |
49 Immediately, //! load symbols immediately | |
50 Lazy //! load symbols when needed | |
51 }; | |
52 | |
53 private: | |
54 Handle m_handle; | |
55 | |
56 void systemInit(); | |
57 Handle systemLoad(const std::string &path, Policy policy) const; | |
58 Sym systemSym(const std::string &name); | |
59 void systemClose(); | |
60 | |
61 public: | |
62 /** | |
63 * Copy is forbidden. | |
64 */ | |
65 Dynlib(const Dynlib &) = delete; | |
66 Dynlib &operator=(const Dynlib &) = delete; | |
67 | |
68 /** | |
69 * Default constructor. | |
70 */ | |
71 Dynlib(); | |
72 | |
73 /** | |
74 * Constructor to load a shared module. The path must | |
75 * be absolute. | |
76 * | |
77 * @param path the absolute path | |
78 * @param policy the policy to load | |
79 * @throw std::runtime_error on error | |
80 */ | |
81 Dynlib(const std::string &path, Policy policy = Immediately); | |
82 | |
83 /** | |
84 * Close the library automatically. | |
85 */ | |
86 ~Dynlib(); | |
87 | |
88 /** | |
89 * Get a symbol from the library. | |
90 * | |
91 * @param name the symbol | |
92 * @return the symbol | |
93 * @throw std::runtime_error on error | |
94 * @throw std::out_of_range if not found | |
95 */ | |
96 template <typename T> | |
97 T sym(const std::string &name) | |
98 { | |
99 return reinterpret_cast<T>(systemSym(name)); | |
100 } | |
101 }; | |
102 | |
103 #endif // !_DYNLIB_H_ |