1 // Copyright 2015-2018 Klemens D. Morgenstern
2 // Copyright Antony Polukhin, 2019-2022
4 // Distributed under the Boost Software License, Version 1.0.
5 // (See accompanying file LICENSE_1_0.txt
6 // or copy at http://www.boost.org/LICENSE_1_0.txt)
8 #ifndef BOOST_DLL_IMPORT_CLASS_HPP_
9 #define BOOST_DLL_IMPORT_CLASS_HPP_
11 /// \file boost/dll/import_class.hpp
12 /// \warning Extremely experimental! Requires C++11! Will change in next version of Boost! boost/dll/import_class.hpp is not included in boost/dll.hpp
13 /// \brief Contains the boost::dll::experimental::import_class function for importing classes.
15 #include <boost/dll/smart_library.hpp>
16 #include <boost/dll/import_mangled.hpp>
19 #if (__cplusplus < 201103L) && (!defined(_MSVC_LANG) || _MSVC_LANG < 201103L)
20 # error This file requires C++11 at least!
23 #ifdef BOOST_HAS_PRAGMA_ONCE
27 namespace boost { namespace dll { namespace experimental {
38 deleter(const destructor<T> & dtor, bool use_deleting = false) :
39 dtor(dtor), use_deleting(use_deleting) {}
44 dtor.call_deleting(t);
47 dtor.call_standard(t);
48 //the thing is actually an array, so delete[]
49 auto p = reinterpret_cast<char*>(t);
55 template<class T, class = void>
56 struct mem_fn_call_proxy;
58 template<class Class, class U>
59 struct mem_fn_call_proxy<Class, boost::dll::experimental::detail::mangled_library_mem_fn<Class, U>>
61 typedef boost::dll::experimental::detail::mangled_library_mem_fn<Class, U> mem_fn_t;
65 mem_fn_call_proxy(mem_fn_call_proxy&&) = default;
66 mem_fn_call_proxy(const mem_fn_call_proxy & ) = delete;
67 mem_fn_call_proxy(Class * t, mem_fn_t & mem_fn)
68 : t(t), mem_fn(mem_fn) {}
70 template<typename ...Args>
71 auto operator()(Args&&...args) const
73 return mem_fn(t, std::forward<Args>(args)...);
78 template<class T, class Return, class ...Args>
79 struct mem_fn_call_proxy<T, Return(Args...)>
82 const std::string &name;
85 mem_fn_call_proxy(mem_fn_call_proxy&&) = default;
86 mem_fn_call_proxy(const mem_fn_call_proxy&) = delete;
87 mem_fn_call_proxy(T *t, const std::string &name, smart_library & _lib)
88 : t(t), name(name), _lib(_lib) {};
90 Return operator()(Args...args) const
92 auto f = _lib.get_mem_fn<T, Return(Args...)>(name);
93 return (t->*f)(static_cast<Args>(args)...);
100 class imported_class;
102 template<typename T, typename ... Args> imported_class<T>
103 import_class(const smart_library& lib, Args...args);
104 template<typename T, typename ... Args> imported_class<T>
105 import_class(const smart_library& lib, const std::string & alias_name, Args...args);
106 template<typename T, typename ... Args> imported_class<T>
107 import_class(const smart_library& lib, std::size_t size, Args...args);
108 template<typename T, typename ... Args> imported_class<T>
109 import_class(const smart_library& lib, std::size_t size,
110 const std::string & alias_name, Args...args);
113 /*! This class represents an imported class.
115 * \note It must be constructed via \ref boost::dll::import_class(const smart_library& lib, std::size_t, Args...)
117 * \tparam The type or type-alias of the imported class.
123 std::unique_ptr<T, detail::deleter<T>> _data;
126 const std::type_info& _ti;
128 template<typename ... Args>
129 inline std::unique_ptr<T, detail::deleter<T>> make_data(const smart_library& lib, Args ... args);
130 template<typename ... Args>
131 inline std::unique_ptr<T, detail::deleter<T>> make_data(const smart_library& lib, std::size_t size, Args...args);
133 template<typename ...Args>
134 imported_class(detail::sequence<Args...> *, const smart_library& lib, Args...args);
136 template<typename ...Args>
137 imported_class(detail::sequence<Args...> *, const smart_library& lib, std::size_t size, Args...args);
139 template<typename ...Args>
140 imported_class(detail::sequence<Args...> *, smart_library&& lib, Args...args);
142 template<typename ...Args>
143 imported_class(detail::sequence<Args...> *, smart_library&& lib, std::size_t size, Args...args);
145 //alias to construct with explicit parameter list
146 template<typename ...Args>
147 static imported_class<T> make(smart_library&& lib, Args...args)
149 typedef detail::sequence<Args...> *seq;
150 return imported_class(seq(), boost::move(lib), static_cast<Args>(args)...);
153 template<typename ...Args>
154 static imported_class<T> make(smart_library&& lib, std::size_t size, Args...args)
156 typedef detail::sequence<Args...> *seq;
157 return imported_class(seq(), boost::move(lib), size, static_cast<Args>(args)...);
159 template<typename ...Args>
160 static imported_class<T> make(const smart_library& lib, Args...args)
162 typedef detail::sequence<Args...> *seq;
163 return imported_class(seq(), lib, static_cast<Args>(args)...);
166 template<typename ...Args>
167 static imported_class<T> make(const smart_library& lib, std::size_t size, Args...args)
169 typedef detail::sequence<Args...> *seq;
170 return imported_class(seq(), lib, size, static_cast<Args>(args)...);
173 typedef imported_class<T> base_t;
174 ///Returns a pointer to the underlying class
175 T* get() {return _data.get();}
176 imported_class() = delete;
178 imported_class(imported_class&) = delete;
179 imported_class(imported_class&&) = default; ///<Move constructor
180 imported_class& operator=(imported_class&) = delete;
181 imported_class& operator=(imported_class&&) = default; ///<Move assignmend
183 ///Check if the imported class is move-constructible
184 bool is_move_constructible() {return !_lib.symbol_storage().template get_constructor<T(T&&)> ().empty();}
185 ///Check if the imported class is move-assignable
186 bool is_move_assignable() {return !_lib.symbol_storage().template get_mem_fn<T, T&(T&&)> ("operator=").empty();}
187 ///Check if the imported class is copy-constructible
188 bool is_copy_constructible() {return !_lib.symbol_storage().template get_constructor<T(const T&)>().empty();}
189 ///Check if the imported class is copy-assignable
190 bool is_copy_assignable() {return !_lib.symbol_storage().template get_mem_fn<T, T&(const T&)>("operator=").empty();}
192 imported_class<T> copy() const; ///<Invoke the copy constructor. \attention Undefined behaviour if the imported object is not copy constructible.
193 imported_class<T> move(); ///<Invoke the move constructor. \attention Undefined behaviour if the imported object is not move constructible.
195 ///Invoke the copy assignment. \attention Undefined behaviour if the imported object is not copy assignable.
196 void copy_assign(const imported_class<T> & lhs) const;
197 ///Invoke the move assignment. \attention Undefined behaviour if the imported object is not move assignable.
198 void move_assign( imported_class<T> & lhs);
200 ///Check if the class is loaded.
201 explicit operator bool() const {return _data;}
203 ///Get a const reference to the std::type_info.
204 const std::type_info& get_type_info() {return _ti;};
206 /*! Call a member function. This returns a proxy to the function.
207 * The proxy mechanic mechanic is necessary, so the signaute can be passed.
212 * im_class.call<void(const char*)>("function_name")("MyString");
215 template<class Signature>
216 const detail::mem_fn_call_proxy<T, Signature> call(const std::string& name)
218 return detail::mem_fn_call_proxy<T, Signature>(_data.get(), name, _lib);
220 /*! Call a qualified member function, i.e. const and or volatile.
225 * im_class.call<const type_alias, void(const char*)>("function_name")("MyString");
228 template<class Tin, class Signature, class = boost::enable_if<detail::unqalified_is_same<T, Tin>>>
229 const detail::mem_fn_call_proxy<Tin, Signature> call(const std::string& name)
231 return detail::mem_fn_call_proxy<Tin, Signature>(_data.get(), name, _lib);
233 ///Overload of ->* for an imported method.
234 template<class Tin, class T2>
235 const detail::mem_fn_call_proxy<Tin, boost::dll::experimental::detail::mangled_library_mem_fn<Tin, T2>>
236 operator->*(detail::mangled_library_mem_fn<Tin, T2>& mn)
238 return detail::mem_fn_call_proxy<Tin, boost::dll::experimental::detail::mangled_library_mem_fn<Tin, T2>>(_data.get(), mn);
241 ///Import a method of the class.
242 template <class ...Args>
243 typename boost::dll::experimental::detail::mangled_import_type<boost::dll::experimental::detail::sequence<T, Args...>>::type
244 import(const std::string & name)
246 return boost::dll::experimental::import_mangled<T, Args...>(_lib, name);
252 //helper function, uses the allocating
254 template<typename ... Args>
255 inline std::unique_ptr<T, detail::deleter<T>> imported_class<T>::make_data(const smart_library& lib, Args ... args)
257 constructor<T(Args...)> ctor = lib.get_constructor<T(Args...)>();
258 destructor<T> dtor = lib.get_destructor <T>();
260 if (!ctor.has_allocating() || !dtor.has_deleting())
262 boost::dll::fs::error_code ec;
264 ec = boost::dll::fs::make_error_code(
265 boost::dll::fs::errc::bad_file_descriptor
268 // report_error() calls dlsym, do not use it here!
269 boost::throw_exception(
270 boost::dll::fs::system_error(
271 ec, "boost::dll::detail::make_data() failed: no allocating ctor or dtor was found"
276 return std::unique_ptr<T, detail::deleter<T>> (
277 ctor.call_allocating(static_cast<Args>(args)...),
278 detail::deleter<T>(dtor, false /* not deleting dtor*/));
281 //helper function, using the standard
283 template<typename ... Args>
284 inline std::unique_ptr<T, detail::deleter<T>> imported_class<T>::make_data(const smart_library& lib, std::size_t size, Args...args)
286 constructor<T(Args...)> ctor = lib.get_constructor<T(Args...)>();
287 destructor<T> dtor = lib.get_destructor <T>();
289 if (!ctor.has_standard() || !dtor.has_standard())
291 boost::dll::fs::error_code ec;
293 ec = boost::dll::fs::make_error_code(
294 boost::dll::fs::errc::bad_file_descriptor
297 // report_error() calls dlsym, do not use it here!
298 boost::throw_exception(
299 boost::dll::fs::system_error(
300 ec, "boost::dll::detail::make_data() failed: no regular ctor or dtor was found"
305 T *data = reinterpret_cast<T*>(new char[size]);
307 ctor.call_standard(data, static_cast<Args>(args)...);
309 return std::unique_ptr<T, detail::deleter<T>> (
310 reinterpret_cast<T*>(data),
311 detail::deleter<T>(dtor, false /* not deleting dtor*/));
317 template<typename ...Args>
318 imported_class<T>::imported_class(detail::sequence<Args...> *, const smart_library & lib, Args...args)
320 _data(make_data<Args...>(lib, static_cast<Args>(args)...)),
321 _is_allocating(false),
323 _ti(lib.get_type_info<T>())
329 template<typename ...Args>
330 imported_class<T>::imported_class(detail::sequence<Args...> *, const smart_library & lib, std::size_t size, Args...args)
332 _data(make_data<Args...>(lib, size, static_cast<Args>(args)...)),
333 _is_allocating(true),
335 _ti(lib.get_type_info<T>())
341 template<typename ...Args>
342 imported_class<T>::imported_class(detail::sequence<Args...> *, smart_library && lib, Args...args)
343 : _lib(boost::move(lib)),
344 _data(make_data<Args...>(lib, static_cast<Args>(args)...)),
345 _is_allocating(false),
347 _ti(lib.get_type_info<T>())
353 template<typename ...Args>
354 imported_class<T>::imported_class(detail::sequence<Args...> *, smart_library && lib, std::size_t size, Args...args)
355 : _lib(boost::move(lib)),
356 _data(make_data<Args...>(lib, size, static_cast<Args>(args)...)),
357 _is_allocating(true),
359 _ti(lib.get_type_info<T>())
365 inline imported_class<T> boost::dll::experimental::imported_class<T>::copy() const
367 if (this->_is_allocating)
368 return imported_class<T>::template make<const T&>(_lib, *_data);
370 return imported_class<T>::template make<const T&>(_lib, _size, *_data);
374 inline imported_class<T> boost::dll::experimental::imported_class<T>::move()
376 if (this->_is_allocating)
377 return imported_class<T>::template make<T&&>(_lib, *_data);
379 return imported_class<T>::template make<T&&>(_lib, _size, *_data);
383 inline void boost::dll::experimental::imported_class<T>::copy_assign(const imported_class<T>& lhs) const
385 this->call<T&(const T&)>("operator=")(*lhs._data);
389 inline void boost::dll::experimental::imported_class<T>::move_assign(imported_class<T>& lhs)
391 this->call<T&(T&&)>("operator=")(static_cast<T&&>(*lhs._data));
397 * Returns an instance of \ref imported_class which allows to call or import more functions.
398 * It takes a copy of the smart_libray, so no added type_aliases will be visible,
401 * Few compilers do implement an allocating constructor, which allows the construction
402 * of the class without knowing the size. That is not portable, so the actual size of the class
403 * shall always be provided.
408 * auto import_class<class type_alias, const std::string&, std::size_t>(lib, "class_name", 20, "param1", 42);
411 * In this example we construct an instance of the class "class_name" with the size 20, which has "type_alias" as an alias,
412 * through a constructor which takes a const-ref of std::string and an std::size_t parameter.
414 * \tparam T Class type or alias
415 * \tparam Args Constructor argument list.
416 * \param lib Path to shared library or shared library to load function from.
417 * \param name Null-terminated C or C++ mangled name of the function to import. Can handle std::string, char*, const char*.
418 * \param mode An mode that will be used on library load.
420 * \return class object.
422 * \throw \forcedlinkfs{system_error} if symbol does not exist or if the DLL/DSO was not loaded.
423 * Overload that accepts path also throws std::bad_alloc in case of insufficient memory.
425 template<typename T, typename ... Args> imported_class<T>
426 import_class(const smart_library& lib_, std::size_t size, Args...args)
428 smart_library lib(lib_);
430 return imported_class<T>::template make<Args...>(boost::move(lib), size, static_cast<Args>(args)...);
433 //! \overload boost::dll::import_class(const smart_library& lib, std::size_t, Args...)
434 template<typename T, typename ... Args> imported_class<T>
435 import_class(const smart_library& lib_, Args...args)
437 smart_library lib(lib_);
438 return imported_class<T>::template make<Args...>(boost::move(lib), static_cast<Args>(args)...);
441 //! \overload boost::dll::import_class(const smart_library& lib, std::size_t, Args...)
442 template<typename T, typename ... Args> imported_class<T>
443 import_class(const smart_library& lib_, const std::string & alias_name, Args...args)
445 smart_library lib(lib_);
446 lib.add_type_alias<T>(alias_name);
447 return imported_class<T>::template make<Args...>(boost::move(lib), static_cast<Args>(args)...);
450 //! \overload boost::dll::import_class(const smart_library& lib, std::size_t, Args...)
451 template<typename T, typename ... Args> imported_class<T>
452 import_class(const smart_library& lib_, std::size_t size, const std::string & alias_name, Args...args)
454 smart_library lib(lib_);
456 lib.add_type_alias<T>(alias_name);
457 return imported_class<T>::template make<Args...>(boost::move(lib), size, static_cast<Args>(args)...);
460 //! \overload boost::dll::import_class(const smart_library& lib, std::size_t, Args...)
461 template<typename T, typename ... Args> imported_class<T>
462 import_class(const smart_library& lib_, const std::string & alias_name, std::size_t size, Args...args)
464 smart_library lib(lib_);
466 lib.add_type_alias<T>(alias_name);
467 return imported_class<T>::template make<Args...>(boost::move(lib), size, static_cast<Args>(args)...);
470 //! \overload boost::dll::import_class(const smart_library& lib, std::size_t, Args...)
471 template<typename T, typename ... Args> imported_class<T>
472 import_class(smart_library && lib, Args...args)
474 return imported_class<T>::template make<Args...>(boost::move(lib), static_cast<Args>(args)...);
477 //! \overload boost::dll::import_class(const smart_library& lib, std::size_t, Args...)
478 template<typename T, typename ... Args> imported_class<T>
479 import_class(smart_library && lib, const std::string & alias_name, Args...args)
481 lib.add_type_alias<T>(alias_name);
482 return imported_class<T>::template make<Args...>(boost::move(lib), static_cast<Args>(args)...);
485 //! \overload boost::dll::import_class(const smart_library& lib, std::size_t, Args...)
486 template<typename T, typename ... Args> imported_class<T>
487 import_class(smart_library && lib, std::size_t size, Args...args)
489 return imported_class<T>::template make<Args...>(boost::move(lib), size, static_cast<Args>(args)...);
492 //! \overload boost::dll::import_class(const smart_library& lib, std::size_t, Args...)
493 template<typename T, typename ... Args> imported_class<T>
494 import_class(smart_library && lib, std::size_t size, const std::string & alias_name, Args...args)
496 lib.add_type_alias<T>(alias_name);
497 return imported_class<T>::template make<Args...>(boost::move(lib), size, static_cast<Args>(args)...);
500 //! \overload boost::dll::import_class(const smart_library& lib, std::size_t, Args...)
501 template<typename T, typename ... Args> imported_class<T>
502 import_class(smart_library && lib, const std::string & alias_name, std::size_t size, Args...args)
504 lib.add_type_alias<T>(alias_name);
505 return imported_class<T>::template make<Args...>(boost::move(lib), size, static_cast<Args>(args)...);
510 /*! \overload boost::dll::import_class(const smart_library& lib, std::size_t, Args...)
511 * \note This function does add the type alias to the \ref boost::dll::experimental::smart_library.
514 template<typename T, typename ... Args> imported_class<T>
515 import_class(smart_library & lib, Args...args)
517 return imported_class<T>::template make<Args...>(lib, static_cast<Args>(args)...);
520 /*! \overload boost::dll::import_class(const smart_library& lib, std::size_t, Args...)
521 * \note This function does add the type alias to the \ref boost::dll::experimental::smart_library.
523 template<typename T, typename ... Args> imported_class<T>
524 import_class(smart_library & lib, const std::string & alias_name, Args...args)
526 lib.add_type_alias<T>(alias_name);
527 return imported_class<T>::template make<Args...>(lib, static_cast<Args>(args)...);
530 /*! \overload boost::dll::import_class(const smart_library& lib, std::size_t, Args...)
531 * \note This function does add the type alias to the \ref boost::dll::experimental::smart_library.
533 template<typename T, typename ... Args> imported_class<T>
534 import_class(smart_library & lib, std::size_t size, Args...args)
536 return imported_class<T>::template make<Args...>(lib, size, static_cast<Args>(args)...);
539 /*! \overload boost::dll::import_class(const smart_library& lib, std::size_t, Args...)
540 * \note This function does add the type alias to the \ref boost::dll::experimental::smart_library.
542 template<typename T, typename ... Args> imported_class<T>
543 import_class(smart_library & lib, std::size_t size, const std::string & alias_name, Args...args)
545 lib.add_type_alias<T>(alias_name);
546 return imported_class<T>::template make<Args...>(lib, size, static_cast<Args>(args)...);
549 /*! \overload boost::dll::import_class(const smart_library& lib, std::size_t, Args...)
550 * \note This function does add the type alias to the \ref boost::dll::experimental::smart_library.
552 template<typename T, typename ... Args> imported_class<T>
553 import_class(smart_library & lib, const std::string & alias_name, std::size_t size, Args...args)
555 lib.add_type_alias<T>(alias_name);
556 return imported_class<T>::template make<Args...>(lib, size, static_cast<Args>(args)...);
565 #endif /* BOOST_DLL_IMPORT_CLASS_HPP_ */