Home poco动态加载class
Post
Cancel

poco动态加载class

了解一下apollo mainboard的启动流程,觉得挺灵活的。在此记录一下,并自己简单实现了一下demo。

mainboard 启动流程

  1. 启动命令mainboad -d xxx.dag
  2. 读取dag文件,尤其是module_library字段和class_name
  3. 通过poco动态加载module_library,并在module_library中加载class_name
  4. 实例化对象Component
  5. 调用其Init,Proc函数 mainboard根据dag文件动态加载不同的class,执行不同的操作。具有极大的灵活方便且统一调度。同时所有component都基于一个基类ComponentBase,约束了其行为。
  • 示例dag文件
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    
    module_config {
      module_library : "XXXX.so"
      components {
          class_name : "XXXX"
          config {
              name: "xxxx"
              config_file_path : "xxxx.pb.txt"
          }
      }
    }
    

基于poco的demo

  • 安装poco
    1. apt安装sudo apt install libpoco-dev
    2. 源码安装见POCO
    3. poco SharedLibrary介绍SaredLibrary
  • 简单demo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/******************************************************************************
 * Copyright 2022  All Rights Reserved.
 ********************************************************/
#include <iostream>
#include <string>

#include <Poco/ClassLibrary.h>   // NOLINT
#include <Poco/ClassLoader.h>    // NOLINT
#include <Poco/SharedLibrary.h>  // NOLINT

class Component {
 public:
  Component() {}
  virtual ~Component() {}
  virtual bool Init() = 0;
  virtual bool Proc() = 0;
};

class RtkComponent : public Component {
 public:
  bool Init() override { return true; }
  bool Proc() override {
    std::cout << "hello rtk component" << std::endl;
    return true;
  }
};

class CameraComponent : public Component {
 public:
  bool Init() override { return true; }
  bool Proc() override {
    std::cout << "hello camera component" << std::endl;
    return true;
  }
};

POCO_BEGIN_MANIFEST(Component)
POCO_EXPORT_CLASS(RtkComponent)
POCO_EXPORT_CLASS(CameraComponent)
POCO_END_MANIFEST

int main(int argc, char* argv[]) {
  if (argc < 3) {
    std::cout << "args not enough" << std::endl;
    std::cout << "example: test_poco library_path classname" << std::endl;
    return -1;
  }
  std::string library_path(argv[1]);
  std::string classname(argv[2]);
  std::cout << "load library:" << library_path
            << " , load classname: " << classname << std::endl;
  Poco::ClassLoader<Component> clc;

  try {
    clc.loadLibrary(library_path);
  } catch (const Poco::Exception e) {
    std::cerr << e.displayText() << std::endl;
    return -1;
  }

  auto* base = clc.create(classname);
  if (base->Init()) {
    base->Proc();
  }

  return 0;
}
// ./test_poco ./libcomponent.so CameraComponent
// ./test_poco ./libcomponent.so RtkComponent

  • cmake文件
1
2
3
4
5
6
7
8
9
10
11
12
13
project(test)

add_executable(test_poco test_poco.cpp)

add_library(component SHARED test_poco.cpp)

target_link_libraries(test_poco
    PocoFoundation
)

target_link_libraries(component
    PocoFoundation
)
This post is licensed under CC BY 4.0 by the author.

匈牙利算法

关于WAVE那些事情