使用c ++ std :: unique_ptr<>管理Objective-C对象或std :: shared_ptr<> [英] Managing objective-C objects with c++ std::unique_ptr<> or std::shared_ptr<>

查看:86
本文介绍了使用c ++ std :: unique_ptr<>管理Objective-C对象或std :: shared_ptr<>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Objective-C可以在某种程度上与c ++混合使用,并且可以相互调用.但是,Objective-C对象仍然或多或少是手动管理的,该语言完全没有RAII习惯用法.我想知道是否有可能使用c ++智能指针来管理Objective-C对象的生存期.特别是现在,升压scoped_ptrshared_ptr都已添加到C ++ 11标准

Objective-C can be mixed with c++ to some extent and can be called to each other. But Objective-C objects still are more or less manually managed, and RAII idiom is entirely absent from the language. I wonder if it is possible to manage the lifetimes of Objective-C objects with c++ smart pointers. Specially now that both boost scoped_ptr and shared_ptr have been added to the C++11 standard

推荐答案

但是,Objective-C对象仍然或多或少是手动管理的,该语言完全没有RAII习惯用法.

But Objective-C objects still are more or less manually managed, and RAII idiom is entirely absent from the language.

我认为这似乎可以回答您的问题.由于Objective-C对象是按引用计数的,因此它们已经满足了创建智能指针的目的:将对象的生命周期与包含对象的方法的范围脱离或联系在一起.可以使用自动释放池重新创建scoped_ptr,和具有-retain--releasestrong引用的shared_ptr.

I think this would seem to answer your question anyhow. Because Objective-C objects are reference counted, they already fulfill the purpose smart pointers were created for: to divorce or tie the lifetime of an object from the scope of the method it's contained in. scoped_ptrs can be recreated with autorelease pools, and shared_ptrs with -retain--release or strong references.

但是说不无聊.如果您真的想像这样混合Objective-C和C ++,我们首先需要放松"Objective-C对象"的定义.运行时可以识别以isa作为其第一个成员作为对象的任何东西,我们可以利用这一点并编写一个带有相应对象接口的简单C ++类,以便向其发送消息:

But saying no is boring. If you really want to mingle Objective-C and C++ like this, we'll need to first loosen the definition of "Objective-C object". The runtime recognizes anything with an isa sitting as its first member as an object, and we can take advantage of that and write a simple C++ class with a corresponding object interface so it can be messaged:

@interface CFIObject : NSObject
- (void)doSomething;
@end

struct CFIObject_cxx {
    Class isa;
public:
    CFIObject_cxx() : isa([CFIObject class]) {}
~CFIObject_cxx() { printf("I'm dying!"); }
};

@implementation CFIObject
- (void)doSomething {
    NSLog("I did something.");
}
@end

我们现在可以实例化C ++对象的实例并将其包装在智能指针中,我将有目的地将其分为两种方法以说明对象的寿命:

We can now instantiate an instance of our C++ object and wrap it in a smart pointer, which I'm going to purposefully split into two methods to illustrate the lifetime of the object:

void func() {
    // Instantiate a smart pointer with our fake object.
    std::unique_ptr<CFIObject_cxx> cppObj (new CFIObject_cxx());
    id obj = (__bridge id)(cppObj.get());
    // The runtime thinks we're an object.
    [obj doSomething];
    // aaaand, it's out of scope.
}

int main(int argc, const char **argv) {
    func();
    return 0;
}

如人们所料,它将打印:

As one would expect, this prints:

2013-12-22 17:23:22.681 Test[77528:303] I did something
I'm dying!

到控制台.

如果需要,可以配备析构函数来调用-dealloc以模拟适当的对象销毁,但是我希望您看到所有这些都是完全不必要的,尤其是当ARC在每个CLANG版本中变得更聪明时.

If need be, the destructor could be outfitted to call through to -dealloc to simulate proper object destruction, but I hope you see that all of this is just wholly unnecessary, especially with ARC getting smarter with each release of CLANG.

这篇关于使用c ++ std :: unique_ptr&lt;&gt;管理Objective-C对象或std :: shared_ptr&lt;&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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