使用引用计数逻辑C ++封装API [英] wrapper around API using reference counting logic C++

查看:114
本文介绍了使用引用计数逻辑C ++封装API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将在我的C ++应用程序中使用大量的C-API,我有很多功能,我需要调用在夫妇。例如,createFoo(void *)在开头,freeFoo(void *)当作业完成时。

I am going to use heavily some C-API in my C++ application, I have a lot of functions available that I need to call in couple. For example createFoo(void*) at the beginning and freeFoo(void*) when the job is done.

所以,我想使用RAII成语,所以我创建了第一个包装器与复制构造函数和赋值运算符私有和未实现。它工作正常,但我想要一个更丰富的副本语义。我想使用引用计数复制语义。我考虑写我自己的版本,但我不想重塑的轮子。另外boost :: shared_ptr已经实现了我想要实现的行为。

So, I thought to use the RAII idiom, so I created the first wrapper with copy constructor and assignment operator private and not implemented. It works fine, however I would like a richer copy semantic. I would like to use a reference counting copy semantic. I have considered to write my own version but I don't want reinvent the wheel. Also the boost::shared_ptr is already implementing pretty much the behave that I want to achieve.

唯一的区别是指针不是用新创建的,它是不释放删除。我想自定义源和sink函数。

The only difference is the pointer is not create with new and it is not release with delete. I would like to customize the source and the sink functions.

我觉得这是一个很好的方法来面对这个问题,但是我不能想出一个类实现我的想法。

I have the feeling that is a good way to face the problem, however I cannot come up with a class that implement my idea.

这里有问题,你认为这是一个很好的方式来解决我的问题吗?有什么开源代码实现类似的东西吗?你有什么提示吗?

Here are the questions, do you think it is a good way to solve my problem? Is there any open source code that implement something similar? Do you have any hints?

感谢

推荐答案

。例如,假设你有C fopen接口:

It's really easy. As an example, suppose you have the C fopen interface:

FILE* fopen(blah);
int fclose(FILE*);

然后你可以像这样用shared_ptr包装FILE资源:

Then you can wrap FILE resource with shared_ptr like this:

shared_ptr<FILE> ptr(fopen("file.txt", "rt"), fclose);
// use the file pointed by ptr
fwrite(..., ptr.get());

// FILE is automatically closed by a call to fclose when 
// reference count drops to zero...

编辑:让我解释一下。 shared_ptr 所有权和所有权弱的参考计数器。删除器是可以使用指向要释放作为参数的资源的指针来调用的任何对象。默认的删除器只是在其参数上调用 delete ,但是你可以传递自己的删除器。

Let me explain a bit.shared_ptr stores a 'deleter' along with the reference counters for ownership and weak ownership. A deleter is any object that can be called with the pointer to the resource to be freed as an argument. The default deleter simply calls delete on its argument, however you can pass your own deleter.

技术上面,我通常将创建包装成一个单独的函数。这简化了代码,并允许我检查从C风格的API返回的错误代码并将其转换为异常:

When I apply the technique above, I usually wrap the creation into a separate function. This simplifies the code and allows me to check for error-codes returned from C-style APIs and convert them to exception:

shared_ptr<FILE> MakeFile(const char* name, const char* mod)
{
    if(FILE *ptr = fopen(name, mod))
        return shared_ptr<FILE>(ptr, fclose);

    throw SomeExceptionType("fopen", errno, ...);
}

这篇关于使用引用计数逻辑C ++封装API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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