123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465 |
- #ifndef BOOST_DLL_SMART_LIBRARY_HPP_
- #define BOOST_DLL_SMART_LIBRARY_HPP_
- #include <boost/dll/config.hpp>
- #if defined(_MSC_VER)
- # include <boost/dll/detail/demangling/msvc.hpp>
- #else
- # include <boost/dll/detail/demangling/itanium.hpp>
- #endif
- #if (__cplusplus < 201103L) && (!defined(_MSVC_LANG) || _MSVC_LANG < 201103L)
- # error This file requires C++11 at least!
- #endif
- #include <boost/dll/shared_library.hpp>
- #include <boost/dll/detail/get_mem_fn_type.hpp>
- #include <boost/dll/detail/ctor_dtor.hpp>
- #include <boost/dll/detail/type_info.hpp>
- #include <boost/type_traits/is_object.hpp>
- #include <boost/type_traits/is_void.hpp>
- #include <boost/type_traits/is_function.hpp>
- namespace boost {
- namespace dll {
- namespace experimental {
- using boost::dll::detail::constructor;
- using boost::dll::detail::destructor;
- class smart_library {
- shared_library _lib;
- detail::mangled_storage_impl _storage;
- public:
-
- const shared_library &shared_lib() const {return _lib;}
- using mangled_storage = detail::mangled_storage_impl;
-
- const mangled_storage &symbol_storage() const {return _storage;}
-
- mangled_storage &symbol_storage() {return _storage;}
-
- smart_library() BOOST_NOEXCEPT {};
-
- smart_library(const boost::dll::fs::path& lib_path, load_mode::type mode = load_mode::default_mode) {
- _lib.load(lib_path, mode);
- _storage.load(lib_path);
- }
-
- smart_library(const boost::dll::fs::path& lib_path, boost::dll::fs::error_code& ec, load_mode::type mode = load_mode::default_mode) {
- load(lib_path, mode, ec);
- }
-
- smart_library(const boost::dll::fs::path& lib_path, load_mode::type mode, boost::dll::fs::error_code& ec) {
- load(lib_path, mode, ec);
- }
-
- smart_library(const smart_library & lib) BOOST_NOEXCEPT
- : _lib(lib._lib), _storage(lib._storage)
- {}
-
- smart_library(BOOST_RV_REF(smart_library) lib) BOOST_NOEXCEPT
- : _lib(boost::move(lib._lib)), _storage(boost::move(lib._storage))
- {}
-
- explicit smart_library(const shared_library & lib) BOOST_NOEXCEPT
- : _lib(lib)
- {
- _storage.load(lib.location());
- }
-
- explicit smart_library(BOOST_RV_REF(shared_library) lib) BOOST_NOEXCEPT
- : _lib(boost::move(static_cast<shared_library&>(lib)))
- {
- _storage.load(lib.location());
- }
-
- ~smart_library() BOOST_NOEXCEPT {};
-
- void load(const boost::dll::fs::path& lib_path, load_mode::type mode = load_mode::default_mode) {
- boost::dll::fs::error_code ec;
- _storage.load(lib_path);
- _lib.load(lib_path, mode, ec);
- if (ec) {
- boost::dll::detail::report_error(ec, "load() failed");
- }
- }
-
- void load(const boost::dll::fs::path& lib_path, boost::dll::fs::error_code& ec, load_mode::type mode = load_mode::default_mode) {
- ec.clear();
- _storage.load(lib_path);
- _lib.load(lib_path, mode, ec);
- }
-
- void load(const boost::dll::fs::path& lib_path, load_mode::type mode, boost::dll::fs::error_code& ec) {
- ec.clear();
- _storage.load(lib_path);
- _lib.load(lib_path, mode, ec);
- }
-
- template<typename T>
- T& get_variable(const std::string &name) const {
- return _lib.get<T>(_storage.get_variable<T>(name));
- }
-
- template<typename Func>
- Func& get_function(const std::string &name) const {
- return _lib.get<Func>(_storage.get_function<Func>(name));
- }
-
- template<typename Class, typename Func>
- typename boost::dll::detail::get_mem_fn_type<Class, Func>::mem_fn get_mem_fn(const std::string& name) const {
- return _lib.get<typename boost::dll::detail::get_mem_fn_type<Class, Func>::mem_fn>(
- _storage.get_mem_fn<Class, Func>(name)
- );
- }
-
- template<typename Signature>
- constructor<Signature> get_constructor() const {
- return boost::dll::detail::load_ctor<Signature>(_lib, _storage.get_constructor<Signature>());
- }
-
- template<typename Class>
- destructor<Class> get_destructor() const {
- return boost::dll::detail::load_dtor<Class>(_lib, _storage.get_destructor<Class>());
- }
-
- template<typename Class>
- const std::type_info& get_type_info() const
- {
- return boost::dll::detail::load_type_info<Class>(_lib, _storage);
- }
-
- template<typename Alias> void add_type_alias(const std::string& name) {
- this->_storage.add_alias<Alias>(name);
- }
-
- void unload() BOOST_NOEXCEPT {
- _storage.clear();
- _lib.unload();
- }
-
- bool is_loaded() const BOOST_NOEXCEPT {
- return _lib.is_loaded();
- }
-
- bool operator!() const BOOST_NOEXCEPT {
- return !is_loaded();
- }
-
- BOOST_EXPLICIT_OPERATOR_BOOL()
-
- bool has(const char* symbol_name) const BOOST_NOEXCEPT {
- return _lib.has(symbol_name);
- }
-
- bool has(const std::string& symbol_name) const BOOST_NOEXCEPT {
- return _lib.has(symbol_name);
- }
-
- smart_library& assign(const smart_library& lib) {
- _lib.assign(lib._lib);
- _storage.assign(lib._storage);
- return *this;
- }
-
- void swap(smart_library& rhs) BOOST_NOEXCEPT {
- _lib.swap(rhs._lib);
- _storage.swap(rhs._storage);
- }
- };
- inline bool operator==(const smart_library& lhs, const smart_library& rhs) BOOST_NOEXCEPT {
- return lhs.shared_lib().native() == rhs.shared_lib().native();
- }
- inline bool operator!=(const smart_library& lhs, const smart_library& rhs) BOOST_NOEXCEPT {
- return lhs.shared_lib().native() != rhs.shared_lib().native();
- }
- inline bool operator<(const smart_library& lhs, const smart_library& rhs) BOOST_NOEXCEPT {
- return lhs.shared_lib().native() < rhs.shared_lib().native();
- }
- inline void swap(smart_library& lhs, smart_library& rhs) BOOST_NOEXCEPT {
- lhs.swap(rhs);
- }
- #ifdef BOOST_DLL_DOXYGEN
- template<class T, class T2>
- void get(const smart_library& sm, const std::string &name);
- #endif
- template<class T>
- typename boost::enable_if<boost::is_object<T>, T&>::type get(const smart_library& sm, const std::string &name)
- {
- return sm.get_variable<T>(name);
- }
- template<class T>
- typename boost::enable_if<boost::is_function<T>, T&>::type get(const smart_library& sm, const std::string &name)
- {
- return sm.get_function<T>(name);
- }
- template<class Class, class Signature>
- auto get(const smart_library& sm, const std::string &name) -> typename detail::get_mem_fn_type<Class, Signature>::mem_fn
- {
- return sm.get_mem_fn<Class, Signature>(name);
- }
- }
- }
- }
- #endif
|