MGJ的地盘

爱拼才会赢!

C++反射类和成员函数的实现 227

作者为 发表

C++反射类和成员函数的实现,需要用到STL标准模板库

//定义反射工厂类以及基类NSObject
#ifndef __NS_OBJECT_FACTORYE_H__
#define __NS_OBJECT_FACTORYE_H__

#include <map>
#include <string>


typedef void* (*SEL)(void);
typedef SEL (*callFuc)(void);

class NSObject{
public:
	Base(){};
	~Base(){};
};

struct NSObjectFactory {

    typedef std::map<std::string, NSObject*(*)()> map_type;
    typedef std::map<std::string, callFuc> fuc_map_type;

private:

    static map_type * m_map;
    static fuc_map_type * fuc_map;

protected:

public:

    static NSObject * createInstance(std::string const& s){
        map_type::iterator it = getMap()->find(s);
        if(it == getMap()->end())
            return 0;
        return it->second();
    }
 
    static SEL callFaction(std::string const& s) {
        fuc_map_type::iterator it = getFunctionMap()->find(s);
        if(it == getFunctionMap()->end())
            return 0;
        return it->second();
 
    }

    static map_type * getMap(){
        // never delete'ed. (exist until program termination)
        // because we can't guarantee correct destruction order 
        if(!m_map) { m_map = new map_type; } 
        return m_map;
    }

    static fuc_map_type* getFunctionMap() {
 
        if(!fuc_map) { fuc_map = new fuc_map_type(); } 
 
        return fuc_map; 
 
    }
    
};

NSObjectFactory::map_type * NSObjectFactory::m_map = 0;

NSObjectFactory::fuc_map_type * NSObjectFactory::fuc_map = 0;

template<typename T> NSObject * createT() { return (NSObject *)(new T); }


template<typename T>
struct NSObjectRegister : NSObjectFactory { 
    NSObjectRegister(std::string const& s) { 
        getMap()->insert(std::make_pair(s, &createT<T>));
    }
};

#define REFECTION_CLASS_IMPLEMENT_DEFINE(NAME) \
    virtual string toString(){return #NAME;}; \
    static NSObjectRegister<NAME> registerNSObject

#define REFECTION_CLASS_IMPLEMENT(NAME) \
    NSObjectRegister<NAME> NAME::registerNSObject(#NAME)

#define NSClassFromString(NAME) \
    NSObjectFactory::createInstance(NAME)

struct FuctionRegister : NSObjectFactory { 
    FuctionRegister(std::string const& s, callFuc f) { 
        getFunctionMap()->insert(std::make_pair(s, f));
    }
 
};
 
#define REFECTION_FUNCTION_IMPLEMENT(NAME) \
    static FuctionRegister* f_reg_##NAME = new FuctionRegister(#NAME, (callFuc)NAME)

#define NSSelectorFromString(NAME) \
    NSObjectFactory::callFaction(NAME)

#endif // __NS_OBJECT_FACTORYE_H__
//示例封装成员函数反射方法定义一个base类
#define __EJ_GET_POINTER_TO(NAME) \
	SEL _ptr_to_##NAME() { \
		return (SEL)NAME; \
	} \
	REFECTION_FUNCTION_IMPLEMENT(_ptr_to_##NAME);

#define EJ_BIND_FUNCTION_DEFINE(NAME, ARGC_NAME, ARGV_NAME) \
	int _func_##NAME(size_t ARGC_NAME, const char* ARGV_NAME)

#define EJ_BIND_FUNCTION(CLASS, NAME, ARGC_NAME, ARGV_NAME) \
	\
	static int _##CLASS##_func_##NAME( \
		Base* object, \
		size_t argc, \
		const char argv[] \
	) { \
		CLASS* instance = (CLASS*)object; \
		int ret = instance->_func_##NAME(argc, argv); \
		return ret; \
	} \
	__EJ_GET_POINTER_TO(_##CLASS##_func_##NAME)\
	\
	/* The actual implementation for this method */ \
	int CLASS::_func_##NAME(size_t ARGC_NAME, const char* ARGV_NAME)

class Base: public NSObject {
public:
	Base();
	~Base();
	REFECTION_CLASS_IMPLEMENT_DEFINE(Base);//反射类申明

	EJ_BIND_FUNCTION(EJBindingEjectaCore,include, argc, argv )//反射成员函数申明
};

REFECTION_CLASS_IMPLEMENT(Base);//反射类实现

//反射成员函数类申明
EJ_BIND_FUNCTION(EJBindingEjectaCore,include, argc, argv ) {
	if( argc < 1 ) { return -1; }
	return 1;
}
//使用方法
typedef int (*CALL_BACK)( Base* object,  size_t argc,  const char argv[] );

int main(){
 
    NSObject* object = NSClassFromString("Base");//获得反射对象
    SEL callback = NSSelectorFromString("_ptr_to__Base_func_include");//获得函数指针
    ((CALL_BACK)callback)((Base*)object, 0, NULL);//调用静态函数使其调用object对象的成员函数
    return 0;
 
}


 主题设计 • skyfrit.com  MGJ的地盘 | 保留所有权利

37 queries in 0.582 seconds.