⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 typedecl.h

📁 java 反射机制详解示例,实现类属性及方法修改
💻 H
📖 第 1 页 / 共 2 页
字号:
#ifndef __TYPEDECL_H__
#define __TYPEDECL_H__

#define RTTI_FIELD(x, flags) \
    *new RTTIFieldDescriptor(#x, (char*)&x-(char*)this, sizeof(x), flags, RTTITypeOf(x))

#define RTTI_ARRAY(x, flags) \
    *new RTTIFieldDescriptor(#x, (char*)&x-(char*)this, sizeof(x), flags, \
			    new RTTIArrayType(RTTITypeOf(*x), sizeof(x)/sizeof(*x)))

#define RTTI_PTR(x, flags) \
    *new RTTIFieldDescriptor(#x, (char*)&x-(char*)this, sizeof(x), flags, \
			    RTTITypeOfPtr(&x))

#define RTTI_PTR_TO_PTR(x, flags) \
    *new RTTIFieldDescriptor(#x, (char*)&x-(char*)this, sizeof(x), flags, \
			    RTTITypeOfPtrToPtr(&x))

#define RTTI_BASE_CLASS(BC, flags)  \
    *new RTTIFieldDescriptor(#BC, (char*)(BC*)this - (char*)this, sizeof(BC), flags, \
                             new RTTIDerivedType(&BC::RTTIDescriptor))


#define RTTI_NO_FIELDS  (*(RTTIFieldDescriptor*)0)
#define RTTI_NO_METHODS (*(RTTIMethodDescriptor*)0)

#define RTTI_FUNC(x, flags) *new RTTIMethodDescriptor(#x, flags, RTTIFuncTypeOf(&self::x))

#define RTTI_PROC(x, flags) *new RTTIMethodDescriptor(#x, flags, RTTIProcTypeOf(&self::x))

#define RTTI_OVL_FUNC(x, signature, return_type, flags) *new RTTIMethodDescriptor(#x #signature, flags, RTTIFuncTypeOf((return_type(self::*)signature)&self::x))

#define RTTI_OVL_PROC(x, signature, flags) *new RTTIMethodDescriptor(#x #signature, flags, RTTIProcTypeOf((void(self::*)signature)&self::x))


#define RTTI_DESCRIBE_STRUCT(components) \
    static RTTIClassDescriptor RTTIDescriptor; \
    static RTTIClassDescriptor* RTTIGetClass() { \
	return &RTTIDescriptor; \
    } \
    RTTIFieldDescriptor* RTTIDescribeFields() { \
	return &components; \
    } \
    RTTIMethodDescriptor* RTTIDescribeMethods() { \
        return NULL; \
    }

#define RTTI_DESCRIBE_CLASS(T, fields, methods) \
    static RTTIClassDescriptor RTTIDescriptor; \
    static RTTIClassDescriptor* RTTIGetClass() { \
	return &RTTIDescriptor; \
    } \
    RTTIFieldDescriptor* RTTIDescribeFields() { \
	return &fields; \
    } \
    typedef T self; \
    RTTIMethodDescriptor* RTTIDescribeMethods() { \
	return &methods; \
    } \
     

#define RTTI_REGISTER_STRUCT(T, flags) RTTI_REGISTER_CLASS(T, flags)

#if defined(__GNUC__) && __GNUC_MINOR__ < 96
#define RTTI_REGISTER_CLASS(T, flags) \
    static RTTIFieldDescriptor* RTTIDescribeFieldsOf##T() { \
        return T().RTTIDescribeFields(); \
    } \
    static RTTIMethodDescriptor* RTTIDescribeMethodsOf##T() { \
        return T().RTTIDescribeMethods(); \
    } \
    static T* RTTIDefaultConstructorOf##T() { \
        return new T(); \
    } \
    RTTIClassDescriptor T::RTTIDescriptor(#T, sizeof(T), &RTTIDescribeFieldsOf##T, \
					  &RTTIDescribeMethodsOf##T, \
					 (RTTIClassDescriptor::RTTICreateInstanceFunc)&RTTIDefaultConstructorOf##T, flags); \
    RTTIClassDescriptor* RTTIGetClassDescriptor(T*) {   \
        return &T::RTTIDescriptor; \
    }

template<class __T>
inline RTTIType* RTTITypeOf(__T&) { 
    extern RTTIClassDescriptor* RTTIGetClassDescriptor(__T*);
    return RTTIGetClassDescriptor((__T*)0);
}

#else

#define RTTI_REGISTER_CLASS(T, flags) \
    static RTTIFieldDescriptor* RTTIDescribeFieldsOf##T() { \
        return T().RTTIDescribeFields(); \
    } \
    static RTTIMethodDescriptor* RTTIDescribeMethodsOf##T() { \
        return T().RTTIDescribeMethods(); \
    } \
    static T* RTTIDefaultConstructorOf##T() { \
        return new T(); \
    } \
    RTTIClassDescriptor T::RTTIDescriptor(#T, sizeof(T), &RTTIDescribeFieldsOf##T, \
					  &RTTIDescribeMethodsOf##T, \
					 (RTTIClassDescriptor::RTTICreateInstanceFunc)&RTTIDefaultConstructorOf##T, flags); \
    template<> \
    RTTIClassDescriptor* RTTIClassDescriptorHelper<T>::getClassDescriptor() {   \
        return &T::RTTIDescriptor; \
    }

template<class __T>
class RTTIClassDescriptorHelper { 
 public:
    static RTTIClassDescriptor* getClassDescriptor();
};

template<class __T>
inline RTTIType* RTTITypeOf(__T&) { 
    return RTTIClassDescriptorHelper<__T>::getClassDescriptor();
}
#endif

template<>
inline RTTIType* RTTITypeOf(char&) { 
    return &RTTIType::charType;
}

template<>
inline RTTIType* RTTITypeOf(unsigned char&) {
    return &RTTIType::ucharType;
}

template<>
inline RTTIType* RTTITypeOf(signed char&) {
    return &RTTIType::scharType;
}

template<>
inline RTTIType* RTTITypeOf(short&) {
    return &RTTIType::shortType;
}

template<>
inline RTTIType* RTTITypeOf(unsigned short&) { 
    return &RTTIType::ushortType;
}

template<>
inline RTTIType* RTTITypeOf(int&) {
    return &RTTIType::intType;
}

template<>
inline RTTIType* RTTITypeOf(unsigned int&) { 
    return &RTTIType::uintType;
}

template<>
inline RTTIType* RTTITypeOf(long&) { 
    return &RTTIType::longType;
}

template<>
inline RTTIType* RTTITypeOf(unsigned long&) { 
    return &RTTIType::ulongType;
}

template<>
inline RTTIType* RTTITypeOf(float&) { 
    return &RTTIType::floatType;
}

template<>
inline RTTIType* RTTITypeOf(double&) { 
    return &RTTIType::doubleType;
}

template<>
inline RTTIType* RTTITypeOf(bool&) { 
    return &RTTIType::boolType;
}


const int RTTI_MAX_PARAMETERS = 5;


inline RTTIType* RTTITypeOfPtr(char*) { 
    return &RTTIType::charType;
}

inline RTTIType* RTTITypeOfPtr(unsigned char*) {
    return &RTTIType::ucharType;
}

inline RTTIType* RTTITypeOfPtr(signed char*) {
    return &RTTIType::scharType;
}

inline RTTIType* RTTITypeOfPtr(short*) {
    return &RTTIType::shortType;
}

inline RTTIType* RTTITypeOfPtr(unsigned short*) { 
    return &RTTIType::ushortType;
}

inline RTTIType* RTTITypeOfPtr(int*) {
    return &RTTIType::intType;
}

inline RTTIType* RTTITypeOfPtr(unsigned int*) { 
    return &RTTIType::uintType;
}

inline RTTIType* RTTITypeOfPtr(long*) { 
    return &RTTIType::longType;
}

inline RTTIType* RTTITypeOfPtr(unsigned long*) { 
    return &RTTIType::ulongType;
}

inline RTTIType* RTTITypeOfPtr(float*) { 
    return &RTTIType::floatType;
}

inline RTTIType* RTTITypeOfPtr(double*) { 
    return &RTTIType::doubleType;
}

inline RTTIType* RTTITypeOfPtr(bool*) { 
    return &RTTIType::boolType;
}
inline RTTIType* RTTITypeOfPtr(void*) { 
    return &RTTIType::voidType;
}


#if defined(__GNUC__) && __GNUC_MINOR__ < 96
template<class __P>
inline RTTIType* RTTITypeOfPtr(__P const*const*) { 
    extern RTTIClassDescriptor* RTTIGetClassDescriptor(__P*);
    return new RTTIPtrType(RTTIGetClassDescriptor((__P*)0));
}
#else
template<class __P>
inline RTTIType* RTTITypeOfPtr(__P const*const*) { 
    return new RTTIPtrType(&__P::RTTIDescriptor);
}
#endif


template<>
inline RTTIType* RTTITypeOfPtr(char const*const*) { 
    return new RTTIPtrType(&RTTIType::charType);
}

template<>
inline RTTIType* RTTITypeOfPtr(unsigned char const*const*) {
    return new RTTIPtrType(&RTTIType::ucharType);
}

template<>
inline RTTIType* RTTITypeOfPtr(signed char const*const*) {
    return new RTTIPtrType(&RTTIType::scharType);
}

template<>
inline RTTIType* RTTITypeOfPtr(short const*const*) {
    return new RTTIPtrType(&RTTIType::shortType);
}

template<>
inline RTTIType* RTTITypeOfPtr(unsigned short const*const*) { 
    return new RTTIPtrType(&RTTIType::ushortType);
}

template<>
inline RTTIType* RTTITypeOfPtr(int const*const*) {
    return new RTTIPtrType(&RTTIType::intType);
}

template<>
inline RTTIType* RTTITypeOfPtr(unsigned int const*const*) { 
    return new RTTIPtrType(&RTTIType::uintType);
}

template<>
inline RTTIType* RTTITypeOfPtr(long const*const*) { 
    return new RTTIPtrType(&RTTIType::longType);
}

template<>
inline RTTIType* RTTITypeOfPtr(unsigned long const*const*) { 
    return new RTTIPtrType(&RTTIType::ulongType);
}

template<>
inline RTTIType* RTTITypeOfPtr(float const*const*) { 
    return new RTTIPtrType(&RTTIType::floatType);
}

template<>
inline RTTIType* RTTITypeOfPtr(double const*const*) { 
    return new RTTIPtrType(&RTTIType::doubleType);
}

template<>
inline RTTIType* RTTITypeOfPtr(bool const*const*) { 
    return new RTTIPtrType(&RTTIType::boolType);
}

template<>
inline RTTIType* RTTITypeOfPtr(void const*const*) { 
    return new RTTIPtrType(&RTTIType::voidType);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -