]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // Copyright 2015-2016 Klemens D. Morgenstern |
2 | // | |
3 | // Distributed under the Boost Software License, Version 1.0. | |
4 | // (See accompanying file LICENSE_1_0.txt | |
5 | // or copy at http://www.boost.org/LICENSE_1_0.txt) | |
6 | ||
7 | ||
8 | #ifndef BOOST_DLL_IMPORT_MANGLED_HPP_ | |
9 | #define BOOST_DLL_IMPORT_MANGLED_HPP_ | |
10 | ||
11 | #include <boost/config.hpp> | |
12 | #include <boost/make_shared.hpp> | |
13 | #include <boost/move/move.hpp> | |
14 | #include <boost/dll/smart_library.hpp> | |
15 | #include <boost/dll/detail/import_mangled_helpers.hpp> | |
16 | #include <boost/utility/addressof.hpp> | |
17 | #include <boost/utility/enable_if.hpp> | |
18 | #include <boost/type_traits/conditional.hpp> | |
19 | #include <boost/type_traits/is_object.hpp> | |
20 | ||
21 | ||
22 | ||
23 | #ifdef BOOST_HAS_PRAGMA_ONCE | |
24 | # pragma once | |
25 | #endif | |
26 | ||
27 | namespace boost { namespace dll { namespace experimental { | |
28 | ||
29 | namespace detail | |
30 | { | |
31 | ||
32 | ||
33 | ||
34 | template <class ... Ts> | |
35 | class mangled_library_function { | |
36 | // Copying of `boost::dll::shared_library` is very expensive, so we use a `shared_ptr` to make it faster. | |
37 | boost::shared_ptr<function_tuple<Ts...>> f_; | |
38 | ||
39 | public: | |
40 | inline mangled_library_function(const boost::shared_ptr<shared_library>& lib, Ts*... func_ptr) BOOST_NOEXCEPT | |
41 | : f_(lib, new function_tuple<Ts...>(func_ptr...)) | |
42 | {} | |
43 | ||
44 | ||
45 | // Compilation error at this point means that imported function | |
46 | // was called with unmatching parameters. | |
47 | // | |
48 | // Example: | |
49 | // auto f = dll::import_mangled<void(int), void(double)>("function", "lib.so"); | |
50 | // f("Hello"); // error: invalid conversion from 'const char*' to 'int' | |
51 | // f(1, 2); // error: too many arguments to function | |
52 | // f(); // error: too few arguments to function | |
53 | template <class... Args> | |
54 | inline auto operator()(Args&&... args) const | |
55 | -> decltype( (*f_)(static_cast<Args&&>(args)...) ) | |
56 | { | |
57 | return (*f_)(static_cast<Args&&>(args)...); | |
58 | } | |
59 | }; | |
60 | ||
61 | ||
62 | template<class Class, class Sequence> | |
63 | class mangled_library_mem_fn; | |
64 | ||
65 | template <class Class, class ... Ts> | |
66 | class mangled_library_mem_fn<Class, sequence<Ts...>> { | |
67 | // Copying of `boost::dll::shared_library` is very expensive, so we use a `shared_ptr` to make it faster. | |
68 | typedef mem_fn_tuple<Ts...> call_tuple_t; | |
69 | boost::shared_ptr<call_tuple_t> f_; | |
70 | ||
71 | public: | |
72 | inline mangled_library_mem_fn(const boost::shared_ptr<shared_library>& lib, | |
73 | typename Ts::mem_fn... func_ptr) BOOST_NOEXCEPT | |
74 | : f_(lib, new call_tuple_t(func_ptr...)) | |
75 | {} | |
76 | ||
77 | template <class ClassIn, class... Args> | |
78 | inline auto operator()(ClassIn *cl, Args&&... args) const -> decltype( (*f_)(cl, static_cast<Args&&>(args)...) ) | |
79 | { | |
80 | return (*f_)(cl, static_cast<Args&&>(args)...); | |
81 | } | |
82 | }; | |
83 | ||
84 | ||
85 | ||
86 | ||
87 | // simple enough to be here | |
88 | template<class Seq> struct is_variable : boost::false_type {}; | |
89 | template<typename T> struct is_variable<sequence<T>> : boost::is_object<T> {}; | |
90 | ||
91 | template <class Sequence, | |
92 | bool isFunction = is_function_seq<Sequence>::value, | |
93 | bool isMemFn = is_mem_fn_seq <Sequence>::value, | |
94 | bool isVariable = is_variable <Sequence>::value> | |
95 | struct mangled_import_type; | |
96 | ||
97 | template <class ...Args> | |
98 | struct mangled_import_type<sequence<Args...>, true,false,false> //is function | |
99 | { | |
100 | typedef boost::dll::experimental::detail::mangled_library_function<Args...> type; | |
101 | static type make( | |
102 | const boost::dll::experimental::smart_library& p, | |
103 | const std::string& name) | |
104 | { | |
105 | return type( | |
106 | boost::make_shared<shared_library>(p.shared_lib()), | |
107 | boost::addressof(p.get_function<Args>(name))...); | |
108 | } | |
109 | }; | |
110 | ||
111 | template <class Class, class ...Args> | |
112 | struct mangled_import_type<sequence<Class, Args...>, false, true, false> //is member-function | |
113 | { | |
114 | typedef typename boost::dll::experimental::detail::make_mem_fn_seq<Class, Args...>::type actual_sequence; | |
115 | typedef typename boost::dll::experimental::detail::mangled_library_mem_fn<Class, actual_sequence> type; | |
116 | ||
117 | ||
118 | template<class ... ArgsIn> | |
119 | static type make_impl( | |
120 | const boost::dll::experimental::smart_library& p, | |
121 | const std::string & name, | |
122 | sequence<ArgsIn...> * ) | |
123 | { | |
124 | return type(boost::make_shared<shared_library>(p.shared_lib()), | |
125 | p.get_mem_fn<typename ArgsIn::class_type, typename ArgsIn::func_type>(name)...); | |
126 | } | |
127 | ||
128 | static type make( | |
129 | const boost::dll::experimental::smart_library& p, | |
130 | const std::string& name) | |
131 | { | |
132 | return make_impl(p, name, static_cast<actual_sequence*>(nullptr)); | |
133 | } | |
134 | ||
135 | }; | |
136 | ||
137 | template <class T> | |
138 | struct mangled_import_type<sequence<T>, false, false, true> //is variable | |
139 | { | |
140 | typedef boost::shared_ptr<T> type; | |
141 | ||
142 | static type make( | |
143 | const boost::dll::experimental::smart_library& p, | |
144 | const std::string& name) | |
145 | { | |
146 | return type( | |
147 | boost::make_shared<shared_library>(p.shared_lib()), | |
148 | boost::addressof(p.get_variable<T>(name))); | |
149 | } | |
150 | ||
151 | }; | |
152 | ||
153 | ||
154 | } // namespace detail | |
155 | ||
156 | ||
157 | #ifndef BOOST_DLL_DOXYGEN | |
158 | # define BOOST_DLL_MANGLED_IMPORT_RESULT_TYPE inline typename \ | |
159 | boost::dll::experimental::detail::mangled_import_type<boost::dll::experimental::detail::sequence<Args...>>::type | |
160 | #endif | |
161 | ||
162 | /* | |
163 | * Variants: | |
164 | * import_mangled<int>("Stuff"); | |
165 | * import_mangled<thingy(xyz)>("Function"); | |
166 | * import mangled<thingy, void(int)>("Function"); | |
167 | */ | |
168 | ||
169 | /*! | |
170 | * Returns callable object or boost::shared_ptr<T> that holds the symbol imported | |
171 | * from the loaded library. Returned value refcounts usage | |
172 | * of the loaded shared library, so that it won't get unload until all copies of return value | |
173 | * are not destroyed. | |
174 | * | |
175 | * For importing symbols by \b alias names use \forcedlink{import_alias} method. | |
176 | * | |
177 | * \b Examples: | |
178 | * | |
179 | * \code | |
180 | * boost::function<int(int)> f = import_mangled<int(int)>("test_lib.so", "integer_func_name"); | |
181 | * | |
182 | * auto f_cpp11 = import_mangled<int(int)>("test_lib.so", "integer_func_name"); | |
183 | * \endcode | |
184 | * | |
185 | * \code | |
186 | * boost::shared_ptr<int> i = import_mangled<int>("test_lib.so", "integer_name"); | |
187 | * \endcode | |
188 | * | |
189 | * Additionally you can also import overloaded symbols, including member-functions. | |
190 | * | |
191 | * \code | |
192 | * auto fp = import_mangled<void(int), void(double)>("test_lib.so", "func"); | |
193 | * \endcode | |
194 | * | |
195 | * \code | |
196 | * auto fp = import_mangled<my_class, void(int), void(double)>("test_lib.so", "func"); | |
197 | * \endcode | |
198 | * | |
199 | * If qualified member-functions are needed, this can be set by repeating the class name with const or volatile. | |
200 | * All following signatures after the redifintion will use this, i.e. the latest. | |
201 | * | |
202 | * * * \code | |
203 | * auto fp = import_mangled<my_class, void(int), void(double), | |
204 | * const my_class, void(int), void(double)>("test_lib.so", "func"); | |
205 | * \endcode | |
206 | * | |
207 | * \b Template \b parameter \b T: Type of the symbol that we are going to import. Must be explicitly specified. | |
208 | * | |
209 | * \param lib Path to shared library or shared library to load function from. | |
210 | * \param name Null-terminated C or C++ mangled name of the function to import. Can handle std::string, char*, const char*. | |
211 | * \param mode An mode that will be used on library load. | |
212 | * | |
213 | * \return callable object if T is a function type, or boost::shared_ptr<T> if T is an object type. | |
214 | * | |
215 | * \throw boost::system::system_error if symbol does not exist or if the DLL/DSO was not loaded. | |
216 | * Overload that accepts path also throws std::bad_alloc in case of insufficient memory. | |
217 | */ | |
218 | ||
219 | ||
220 | template <class ...Args> | |
221 | BOOST_DLL_MANGLED_IMPORT_RESULT_TYPE import_mangled(const boost::filesystem::path& lib, const char* name, | |
222 | load_mode::type mode = load_mode::default_mode) | |
223 | { | |
224 | typedef typename boost::dll::experimental::detail::mangled_import_type< | |
225 | boost::dll::experimental::detail::sequence<Args...>> type; | |
226 | ||
227 | boost::dll::experimental::smart_library p(lib, mode); | |
228 | //the load | |
229 | return type::make(p, name); | |
230 | } | |
231 | ||
232 | ||
233 | ||
234 | //! \overload boost::dll::import(const boost::filesystem::path& lib, const char* name, load_mode::type mode) | |
235 | template <class ...Args> | |
236 | BOOST_DLL_MANGLED_IMPORT_RESULT_TYPE import_mangled(const boost::filesystem::path& lib, const std::string& name, | |
237 | load_mode::type mode = load_mode::default_mode) | |
238 | { | |
239 | return import_mangled<Args...>(lib, name.c_str(), mode); | |
240 | } | |
241 | ||
242 | //! \overload boost::dll::import(const boost::filesystem::path& lib, const char* name, load_mode::type mode) | |
243 | template <class ...Args> | |
244 | BOOST_DLL_MANGLED_IMPORT_RESULT_TYPE import_mangled(const smart_library& lib, const char* name) { | |
245 | typedef typename boost::dll::experimental::detail::mangled_import_type<detail::sequence<Args...>> type; | |
246 | ||
247 | return type::make(lib, name); | |
248 | } | |
249 | ||
250 | //! \overload boost::dll::import(const boost::filesystem::path& lib, const char* name, load_mode::type mode) | |
251 | template <class ...Args> | |
252 | BOOST_DLL_MANGLED_IMPORT_RESULT_TYPE import_mangled(const smart_library& lib, const std::string& name) { | |
253 | return import_mangled<Args...>(lib, name.c_str()); | |
254 | } | |
255 | ||
256 | //! \overload boost::dll::import(const boost::filesystem::path& lib, const char* name, load_mode::type mode) | |
257 | template <class ...Args> | |
258 | BOOST_DLL_MANGLED_IMPORT_RESULT_TYPE import_mangled(BOOST_RV_REF(smart_library) lib, const char* name) { | |
259 | typedef typename boost::dll::experimental::detail::mangled_import_type<detail::sequence<Args...>> type; | |
260 | ||
261 | return type::make(lib, name); | |
262 | } | |
263 | ||
264 | //! \overload boost::dll::import(const boost::filesystem::path& lib, const char* name, load_mode::type mode) | |
265 | template <class ...Args> | |
266 | BOOST_DLL_MANGLED_IMPORT_RESULT_TYPE import_mangled(BOOST_RV_REF(smart_library) lib, const std::string& name) { | |
267 | return import_mangled<Args...>(boost::move(lib), name.c_str()); | |
268 | } | |
269 | ||
270 | //! \overload boost::dll::import(const boost::filesystem::path& lib, const char* name, load_mode::type mode) | |
271 | template <class ...Args> | |
272 | BOOST_DLL_MANGLED_IMPORT_RESULT_TYPE import_mangled(const shared_library& lib, const char* name) { | |
273 | typedef typename boost::dll::experimental::detail::mangled_import_type<detail::sequence<Args...>> type; | |
274 | ||
275 | boost::shared_ptr<boost::dll::experimental::smart_library> p = boost::make_shared<boost::dll::experimental::smart_library>(lib); | |
276 | return type::make(p, name); | |
277 | } | |
278 | ||
279 | //! \overload boost::dll::import(const boost::filesystem::path& lib, const char* name, load_mode::type mode) | |
280 | template <class ...Args> | |
281 | BOOST_DLL_MANGLED_IMPORT_RESULT_TYPE import_mangled(const shared_library& lib, const std::string& name) { | |
282 | return import_mangled<Args...>(lib, name.c_str()); | |
283 | } | |
284 | ||
285 | //! \overload boost::dll::import(const boost::filesystem::path& lib, const char* name, load_mode::type mode) | |
286 | template <class ...Args> | |
287 | BOOST_DLL_MANGLED_IMPORT_RESULT_TYPE import_mangled(BOOST_RV_REF(shared_library) lib, const char* name) { | |
288 | typedef typename boost::dll::experimental::detail::mangled_import_type<detail::sequence<Args...>> type; | |
289 | ||
290 | boost::dll::experimental::smart_library p(boost::move(lib)); | |
291 | ||
292 | return type::make(p, name); | |
293 | } | |
294 | ||
295 | //! \overload boost::dll::import(const boost::filesystem::path& lib, const char* name, load_mode::type mode) | |
296 | template <class ...Args> | |
297 | BOOST_DLL_MANGLED_IMPORT_RESULT_TYPE import_mangled(BOOST_RV_REF(shared_library) lib, const std::string& name) { | |
298 | return import_mangled<Args...>(boost::move(lib), name.c_str()); | |
299 | } | |
300 | ||
301 | #undef BOOST_DLL_MANGLED_IMPORT_RESULT_TYPE | |
302 | ||
303 | }}} | |
304 | ||
305 | ||
306 | #endif /* BOOST_DLL_IMPORT_MANGLED_HPP_ */ |