简体   繁体   English

带有回调到objective-c++的c++ api

[英]c++ api with callbacks to objective-c++

I am trying to wrap a c++ library with objective-c++ so i can use it in and iOS application.我正在尝试用 Objective-C++ 包装一个 C++ 库,以便我可以在 iOS 应用程序中使用它。 The library is not finished, only the specifications and a draft of the header file that I will be using.库还没有完成,只有我将使用的头文件的规范和草稿。

The api has a function Result initialize(const char* oemUser, const char* oemPassword, Callback* pCallback); api 有一个函数Result initialize(const char* oemUser, const char* oemPassword, Callback* pCallback);

where pCallback will be called with different states and progress.其中 pCallback 将在不同的状态和进度下被调用。 Callback is a pure virtual Class . Callback是一个纯virtual Class

I have been following some guides to wrap the code, and I think I understand how to properly wrap it so I can call the C++ functions from Objective-C++ but not the other way around.我一直在遵循一些指南来包装代码,我想我明白如何正确包装它,这样我就可以从Objective-C++调用C++函数,而不是相反。

Some code:一些代码:

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

Here you can see that I am providing foo::bar::Callback to the obj-c++ init parameter, but somehow I think this should be an Obj-c++ Object .在这里你可以看到我正在为 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

I just want to be able to write a callback in Obj-C++ that later will be called by my C++-Api.我只想能够在 Obj-C++ 中编写一个回调,稍后将被我的 C++-Api 调用。 Where and how to proceed?在哪里以及如何进行?

Remember that C++ is allowed in Objective-C++, so the standard way is to just add a plain C or C++ function in your Objective-C++ part (outside of the library), and let that function - which is compiled as Objective-C++, operate with the Obj-C++ stuff, ie perform methods on Obj-C++-Objects.请记住,Objective-C++ 中允许使用 C++,因此标准方法是在 Objective-C++ 部分(库之外)添加一个普通的 C 或 C++ 函数,然后让该函数 - 编译为 Objective-C++,操作 Obj-C++ 的东西,即在 Obj-C++-Objects 上执行方法。 If you need to pass References to Objects, some void * will usually do.如果您需要将引用传递给对象,通常会使用一些 void * 。 Alternatively, some singleton / look-up pattern can be used.或者,可以使用一些单例/查找模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM