更新Object.h/ObjectBaseInfo
This commit is contained in:
parent
d09fe4326e
commit
798a69e6b9
@ -1,6 +1,9 @@
|
||||
#pragma once
|
||||
#include<hgl/type/DataType.h>
|
||||
#include<hgl/type/object/ObjectBaseInfo.h>
|
||||
#include<hgl/SourceCodeLocation.h>
|
||||
|
||||
#include<tsl/robin_map.h>
|
||||
|
||||
namespace hgl
|
||||
{
|
||||
@ -11,12 +14,44 @@ namespace hgl
|
||||
{
|
||||
ObjectBaseInfo object_info;
|
||||
|
||||
protected:
|
||||
|
||||
tsl::robin_map<size_t,void *> inherit_class_map; ///<继承类的hash值与this指针的映射表
|
||||
|
||||
bool RegistryInheritClass(const size_t &hash_code,void *class_this)noexcept
|
||||
{
|
||||
if(inherit_class_map.find(hash_code)!=inherit_class_map.end())
|
||||
return(false);
|
||||
|
||||
inherit_class_map[hash_code]=class_this;
|
||||
|
||||
return(true);
|
||||
}
|
||||
|
||||
template<typename T> bool RegistryInheritClass(T *class_this)noexcept
|
||||
{
|
||||
return RegistryInheritClass(hgl::GetTypeHash<T>(),class_this);
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
const ObjectBaseInfo & GetObjectBaseInfo () const noexcept { return object_info; } ///<获取对象简单信息
|
||||
const ObjectBaseInfo & GetObjectBaseInfo () const noexcept { return object_info; } ///<获取对象简单信息
|
||||
|
||||
const size_t GetTypeHash () const noexcept { return object_info.hash_code; } ///<获取对象数据类型的hash值
|
||||
const size_t GetUniqueID () const noexcept { return object_info.unique_id; } ///<获取对象的唯一序列号
|
||||
virtual const size_t GetTypeHash () const noexcept { return object_info.info->hash_code(); } ///<获取对象数据类型的hash值
|
||||
virtual const char * GetTypeName () const noexcept { return object_info.info->name(); } ///<获取对象数据类型的名称
|
||||
const size_t GetUniqueID () const noexcept { return object_info.unique_id; } ///<获取对象的唯一序列号
|
||||
|
||||
template<typename T> T *Cast()
|
||||
{
|
||||
auto it=inherit_class_map.find(hgl::GetTypeHash<T>());
|
||||
|
||||
if(it!=inherit_class_map.end())
|
||||
return (T *)it->second;
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
template<typename T> operator T *(){return Cast<T>();}
|
||||
|
||||
public:
|
||||
|
||||
@ -25,17 +60,42 @@ namespace hgl
|
||||
|
||||
Object(const ObjectBaseInfo &oi) noexcept { object_info=oi; }
|
||||
virtual ~Object()=default;
|
||||
|
||||
bool operator == (const Object &obj) const noexcept
|
||||
{
|
||||
if(GetUniqueID()!=obj.GetUniqueID())
|
||||
return(false);
|
||||
|
||||
//为什么GetTypeHash可以比较
|
||||
|
||||
// 因为GetTypeHash获取的是object_info中的TypeHash,而不是inherit_class_map中的TypeHash
|
||||
// 只有NewObjectSCL函数在创建对象时会产生object_info。
|
||||
|
||||
if(GetTypeHash()!=obj.GetTypeHash())
|
||||
return(false);
|
||||
|
||||
return(true);
|
||||
}
|
||||
};//class Object
|
||||
|
||||
template<typename T> inline T *New(const SourceCodeLocation &scl)
|
||||
#define HGL_OBJECT_BODY(class_name) public:\
|
||||
static const size_t StaticHash()noexcept { return typeid(class_name).hash_code(); } \
|
||||
static const char * StaticName()noexcept { return typeid(class_name).name(); } \
|
||||
virtual const size_t GetTypeHash()const noexcept override { return class_name::StaticHash();} \
|
||||
virtual const char * GetTypeName()const noexcept override { return class_name::StaticName();}
|
||||
|
||||
#define HGL_OBJECT_CONSTRUCT(class_name) class_name(const ObjectBaseInfo &obi):Object(obi){}
|
||||
#define HGL_OBJECT_CONSTRUCT_SC(class_name,super_class) class_name(const ObjectBaseInfo &obi):super_class(obi){}
|
||||
|
||||
template<typename T> inline T *NewObjectSCL(const SourceCodeLocation &scl)
|
||||
{
|
||||
static size_t new_count=0;
|
||||
|
||||
ObjectBaseInfo obi;
|
||||
|
||||
obi.hash_code=GetTypeHash<T>();
|
||||
obi.unique_id=new_count;
|
||||
obi.scl=scl;
|
||||
obi.info =&typeid(T);
|
||||
obi.unique_id =new_count;
|
||||
obi.scl =scl;
|
||||
|
||||
++new_count;
|
||||
|
||||
@ -43,4 +103,6 @@ namespace hgl
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
#define NewObject(type) NewObjectSCL<type>(HGL_SCL_HERE)
|
||||
}//namespace hgl
|
||||
|
@ -10,23 +10,27 @@ namespace hgl
|
||||
*/
|
||||
struct ObjectBaseInfo
|
||||
{
|
||||
size_t hash_code; ///<对象数据类型的hash值
|
||||
const std::type_info *info; ///<对象数据类型的type_info指针
|
||||
size_t unique_id; ///<唯一序列号
|
||||
|
||||
SourceCodeLocation scl;
|
||||
};
|
||||
|
||||
struct ObjectTotalInfo
|
||||
struct ObjectTypeInfo
|
||||
{
|
||||
size_t hash_code;
|
||||
|
||||
std::type_info *info;
|
||||
const std::type_info *info;
|
||||
|
||||
size_t count;
|
||||
};
|
||||
|
||||
void RegistryObjectHash(const size_t &hash_code,const std::type_info *);
|
||||
void RegistryObjectTypeInfo(const size_t &hash_code,const std::type_info *);
|
||||
|
||||
const ObjectTotalInfo *GetObjectTotalInfo(const size_t &hash_code);
|
||||
const ObjectTypeInfo *GetObjectTypeInfoByHash(const size_t &hash_code);
|
||||
|
||||
template<typename T>
|
||||
inline const ObjectTypeInfo *GetObjectTypeInfo()
|
||||
{
|
||||
return GetObjectTypeInfoByHash(typeid(T).hash_code());
|
||||
}
|
||||
}//namespace hgl
|
||||
|
||||
|
@ -6,10 +6,10 @@ namespace hgl
|
||||
{
|
||||
namespace
|
||||
{
|
||||
tsl::robin_map<size_t,const std::type_info *> type_info_map;
|
||||
tsl::robin_map<size_t,ObjectTypeInfo *> type_info_map;
|
||||
}//namespace
|
||||
|
||||
void RegistryObjectHash(const size_t &hash_code,const std::type_info *info)
|
||||
void RegistryObjectTypeInfo(const size_t &hash_code,const std::type_info *info)
|
||||
{
|
||||
if(!info)
|
||||
return;
|
||||
@ -17,6 +17,21 @@ namespace hgl
|
||||
if(type_info_map.contains(hash_code))
|
||||
return;
|
||||
|
||||
type_info_map.emplace(hash_code,info);
|
||||
ObjectTypeInfo *oti=new ObjectTypeInfo;
|
||||
|
||||
oti->count=0;
|
||||
oti->info=info;
|
||||
|
||||
type_info_map.emplace(hash_code,oti);
|
||||
}
|
||||
|
||||
const ObjectTypeInfo *GetObjectTypeInfoByHash(const size_t &hash_code)
|
||||
{
|
||||
auto it=type_info_map.find(hash_code);
|
||||
|
||||
if ( it==type_info_map.end() )
|
||||
return nullptr;
|
||||
|
||||
return it.value();
|
||||
}
|
||||
}//namespace hgl
|
||||
|
Loading…
x
Reference in New Issue
Block a user