]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/dll/import_mangled.hpp
import new upstream nautilus stable release 14.2.8
[ceph.git] / ceph / src / boost / boost / dll / import_mangled.hpp
1 // Copyright 2015-2018 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/dll/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/core/addressof.hpp>
17 #include <boost/core/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<shared_library> lib_;
38 function_tuple<Ts...> f_;
39 public:
40 constexpr mangled_library_function(const boost::shared_ptr<shared_library>& lib, Ts*... func_ptr) BOOST_NOEXCEPT
41 : lib_(lib)
42 , f_(func_ptr...)
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>
55 auto operator()(Args&&... args) const
56 -> decltype( f_(static_cast<Args&&>(args)...) )
57 {
58 return f_(static_cast<Args&&>(args)...);
59 }
60 };
61
62
63 template<class Class, class Sequence>
64 class mangled_library_mem_fn;
65
66 template <class Class, class ... Ts>
67 class 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;
70 boost::shared_ptr<shared_library> lib_;
71 call_tuple_t f_;
72
73 public:
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...)
77 {}
78
79 template <class ClassIn, class... Args>
80 auto operator()(ClassIn *cl, Args&&... args) const
81 -> decltype( f_(cl, static_cast<Args&&>(args)...) )
82 {
83 return f_(cl, static_cast<Args&&>(args)...);
84 }
85 };
86
87
88
89
90 // simple enough to be here
91 template<class Seq> struct is_variable : boost::false_type {};
92 template<typename T> struct is_variable<sequence<T>> : boost::is_object<T> {};
93
94 template <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>
98 struct mangled_import_type;
99
100 template <class ...Args>
101 struct 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
114 template <class Class, class ...Args>
115 struct 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
140 template <class T>
141 struct 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 \forcedlinkfs{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
223 template <class ...Args>
224 BOOST_DLL_MANGLED_IMPORT_RESULT_TYPE import_mangled(const boost::dll::fs::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::dll::fs::path& lib, const char* name, load_mode::type mode)
238 template <class ...Args>
239 BOOST_DLL_MANGLED_IMPORT_RESULT_TYPE import_mangled(const boost::dll::fs::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::dll::fs::path& lib, const char* name, load_mode::type mode)
246 template <class ...Args>
247 BOOST_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::dll::fs::path& lib, const char* name, load_mode::type mode)
254 template <class ...Args>
255 BOOST_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::dll::fs::path& lib, const char* name, load_mode::type mode)
260 template <class ...Args>
261 BOOST_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::dll::fs::path& lib, const char* name, load_mode::type mode)
268 template <class ...Args>
269 BOOST_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::dll::fs::path& lib, const char* name, load_mode::type mode)
274 template <class ...Args>
275 BOOST_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::dll::fs::path& lib, const char* name, load_mode::type mode)
283 template <class ...Args>
284 BOOST_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::dll::fs::path& lib, const char* name, load_mode::type mode)
289 template <class ...Args>
290 BOOST_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::dll::fs::path& lib, const char* name, load_mode::type mode)
299 template <class ...Args>
300 BOOST_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_ */