C++中动态加载运行测试代码

//
//  baseclass.hpp
//  agent
//
//  Created by liujc on 17/7/12.
//  Copyright © 2017年 liujc. All rights reserved.
//

#ifndef baseclass_hpp
#define baseclass_hpp

#ifdef __GNUC__
#include <cxxabi.h>
#endif

#include <stdio.h>
#include <string>
#include <assert.h>
#include <stdlib.h>
#include <vector>
#include <map>
#include <string.h>
#include <typeinfo>
#include "../json-c/json.h"

#define __OSTYPE_IOS
using namespace std;
struct test_class_method{
    string class_name;
    string method_name;
};

class baseObject
{
public:
    baseObject(){initLogObject();}
    virtual ~baseObject(){}
    virtual void run(const string params){}
    string get_test_data();
    test_class_method* get_test_method_name();
    string get_callback_url();
    string get_data_field_value(const string name);
    void log(const string name, const string data);
    void setTestData(const string data);
protected:
    zdkjson_json_object *jtestdata=NULL;
private:
    zdkjson_json_object *jlog=NULL;
    void initLogObject();
    void SplitString(const string& s, vector<string>& v, const string& c);
};

class baseObjectFactory
{
public:
    typedef baseObject* (*CreateFunction)();
    static baseObjectFactory & Instance()
    {
        static baseObjectFactory fac;
        return fac;
    }
    
    static string ReadTypeName(const char* name){
#ifndef __GNUC__
        const char * p = strstr(name, " ");
        if (p)
        {
            size_t prev_len = (size_t)(p - name);
            if (memcmp(name, "class", prev_len) == 0 ||
                memcmp(name, "struct", prev_len) == 0 ||
                memcmp(name, "enum", prev_len) == 0 ||
                memcmp(name, "union", prev_len) == 0)
            {
                p += 1;
                return string(p);
            }
        }
        
        return string(name);
#else
        char * real_name = abi::__cxa_demangle(name, nullptr, nullptr, nullptr);
        string real_name_string(real_name);
        free(real_name);
        return real_name_string;
#endif
    }

    bool Regist(const char* name, CreateFunction func){
        if (!func)
        {
            return false;
        }
        string type_name = ReadTypeName(name);
        return _create_function_map.insert(make_pair(type_name, func)).second;
    }
    
    baseObject * Create(const string & type_name)
    {
        if (type_name.empty())
        {
            return NULL;
        }
        
        map<string, CreateFunction>::iterator it = _create_function_map.find(type_name);
        if (it == _create_function_map.end())
        {
            return NULL;
        }
        
        return it->second();
    }
    
    template<typename T>
    T * Create(const string & type_name)
    {
        baseObject * obj = Create(type_name);
        if (!obj)
        {
            return NULL;
        }
        T * real_obj = dynamic_cast<T*>(obj);
        if (!real_obj)
        {
            delete obj;
            return NULL;
        }
        return real_obj;
    }
public:
    map<string, CreateFunction> _create_function_map;

};

template<typename T>
class baseObjectCreate : public baseObject
{
public:
    static baseObject * CreateObject()
    {
        return new T();
    }
    
    struct Registor
    {
        Registor()
        {
            if (!baseObjectFactory::Instance().Regist(typeid(T).name(), CreateObject))
            {
                assert(false);
            }
        }
        
        inline void do_nothing()const { }
    };
    
    static Registor s_registor;
    
public:
    baseObjectCreate()
    {
        s_registor.do_nothing();
    }
    
    virtual ~baseObjectCreate()
    {
        s_registor.do_nothing();
    }
};

template <typename T>
typename baseObjectCreate<T>::Registor baseObjectCreate<T>::s_registor;
#endif /* baseclass_hpp */

 

posted @ 2018-02-01 17:50  GavinLJC  阅读(357)  评论(0编辑  收藏  举报