1 Star 4 Fork 0

shaoguangcn / pf4cpp

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

介绍

Cross-platform plugin framework for cplusplus.

支持平台

  • Windows
  • Linux
  • MacOS

UML类图

构建教程

mkdir cmake-build
cd cmake-build
cmake ../
make

使用说明

  1. 定义一个插件接口类(纯虚类),该类继承自PluginObject

plugin_interface.hpp

#ifndef PLUGIN_INTERFACE_HPP
#define PLUGIN_INTERFACE_HPP

#include <pf4cpp/plugin_object.hpp>

#include <string>

// for windows
#ifdef PLUGINDEMO_EXPORTS
#  define PLUGINDEMO_API PF4CPP_DECL_EXPORT
#else
#  define PLUGINDEMO_API PF4CPP_DECL_IMPORT
#endif

class PLUGINDEMO_API PluginInterface : public pf4cpp::PluginObject
{
public:
    PluginInterface() { }
    ~PluginInterface() { }

    virtual std::string GetVersion() const = 0;
    virtual std::string GetName() const = 0;
    virtual int Add(int a, int b) const = 0;
};

#endif // PLUGIN_INTERFACE_HPP
  1. 实现插件接口,并声明为插件

声明插件使用PF4CPP_DECLARE_PLUGIN_INTERFACE宏,该宏必须处于任何命名空间之外。

plugin_demo.hpp

#ifndef PLUGIN_DEMO_HPP
#define PLUGIN_DEMO_HPP

#include "plugin_interface.hpp"

class PLUGINDEMO_API PluginDemo : public PluginInterface
{
public:
    PluginDemo();
    ~PluginDemo();

    std::string GetVersion() const override;
    std::string GetName() const override;
    int Add(int a, int b) const override;
};

PF4CPP_DECLARE_PLUGIN_INTERFACE(PluginDemo)

#endif // PLUGIN_DEMO_HPP

plugin_demo.cpp

#include "plugin_demo.hpp"

PluginDemo::PluginDemo() { }

PluginDemo::~PluginDemo() { }

std::string PluginDemo::GetVersion() const
{
    return "1.0.0";
}

std::string PluginDemo::GetName() const
{
    return "PluginDemo";
}

int PluginDemo::Add(int a, int b) const
{
    return a + b;
}
  1. 将插件编译为动态链接库

  2. 主程序中使用PluginLoader加载插件

main.cpp

#include <iostream>

#include <pf4cpp/plugin_loader.hpp>

#include "plugin_interface.hpp"

int main(int argc, char* argv[])
{
    pf4cpp::PluginLoader loader("./plugin_demo");

    bool loaded = loader.Load();
    if (!loaded) {
        std::cout << "Load plugin failed: " << loader.GetErrorString() << std::endl;
    }
    std::cout << "Load plugin successfully" << std::endl;
    std::cout << "file name: " << loader.GetFileName() << std::endl;
    std::cout << "complete file name: " << loader.GetCompleteFileName() << std::endl;

    pf4cpp::PluginObject* object = loader.GetInstance();
    PluginInterface* plugin = dynamic_cast<PluginInterface*>(object);
    if (plugin) {
        std::cout << "Plugin version: " << plugin->GetVersion() << std::endl;
        std::cout << "Plugin Name: " << plugin->GetName() << std::endl;
        std::cout << "Add(1, 2) function in plugin: " << plugin->Add(1, 2) << std::endl;
    }

    return 0;
}
  1. 注: 如果是Linux平台,编译主程序时,需要链接库libdl.so : -ldl

LICENSE

MIT License

MIT License Copyright (c) 2022 shaoguangcn<shaoguangcn@163.com> Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

跨平台的C++插件框架, plugin framework for cplusplus 展开 收起
C/C++ 等 3 种语言
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
C/C++
1
https://gitee.com/shaoguangcn/pf4cpp.git
git@gitee.com:shaoguangcn/pf4cpp.git
shaoguangcn
pf4cpp
pf4cpp
develop

搜索帮助