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

📄 type.h

📁 java 反射机制详解示例,实现类属性及方法修改
💻 H
字号:
#ifndef __TYPE_H__
#define __TYPE_H__

#include <string.h>

class RTTIClassDescriptor;
class RTTIFieldDescriptor;

/**
 * Class representing type
 */
class RTTI_DLL_ENTRY RTTIType { 
  public:
    /**
     * Get type tag
     * @return one of <code>TypeTag</code> enumaration constants
     */     
    int  getTag() { 
	return tag;
    }

    /**
     * Get name of the type as it is written in C, for example (</code>char*, int[10]</code>)
     * @param buf buffer to which type name will be printed
     */    
    virtual void getTypeName(char* buf);

    static RTTIType voidType;
    static RTTIType charType;
    static RTTIType ucharType;
    static RTTIType scharType;
    static RTTIType shortType;
    static RTTIType ushortType;
    static RTTIType intType;
    static RTTIType uintType;
    static RTTIType longType;
    static RTTIType ulongType;
    static RTTIType floatType;
    static RTTIType doubleType;
    static RTTIType boolType;
    static RTTIType unknownType;

    enum TypeTag { 
	RTTI_UNKNOWN, 
	RTTI_VOID, 
	RTTI_CHAR, 
	RTTI_UCHAR, 
	RTTI_SCHAR, 
	RTTI_SHORT, 
	RTTI_USHORT, 
	RTTI_INT, 
	RTTI_UINT, 
	RTTI_LONG, 
	RTTI_ULONG, 
	RTTI_FLOAT, 
	RTTI_DOUBLE, 
	RTTI_BOOL, 
	RTTI_ARRAY, 
	RTTI_STRUCT, 
	RTTI_PTR,  
	RTTI_DERIVED,
	RTTI_METHOD 
    };
    
    /**
     * Checks whether it is built-in type
     */
    bool isBuiltin() { 
	return tag <= RTTI_BOOL;
    }
    
    /**
     * Checks whether it is integer of floating point type
     */
    bool isScalar() { 
	return tag > RTTI_VOID && tag <= RTTI_BOOL;
    } 
	
    /**
     * Checks whether it is array type
     */
    bool isArray() { 
	return tag == RTTI_ARRAY;
    }

    /**
     * Checks whether it is pointer type
     */
    bool isPointer() { 
	return tag == RTTI_PTR;
    }

    /**
     * Checks whether it is  class or struct type
     */
    bool isClass() { 
	return tag == RTTI_STRUCT;
    }

    /**
     * Checks whether type represents inhertited base class
     */
    bool isBaseClass() { 
	return tag == RTTI_DERIVED;
    }
    
    /**
     * Type descructor
     */
    ~RTTIType();

  protected:
    friend class RTTIClassDescriptor;
    friend class RTTIFieldDescriptor;
    friend class RTTIMethodDescriptor;

    int   tag;

    void destroy() {
	if (!isBuiltin()) { 
	    delete this;
	}
    }

    RTTIType(int tag) { 
	this->tag = tag;
    }
};
 

/**
 * Pointer type
 */
class RTTI_DLL_ENTRY RTTIPtrType : public RTTIType {     
  public:
    RTTIPtrType(RTTIType* ptrType) : RTTIType(RTTI_PTR) {
	this->ptrType = ptrType;
    }
    void getTypeName(char* buf);
  protected:
    RTTIType* ptrType;
};

/**
 * Array type
 */
class RTTI_DLL_ENTRY RTTIArrayType : public RTTIType {     
  public:
    RTTIArrayType(RTTIType* elemType, int nElems) : RTTIType(RTTI_ARRAY) {
	this->elemType = elemType;
	this->nElems = nElems;
    }
    void getTypeName(char* buf);

    int  getArraySize() { 
	return nElems;
    }

    RTTIType* getElementType() { 
	return elemType;
    }

  protected:
    RTTIType* elemType;
    int       nElems;
};

/**
 * Type for base class 
 */
class RTTI_DLL_ENTRY RTTIDerivedType : public RTTIType {     
  public:
    RTTIDerivedType(RTTIClassDescriptor* baseClass) : RTTIType(RTTI_DERIVED) {
	this->baseClass = baseClass;
    }
    
    RTTIClassDescriptor* getBaseClass() { 
	return baseClass;
    }

    void getTypeName(char* buf);

  protected:
    RTTIClassDescriptor* baseClass;
};

/**
 * Method type
 */
class RTTI_DLL_ENTRY RTTIMethodType : public RTTIType { 
  public:
    void getTypeName(char* buf);
    
    void getMethodDeclaration(char* buf, char const* name);

    virtual void invoke(void* result, void* obj, void* parameters[]) = 0;

    RTTIClassDescriptor* getClass() { 
	return methodClass;
    }

    RTTIType* getReturnType() { 
	return returnType;
    }

    RTTIType** getParameterTypes() { 
	return paramTypes;
    }

    int getNumberOfParameters() { 
	return nParams;
    }

    RTTIMethodType() : RTTIType(RTTI_METHOD) {}
    ~RTTIMethodType() { 
	delete[] paramTypes;
    }
  protected:
    friend class RTTIMethodDescriptor;
    friend class RTTIBfdRepository;

    RTTIType*  returnType;
    int        nParams;
    RTTIType** paramTypes;
    RTTIClassDescriptor* methodClass;
    bool       isStatic;
};
	

#endif

⌨️ 快捷键说明

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