树形插件结构

本文是构建一个插件结构。

 

plugin 目录下 放置各种插件, 模块先枚举插件构建一个插件树, 客户可以动态配置插件的启用情况。 具体代码如下。 水平有限, 之作讨论。

 

//test

#include "../CssVcamCore/Vcam_Core.h"
#include "../CssVcamCore/Vcam_Plugin_Parse_Factory.h"
#include "../CssVcamCore/Vcam_Plugin_Component_Factory.h"
#include "../CssVcamCore/Vcam_Plugin_Scale_Interface.h"

 

int main(int argc, char *argv[])
{
    assert(argc > 0);

 

//根据指定目录构建插件树

 CSS_VCAM_PLUGIN_PARSE_FACTORY()->parse_plugin("plugin//*.*",
                                            CSS_Vcam_Plugin_Component_Factory::instance());

 

//获取插件树的分支即 具体插件的代理

 CSS_Vcam_Plugin_Component *proxy
                         = CSS_VCAM_PLUGIN_COMPONENT_ROOT()->get(0)->get(0);

 

//获取插件信息

 CSS_VCAM_PLUGIN_INFO info = proxy->plugin_info();

 

//激活插件

proxy->set_active(true);

 

 

//获取插件

 CSS_Vcam_Plugin_Scale_Interface *scale
                               = (CSS_Vcam_Plugin_Scale_Interface *)(proxy->plugin());

 

//打印插件信息

 std::cout << info.version_ << std::endl;
 std::cout << info.name_<< std::endl;
 std::cout << info.type_ << std::endl;
 std::cout << info.description_ << std::endl;

 

//运行插件
 scale->do_scale(0,0,0,0,0);

 

 getchar();
 return 0;
}

 

//共用结构定义

 

#ifndef CSS_VCAM_CORE_H
#define CSS_VCAM_CORE_H

////////////////////////////////////////////////////////
#include "vcam_core_export.h"

 

///////////////////////////////////////////////////////
static const std::string ipc_name_= "CSS Vcam Ipc";
static const std::string plugin_root_name_ = "CSS Vcam root category";
static const std::string category_scale_name_ = "CSS Vcam Scale category";
static const std::string create_scale_func_= "create_scale_func_module";

 

////////////////////////////////////////////////////////
class CSS_Vcam_Core
{
public:
 enum DEVICE_TYPE
 {
  DEVICE_TYPE_CAMERA,
  DEVICE_TYPE_VIDEO,
  DEVICE_TYPE_UNKNOW
 };

 enum TASK_TYPE
 {
  TASK_TYPE_CAMERA,
  TASK_TYPE_VIDEO,
  TASK_TYPE_UNKNOW
 };

 enum PLUGIN_COMPONENT_TYPE
 {
  PLUGIN_COMPONENT_TYPE_ROOT,
  PLUGIN_COMPONENT_TYPE_CATEGORY,
        PLUGIN_COMPONENT_TYPE_PROXY,
  PLUGIN_COMPONENT_TYPE_UNKNOW
 };

 enum PLUGIN_TYPE
 {
  PLUGIN_TYPE_SCALE,
  PLUGIN_TYPE_UNKNOW
 };


};

 

////////////////////////////////////////////////////////
struct CSS_VCAM_PLUGIN_INFO
{
 char version_[32];
 char name_[32];
 char description_[256];
 CSS_Vcam_Core::PLUGIN_TYPE type_;
};

 

////////////////////////////////////////////////////////

#endif

 

 

//树形插件结构工厂

 

#ifndef CSS_PLUGIN_FACTORY_COMPONENT_H
#define CSS_PLUGIN_FACTORY_COMPONENT_H

////////////////////////////////////////////////////


#include "Vcam_Core.h"
#include "Vcam_Plugin_Component.h"

///////////////////////////////////////////////////
#define  CSS_VCAM_PLUGIN_COMPONENT_ROOT()  /
      CSS_Vcam_Plugin_Component_Factory::instance()->get_root()

 

////////////////////////////////////////////////////
class CSS_Vcam_Core_Export CSS_Vcam_Plugin_Component_Factory : public CSS_Non_Copyable
{
private:
 CSS_Vcam_Plugin_Component_Factory();

public:
 ~CSS_Vcam_Plugin_Component_Factory();

 static CSS_Vcam_Plugin_Component_Factory *instance();

public:
 virtual CSS_Vcam_Plugin_Component *get_plugin_object(CSS_Vcam_Core::PLUGIN_COMPONENT_TYPE type,
                                                   const std::string &name,
               const std::string &path) const;

    virtual CSS_Vcam_Plugin_Component* get_root();

private:
    CSS_Vcam_Plugin_Component *root_;
 static CSS_Vcam_Plugin_Component_Factory *instance_;
}; 

 

////////////////////////////////////////////////////

#endif

 

 

//树形插件结构子类接口

 

#ifndef CSS_VCAM_PLUGIN_COMPONENT_H
#define CSS_VCAM_PLUGIN_COMPONENT_H

///////////////////////////////////////////////////////////
#include "Vcam_Core.h"
#include "Vcam_Plugin_Interface.h"

 

///////////////////////////////////////////////////////////
class CSS_Vcam_Plugin_Component
{
public:
 virtual void set_active(bool active) = 0;

 virtual void set_name(const std::string &name) = 0;

 virtual void set_path(const std::string &path) = 0;

 virtual void set_plugin_info(CSS_VCAM_PLUGIN_INFO &info) = 0;

 

public:
 virtual bool active() const = 0;

 virtual std::string name() const = 0;

 virtual std::string path() const = 0;

 virtual CSS_VCAM_PLUGIN_INFO plugin_info() const = 0;

 virtual CSS_Vcam_Plugin_Interface *plugin() = 0;

 virtual CSS_Vcam_Core::PLUGIN_COMPONENT_TYPE rtti() const= 0;

 

public:
 virtual bool add(CSS_Vcam_Plugin_Component *c) = 0;

 virtual void remove(CSS_Vcam_Plugin_Component *c) = 0;

 virtual void remove_all() = 0;

 virtual int count() const = 0;

 virtual CSS_Vcam_Plugin_Component* get(int index) = 0;

 

public:
 friend std::ostream &operator << (std::ostream &os, CSS_Vcam_Plugin_Component& c) ;
};

///////////////////////////////////////////////////////////

 

#endif

 

 

////树形插件结构子类 - 插件目录

 

#ifndef CSS_VCAM_PLUGIN_CATEGORY_H
#define CSS_VCAM_PLUGIN_CATEGORY_H

///////////////////////////////////////////////////////////
#include "Vcam_Core.h"
#include "Vcam_Plugin_Component.h"

///////////////////////////////////////////////////////////
class CSS_Vcam_Plugin_Category : public CSS_Vcam_Plugin_Component
{
public:
 CSS_Vcam_Plugin_Category(const std::string &name);

 virtual ~CSS_Vcam_Plugin_Category();

public:
 virtual void set_active(bool active) ;

 virtual void set_name(const std::string &name) ;

 virtual void set_path(const std::string &path) ;

 virtual void set_plugin_info(CSS_VCAM_PLUGIN_INFO &info) ;

public:
 virtual bool active() const ;

 virtual std::string name() const;

 virtual std::string path() const ;

 virtual CSS_VCAM_PLUGIN_INFO plugin_info() const ;

 virtual CSS_Vcam_Plugin_Interface *plugin() ;

 virtual CSS_Vcam_Core::PLUGIN_COMPONENT_TYPE rtti() const;

public:
 virtual bool add(CSS_Vcam_Plugin_Component *c) ;

 virtual void remove(CSS_Vcam_Plugin_Component *c) ;

 virtual void remove_all();

 virtual int count() const ;

 virtual CSS_Vcam_Plugin_Component* get(int index) ;

public:
 friend std::ostream &operator << (std::ostream &os, CSS_Vcam_Plugin_Category& c) ;

private:
 std::string name_;
 std::vector<CSS_Vcam_Plugin_Component *> component_;
};

///////////////////////////////////////////////////////////

#endif

 

 

//////树形插件结构子类 - 插件代理

#include "Vcam_Plugin_Component.h"
#include "Vcam_Plugin_Helper.h"
#include "Vcam_Plugin_Interface.h"

 

///////////////////////////////////////////////////////////
class CSS_Vcam_Plugin_Proxy : public CSS_Vcam_Plugin_Component
{
public:
 CSS_Vcam_Plugin_Proxy(const std::string &name, const std::string &path);

 virtual ~CSS_Vcam_Plugin_Proxy();

public:
 virtual void set_active(bool active) ;

 virtual void set_name(const std::string &name) ;

 virtual void set_path(const std::string &path) ;

 virtual void set_plugin_info(CSS_VCAM_PLUGIN_INFO &info) ;

public:
 virtual bool active() const ;

 virtual std::string name() const ;

 virtual std::string path() const ;

 virtual CSS_VCAM_PLUGIN_INFO plugin_info() const ;

 virtual CSS_Vcam_Plugin_Interface *plugin() ;

 virtual CSS_Vcam_Core::PLUGIN_COMPONENT_TYPE rtti() const;

public:
 virtual bool add(CSS_Vcam_Plugin_Component *c) ;

 virtual void remove(CSS_Vcam_Plugin_Component *c) ;

 virtual void remove_all();

 virtual int count() const;

 virtual CSS_Vcam_Plugin_Component* get(int index) ;

public:
 friend std::ostream &operator << (std::ostream &os, CSS_Vcam_Plugin_Proxy& c) ;

private:
 bool active_;
 std::string name_;
 std::string path_;

 CSS_VCAM_PLUGIN_INFO plugin_info_;
 CSS_Vcam_Plugin_Helper<CSS_Vcam_Plugin_Interface> *plugin_;
};

///////////////////////////////////////////////////////////

#endif

 

 

//插件接口

#ifndef CSS_VCAM_PLUGIN_INTERFACE_H
#define CSS_VCAM_PLUGIN_INTERFACE_H

///////////////////////////////////////////////////////////
#include "Vcam_Core.h"

///////////////////////////////////////////////////////////
class CSS_Vcam_Plugin_Interface
{
public:
 virtual std::string version() const = 0;

 virtual std::string name() const = 0;

 virtual std::string description() const = 0;

 virtual CSS_VCAM_PLUGIN_INFO plugin_info() const = 0;

 virtual CSS_Vcam_Core::PLUGIN_TYPE rtti() const= 0;

 virtual void release() = 0;
};

///////////////////////////////////////////////////////////

#endif

 

 

// //插件接口 - 位图缩放插件接口

#ifndef CSS_VCAM_PLUGIN_SCALE_H
#define CSS_VCAM_PLUGIN_SCALE_H

/////////////////////////////////////////////////////
#include "Vcam_Core.h"
#include "Vcam_Plugin_Interface.h"

/////////////////////////////////////////////////////
class CSS_Vcam_Plugin_Scale_Interface : public CSS_Vcam_Plugin_Interface
{
public:
 virtual bool do_scale(unsigned char *dest,
                                   unsigned char *src,
                                   size_t len,
                                  int w, int h) = 0;

};
/////////////////////////////////////////////////////

#endif

 

/////插件代理

#ifndef CSS_VCAM_PLUGIN_HELPER_H
#define CSS_VCAM_PLUGIN_HELPER_H

//////////////////////////////////////////////////////////
#include "Vcam_Core.h"

//////////////////////////////////////////////////////////
typedef void *(module_create_func)();

//////////////////////////////////////////////////////////
template <class Interface>
class CSS_Vcam_Plugin_Helper
{
public:
 CSS_Vcam_Plugin_Helper(const std::string& module,
                     const std::string& func);

 ~CSS_Vcam_Plugin_Helper();

public:
 Interface * operator->() { return get_interface(); }

 Interface * get_interface() { create_interface(); return interface_; }

private:
 bool create_interface();

 void release_interface();

private:
 std::string  module_;  
 std::string  func_;
 HINSTANCE  instance_;    
 Interface    *interface_;    
};


//////////////////////////////////////////////////////////
#include "Vcam_Plugin_Helper.inl"

//////////////////////////////////////////////////////////

 

#endif

 

 

//插件枚举接口工厂

 

//#ifndef CSS_VCAM_PLUGIN_PARSE_FACTORY_H
#define CSS_VCAM_PLUGIN_PARSE_FACTORY_H

////////////////////////////////////////////////
#include "Vcam_Core.h"
#include "Vcam_Plugin_Parse_Interface.h"

////////////////////////////////////////////////
#define CSS_VCAM_PLUGIN_PARSE_FACTORY()/
 CSS_Vcam_Plugin_Parse_Factory::instance()->get_factory_object()
    

////////////////////////////////////////////////
class CSS_Vcam_Core_Export CSS_Vcam_Plugin_Parse_Factory : public CSS_Non_Copyable
{
private:
 CSS_Vcam_Plugin_Parse_Factory();

public:
 ~CSS_Vcam_Plugin_Parse_Factory();

    static CSS_Vcam_Plugin_Parse_Factory* instance();

public:
 CSS_Vcam_Plugin_Parse_Interface *get_factory_object();
 
private:
 static CSS_Vcam_Plugin_Parse_Factory *instance_;
};

////////////////////////////////////////////////

#endif

 

 

//插件枚举接口

#ifndef CSS_VCAM_PLUGIN_PRASE_INTERFACE_H
#define CSS_VCAM_PLUGIN_PRASE_INTERFACE_H

////////////////////////////////////////////////////////////
#include "Vcam_Core.h"
#include "Vcam_Plugin_Component_Factory.h"

////////////////////////////////////////////////////////////
class CSS_Vcam_Plugin_Parse_Interface
{
public:
 virtual bool parse_plugin(const char* path, CSS_Vcam_Plugin_Component_Factory *f) = 0;
};

////////////////////////////////////////////////////////////

#endif

 

 

//插件枚举类

#ifndef CSS_VCAM_PLUGIN_PARSE_H
#define CSS_VCAM_PLUGIN_PARSE_H

///////////////////////////////////////////////////
#include "Vcam_Core.h"
#include "Vcam_Plugin_Parse_Interface.h"
#include "Vcam_Plugin_Helper.h"

///////////////////////////////////////////////////
class CSS_Vcam_Plugin_Parse : public CSS_Vcam_Plugin_Parse_Interface
{
public:
 CSS_Vcam_Plugin_Parse();

 virtual ~CSS_Vcam_Plugin_Parse();

public:
 virtual bool parse_plugin(const char* path, CSS_Vcam_Plugin_Component_Factory *f);
};
///////////////////////////////////////////////////

#endif

 

 

//插件dll

#ifndef CSS_VCAM_NNI_SCALE_H
#define CSS_VCAM_NNI_SCALE_H

///////////////////////////////////////////////////////////
#include "vcam_nni_scale_export.h"
#include "../CssVcamCore/Vcam_Plugin_Scale_Interface.h"

///////////////////////////////////////////////////////////
class CSS_Vcam_Nni_Scale_Export CSS_Vcam_Nni_Scale : public CSS_Vcam_Plugin_Scale_Interface
{
public:
 CSS_Vcam_Nni_Scale();

 virtual ~CSS_Vcam_Nni_Scale();

public:
 virtual std::string version() const ;

 virtual std::string name() const;

 virtual std::string description() const ;

 virtual CSS_VCAM_PLUGIN_INFO plugin_info() const;

 virtual CSS_Vcam_Core::PLUGIN_TYPE rtti() const;

 virtual void release() ;

 virtual bool do_scale(unsigned char *dest,
                    unsigned char *src,
        size_t len,
        int w, int h) ;

};
///////////////////////////////////////////////////////////

#endif

 

//

#include "Vcam_Nni_Scale.h"

//////////////////////////////////////////////////////////
CSS_VCAM_PLUGIN_INFO info_ =
{
 "Vcam Nni Scale 1.0",
 "Vcam Nni Scale",
 "Vcam Nni Scale",
 CSS_Vcam_Core::PLUGIN_TYPE_SCALE

};

//////////////////////////////////////////////////////////

CSS_Vcam_Nni_Scale::CSS_Vcam_Nni_Scale()
{
}

CSS_Vcam_Nni_Scale::~CSS_Vcam_Nni_Scale()
{
}

std::string CSS_Vcam_Nni_Scale::version() const
{
 return info_.version_;
}

std::string CSS_Vcam_Nni_Scale::name() const
{
 return info_.name_;
}

std::string CSS_Vcam_Nni_Scale::description() const
{
 return info_.description_;
}

CSS_VCAM_PLUGIN_INFO CSS_Vcam_Nni_Scale::plugin_info() const
{
 return info_;
}

CSS_Vcam_Core::PLUGIN_TYPE CSS_Vcam_Nni_Scale::rtti() const
{
 return info_.type_;
}

void CSS_Vcam_Nni_Scale::release()
{
 delete this;
}

bool CSS_Vcam_Nni_Scale::do_scale(unsigned char *dest,
          unsigned char *src,
          size_t len,
          int w, int h)
{
 std::cout << "do scale" << std::endl;
 return true;
}

/////////////////////////////////////////////////////////////////////////////////
extern "C" __declspec(dllexport)
void *  create_scale_func_module(void)
{
 CSS_Vcam_Nni_Scale * scale = new CSS_Vcam_Nni_Scale;
 return scale;
}
/////////////////////////////////////////////////////////////////////////////////

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭