diff --git a/inc/hgl/type/object/Object.h b/inc/hgl/type/object/Object.h index ea18234..70288bc 100644 --- a/inc/hgl/type/object/Object.h +++ b/inc/hgl/type/object/Object.h @@ -1,6 +1,9 @@ #pragma once #include #include +#include + +#include namespace hgl { @@ -11,12 +14,44 @@ namespace hgl { ObjectBaseInfo object_info; + protected: + + tsl::robin_map 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 bool RegistryInheritClass(T *class_this)noexcept + { + return RegistryInheritClass(hgl::GetTypeHash(),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 T *Cast() + { + auto it=inherit_class_map.find(hgl::GetTypeHash()); + + if(it!=inherit_class_map.end()) + return (T *)it->second; + + return nullptr; + } + + template operator T *(){return Cast();} 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 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 inline T *NewObjectSCL(const SourceCodeLocation &scl) { static size_t new_count=0; ObjectBaseInfo obi; - obi.hash_code=GetTypeHash(); - 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(HGL_SCL_HERE) }//namespace hgl diff --git a/inc/hgl/type/object/ObjectBaseInfo.h b/inc/hgl/type/object/ObjectBaseInfo.h index 2b062dd..eb58a9c 100644 --- a/inc/hgl/type/object/ObjectBaseInfo.h +++ b/inc/hgl/type/object/ObjectBaseInfo.h @@ -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 + inline const ObjectTypeInfo *GetObjectTypeInfo() + { + return GetObjectTypeInfoByHash(typeid(T).hash_code()); + } }//namespace hgl diff --git a/src/Object/ObjectBaseInfo.cpp b/src/Object/ObjectBaseInfo.cpp index e6f6d84..3cba226 100644 --- a/src/Object/ObjectBaseInfo.cpp +++ b/src/Object/ObjectBaseInfo.cpp @@ -6,10 +6,10 @@ namespace hgl { namespace { - tsl::robin_map type_info_map; + tsl::robin_map 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