• 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(1);
        }
    
        create_t * create_triangle = (create_t *)dlsym(handle, "create");
    
        CPolygon * pObj = create_triangle();
    
        if( 0 != pObj )
        {
        pObj->area();
        }
    
        delete pObj;
    
        dlclose(handle);
        return 0;
    }

    生成动态库: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 ) = 0;
        };
    
        /* 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[4] = {0};
                sprintf(szSize, "%d", strlen(class_name) );
        strClassName = szSize;
    #endif
                strClassName += class_name;
                printf("%s vs %s 
    ", typeid(_object).name(), strClassName.c_str());
                /* RTTI support */
                return !strcmp( typeid(_object).name(), strClassName.c_str() )
                        ? (void*)( new t ) : 0 ;
    
            }
        };
    }
    #endif /* __DYNAMIC_H__ */
    /***************************************************************************/
    /*                             dynclass.cpp                                */
    /***************************************************************************/
    #include "dynclass.h"
    
    namespace DYN_CLASS
    {
        CFactoryList * CFactoryList::_head = 0;
    
        void *Create( const char * class_name )
        {
            void * new_object = 0;
            const CFactoryList * cur = CFactoryList::_head;
    
            for( ; cur ; cur = cur->m_next )
    {
        printf("find 
    ");
         /* 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;
                }
        }
    }

    参考:

    1. Why does C++ not have reflection?
    2. How can I add reflection to a C++ application?
    3. https://my.oschina.net/u/1450061/blog/204563 
    4. https://my.oschina.net/u/1450061/blog/204608

    5. https://my.oschina.net/u/1450061/blog/204564

    6. http://m.blog.csdn.net/article/details?id=9254239

    7. http://blog.csdn.net/dengyunze/article/details/763558

    8. http://www.ithao123.cn/content-9688046.html

  • 相关阅读:
    【MVC】过滤器
    【C#】开发可以可视化操作的windows服务
    【JS】导出table到excel,同时兼容FF和IE
    【.Net】文件并发(日志处理)--队列--Redis+Log4Net
    【.Net】从.NET平台调用Win32 API
    『录』最全前端资源汇集
    $.ajax()方法详解
    mvc的视图中显示DataTable的方法
    C# 对XML基本操作总结
    Ninject简介
  • 原文地址:https://www.cnblogs.com/hero4china/p/5898252.html
Copyright © 2020-2023  润新知