1 // Copyright 2015-2016 Klemens D. Morgenstern
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)
8 #ifndef BOOST_DLL_IMPORT_MANGLED_HPP_
9 #define BOOST_DLL_IMPORT_MANGLED_HPP_
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>
23 #ifdef BOOST_HAS_PRAGMA_ONCE
27 namespace boost { namespace dll { namespace experimental {
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_;
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...))
45 // Compilation error at this point means that imported function
46 // was called with unmatching parameters.
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)...) )
57 return (*f_)(static_cast<Args&&>(args)...);
62 template<class Class, class Sequence>
63 class mangled_library_mem_fn;
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_;
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...))
77 template <class ClassIn, class... Args>
78 inline auto operator()(ClassIn *cl, Args&&... args) const -> decltype( (*f_)(cl, static_cast<Args&&>(args)...) )
80 return (*f_)(cl, static_cast<Args&&>(args)...);
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> {};
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;
97 template <class ...Args>
98 struct mangled_import_type<sequence<Args...>, true,false,false> //is function
100 typedef boost::dll::experimental::detail::mangled_library_function<Args...> type;
102 const boost::dll::experimental::smart_library& p,
103 const std::string& name)
106 boost::make_shared<shared_library>(p.shared_lib()),
107 boost::addressof(p.get_function<Args>(name))...);
111 template <class Class, class ...Args>
112 struct mangled_import_type<sequence<Class, Args...>, false, true, false> //is member-function
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;
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...> * )
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)...);
129 const boost::dll::experimental::smart_library& p,
130 const std::string& name)
132 return make_impl(p, name, static_cast<actual_sequence*>(nullptr));
138 struct mangled_import_type<sequence<T>, false, false, true> //is variable
140 typedef boost::shared_ptr<T> type;
143 const boost::dll::experimental::smart_library& p,
144 const std::string& name)
147 boost::make_shared<shared_library>(p.shared_lib()),
148 boost::addressof(p.get_variable<T>(name)));
154 } // namespace detail
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
164 * import_mangled<int>("Stuff");
165 * import_mangled<thingy(xyz)>("Function");
166 * import mangled<thingy, void(int)>("Function");
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
175 * For importing symbols by \b alias names use \forcedlink{import_alias} method.
180 * boost::function<int(int)> f = import_mangled<int(int)>("test_lib.so", "integer_func_name");
182 * auto f_cpp11 = import_mangled<int(int)>("test_lib.so", "integer_func_name");
186 * boost::shared_ptr<int> i = import_mangled<int>("test_lib.so", "integer_name");
189 * Additionally you can also import overloaded symbols, including member-functions.
192 * auto fp = import_mangled<void(int), void(double)>("test_lib.so", "func");
196 * auto fp = import_mangled<my_class, void(int), void(double)>("test_lib.so", "func");
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.
203 * auto fp = import_mangled<my_class, void(int), void(double),
204 * const my_class, void(int), void(double)>("test_lib.so", "func");
207 * \b Template \b parameter \b T: Type of the symbol that we are going to import. Must be explicitly specified.
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.
213 * \return callable object if T is a function type, or boost::shared_ptr<T> if T is an object type.
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.
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)
224 typedef typename boost::dll::experimental::detail::mangled_import_type<
225 boost::dll::experimental::detail::sequence<Args...>> type;
227 boost::dll::experimental::smart_library p(lib, mode);
229 return type::make(p, name);
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)
239 return import_mangled<Args...>(lib, name.c_str(), mode);
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;
247 return type::make(lib, name);
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());
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;
261 return type::make(lib, name);
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());
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;
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);
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());
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;
290 boost::dll::experimental::smart_library p(boost::move(lib));
292 return type::make(p, name);
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());
301 #undef BOOST_DLL_MANGLED_IMPORT_RESULT_TYPE
306 #endif /* BOOST_DLL_IMPORT_MANGLED_HPP_ */