CMExamples/datatype/typeinfo/ObjectRelationTest.cpp

173 lines
3.8 KiB
C++
Raw Normal View History

2024-11-19 01:38:03 +08:00
/**
1. type_hash , typeid(T).hash_code()
serial number
2.
3.
4.serial
*/
#include<hgl/type/object/Object.h>
#include<hgl/type/List.h>
#include<tsl/robin_set.h>
2024-11-19 01:38:03 +08:00
#include<iostream>
using namespace hgl;
using namespace std;
2024-11-19 02:06:33 +08:00
#define CLASS_TYPE_HASH(type) static const size_t StaticTypeHash(){return typeid(type).hash_code();}
2024-11-19 01:38:03 +08:00
class BaseObject
{
ObjectSimpleInfo object_simple_info;
2024-11-19 01:38:03 +08:00
public:
BaseObject(const ObjectSimpleInfo &osi)
2024-11-19 01:38:03 +08:00
{
object_simple_info=osi;
2024-11-19 01:38:03 +08:00
}
virtual ~BaseObject()=default;
2024-11-19 02:06:33 +08:00
CLASS_TYPE_HASH(BaseObject)
const size_t GetTypeHash()const{return object_simple_info.hash_code;}
const size_t GetObjectSerial()const{return object_simple_info.serial_number;}
2024-11-19 01:38:03 +08:00
};//class BaseObject
2024-11-19 02:06:33 +08:00
#define CLASS_BODY(class_type) private: \
2024-11-19 01:38:03 +08:00
static const size_t CreateObjectSerial(){static size_t serial=0;return ++serial;} \
public: \
class_type():BaseObject(ObjectSimpleInfo(class_type::StaticTypeHash(),class_type::CreateObjectSerial())){} \
2024-11-19 02:06:33 +08:00
virtual ~class_type()=default; \
CLASS_TYPE_HASH(class_type)
2024-11-19 01:38:03 +08:00
template<typename T> inline const bool IsType(BaseObject *bo){return bo?(bo->GetTypeHash()==T::StaticTypeHash()):false;}
2024-11-19 02:06:33 +08:00
inline const bool TypeEqual(BaseObject *bo1,BaseObject *bo2)
{
if(!bo1||!bo2)return(false);
return bo1->GetTypeHash()==bo2->GetTypeHash();
}
2024-11-19 01:38:03 +08:00
class TestObject:public BaseObject
{
CLASS_BODY(TestObject)
public:
};
using ObjectSimpleInfoSet=tsl::robin_set<ObjectSimpleInfo>;
/**
* <br>
* std::shared_ptr,SharedPtr
*/
template<typename T> class RefPtr
2024-11-19 01:38:03 +08:00
{
struct RefData
{
void *obj;
ObjectSimpleInfoSet ref_set;
ObjectSimpleInfoSet weak_set;
};
2024-11-19 01:38:03 +08:00
RefData *data;
2024-11-19 01:38:03 +08:00
public:
RefPtr()
2024-11-19 01:38:03 +08:00
{
obj=nullptr;
ref_count=0;
weak_count=0;
}
~RefPtr()
2024-11-19 01:38:03 +08:00
{
if(obj)
delete obj;
}
void SetObject(T *o)
{
if(obj)
delete obj;
obj=o;
}
T *GetObject()
{
return obj;
}
void AddRef()
{
++ref_count;
}
void DelRef()
{
if(ref_count>0)
--ref_count;
}
void AddWeak()
{
++weak_count;
}
void DelWeak()
{
if(weak_count>0)
--weak_count;
}
int GetRefCount()const
{
return ref_count;
}
int GetWeakCount()const
{
return weak_count;
}
};
int main(int,char **)
{
TestObject to1,to2,to3;
BaseObject *bo=&to3;
std::cout<<"TestObject::StaticHash:"<<TestObject::StaticTypeHash()<<std::endl;
std::cout<<"to1.serial: "<<to1.GetObjectSerial()<<std::endl;
std::cout<<"to2.serial: "<<to2.GetObjectSerial()<<std::endl;
std::cout<<"bo->StaticHash: "<<bo->StaticTypeHash()<<std::endl;
std::cout<<"bo->TypeHash: "<<bo->GetTypeHash()<<std::endl;
std::cout<<"bo->Serial: "<<bo->GetObjectSerial()<<std::endl;
2024-11-19 02:06:33 +08:00
bool result=IsType<TestObject>(bo);
2024-11-19 01:38:03 +08:00
std::cout<<"IsType<TestObject>(bo) result is "<<(result?"true":"false")<<std::endl;
2024-11-19 02:06:33 +08:00
result=TypeEqual(&to1,bo);
std::cout<<"TypeEqual(&to1,bo) result is "<<(result?"true":"false")<<std::endl;
2024-11-19 01:38:03 +08:00
return 0;
}