I'm making a helper function for loading shared libs and putting the result into a std::unique_ptr
with a custom deleter (which is the module destruction function).
This works fine when I'm not setting custom deleter but as soon as I add them I get an error saying that the type of the custom deleter cannot be deduced (which is fair).
The thing is, if I where to specify the deleter type when I call the function, it will end up looking very ugly.
The question is, how can my function deduce automatically the type of the custom deleter ? And how should I declare a std::vector
of the resulting std::unique_ptr
s ?
My code (I'm also open for any suggestion on the code):
template <typename T, typename D>
std::unique_ptr<T, D> openLib(const std::string &lib_path,
const std::string &csym = "create",
const std::string &dsym = "destroy")
{
void *handle;
if (!(handle = dlopen(lib_path.c_str(), RTLD_LAZY)))
{
std::cerr << "dlopen " << dlerror() << std::endl;
return nullptr;
}
T *(*create)();
if (!(create = (T * (*)()) dlsym(handle, csym.c_str())))
{
std::cerr << "dlsym " << csym << dlerror() << std::endl;
return nullptr;
}
void (*destroy)();
if (!(destroy = (void (*)()) dlsym(handle, dsym.c_str())))
{
std::cerr << "dlsym " << dsym << dlerror() << std::endl;
return nullptr;
}
auto cDel = [destroy](T *lib) { destroy(); };
std::unique_ptr<T, decltype(cDel)> lib_ptr((T *)create(), cDel);
return lib_ptr;
}
Write a deleter type and factory.
using deleter = void();
using ptr_deleter = deleter*;
struct lib_unloader {
deleter* del = 0;
void operator()(void*)const { if (del) del(); }
explicit operator bool() const { return del; }
};
lib_unloader get_lib_unloader( void* handle, char const* destroy_name ) {
auto del = ptr_deleter( dlsym( handle, destroy_name ) );
return {del};
}
template<class T>
using lib_up = std::unique_ptr< T, lib_unloader >;
template<class T>>
lib_up<T> openLib(const std::string &lib_path,
const std::string &csym = "create",
const std::string &dsym = "destroy")
{
void *handle;
if (!(handle = dlopen(lib_path.c_str(), RTLD_LAZY)))
{
std::cerr << "dlopen " << dlerror() << std::endl;
return nullptr;
}
T *(*create)();
if (!(create = (T * (*)()) dlsym(handle, csym.c_str())))
{
std::cerr << "dlsym " << csym << dlerror() << std::endl;
return nullptr;
}
auto destroy = get_lib_unloader( handle, dsym.c_str() );
if (!destroy) {
std::cerr << "dlsym " << dsym << dlerror() << std::endl;
return nullptr;
}
return {create(), destroy};
}
myself, I'd write a better handle. Note that your code leaks dynamic library handles like crazy.
using libhandle_ptr = std::unique_ptr<void, std::integral_constant<int(*)(void*), dlclose>>;
libhandle_ptr make_libhandle(const char* filename, int flags) {
return libhandle_ptr( dlopen(filename, flags) );
}
template<class T>
T* get_sym( libhandle_ptr const& handle, const char* symbol ) {
if (!handle || !symbol) return nullptr;
return static_cast<T*>( dlsym( handle.get(), symbol ) );
}
now we get:
lib_unloader get_lib_unloader( libhandle const& handle, char const* destroy_name ) {
auto del = get_sym<deleter>( handle, destroy_name );
return {del};
}
template<class T>>
lib_up<T> openLib( libhandle_ptr const& handle,
const char* csym = "create",
const char* dsym = "destroy")
{
if (!handle)
return {};
auto create = get_sym<T*()>( handle, csym );
if (!create)
{
std::cerr << "dlsym " << csym << dlerror() << std::endl;
return {};
}
auto destroy = get_lib_unloader( handle, dsym.c_str() );
if (!destroy) {
std::cerr << "dlsym " << dsym << dlerror() << std::endl;
return {};
}
return {create(), destroy};
}
where users are expected to provide a libhandle_ptr
when they wantna symbol.
Note that boost has a dll symbol loading library that probably does a better job.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.