C语言如何开发简单的插件
linux 通过dlopen来实现:
#include "polygon.h"
#include <stdlib.h>
#include <dlfcn.h> int main()
{
typedef CPolygon* create_t(); void * handle = dlopen("./triangle.so", RTLD_LAZY); if( !handle )
{
std::cerr << dlerror() << std::endl;
exit();
} create_t * create_triangle = (create_t *)dlsym(handle, "create"); CPolygon * pObj = create_triangle(); if( != pObj )
{
pObj->area();
} delete pObj; dlclose(handle);
return ;
}
生成动态库:g++ -fPIC -shared triangle.cpp -o triangle.so
生成test主函数:g++ -fPIC test.cpp -ldl -o test
具体demo:http://files.cnblogs.com/files/hero4china/plugins_1.zip
/***************************************************************************/
/* dynclass.h */
/* 使用的时候#include "dynclass.h",在类的声明后使用DYNCLASS(类名)注册*/
/***************************************************************************/ #ifndef __DYNAMIC_H__
#define __DYNAMIC_H__ #include <cstdio>
#include <string>
#include <typeinfo>
#include <cstring> #if !defined ( DYN_DECLARE )
#define DYN_DECLARE(class_name) DYN_CLASS::CFactory<class_name> class_name
#endif #if !defined ( DYN_CREATE )
#define DYN_CREATE(class_name) DYN_CLASS::Create(class_name)
#endif namespace DYN_CLASS
{
/* create object by class name */
void * Create( const char * class_name ); /* interface of class factory*/
class CAbstractFactory
{
public:
virtual void * Create( const char * class_name ) = ;
}; /* list of class factory */
class CFactoryList
{
friend void * Create( const char * class_name );
private:
static CFactoryList * _head;
CFactoryList * m_next;
CAbstractFactory * m_item;
public:
CFactoryList( CAbstractFactory * fact );
virtual ~CFactoryList( void );
}; /* ctor of CFactoryList, add a class factory to list */
inline CFactoryList::CFactoryList( CAbstractFactory * fact )
: m_item( fact )
{
m_next = _head;
_head = this;
} #if defined ( _MSC_VER )
/* disable warning for the following line : CFactory( void ): m_item( this ) {} */
#pragma warning(disable : 4355)
#endif /* realization of class factory */
template <class t>
class CFactory: public CAbstractFactory
{
static t _object;
CFactoryList m_item; public: /* add itself to list of class factory when constructed */
CFactory( void ) : m_item( this ) {} virtual ~CFactory() {} /* create object of this class if matched */
void * Create( const char * class_name )
{
std::string strClassName; #if defined (_MSC_VER )
strClassName = ( "class " );
#else
char szSize[] = {};
sprintf(szSize, "%d", strlen(class_name) );
strClassName = szSize;
#endif
strClassName += class_name;
printf("%s vs %s \n", typeid(_object).name(), strClassName.c_str());
/* RTTI support */
return !strcmp( typeid(_object).name(), strClassName.c_str() )
? (void*)( new t ) : ; }
};
}
#endif /* __DYNAMIC_H__ */
/***************************************************************************/
/* dynclass.cpp */
/***************************************************************************/
#include "dynclass.h" namespace DYN_CLASS
{
CFactoryList * CFactoryList::_head = ; void *Create( const char * class_name )
{
void * new_object = ;
const CFactoryList * cur = CFactoryList::_head; for( ; cur ; cur = cur->m_next )
{
printf("find \n");
/* if class_name matched, object will then be created and returned */
if( new_object = cur->m_item->Create(class_name) )
{
break;
}
} return new_object;
} /* delete linkage from CFactoryList when some class factory destroyed */
CFactoryList::~CFactoryList( void )
{
CFactoryList ** m_nextp = &CFactoryList::_head; for( ; *m_nextp ; m_nextp = &(*m_nextp)->m_next )
if( *m_nextp == this )
{
*m_nextp = (*m_nextp)->m_next;
break;
}
}
}
参考:
- Why does C++ not have reflection?
- How can I add reflection to a C++ application?
- https://my.oschina.net/u/1450061/blog/204563
https://my.oschina.net/u/1450061/blog/204608
https://my.oschina.net/u/1450061/blog/204564
http://m.blog.csdn.net/article/details?id=9254239
http://blog.csdn.net/dengyunze/article/details/763558
http://www.ithao123.cn/content-9688046.html
C语言如何开发简单的插件的更多相关文章
- 用GO语言开发editplus编辑器插件(附源码)
我要开发的插件功能极为简单,就是对用户选中的内容进行base64编码或解密工作. 其中所涉及的技术部分主要是GO语言程序开发和editplus插件配置的部分,首先我们来看一下GO语言代码的写法,如下: ...
- [Songqw.Net 基础]WPF实现简单的插件化开发
原文:[Songqw.Net 基础]WPF实现简单的插件化开发 版权声明:本文为博主原创文章,未经博主允许可以随意转载 https://blog.csdn.net/songqingwei1988/ar ...
- JVM 平台上的各种语言的开发指南
JVM 平台上的各种语言的开发指南 为什么我们需要如此多的JVM语言? 在2013年你可以有50中JVM语言的选择来用于你的下一个项目.尽管你可以说出一大打的名字,你会准备为你的下一个项目选择一种新的 ...
- Sublime Text 3前端开发常用优秀插件介绍
. 首页 博客园 联系我 前言:关于Sublime Text 3. Package Control插件管理. Package Control使用方法/安装Emmet插件. Emmet插件. JsFor ...
- 在NPAPI开发火狐浏览器插件在NPAPI插件
1.插件是什么 插件是一种遵循一定规范的应用程序接口编写出来的程序.插件必须依附于一个宿主程序,为宿主程序提供增强功能.插件的种类有很多,这里主要讨论浏览器插件. IE下利用OLE和COM技术开发的浏 ...
- VS简单注释插件——VS插件开发续
VS简单注释插件——VS插件开发续 前些时候,我写过一篇<VS版权信息插件——初试VS插件开发小记>分享过一个用于添加注释信息的插件,但那个插件有几个问题: 不能添加带块注释(/**/), ...
- 如何开发一个 PyCharm 插件
PyCharm 是很多 Python 开发者优先选择的 IDE,功能强大,跨平台,提供免费社区版,非常良心.如果你想自己给PyCharm添加一些功能怎么办呢?有两个办法: 通过提需求实现,到 JetB ...
- C语言可以开发哪些项目?
C语言是我们大多数人的编程入门语言,对其也再熟悉不过了,不过很多初学者在学习的过程中难免会出现迷茫,比如:不知道C语言可以开发哪些项目,可以应用在哪些实际的开发中--,这些迷茫也导致了我们在学习的过程 ...
- 如何开发一个Jquery插件
Jquery有两种开发插件的方法: 1.jquery.fn.extend(object); 2.jquery.extend(object); 第一种方法是给Jquery对象添加方法,jquery.fn ...
随机推荐
- 51nod1089(最长回文子串之manacher算法)
题目链接: https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1089 题意:中文题诶~ 思路: 我前面做的那道回文子串的题 ...
- scala安装
一:在官网下载相应的版本http://www.scala-lang.org/download/2.10.6.html 二,在linux中解压下载下来的scala包 三:配置环境变量 export ...
- DB2 license过期的问题
今天启动DB2,无论如何都启动不了,报一个错误:“Windows 不能在 本地计算机 启动 DB2 - DB2COPY - DB2-0.有关更多信息,查阅系统事件日志.如果这是非 Microsoft ...
- java线程池ThreadPoolExecutor使用简介
一.简介线程池类为 java.util.concurrent.ThreadPoolExecutor,常用构造方法为:ThreadPoolExecutor(int corePoolSize, int m ...
- Java代码常用写法总结
1.字符串是否为空判断 以下是java 判断字符串是否为空的四种方法:方法一: 最多人使用的一个方法, 直观, 方便, 但效率很低: if(s == null ||"".equal ...
- three.js材质
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xht ...
- hihoCoder1388 Periodic Signal(2016北京网赛F:NTT)
题目 Source http://hihocoder.com/problemset/problem/1388 Description Profess X is an expert in signal ...
- 【Unity3d游戏开发】UGUI插件入门之游戏菜单
ugui是unity4.6开始加入的一个新的ui系统,非常强大,下面我们将通过一系列博客的方式一起来学习一下ugui的使用.本篇博客会介绍如何使用ugui制作一个游戏菜单,并且了解如何让物体与ugui ...
- LINUX内核参数网络相关
有助于提高网络性能和吞吐量的参数 net.core.somaxconn = 128 已完成连接队列(completed connection queue) (1)三次握手已经完成,但还未被应用层接收( ...
- CSS居中布局总结【转】
居中布局 <div class="parent"> <div class="child">demo</div> </d ...