]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/dll/include/boost/dll/import_class.hpp
bump version to 12.2.2-pve1
[ceph.git] / ceph / src / boost / libs / dll / include / boost / dll / import_class.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#ifndef BOOST_DLL_IMPORT_CLASS_HPP_
8#define BOOST_DLL_IMPORT_CLASS_HPP_
9
10#include <boost/dll/smart_library.hpp>
11#include <boost/dll/import_mangled.hpp>
12#include <memory>
13
14#ifdef BOOST_HAS_PRAGMA_ONCE
15# pragma once
16#endif
17
18namespace boost { namespace dll { namespace experimental {
19
20namespace detail
21{
22
23template<typename T>
24struct deleter
25{
26 destructor<T> dtor;
27 bool use_deleting;
28
29 deleter(const destructor<T> & dtor, bool use_deleting = false) :
30 dtor(dtor), use_deleting(use_deleting) {}
31
32 void operator()(T*t)
33 {
34 if (use_deleting)
35 dtor.call_deleting(t);
36 else
37 {
38 dtor.call_standard(t);
39 //the thing is actually an array, so delete[]
40 auto p = reinterpret_cast<char*>(t);
41 delete [] p;
42 }
43 }
44};
45
46template<class T, class = void>
47struct mem_fn_call_proxy;
48
49template<class Class, class U>
50struct mem_fn_call_proxy<Class, boost::dll::experimental::detail::mangled_library_mem_fn<Class, U>>
51{
52 typedef boost::dll::experimental::detail::mangled_library_mem_fn<Class, U> mem_fn_t;
53 Class* t;
54 mem_fn_t & mem_fn;
55
56 mem_fn_call_proxy(mem_fn_call_proxy&&) = default;
57 mem_fn_call_proxy(const mem_fn_call_proxy & ) = delete;
58 mem_fn_call_proxy(Class * t, mem_fn_t & mem_fn)
59 : t(t), mem_fn(mem_fn) {}
60
61 template<typename ...Args>
62 auto operator()(Args&&...args) const
63 {
64 return mem_fn(t, std::forward<Args>(args)...);
65 }
66
67};
68
69template<class T, class Return, class ...Args>
70struct mem_fn_call_proxy<T, Return(Args...)>
71{
72 T* t;
73 const std::string &name;
74 smart_library &_lib;
75
76 mem_fn_call_proxy(mem_fn_call_proxy&&) = default;
77 mem_fn_call_proxy(const mem_fn_call_proxy&) = delete;
78 mem_fn_call_proxy(T *t, const std::string &name, smart_library & _lib)
79 : t(t), name(name), _lib(_lib) {};
80
81 Return operator()(Args...args) const
82 {
83 auto f = _lib.get_mem_fn<T, Return(Args...)>(name);
84 return (t->*f)(static_cast<Args>(args)...);
85 }
86};
87
88}
89
90template<typename T>
91class imported_class;
92
93template<typename T, typename ... Args> imported_class<T>
94import_class(const smart_library& lib, Args...args);
95template<typename T, typename ... Args> imported_class<T>
96import_class(const smart_library& lib, const std::string & alias_name, Args...args);
97template<typename T, typename ... Args> imported_class<T>
98import_class(const smart_library& lib, std::size_t size, Args...args);
99template<typename T, typename ... Args> imported_class<T>
100import_class(const smart_library& lib, std::size_t size,
101 const std::string & alias_name, Args...args);
102
103
104/*! This class represents an imported class.
105 *
106 * \note It must be constructed via \ref boost::dll::import_class(const smart_library& lib, std::size_t, Args...)
107 *
108 * \tparam The type or type-alias of the imported class.
109 */
110template<typename T>
111class imported_class
112{
113 smart_library _lib;
114 std::unique_ptr<T, detail::deleter<T>> _data;
115 bool _is_allocating;
116 std::size_t _size;
117 const std::type_info& _ti;
118
119 template<typename ... Args>
120 inline std::unique_ptr<T, detail::deleter<T>> make_data(const smart_library& lib, Args ... args);
121 template<typename ... Args>
122 inline std::unique_ptr<T, detail::deleter<T>> make_data(const smart_library& lib, std::size_t size, Args...args);
123
124 template<typename ...Args>
125 imported_class(detail::sequence<Args...> *, const smart_library& lib, Args...args);
126
127 template<typename ...Args>
128 imported_class(detail::sequence<Args...> *, const smart_library& lib, std::size_t size, Args...args);
129
130 template<typename ...Args>
131 imported_class(detail::sequence<Args...> *, smart_library&& lib, Args...args);
132
133 template<typename ...Args>
134 imported_class(detail::sequence<Args...> *, smart_library&& lib, std::size_t size, Args...args);
135public:
136 //alias to construct with explicit parameter list
137 template<typename ...Args>
138 static imported_class<T> make(smart_library&& lib, Args...args)
139 {
140 typedef detail::sequence<Args...> *seq;
141 return imported_class(seq(), boost::move(lib), static_cast<Args>(args)...);
142 }
143
144 template<typename ...Args>
145 static imported_class<T> make(smart_library&& lib, std::size_t size, Args...args)
146 {
147 typedef detail::sequence<Args...> *seq;
148 return imported_class(seq(), boost::move(lib), size, static_cast<Args>(args)...);
149 }
150 template<typename ...Args>
151 static imported_class<T> make(const smart_library& lib, Args...args)
152 {
153 typedef detail::sequence<Args...> *seq;
154 return imported_class(seq(), lib, static_cast<Args>(args)...);
155 }
156
157 template<typename ...Args>
158 static imported_class<T> make(const smart_library& lib, std::size_t size, Args...args)
159 {
160 typedef detail::sequence<Args...> *seq;
161 return imported_class(seq(), lib, size, static_cast<Args>(args)...);
162 }
163
164 typedef imported_class<T> base_t;
165 ///Returns a pointer to the underlying class
166 T* get() {return _data.get();}
167 imported_class() = delete;
168
169 imported_class(imported_class&) = delete;
170 imported_class(imported_class&&) = default; ///<Move constructor
171 imported_class& operator=(imported_class&) = delete;
172 imported_class& operator=(imported_class&&) = default; ///<Move assignmend
173
174 ///Check if the imported class is move-constructible
175 bool is_move_constructible() {return !_lib.symbol_storage().template get_constructor<T(T&&)> ().empty();}
176 ///Check if the imported class is move-assignable
177 bool is_move_assignable() {return !_lib.symbol_storage().template get_mem_fn<T, T&(T&&)> ("operator=").empty();}
178 ///Check if the imported class is copy-constructible
179 bool is_copy_constructible() {return !_lib.symbol_storage().template get_constructor<T(const T&)>().empty();}
180 ///Check if the imported class is copy-assignable
181 bool is_copy_assignable() {return !_lib.symbol_storage().template get_mem_fn<T, T&(const T&)>("operator=").empty();}
182
183 imported_class<T> copy() const; ///<Invoke the copy constructor. \attention Undefined behaviour if the imported object is not copy constructible.
184 imported_class<T> move(); ///<Invoke the move constructor. \attention Undefined behaviour if the imported object is not move constructible.
185
186 ///Invoke the copy assignment. \attention Undefined behaviour if the imported object is not copy assignable.
187 void copy_assign(const imported_class<T> & lhs) const;
188 ///Invoke the move assignment. \attention Undefined behaviour if the imported object is not move assignable.
189 void move_assign( imported_class<T> & lhs);
190
191 ///Check if the class is loaded.
192 explicit operator bool() const {return _data;}
193
194 ///Get a const reference to the std::type_info.
195 const std::type_info& get_type_info() {return _ti;};
196
197 /*! Call a member function. This returns a proxy to the function.
198 * The proxy mechanic mechanic is necessary, so the signaute can be passed.
199 *
200 * \b Example
201 *
202 * \code
203 * im_class.call<void(const char*)>("function_name")("MyString");
204 * \endcode
205 */
206 template<class Signature>
207 const detail::mem_fn_call_proxy<T, Signature> call(const std::string& name)
208 {
209 return detail::mem_fn_call_proxy<T, Signature>(_data.get(), name, _lib);
210 }
211 /*! Call a qualified member function, i.e. const and or volatile.
212 *
213 * \b Example
214 *
215 * \code
216 * im_class.call<const type_alias, void(const char*)>("function_name")("MyString");
217 * \endcode
218 */
219 template<class Tin, class Signature, class = boost::enable_if<detail::unqalified_is_same<T, Tin>>>
220 const detail::mem_fn_call_proxy<Tin, Signature> call(const std::string& name)
221 {
222 return detail::mem_fn_call_proxy<Tin, Signature>(_data.get(), name, _lib);
223 }
224 ///Overload of ->* for an imported method.
225 template<class Tin, class T2>
226 const detail::mem_fn_call_proxy<Tin, boost::dll::experimental::detail::mangled_library_mem_fn<Tin, T2>>
227 operator->*(detail::mangled_library_mem_fn<Tin, T2>& mn)
228 {
229 return detail::mem_fn_call_proxy<Tin, boost::dll::experimental::detail::mangled_library_mem_fn<Tin, T2>>(_data.get(), mn);
230 }
231
232 ///Import a method of the class.
233 template <class ...Args>
234 typename boost::dll::experimental::detail::mangled_import_type<boost::dll::experimental::detail::sequence<T, Args...>>::type
235 import(const std::string & name)
236 {
237 return boost::dll::experimental::import_mangled<T, Args...>(_lib, name);
238 }
239};
240
241
242
243//helper function, uses the allocating
244template<typename T>
245template<typename ... Args>
246inline std::unique_ptr<T, detail::deleter<T>> imported_class<T>::make_data(const smart_library& lib, Args ... args)
247{
248 constructor<T(Args...)> ctor = lib.get_constructor<T(Args...)>();
249 destructor<T> dtor = lib.get_destructor <T>();
250
251 if (!ctor.has_allocating() || !dtor.has_deleting())
252 {
253 boost::system::error_code ec;
254
255 ec = boost::system::error_code(
256 boost::system::errc::bad_file_descriptor,
257 boost::system::generic_category()
258 );
259
260 // report_error() calls dlsym, do not use it here!
261 boost::throw_exception(
262 boost::system::system_error(
263 ec, "boost::dll::detail::make_data() failed: no allocating ctor or dtor was found"
264 )
265 );
266 }
267
268 return std::unique_ptr<T, detail::deleter<T>> (
269 ctor.call_allocating(static_cast<Args>(args)...),
270 detail::deleter<T>(dtor, false /* not deleting dtor*/));
271}
272
273//helper function, using the standard
274template<typename T>
275template<typename ... Args>
276inline std::unique_ptr<T, detail::deleter<T>> imported_class<T>::make_data(const smart_library& lib, std::size_t size, Args...args)
277{
278 constructor<T(Args...)> ctor = lib.get_constructor<T(Args...)>();
279 destructor<T> dtor = lib.get_destructor <T>();
280
281 if (!ctor.has_standard() || !dtor.has_standard())
282 {
283 boost::system::error_code ec;
284
285 ec = boost::system::error_code(
286 boost::system::errc::bad_file_descriptor,
287 boost::system::generic_category()
288 );
289
290 // report_error() calls dlsym, do not use it here!
291 boost::throw_exception(
292 boost::system::system_error(
293 ec, "boost::dll::detail::make_data() failed: no regular ctor or dtor was found"
294 )
295 );
296 }
297
298 T *data = reinterpret_cast<T*>(new char[size]);
299
300 ctor.call_standard(data, static_cast<Args>(args)...);
301
302 return std::unique_ptr<T, detail::deleter<T>> (
303 reinterpret_cast<T*>(data),
304 detail::deleter<T>(dtor, false /* not deleting dtor*/));
305
306}
307
308
309template<typename T>
310template<typename ...Args>
311imported_class<T>::imported_class(detail::sequence<Args...> *, const smart_library & lib, Args...args)
312 : _lib(lib),
313 _data(make_data<Args...>(lib, static_cast<Args>(args)...)),
314 _is_allocating(false),
315 _size(0),
316 _ti(lib.get_type_info<T>())
317{
318
319}
320
321template<typename T>
322template<typename ...Args>
323imported_class<T>::imported_class(detail::sequence<Args...> *, const smart_library & lib, std::size_t size, Args...args)
324 : _lib(lib),
325 _data(make_data<Args...>(lib, size, static_cast<Args>(args)...)),
326 _is_allocating(true),
327 _size(size),
328 _ti(lib.get_type_info<T>())
329{
330
331}
332
333template<typename T>
334template<typename ...Args>
335imported_class<T>::imported_class(detail::sequence<Args...> *, smart_library && lib, Args...args)
336 : _lib(boost::move(lib)),
337 _data(make_data<Args...>(lib, static_cast<Args>(args)...)),
338 _is_allocating(false),
339 _size(0),
340 _ti(lib.get_type_info<T>())
341{
342
343}
344
345template<typename T>
346template<typename ...Args>
347imported_class<T>::imported_class(detail::sequence<Args...> *, smart_library && lib, std::size_t size, Args...args)
348 : _lib(boost::move(lib)),
349 _data(make_data<Args...>(lib, size, static_cast<Args>(args)...)),
350 _is_allocating(true),
351 _size(size),
352 _ti(lib.get_type_info<T>())
353{
354
355}
356
357template<typename T>
358inline imported_class<T> boost::dll::experimental::imported_class<T>::copy() const
359{
360 if (this->_is_allocating)
361 return imported_class<T>::template make<const T&>(_lib, *_data);
362 else
363 return imported_class<T>::template make<const T&>(_lib, _size, *_data);
364}
365
366template<typename T>
367inline imported_class<T> boost::dll::experimental::imported_class<T>::move()
368{
369 if (this->_is_allocating)
370 return imported_class<T>::template make<T&&>(_lib, *_data);
371 else
372 return imported_class<T>::template make<T&&>(_lib, _size, *_data);
373}
374
375template<typename T>
376inline void boost::dll::experimental::imported_class<T>::copy_assign(const imported_class<T>& lhs) const
377{
378 this->call<T&(const T&)>("operator=")(*lhs._data);
379}
380
381template<typename T>
382inline void boost::dll::experimental::imported_class<T>::move_assign(imported_class<T>& lhs)
383{
384 this->call<T&(T&&)>("operator=")(static_cast<T&&>(*lhs._data));
385}
386
387
388
389/*!
390* Returns an instance of \ref imported_class which allows to call or import more functions.
391* It takes a copy of the smart_libray, so no added type_aliases will be visible,
392* for the object.
393*
394* Few compilers do implement an allocating constructor, which allows the construction
395* of the class without knowing the size. That is not portable, so the actual size of the class
396* shall always be provided.
397*
398* \b Example:
399*
400* \code
401* auto import_class<class type_alias, const std::string&, std::size_t>(lib, "class_name", 20, "param1", 42);
402* \endcode
403*
404* In this example we construct an instance of the class "class_name" with the size 20, which has "type_alias" as an alias,
405* through a constructor which takes a const-ref of std::string and an std::size_t parameter.
406*
407* \tparam T Class type or alias
408* \tparam Args Constructor argument list.
409* \param lib Path to shared library or shared library to load function from.
410* \param name Null-terminated C or C++ mangled name of the function to import. Can handle std::string, char*, const char*.
411* \param mode An mode that will be used on library load.
412*
413* \return class object.
414*
415* \throw boost::system::system_error if symbol does not exist or if the DLL/DSO was not loaded.
416* Overload that accepts path also throws std::bad_alloc in case of insufficient memory.
417*/
418template<typename T, typename ... Args> imported_class<T>
419import_class(const smart_library& lib_, std::size_t size, Args...args)
420{
421 smart_library lib(lib_);
422
423 return imported_class<T>::template make<Args...>(boost::move(lib), size, static_cast<Args>(args)...);
424}
425
426//! \overload boost::dll::import_class(const smart_library& lib, std::size_t, Args...)
427template<typename T, typename ... Args> imported_class<T>
428import_class(const smart_library& lib_, Args...args)
429{
430 smart_library lib(lib_);
431 return imported_class<T>::template make<Args...>(boost::move(lib), static_cast<Args>(args)...);
432}
433
434//! \overload boost::dll::import_class(const smart_library& lib, std::size_t, Args...)
435template<typename T, typename ... Args> imported_class<T>
436import_class(const smart_library& lib_, const std::string & alias_name, Args...args)
437{
438 smart_library lib(lib_);
439 lib.add_type_alias<T>(alias_name);
440 return imported_class<T>::template make<Args...>(boost::move(lib), static_cast<Args>(args)...);
441}
442
443//! \overload boost::dll::import_class(const smart_library& lib, std::size_t, Args...)
444template<typename T, typename ... Args> imported_class<T>
445import_class(const smart_library& lib_, std::size_t size, const std::string & alias_name, Args...args)
446{
447 smart_library lib(lib_);
448
449 lib.add_type_alias<T>(alias_name);
450 return imported_class<T>::template make<Args...>(boost::move(lib), size, static_cast<Args>(args)...);
451}
452
453//! \overload boost::dll::import_class(const smart_library& lib, std::size_t, Args...)
454template<typename T, typename ... Args> imported_class<T>
455import_class(const smart_library& lib_, const std::string & alias_name, std::size_t size, Args...args)
456{
457 smart_library lib(lib_);
458
459 lib.add_type_alias<T>(alias_name);
460 return imported_class<T>::template make<Args...>(boost::move(lib), size, static_cast<Args>(args)...);
461}
462
463//! \overload boost::dll::import_class(const smart_library& lib, std::size_t, Args...)
464template<typename T, typename ... Args> imported_class<T>
465import_class(smart_library && lib, Args...args)
466{
467 return imported_class<T>::template make<Args...>(boost::move(lib), static_cast<Args>(args)...);
468}
469
470//! \overload boost::dll::import_class(const smart_library& lib, std::size_t, Args...)
471template<typename T, typename ... Args> imported_class<T>
472import_class(smart_library && lib, const std::string & alias_name, Args...args)
473{
474 lib.add_type_alias<T>(alias_name);
475 return imported_class<T>::template make<Args...>(boost::move(lib), static_cast<Args>(args)...);
476}
477
478//! \overload boost::dll::import_class(const smart_library& lib, std::size_t, Args...)
479template<typename T, typename ... Args> imported_class<T>
480import_class(smart_library && lib, std::size_t size, Args...args)
481{
482 return imported_class<T>::template make<Args...>(boost::move(lib), size, static_cast<Args>(args)...);
483}
484
485//! \overload boost::dll::import_class(const smart_library& lib, std::size_t, Args...)
486template<typename T, typename ... Args> imported_class<T>
487import_class(smart_library && lib, std::size_t size, const std::string & alias_name, Args...args)
488{
489 lib.add_type_alias<T>(alias_name);
490 return imported_class<T>::template make<Args...>(boost::move(lib), size, static_cast<Args>(args)...);
491}
492
493//! \overload boost::dll::import_class(const smart_library& lib, std::size_t, Args...)
494template<typename T, typename ... Args> imported_class<T>
495import_class(smart_library && lib, const std::string & alias_name, std::size_t size, Args...args)
496{
497 lib.add_type_alias<T>(alias_name);
498 return imported_class<T>::template make<Args...>(boost::move(lib), size, static_cast<Args>(args)...);
499}
500
501
502
503/*! \overload boost::dll::import_class(const smart_library& lib, std::size_t, Args...)
504 * \note This function does add the type alias to the \ref boost::dll::experimental::smart_library.
505 */
506
507template<typename T, typename ... Args> imported_class<T>
508import_class(smart_library & lib, Args...args)
509{
510 return imported_class<T>::template make<Args...>(lib, static_cast<Args>(args)...);
511}
512
513/*! \overload boost::dll::import_class(const smart_library& lib, std::size_t, Args...)
514 * \note This function does add the type alias to the \ref boost::dll::experimental::smart_library.
515 */
516template<typename T, typename ... Args> imported_class<T>
517import_class(smart_library & lib, const std::string & alias_name, Args...args)
518{
519 lib.add_type_alias<T>(alias_name);
520 return imported_class<T>::template make<Args...>(lib, static_cast<Args>(args)...);
521}
522
523/*! \overload boost::dll::import_class(const smart_library& lib, std::size_t, Args...)
524 * \note This function does add the type alias to the \ref boost::dll::experimental::smart_library.
525 */
526template<typename T, typename ... Args> imported_class<T>
527import_class(smart_library & lib, std::size_t size, Args...args)
528{
529 return imported_class<T>::template make<Args...>(lib, size, static_cast<Args>(args)...);
530}
531
532/*! \overload boost::dll::import_class(const smart_library& lib, std::size_t, Args...)
533 * \note This function does add the type alias to the \ref boost::dll::experimental::smart_library.
534 */
535template<typename T, typename ... Args> imported_class<T>
536import_class(smart_library & lib, std::size_t size, const std::string & alias_name, Args...args)
537{
538 lib.add_type_alias<T>(alias_name);
539 return imported_class<T>::template make<Args...>(lib, size, static_cast<Args>(args)...);
540}
541
542/*! \overload boost::dll::import_class(const smart_library& lib, std::size_t, Args...)
543 * \note This function does add the type alias to the \ref boost::dll::experimental::smart_library.
544 */
545template<typename T, typename ... Args> imported_class<T>
546import_class(smart_library & lib, const std::string & alias_name, std::size_t size, Args...args)
547{
548 lib.add_type_alias<T>(alias_name);
549 return imported_class<T>::template make<Args...>(lib, size, static_cast<Args>(args)...);
550}
551
552}
553}
554}
555
556
557
558#endif /* BOOST_DLL_IMPORT_CLASS_HPP_ */