带有 Objective-C++ 回调的 C++ API
c++ api with callbacks to objective-c++
我正在尝试用objective-c ++包装一个c ++库,以便我可以在iOS应用程序中使用它。该库尚未完成,只有我将使用的头文件的规范和草稿。
该接口具有函数 Result initialize(const char* oemUser, const char* oemPassword, Callback* pCallback);
其中 pCallback 将以不同的状态和进度调用。 Callback
是一个纯粹的virtual Class
。
我一直在遵循一些指南来包装代码,我想我了解如何正确包装它,以便我可以从Objective-C++
调用C++
函数,但不能反过来。
一些代码:
foo.hpp (c++ api-headers)
#ifndef FOO_API_H
#define FOO_API_H
namespace foo {
namespace bar {
enum struct Result
{
Ok,
Error,
};
enum struct State
{
Uninitialized,
Done,
kError,
};
class Callback
{
public:
virtual ~Callback() {}
virtual Result enumerateEndpoints(int index,
char* info,
unsigned length) const = 0;
virtual Result processState(State state,
float progress,
bool reqNext) = 0;
};
/** Initialize - initializes the FOO library
param oemUser OEM user for validating library
param oemPassword OEM password for validating library
param pCallback Pointer to an object implementing Callback
interface
return Result
*/
Result initialize(const char* oemUser,
const char* oemPassword,
Callback* pCallback);
Result terminate();
State getCurrentState();
Result getLastError(char* buffer, unsigned length);
}
}
#endif
FooApiControlWrapper.h (My main api wrapper i obj-c++)
#import <Foundation/Foundation.h>
#include "FooApiCallbackWrapper.h"
#include "foo_api.hpp"
@interface FooApiControlWrapper : NSObject
- (foo::bar::Result)initialize:(NSString*)oemUser with:(NSString*)oemPassword using:(foo::bar::Callback*)callback;
- (foo::bar::Result)terminate;
- (foo::bar::State)getCurrentState;
- (foo::bar::Result)getLastError:(NSString*)buffer lengt:(NSInteger*)length;
@end
FooApiControlWrapper.mm
在这里你可以看到我正在为 obj-c++ init 参数提供foo::bar::Callback
,但不知何故我认为这应该是一个Obj-c++ Object
。
#import "FooApiControlWrapper.h"
#include "foo_api.hpp"
@implementation FooApiControlWrapper
- (foo::bar::Result)initialize:(NSString*)oemUser with:(NSString*)oemPassword using:(foo::bar::Callback*)callback {
return foo::bar::initialize([oemUser UTF8String], [oemPassword UTF8String], callback);
}
- (foo::bar::Result)terminate {
return foo::bar::Result::Ok;
}
- (foo::bar::State)getCurrentState {
return foo::bar::State::Uninitialized;
}
- (foo::bar::Result)getLastError:(NSString*)buffer lengt:(NSInteger*)length {
return foo::bar::Result::Ok;
}
@end
FooApiCallbackWrapper.h (My wrapper for the callback class)
#import <Foundation/Foundation.h>
#include "foo_api.hpp"
@interface FooApiCallbackWrapper : NSObject
- (instancetype) init;
- (foo::bar::Result)enumerateEndpoints:(NSInteger*)index with:(NSString*)info and:(NSInteger*)length;
- (foo::bar::Result)processState:(foo::bar::State)state progress:(float)progress reqNext:(Boolean)reqNext;
@end
FooApiCallbackWrapper.mm
#import "FooApiCallbackWrapper.h"
#include "foo_api.hpp"
@implementation FooApiCallbackWrapper
- (instancetype) init{
self = [super init];
return self;
}
- (void)dealloc{
}
- (foo::bar::Result)enumerateEndpoints:(NSInteger*)index with:(NSString*)info and:(NSInteger*)length {
return foo::bar::Result::Ok;
}
- (foo::bar::Result)processState:(foo::bar::State)state progress:(float)progress reqNext:(Boolean)reqNext {
return foo::bar::Result::Ok;
}
@end
我只是希望能够在 Obj-C++ 中编写一个回调,稍后将由我的 C++-API 调用。在哪里以及如何进行?
部分(库外部(添加一个普通的 C 或 C++ 函数,并让该函数(编译为 Objective-C++(与 Obj-C++ 的东西一起运行,即在 Obj-C++-Object 上执行方法。如果需要传递对对象的引用,通常可以使用一些 void *。或者,可以使用一些单例/查找模式。
- 用于访问容器<T>数据成员的正确 API
- 如何使用Luacneneneba API正确读取字符串和表参数
- C++MySQL C api用户输入行
- 如何使用 AWS Transcribe C++ API 中的'StartTranscriptionJobRequest'?
- 将std::string传递给WriteConsole API
- EvtExportLogneneneba API正在将远程计算机的事件日志保存到远程PC本身.如何将其保存到主机
- 在gtest.中使用fff.h模拟系统API
- 有没有任务栏API可以立即应用注册表更改
- C++win32 API创建多个类似视口的窗口
- 使用libcurl提交批量url的正确BING Api POST url是什么
- 如何将真正的字符串从python c-api转换为python脚本
- 使用Qt框架在c ++类中创建API调用
- Libreoffice API (UNO):需要更改用户的 xTextField 文本
- 使用 WIN32 API (C/C++) 对特定树视图项进行着色
- 使用 Python Extension API 包装复杂C++类
- 如何使用 samtools C API 构建一个简单的主.cpp文件
- 第三方 API 中的编译错误 - Visual Studio
- Tensorflow c++ api undefined reference to 'tflite::D efaultErrorReporter()'
- clang 的 libFuzzer 可以在同一二进制文件中测试超过 1 个 API 吗?
- Issues with Win32 ReadProcessMemory API