gpt4 book ai didi

c - 将 Objective-C 类包装在 C 结构中以反射(reflect)类名

转载 作者:行者123 更新时间:2023-12-04 10:21:01 25 4
gpt4 key购买 nike

有没有办法将 Objective-C 类包装在 C 中 struct使得 struct名字反射(reflect)了类(class)的名字?我理想地希望模仿使用 extern "C" in C++ as shown here 的行为, 而不是必须指向 TestObject 类型的结构体的成员,在下面的简单程序中进行了演示。

将 Objective-C 类包装到 C 结构体

testobject.m

#ifndef testobject_h
#define testobject_h

#ifdef __OBJC__
@interface TestObject : NSObject
- (void) printHello;
@end
#endif

typedef struct TestObjectC TestObjectC;
TestObjectC* newTestObject(void);
void TestObject_printHello(TestObjectC *t);

#endif /* testobject_h */

testobject.m

#import <Foundation/Foundation.h>
#import "testobject.h"

@implementation TestObject

- (void)printHello
{
printf("HELLO!\n");
}

@end

typedef struct TestObject_t
{
TestObject *testObject;
}TestObjectC;

TestObjectC* newTestObject(void)
{
TestObjectC *t = malloc(sizeof(TestObjectC));
t->testObject = [TestObject new];
return t;
}

void TestObject_printHello(TestObjectC* t)
{
[t->testObject printHello];
}


main.c

#include "testobject.h"

int main(int argc, const char * argv[])
{
TestObjectC *t = newTestObject();
TestObject_printHello(t);
return 0;
}

将 C++ 类包装到 C 结构体

TestClass.h

#ifndef __TESTCLASS_H
#define __TESTCLASS_H

class TestClass
{
public:
void printHello(void);
};

#endif

TestClass.cc

#include "TestClass.h"
void TestClass::printHello(void)
{
printf("HELLO!\n");
}

MyWrapper.h

#ifndef __MYWRAPPER_H
#define __MYWRAPPER_H

#ifdef __cplusplus
extern "C" {
#endif

typedef struct TestClass TestClass;

TestClass* newTestClass();
void TestClass_printHello(TestClass* v);
void deleteTestClass(TestClass* v);

#ifdef __cplusplus
}
#endif
#endif

MyWrapper.cc

#include "TestClass.h"
#include "MyWrapper.h"

extern "C"
{
TestClass* newTestClass()
{
return new TestClass();
}

void TestClass_int_set(TestClass* v, int i)
{
v->printHello();
}
void deleteTestClass(TestClass* v)
{
delete v;
}
}

C程序

#include "MyWrapper.h"
#include <stdio.h>

int main(int argc, char* argv[])
{
struct TestClass* c = newTestClass();
TestClass_printHello(c);
deleteTestClass(c);
}

最佳答案

这里不需要包装器。 ObjC 对象可以毫无困难地通过 C 来回传输,甚至可以支持 ARC。我将在这里稍微更改样式以匹配 CoreFoundation。我将再次编写它以显示它不会将调用者暴露给 CoreFoundation 的样子。

主文件

int main(int argc, const char * argv[])
{
// "Ref" means pointer. ...Create() means you must Release it.
TestObjectRef t = TestObjectCreate();

// Call your function
TestObjectPrintHello(t);

// And release it.
CFRelease(t);
return 0;
}

测试对象.h
#ifndef testobject_h
#define testobject_h

#import <CoreFoundation/CoreFoundation.h>

// Only define the ObjC part in ObjC
#ifdef __OBJC__
#import <Foundation/Foundation.h>

@interface TestObject : NSObject
- (void) printHello;
@end

#endif

// This is the "void *" of CoreFoundation, but it lets the compiler know this
// this is a bridged ObjC object. This is the standard CF way of naming pointer types.
typedef CFTypeRef TestObjectRef;

// This is the standard CF way of naming functions. "Create" means you need to release it.
TestObjectRef TestObjectCreate(void);

// CF functions always start with their class
void TestObjectPrintHello(TestObjectRef t);

#endif /* testobject_h */

测试对象.m
#import "testobject.h"

@implementation TestObject

- (void)printHello
{
printf("HELLO!\n");
}

@end

// ARC is no problem. We just need to tell ARC that we intend to keep an extra retain.
TestObjectRef TestObjectCreate(void) {
return CFBridgingRetain([TestObject new]);
}

// And we can get back to ObjC using `__bridge`
void TestObjectPrintHello(TestObjectRef t) {
[(__bridge TestObject *)t printHello];
}

该解决方案将 CoreFoundation.h 泄露给调用者,并强加了 CF 风格的命名。我碰巧在我的系统中非常喜欢它。但有时我正在处理现有的跨平台 C 系统,而您想隐藏这些细节。因此,如果我试图匹配您示例中的样式,我会这样做:

主文件
int main(int argc, const char * argv[])
{
TestObjectC *t = newTestObject();
TestObject_printHello(t);
TestObject_release(t); // Still need memory management. Can't avoid that.
return 0;
}

测试对象.h
#ifndef testobject_h
#define testobject_h

#ifdef __OBJC__
#import <Foundation/Foundation.h>
@interface TestObject : NSObject
- (void) printHello;
@end
#endif

typedef const void TestObjectC; // 'const void *' is more CF-like, but either is fine

TestObjectC* newTestObject(void);
void TestObject_printHello(TestObjectC *t);
void TestObject_release(TestObjectC *t);

#endif /* testobject_h */

测试对象.m
#import "testobject.h"

@implementation TestObject

- (void)printHello
{
printf("HELLO!\n");
}

@end

TestObjectC* newTestObject(void) {
return CFBridgingRetain([TestObject new]);
}

void TestObject_printHello(TestObjectC *t) {
[(__bridge TestObject *)t printHello];
}
void TestObject_release(TestObjectC *t) {
CFRelease(t);
}

关于c - 将 Objective-C 类包装在 C 结构中以反射(reflect)类名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60849920/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com