栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

AndroidStudio中使用c++跨模块调用函数

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

AndroidStudio中使用c++跨模块调用函数

c++跨模块调用函数

例:要在检测模块A(ASever)返回检测结果给检测模块B(BDetection)。其中与两个模块同级的有C_handle.cpp文件及头文件。

基本思路:在A检测模块每次检测完毕则调用B检测模块的函数,将结果更新过去。

要解决A检测模块调用B检测模块函数,可以将函数作为参数传入A检测模块。

具体实现过程:

在A检测模块最外层文件A_server_handle.cpp文件中,实现以下函数,并在初始化时调用,将函数传入进来。

  • 首先定义函数类型,并将Chandle.cpp中的函数传入A检测模块
typedef std::function)> AResult; 
//AResult是一个函数类型,将函数传入,赋值给模块类A_server_中的setAServerListener
void AServerHandle_setAServerListener(const AResult& listener)
{
        A_server_->setAServerListener(listener);
}
//在初始化函数内调用,CHandle_recvAInfo是在C_handle.cpp中实现的函数
AServerHandle_setAServerListener([&](long long tms, std::vector objs){
           CHandle_recvAInfo(tms, objs);
});

将该函数传入A检测算法模块的实现文件A_helper.cpp。

void AServer::setAServerListener(const AResult& listener)
{ 
    std::lock_guard od_lk(A_listener_mutex_);
    if(A_listener_ == nullptr)
        A_listener_ = listener;
}
//在检测完成后,调用该函数
...
    A_listener_(tms, rects);//rects为传入的结果
             

在C_handle.cpp中,定义函数

typedef std::function)> HandleAFunc;
static std::vector handleAFuncList; 
//A检测模块调用的函数的实现,该函数实现handleAFuncList里面的函数
void CHandle_recvAInfo(long long tms, const std::vector objs)
{
    for(auto func : handleAFuncList)
    {
        func(tms, objs);
    }
}
//在初始化中,将函数放入模块需执行的函数列表中。函数在B检测模块中实现
handleAFuncList.push_back(AHandle_updateAInfo);

B检测模块中,B_handle.cpp中,实现上述函数。

void BHandle_updateAInfo( long long tms, const std::vector objs)
{
       
   Bdetector_->updateAInfo(tms, objs);
} //函数再调用该模块类中的函数updateAInfo

在该算法模块的算法实现文件B_detect.cpp中,完成该函数的具体实现

//接收到目标检测的结果
void BDetect::updateAInfo(long long tms, std::vector objs)
{
  {
      std::lock_guard lk(det_result_mutex);
      det_result_.tms = tms;
      det_result_.resRect = objs;
  }
}

至此,实现了跨模块调用函数。在A检测模块中,调用A_listener_(res) ==》行人检测模块函数的updateAInfo(objs)函数

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/291724.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号