经常遇到上层是OC部分下层是C++部分的结构.这样就会OC调用C++部分,C++回调OC的部分.
此示例部分1 是OC通过域名空间调用了C++接口,也避免了new对象.这部分很简单,看下loginWithUserName接口.
部分2是描述了C++回调OC部分.这里要说下为什么要使用libMananger单例.原因之一是模块化了,当然更重要的是在static void ErrorEvent函数能识别.难怪当初C与OC的回调中,我使用类时就识别不到该类,当初我是使用Notification传出去的.这里使用单例也可以的.部分2要点在于initialize中,这里创建TestEvent.查看TestEvent类就知道TestEvent与回调函数ErrorEvent实现了绑定关系.即TestEvent(ErrorEvent)构造函数把OC的static ErrorEvent接口与TestEvent关联并返回handle.然后传给C++层. C++层通过调用TestEvent中ErrorEvent方法(多态关系).从而响应到位置1地方.
源码中的registerEventObserver只是注册一个事件观察者.把libMananger事件抛给OC的更上层.unRegisterEventObserverForKey就是注销事件观察者
以下是源码
//TestEvent.h
//定义Observer
class MyEventObserver {
public:
virtual void ErrorEvent(int ErrorCode,constchar* ErrorDesciption) =0;
};
//定义回调函数
typedef void(* ErrorEventCallBack)(int ErrorCode,constchar* ErrorDesciption);
class TestEvent : publicMyEventObserver {
public :
TestEvent(){} //默认构造函数
TestEvent(ErrorEventCallBack erroreventcb) :_erroreventcb(erroreventcb) {} //带参数的构造函数,这里带入ErrorEventCallBack回调
//实现父类的ErrorEvent方法,父类的是纯虚函数
void ErrorEvent(int ErrorCode,constchar* ErrorDesciption)
{
_erroreventcb(ErrorCode,ErrorDesciption);
}
ErrorEventCallBack _erroreventcb;
};
//libManager.h
@interface libMananger : NSObject
+ (libMananger *)sharedLibMananger;
- (void)initialize;
- (void)unInitialize;
///
- (void)registerEventObserver:(id)observer forKey:(id)key;
- (void)unRegisterEventObserverForKey:(id)key;
////
- (void)loginWithUserName:(NSString *)userName;
@end
//libManager.mm // 这里必须是.mm
#include "TestEvent.h"
#import "libMananger.h"
@interface libMananger ()
{
dispatch_group_t _group;
dispatch_queue_t _queue;
TestEvent *_event;
}
@property (strong, nonatomic) NSMutableDictionary *obersvers;
@end
@implementation libMananger
static void ErrorEvent(int ErrorCode,constchar* ErrorDesciption) {
//---位置1
[[[libMananger sharedLibMananger] obersvers] enumerateKeysAndObjectsUsingBlock:^(id key,id obj, BOOL *stop) {
//实现libMananger的委托
}];
}
+ (libMananger *)sharedLibMananger
{
static libMananger *sharedInstance =nil ;
static dispatch_once_t onceToken;
dispatch_once (& onceToken, ^ {
sharedInstance = [[self alloc]init];
});
return sharedInstance;
}
- (id)init
{
if (self = [superinit]) {
_group =dispatch_group_create();
_queue =dispatch_queue_create("Queue",NULL);
_obersvers = [[NSMutableDictionaryalloc]init];
_event; = NULL;
}
return self;
}
- (void)initialize
{
_event = newTestEvent(ErrorEvent);//
/*这里创建的_event;传给C++底层,在底层调用了ErrorEvent函数后才能回调到位置1 */
myNameSpace::Initialize(_event); //这里使用了namespace,否则new对象方式调用Initialize
}
- (void)unInitialize
{
}
- (void)registerEventObserver:(id)observer forKey:(id)key
{
dispatch_group_async(_group,_queue, ^{
[self.obersverssetObject:observerforKey:key];
});
}
- (void)unRegisterEventObserverForKey:(id)key
{
dispatch_group_async(_group,_queue, ^{
[self.obersversremoveObjectForKey:key];
});
}
- (void)loginWithUserName:(NSString *)userName {
// myNameSpace::LogIn(userName); //这里使用了namespace,否则new对象方式调用LogIn
}
///////////部分2,对上述中的回调函数进行扩展
class MyEventObserver {
public:
virtual void SingleDeviceErrorEvent(int ErrorCode,const char* ErrorDesciption, int deviceID) = 0;
virtual void SingleDeviceInfo(char* data) =0;
};
typedef void(* SingleDeviceErrorEventCallBack)(int ErrorCode,const char* ErrorDesciption, int deviceID);
typedef void(* SingleDeviceInfoCallBack)(char* data,void* obsever);
class TestEvent : publicMyEventObserver {
public :
//构造函数参数为3个
TestEvent(SingleDeviceErrorEventCallBack singleDeviceErrorEventcb,SingleDeviceInfoCallBack singleDeviceInfocb,void* observer)
: _singleDeviceErrorEventcb(singleDeviceErrorEventcb),
_observer(observer),
_singleDeviceInfocb(singleDeviceInfocb) {
}
//实现父类的SingleDeviceErrorEvent方法
void SingleDeviceErrorEvent(int ErrorCode,const char* ErrorDesciption,int deviceID)
{
_singleDeviceErrorEventcb(ErrorCode,ErrorDesciption,deviceID);
}
//实现父类的SingleDeviceInfo方法
void SingleDeviceInfo(char* data)//这个是父类
{
_singleDeviceInfocb(data,_observer);//这个是回调函数
}
private:
SingleDeviceErrorEventCallBack _singleDeviceErrorEventcb;
SingleDeviceInfoCallBack _singleDeviceInfocb;
void* _observer;
};
//libManager.mm // 这里必须是.mm
/*回调的是typedef void(* SingleDeviceErrorEvent)(int ErrorCode, const char* ErrorDesciption,int deviceID);*/
static void SingleDeviceErrorEvent(int ErrorCode,const char* ErrorDesciption,int deviceID) {
//--- 位置1
//这里要根据deviceID查找对应的TestEvent类.
}
/*回调的是typedef void(* SingleDeviceInfoCallBack)(char* data, void* obsever);*/
static void SingleDeviceInfo(char* data,void* obsever) {
//--- 位置1
//返回obsever对象.直接找到对应委托
/*if ([(__bridge id)obsever respondsToSelector:@selector(errorEventInlibMananger:Desciption:)]) {
[(__bridge id)obsever errorEventInlibMananger:ErrorCode Desciption:ErrorDesciption];
}*/
}
- (void)initialize{
_event= newTestEvent(SingleDeviceErrorEvent,SingleDeviceInfo,self);
}