带有 Objective-C++ 回调的 C++ API

c++ api with callbacks to objective-c++

本文关键字:C++ API 回调 Objective-C++ 带有      更新时间:2023-10-16

我正在尝试用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 调用。在哪里以及如何进行?

请记住,在 Objective-C++ 中允许C++,因此标准方法是在 Objective-C++

部分(库外部(添加一个普通的 C 或 C++ 函数,并让该函数(编译为 Objective-C++(与 Obj-C++ 的东西一起运行,即在 Obj-C++-Object 上执行方法。如果需要传递对对象的引用,通常可以使用一些 void *。或者,可以使用一些单例/查找模式。