reflect.cpp

来自「java 反射机制详解示例,实现类属性及方法修改」· C++ 代码 · 共 46 行

CPP
46
字号
#include "reflect.h"

RTTIRepository RTTIRepository::theRepository;

inline unsigned hashFunction(const char* name)
{ 
    unsigned h = 0;
    while (*name != '\0') { 
	h = ((h << 8) ^ (*name++ & 0xFF)) | (h >> 24);
    }
    return h;
}

bool RTTIRepository::addClass(RTTIClassDescriptor* cls) 
{ 
    unsigned h = hashFunction(cls->name);
    
    for (RTTIClassDescriptor* cp = hashTable[h % RTTI_CLASS_HASH_SIZE]; cp != NULL; cp = cp->collisionChain) { 
	if (cp->hashCode == h && strcmp(cp->name, cls->name) == 0) {
	    return false;
	}
    }	    
    cls->next = classes;
    classes = cls;
    cls->collisionChain = hashTable[h % RTTI_CLASS_HASH_SIZE];    
    hashTable[h % RTTI_CLASS_HASH_SIZE] = cls;
    cls->hashCode = h;
    return true;
}
	
RTTIClassDescriptor* RTTIRepository::findClass(char const* name)
{
    unsigned h = hashFunction(name);
    for (RTTIClassDescriptor* cls = hashTable[h % RTTI_CLASS_HASH_SIZE]; cls != NULL; cls = cls->collisionChain) { 
	if (cls->hashCode == h && strcmp(cls->name, name) == 0) { 
	    return cls;
	}
    }
    return NULL;
}

bool RTTIRepository::load(char const* filePath)
{
    return false;
}

⌨️ 快捷键说明

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