Furedal
Furedal

Reputation: 533

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. 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);

where pCallback will be called with different states and progress. Callback is a pure 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.

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.

#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. Where and how to proceed?

Upvotes: 1

Views: 161

Answers (1)

Eiko
Eiko

Reputation: 25632

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, i.e. perform methods on Obj-C++-Objects. If you need to pass References to Objects, some void * will usually do. Alternatively, some singleton / look-up pattern can be used.

Upvotes: 1

Related Questions