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

📄 field.h

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

#include <string.h>

/**
 * Field qualifying flags
 */
enum RTTIFieldFlags { 
    RTTI_FLD_INSTANCE  = 0x0001, 
    RTTI_FLD_STATIC    = 0x0002, 
    RTTI_FLD_CONST     = 0x0004, 
    RTTI_FLD_PUBLIC    = 0x0010, 
    RTTI_FLD_PROTECTED = 0x0020, 
    RTTI_FLD_PRIVATE   = 0x0040, 
    RTTI_FLD_VIRTUAL   = 0x0100, // used for virtual base classes
    RTTI_FLD_VOLATILE  = 0x0200, 
    RTTI_FLD_TRANSIENT = 0x0400
}; 


/**
 * Field descriptor
 */
class RTTIFieldDescriptor { 
  public:
    /**
     * Get field's name
     * @return name of the field
     */
    char const* getName() { 
	return name;
    }

    /**
     * Set field value
     * @param obj pointer to the object to which field belongs. If there are several nested structures, 
     ** address of the most enclosed structure should be used. This paramter is agnredfor static
     * fields.
     * @param buf buffer with stored value 
     */
    void setValue(void* obj, void* buf) { 
	memcpy((char*)obj + offs, buf, size);
    }

    /**
     * Get field value
     * @param obj pointer to the object to which field belongs. If there are several nested structures, 
     ** address of the most enclosed structure should be used. This paramter is agnredfor static
     * fields.
     * @param buf buffer to receive field value.
     */
    void getValue(void* obj, void* buf) { 
	memcpy(buf, (char*)obj + offs, size);
    }

    /**
     * Get class to which the field belongs
     * @return descriptor of the class containing this field
     */
    RTTIClassDescriptor* getDeclaringClass() { 
	return declaringClass;
    }
    
    /**
     * Get offset of the field in the class
     * @return offset of the field in bytes within the class
     */
    int getOffset() { 
	return offs;
    }
	
    /**
     * Get field size
     * @return size of the field in bytes
     */
    int getSize() { 
	return size;
    }
	
    /**
     * Get field type
     * @return descriptor of the field's type
     */
    RTTIType* getType() { 
	return type;
    }

    /**
     * Get flags associated with the field
     * @return combination of <code>RTTIFieldFlags>/code> flags
     */
    int getFlags() { 
	return flags;
    }


    /**
     * Constructor of the field descriptor
     * @param name name of the field
     * @param offs offset within class
     * @param size size of the field
     * @param flags flags associated with the field
     * @param type type of the field
     */
    RTTIFieldDescriptor(char const* name, int offs, int size, int flags, RTTIType* type) { 
	this->name = name;
	this->offs = offs;
	this->size = size;
	this->type = type;
	this->flags = flags;
	next = NULL; 
	chain = &next;
    }

    /**
     * Comma operator used to concatenate field descriptors in the list
     * @param field descriptor tail conctateneted with this head field descriptor, <code>chain</code>
     * component of which contains address of <code>next</code> field of last element of the list     
     * @return head of the list
     */
    RTTIFieldDescriptor& operator, (RTTIFieldDescriptor& field) {
	*chain = &field;
	chain = &field.next;
	return *this;
    }

    /**
     * Get index of the field in the array returned by <Code>RTTIClassDescriptor::getFields()</code> method
     * @param index of the field descriptor in the array sorted by field name
     */
    int getIndex() { 
	return index;
    }
    
    /**
     * Destructor of field descriptor
     */
    ~RTTIFieldDescriptor() { 
	type->destroy();
    }
  protected:
    friend class RTTIType;
    friend class RTTIClassDescriptor;
    friend class RTTIBfdRepository;

    int         flags;
    int         index;
    RTTIType*   type;
    int         offs;
    int         size;
    char const* name;

    RTTIClassDescriptor*  declaringClass;

    RTTIFieldDescriptor*  next;
    RTTIFieldDescriptor** chain;
};

#endif

⌨️ 快捷键说明

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