从c++向目标c调用回调函数
Calling callback function from c++ to objective c
- 我在我的目标c项目中使用c++库
- 我集成了c++库,并实现了.mm文件来桥接c++和objectivec
-
使用这个.mm桥,我可以成功地从目标c调用c++函数。
-
问题是,给定c++库中的方法返回的是nothing,即Void。例如void login(const char*电子邮件,const char*password);
-
这个c++库实现了回调函数来了解这个登录方法的结果。
示例:
class DemoApp : public XClass
{
int urandomfd;
public:
uint32_t dstime(void);
FileAccess* newfile();
void request_error(MegaClient*, error);
void login_result(MegaClient*, error);
void users_updated(MegaClient*, User**, int);
void nodes_updated(MegaClient*, Node**, int);
int prepare_download(MegaClient*, Node*);
void share_result(MegaClient*, int, error);
void account_details(MegaClient*, AccountDetails*, int, int, int, int, int, int);
void topen_result(MegaClient*, int, error);
void topen_result(MegaClient*, int, string*, const char*, int);
void transfer_update(MegaClient*, int, off_t, off_t, uint32_t);
void transfer_error(MegaClient*, int, int, int);
void transfer_failed(MegaClient*, int, error);
void transfer_failed(MegaClient*, int, string&, error);
void transfer_limit(MegaClient*, int);
void transfer_complete(MegaClient*, int, chunkmac_map*, const char*);
void transfer_complete(MegaClient*, int, const byte*, const byte*, SymmCipher*);
void changepw_result(MegaClient*, error);
void reload(MegaClient*, const char*);
void notify_retry(MegaClient*, int);
void debug_log(MegaClient*, const char*);
DemoApp();
};
所以现在我关心的是,我应该如何以及何时调用目标C库中的这些CALLBACK函数,在我看来,它们是在C++库中内部调用的。
这是我的wrapper.mm文件,包装c++方法,需要调用目标c。
-(void)包装登录:(NSString*)电子邮件:(NSString*)pwd{
self.wrappedModelAccessMega->登录([email-UTF8String],[pwd-UTF8String]);//没有返回,因为来自c++库的登录功能没有返回任何内容,即无效
}
我已经花了一段时间来研究它,并且已经努力将这个库集成到我的目标C库中,现在由于这些回调函数,我一直坚持使用它。请举一个小例子,展示我应该如何使用c++的回调函数,包装它,并在我的目标c中调用,以获取/知道我的登录函数的结果/返回,这对我来说会很好。
简单的答案是:
- 编写回调函数背后的逻辑是,它们应该得到调用当响应来自服务器或某个事件发生时在内部
- 如果你想使用它,你需要实现逻辑,比如说存储值,并在回调后返回此变量函数被调用。(不推荐)
- 如果你想在其他平台上使用这个回调函数,比如目标C然后将这个回调函数与Delegates桥接起来。(推荐)
感谢Jaggu先生帮助我了解了这一点。
相关文章:
- 用于在回调中调用解析器的设计模式
- 从不同的 cpp 调用回调函数会导致bad_function_call
- 处理类内的回调时,必须调用对非静态成员函数的引用
- MSVC __debugbreak() 与 openGL 错误回调一起使用时不会产生调用堆栈
- LIBEV 非阻滞套接字连续调用回调
- 共享指针和回调注册的结构.由于我之外的原因调用回调时,原始指针值发生了变化
- 统一处理具有协变类型的函数指针(如何使用派生类型调用回调?
- 在本机节点中的回调中调用回调
- boost asio,async_read和acync_write未调用回调
- cuda流:流执行后未调用回调
- VDS(虚拟磁盘服务)COM接口通知-仅在注销期间调用回调(接收器)(Unadvise)
- 提升 ASIO,async_read_some未调用回调
- 如何在成员函数线程中调用回调
- 从c++向目标c调用回调函数
- c++调用回调后删除对象
- 可调用(回调)作为类模板参数的模板
- 在异步API (c++)中,如何保证API在调用回调之前返回
- 装饰器模式:如何从核心类中调用(回调)装饰器包装器的一些成员
- Cython使用python-c-api调用回调segfault
- 为什么boost asio async_read_some在特定情况下不调用回调