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