use ObjectSimpleInfo instead of hash/serial

This commit is contained in:
hyzboy 2024-11-19 02:22:55 +08:00
parent 881a656f34
commit 8b37447db3

View File

@ -16,6 +16,7 @@
#include<hgl/type/object/Object.h> #include<hgl/type/object/Object.h>
#include<hgl/type/List.h> #include<hgl/type/List.h>
#include<tsl/robin_set.h>
#include<iostream> #include<iostream>
using namespace hgl; using namespace hgl;
@ -25,29 +26,27 @@ using namespace std;
class BaseObject class BaseObject
{ {
size_t object_hash_code; ObjectSimpleInfo object_simple_info;
size_t object_serial;
public: public:
BaseObject(const size_t hash,const size_t os) BaseObject(const ObjectSimpleInfo &osi)
{ {
object_hash_code=hash; object_simple_info=osi;
object_serial =os;
} }
virtual ~BaseObject()=default; virtual ~BaseObject()=default;
CLASS_TYPE_HASH(BaseObject) CLASS_TYPE_HASH(BaseObject)
const size_t GetTypeHash()const{return object_hash_code;} const size_t GetTypeHash()const{return object_simple_info.hash_code;}
const size_t GetObjectSerial()const{return object_serial;} const size_t GetObjectSerial()const{return object_simple_info.serial_number;}
};//class BaseObject };//class BaseObject
#define CLASS_BODY(class_type) private: \ #define CLASS_BODY(class_type) private: \
static const size_t CreateObjectSerial(){static size_t serial=0;return ++serial;} \ static const size_t CreateObjectSerial(){static size_t serial=0;return ++serial;} \
public: \ public: \
class_type():BaseObject(class_type::StaticTypeHash(),class_type::CreateObjectSerial()){} \ class_type():BaseObject(ObjectSimpleInfo(class_type::StaticTypeHash(),class_type::CreateObjectSerial())){} \
virtual ~class_type()=default; \ virtual ~class_type()=default; \
CLASS_TYPE_HASH(class_type) CLASS_TYPE_HASH(class_type)
@ -69,23 +68,34 @@ public:
}; };
template<typename T> class SharedObject using ObjectSimpleInfoSet=tsl::robin_set<ObjectSimpleInfo>;
{
T *obj;
int ref_count; /**
int weak_count; * <br>
* std::shared_ptr,SharedPtr
*/
template<typename T> class RefPtr
{
struct RefData
{
void *obj;
ObjectSimpleInfoSet ref_set;
ObjectSimpleInfoSet weak_set;
};
RefData *data;
public: public:
SharedObject() RefPtr()
{ {
obj=nullptr; obj=nullptr;
ref_count=0; ref_count=0;
weak_count=0; weak_count=0;
} }
~SharedObject() ~RefPtr()
{ {
if(obj) if(obj)
delete obj; delete obj;