什么是C ++中的插件?

时间:2017-02-28 04:17:51

标签: plugins runtime ros

有人可以用更简单,更精细的术语解释the following paragraph的含义吗?

  

Class_loader是一个独立于ROS的包,允许用户在运行时从运行时库(即.so / .dll文件)动态加载导出的C ++类,并创建这些类的对象。使通过class_loader加载的类与仅仅链接运行时库和使用它的类不同的是,您的代码不需要在客户端代码中定义类(即类的头文件)。以这种方式加载的类通常也称为插件。

1 个答案:

答案 0 :(得分:0)

我不知道你对共享库,ros生态系统C ++继承有多了解,但这里有一些基本的东西要知道

ROS附带两个软件包

  • class_loader:在运行时加载共享库
  • pluginlib:基于class_loader的特定类加载器。这有助于您在ros生态系统中查找和加载库。

这是一个基本设置

  1. 您有一个名为MyLibrary的库,编译为" libMyLibrary.so" 在这个库中,您有一个抽象接口类MyBase以及它的几个实现:

    // file: MyBase.h
    class MyBase{
    public:
        virtual void foo() = 0;
    };
    
    -------
    
    // file: MyBaseMainImplementation.h
    class MyBaseMainImplementation: public MyBase{
    public:
        void foo();
    };
    
    // file: MyBaseMainImplementation.cpp
    void MyBaseMainImplementation::foo() { std::cout << "bar" << std::endl; };   
    
    // Make this class loadable from other libraries. This enables it to act like a
    // plugin
    CLASS_LOADER_REGISTER_CLASS(MyBaseMainImplementation, MyBase);
    
    -------
    
    // file: MyBaseOtherImplementation.h
    class MyBaseOtherImplementation: public MyBase{
    public:
        void foo();
    };
    
    // file: MyBaseOtherImplementation.cpp
    void MyBaseMainImplementation::foo() { std::cout << "foo bar" << std::endl; };   
    
    // Make this class loadable from other libraries. This enables it to act like a
    // plugin
    CLASS_LOADER_REGISTER_CLASS(MyBaseOtherImplementation, MyBase);
    
  2. 在第二个库或可执行文件(程序)中,您希望使用MyBase的某些实现,但您不知道在编译期间哪些可用。您可以做的是使用class_loader在运行时加载任何库,该库具有您想要的MyBase

    的实现
    // load libMyLibrary.so
    class_loader::ClassLoader loader("libMyLibrary.so");
    
    // create instance of a class from libMyLibrary.so
    boost::shared_ptr<MyBase> my_base_impl = 
        loader.createInstance<MyBase>("MyBaseMainImplementation");
    

    这里唯一需要的是MyBase类的标题,即MyBase.h

  3. 回到主要问题 C ++中的插件是什么?

    这里MyBase的各种实现,即MyBaseMainImplementationMyBaseOtherImplementation是插件,可以在运行时加载而不链接 libMyLibrary.so 。这就是它的全部内容。

相关问题