使用自定义删除器返回unique_ptr的工厂函数
Factory function returning unique_ptr with custom deleter
我正在制作一个辅助函数,用于加载共享库并将结果放入带有自定义删除器(即模块销毁函数(的std::unique_ptr
中。
当我不设置自定义删除器时,这工作正常,但是一旦我添加它们,我就会收到一个错误,指出无法推断出自定义删除器的类型(这是公平的(。
问题是,如果我在调用函数时在哪里指定删除器类型,它最终会看起来非常丑陋。
问题是,我的函数如何自动推断自定义删除程序的类型?我应该如何声明结果std::unique_ptr
的std::vector
?
我的代码(我也愿意接受有关代码的任何建议(:
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;
}
编写删除程序类型和工厂。
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};
}
我自己,我会写一个更好的句柄。 请注意,您的代码会疯狂地泄漏动态库句柄。
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 ) );
}
现在我们得到:
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};
}
用户在需要符号时应提供libhandle_ptr
。
请注意,boost 有一个 dll 符号加载库,它可能做得更好。
相关文章:
- 派生类是否可以在抽象工厂设计模式中具有数据成员
- CLANG 编译器 说:变量"PTR"可能未初始化
- 使用Unique_ptr确保工厂中的对象唯一
- 带有继承的C++工厂
- 在以唯一ptr为值的C++映射中,动态内存何时会被销毁
- 将 ptr 传递给 ptr 到 A 作为参数传递给 A 的函数是不好的做法吗?
- 为共享 ptr 向量实现复制 c'tor?
- 如何在工厂方法中返回指向基于基础操作系统的派生类的有效指针
- 字符和整数中 **(ptr+1) 的值差异
- C++:在不中断共享的情况下通过引用传递共享 PTR?
- 如何将派生类从基 ptr 分配给 nlohmann::json
- 工厂方法:分配和strcpy_s的差异
- C++库和自注册类:客户端应用程序中的工厂映射为空
- 引用 std::shared:ptr 以避免引用计数
- 这个工厂类在这个C++视频中的意义何在?
- 此工厂功能有什么问题?
- 注册对对象工厂的调用会导致段错误
- 工厂设计模式优化
- 为什么我不能在不进行任何转换的情况下将浮点数放入任何类型的 ptr 中?
- 在 C++ 中返回新的构造函数(*this)工厂