调用从c ++到objective c的回调函数

时间:2013-06-26 18:22:56

标签: c++ objective-c

  1. 我在我的目标c项目中使用c ++库。
  2. 我集成了c ++库并实现了.mm文件来桥接c ++和目标c。
  3. 我可以使用这个.mm网桥从我的目标c成功调用c ++函数。

  4. 问题是给定c ++库中的方法没有返回,即Void。 例如void login(const char * email,const char * password);

  5. 此c ++库已实现回调函数,以了解此登录方法的结果。

  6. 示例:

    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();
    };
    
    1. 所以现在关注的是我应该如何以及何时在我的目标C库中调用这些CALLBACK函数,这对我来说是在c ++库中内部调用它们。

    2. 这是我的wrapper.mm文件,包装c ++方法,需要在目标c中调用。

      - (void)WrapLogin:(NSString *)email:(NSString *)pwd {

      self.wrappedModelAccessMega->登录([email UTF8String],[pwd UTF8String]);   //没有返回,因为来自c ++库的Login mfunction没有返回任何内容,即void

      }

    3. 我正在研究它已经有一段时间了,并且已经很难将这个库集成到我的目标C库中,现在我因为这些回调函数而陷入困境。 请举例说明我应该如何使用c ++的回调函数,将其包装并调用我的目标c来获取/知道我的登录函数的结果/返回将为我做好工作。

1 个答案:

答案 0 :(得分:2)

简单的答案是:

  1. 编写回调函数背后的逻辑是它们应该调用 内部响应来自服务器或某些事件发生。
  2. 如果你想使用它,你需要实现逻辑让我们说商店 变量中的值,并在此回调后返回此变量 函数被调用。 (不推荐)
  3. 如果你想在其他平台上使用这个回调函数,那么就说 Objective C然后使用Delegates桥接此回调函数。 (推荐)
  4. 感谢Jaggu先生帮助我了解这一点。