是否有适当的'所有权在一个包'的'手柄'可用? [英] Is there a proper 'ownership-in-a-package' for 'handles' available?

查看:179
本文介绍了是否有适当的'所有权在一个包'的'手柄'可用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

句柄 具有除指针之外的适当语义。所以对我来说这样一个例子(摘自 规则of ):

 类模块{
public:
explicit模块(std :: wstring const& name)
:handle {:: LoadLibrary(name.c_str()),& :: FreeLibrary} {}

//其他模块相关函数go here

private:
using module_handle = std :: unique_ptr< void,decltype(& :: FreeLibrary)>

module_handle handle;
};

使用 unique_ptr -a-package'是一个坏的例子。首先,它利用内部知识,句柄是一个指针类型,并使用它来创建一个 unique_ptr 基本类型opaque句柄类型。 / p>

句柄可以是任何类型,它们可以是指针,它们可以是索引或谁知道。最重要的是,你手头上的(例如大多数C API)是一个句柄及其资源释放功能。



in-a-package'in in handle semantics?



对我来说, unique_ptr et al。不工作,我必须做什么句柄类型是不必要的假设,当我想要的只是通过不透明句柄类型和释放的一个所有者在一个包函数。



在句柄类型内部对这些信息进行构造没有意义。



我会在这里引用其他问题 answer


创建一个特定的智能指针类,不会花费很长时间。不要滥用
库类。句柄语义与
C ++指针非常不同;一个事情,解除引用一个HANDLE没有意义。



使用自定义智能句柄类的另一个原因 - NULL不是
总是意味着一个空句柄。有时它是INVALID_HANDLE_VALUE,
这是不一样的。


免责声明:

这个问题根据这个问题重新制定和构建:




解决方案

类型 unique_ptr 不如短语handle一般,是。但为什么不应该呢?只有一个句柄示例(例如,一个整数索引),正如 unique_ptr 一样。你不能比较一个特定类型的句柄和所有句柄。



如果你想要一个单一的,具体的C ++类型(或类型模板)没有实际定义任何特定的处理语义,然后...我不能帮助你。我不认为任何人都可以。


Handles have proper semantics other than pointers. So for me an example like this (extracted from the Rule of Zero):

class module {
public:
    explicit module(std::wstring const& name)
    : handle { ::LoadLibrary(name.c_str()), &::FreeLibrary } {}

    // other module related functions go here

private:
    using module_handle = std::unique_ptr<void, decltype(&::FreeLibrary)>;

    module_handle handle;
};

using unique_ptr as an 'ownership-in-a-package' for handles is a bad example. First, it makes use of internal knowledge that the handle is a pointer type, and use this to make a unique_ptr to the basic type the "opaque" handle type builds upon.

Handles can be any type, they may be a pointer, they may be an index or who knows. Most importantly, what you have at hand (from most C API's for example) is a handle and its resource releasing function.

Is there a proper 'ownership-in-a-package' that works in handle semantics? I mean, already publicly available for one to use?

For me, unique_ptr et. al. doesn't work, I must make unnecessary assumptions about what the handle type is, when what I want is just to get an 'ownership-in-a-package' through the opaque handle type and its releasing function, solely.

It doesn't make sense for one to peer inside the handle type to make constructions upon this information. It's a handle, it should not matter.

I'll quote here the feelings of another SO user in another question's answer:

Create a specific "smart pointer" class, won't take long. Don't abuse library classes. Handle semantics is quite different from that of a C++ pointer; for one thing, dereferencing a HANDLE makes no sense.

One more reason to use a custom smart handle class - NULL does not always mean an empty handle. Sometimes it's INVALID_HANDLE_VALUE, which is not the same.

Disclaimer:

This question reformulates and builds upon this one:

解决方案

The type unique_ptr is less general than the phrase "handle", yes. But why shouldn't it be? Just one of your "handle" examples (say, the one that is an integer index), is precisely as general as unique_ptr. You can't compare one specific kind of handle with "all handles ever".

If you want a single, concrete C++ type (or type template) that is a handle without actually defining any specific handling semantics then... I can't help you. I don't think anyone tractibly could.

这篇关于是否有适当的'所有权在一个包'的'手柄'可用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆