将objective-c类封装为c++类:最佳实践

Wrapping objective-c classes into c++ classes: best practices?

本文关键字:最佳 objective-c 封装 c++      更新时间:2023-10-16

我有一个在objective-c++(.mm)文件中实现的c++类。这个类包含一些Cocoa对象,比如NSToolbar,作为(私有)成员变量。该类应公开为纯c++接口,并可由纯c++客户端使用。换句话说,我正在尝试将obj-c对象包装在一个c++类中。

我想到的第一件事是在类接口中使用void指针然后每当_toolbar需要被视为NSToolbar时,在类实现中进行强制转换。

例如,我会有一个接口:

// NSToolbarWrapper.h
class NSToolbarWrapper {
private:
void * _toolbar;
//... rest of the class ...
}

以及实现:

// NSToolbarWrapper.mm
...
ToolbarWrapper::ToolbarWrapper (...){
_toolbar = (__bridge void *)[[NSToolbar alloc] initWithIdentifier:@"My toolbar!"];
...
}

我不确定这是最聪明的方法。在这种情况下有最佳实践吗?

带有c++接口和目标c++实现的Pimpl习语。如果使用unique_ptr作为pimpl,则需要声明析构函数并在.mm文件中定义它;

h级:

class myclass {
    class impl;
    std::unique_ptr<impl> _impl; // or shared_ptr if you want shared handle behaviour
public:
    myclass(const char* s);
    ~myclass(); // only declare here
};

class.mm:

class myclass::impl {
    NSString* _str;
public:
    impl(const char* s) 
    : _str([NSString stringWithCString:s encoding: NSASCIIStringEncoding]) 
    {}
};
myclass::myclass(const char* s)
: _impl(new impl(s))
{}
myclass::~myclass() = default;  // define here

为了文档,并在将来提醒自己。

我见过一个非常著名的开源库这样做,这似乎也是一种有效的方法:

// NSToolbarWrapper.h
#ifdef __OBJC__
typedef NSToolbar *Toolbar;
#else
typedef class NSToolbar_opaque *Toolbar;
#endif // __OBJC__
class NSToolbarWrapper {
private:
Toolbar _toolbar;
//... rest of the class ...
}