little update codes.

This commit is contained in:
2024-11-20 02:47:51 +08:00
parent 8b37447db3
commit 53d2a61d60
8 changed files with 200 additions and 91 deletions

View File

@@ -39,8 +39,14 @@ public:
CLASS_TYPE_HASH(BaseObject)
const ObjectSimpleInfo &GetObjectSimpleInfo()const{return object_simple_info;}
const size_t GetTypeHash()const{return object_simple_info.hash_code;}
const size_t GetObjectSerial()const{return object_simple_info.serial_number;}
virtual void Destory()
{
delete this;
}
};//class BaseObject
#define CLASS_BODY(class_type) private: \
@@ -68,85 +74,7 @@ public:
};
using ObjectSimpleInfoSet=tsl::robin_set<ObjectSimpleInfo>;
/**
* 数据指针引用<br>
* 类似于std::shared_ptr,SharedPtr。但这个类强大在于它会记录谁引用了它以及它引用了谁。
*/
template<typename T> class RefPtr
{
struct RefData
{
void *obj;
ObjectSimpleInfoSet ref_set;
ObjectSimpleInfoSet weak_set;
};
RefData *data;
public:
RefPtr()
{
obj=nullptr;
ref_count=0;
weak_count=0;
}
~RefPtr()
{
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 **)
void test1()
{
TestObject to1,to2,to3;
@@ -168,6 +96,129 @@ int main(int,char **)
result=TypeEqual(&to1,bo);
std::cout<<"TypeEqual(&to1,bo) result is "<<(result?"true":"false")<<std::endl;
}
using ObjectSimpleInfoSet=tsl::robin_set<ObjectSimpleInfo>;
template<typename T> class RefPtr;
/**
* 引用对象
*/
template<typename T> class RefObject
{
T *obj;
ObjectSimpleInfoSet ref_me_set; ///<引用我的对象
ObjectSimpleInfoSet me_ref_set; ///<我引用的对象
template<typename T> friend class RefPtr;
public:
RefObject(T *o)
{
obj=o;
}
~RefObject()
{
if(obj)
obj->Destory();
}
/**
* 申请一个引用
*/
RefPtr<T> Acquire(const ObjectSimpleInfo *osi,const SourceCodeLocation &scl);
void Release(RefPtr<T> *rp,const SourceCodeLocation &)
{
if(!rp)return;
}
};//template<typename T> class RefObject
template<typename T> class RefPtr
{
RefObject<T> *ref_obj;
public:
RefPtr()
{
ref_obj=nullptr;
}
RefPtr(RefObject<T> *ro)
{
ref_obj=ro;
}
~RefPtr()
{
if(ref_obj)
ref_obj->Release(this,HGL_SCL_HERE);
}
bool IsValid()const
{
if(!this)return(false);
if(!ref_obj)return(false);
if(!ref_obj->obj)return(false);
return(true);
}
operator T *()
{
return ref_obj->obj;
}
void Release(const SourceCodeLocation &scl)
{
if(!ref_obj)return;
ref_obj->Release(this,scl);
}
};
#define ACQUIRE_REF(ref_object,self) ref_object->Acquire(&self->GetObjectSimpleInfo(),HGL_SCL_HERE);
#define REF_PTR_RELEASE(obj) obj->Release(HGL_SCL_HERE);
class TestTexture:public BaseObject
{
CLASS_BODY(TestTexture)
};
class TestMaterial:public BaseObject
{
CLASS_BODY(TestMaterial)
RefPtr<TestTexture> texture;
public:
void Init(RefObject<TestTexture> *ref_tex)
{
texture=ACQUIRE_REF(ref_tex,this);
}
};
void test2()
{
RefObject<TestTexture> ref_tex1=new TestTexture;
TestMaterial *mtl=new TestMaterial;
mtl->Init(&ref_tex1);
}
int main(int,char **)
{
return 0;
}

View File

@@ -0,0 +1,128 @@
#include<iostream>
static int BaseClassCount=0;
class BaseClass
{
int count;
public:
BaseClass()
{
count=BaseClassCount++;
std::cout<<"BaseClass "<<count<<std::endl;
}
virtual ~BaseClass()
{
std::cout<<"~BaseClass "<<count<<std::endl;
}
};
class SecondClassA:virtual public BaseClass
{
public:
SecondClassA()
{
std::cout<<"SecondClassA"<<std::endl;
}
virtual ~SecondClassA()
{
std::cout<<"~SecondClassA"<<std::endl;
}
};
class SecondClassB:virtual public BaseClass
{
public:
SecondClassB()
{
std::cout<<"SecondClassB"<<std::endl;
}
virtual ~SecondClassB()
{
std::cout<<"~SecondClassB"<<std::endl;
}
};
class ThirdClass:public SecondClassA,public SecondClassB
{
public:
ThirdClass()
{
std::cout<<"ThirdClass"<<std::endl;
}
virtual ~ThirdClass()
{
std::cout<<"~ThirdClass"<<std::endl;
}
};
class SpecialClass:public SecondClassA
{
public:
SpecialClass()
{
std::cout<<"SpecialClass"<<std::endl;
}
virtual ~SpecialClass()
{
std::cout<<"~SpecialClass"<<std::endl;
}
};
int main(int,char **)
{
ThirdClass tc;
BaseClass *bc=&tc;
SecondClassA *a_from_tc=(SecondClassA *)&tc;
SecondClassB *b_from_tc=(SecondClassB *)&tc;
SecondClassA *a_cast_tc=dynamic_cast<SecondClassA *>(&tc);
SecondClassB *b_cast_tc=dynamic_cast<SecondClassB *>(&tc);
SecondClassA *a_cast_bc=dynamic_cast<SecondClassA *>(bc);
SecondClassB *b_cast_bc=dynamic_cast<SecondClassB *>(bc);
SpecialClass sc;
BaseClass *sb=&sc;
SecondClassA *a_from_sc=dynamic_cast<SecondClassA *>(&sc);
SecondClassB *b_from_sc=dynamic_cast<SecondClassB *>(&sc);
SecondClassA *a_from_sb=dynamic_cast<SecondClassA *>(sb);
SecondClassB *b_from_sb=dynamic_cast<SecondClassB *>(sb);
std::cout<<"tc: "<<&tc<<std::endl;
std::cout<<"bc: "<<bc<<std::endl;
std::cout<<"a_from_tc: "<<a_from_tc<<std::endl;
std::cout<<"b_from_tc: "<<b_from_tc<<std::endl;
std::cout<<"a_cast_tc: "<<a_cast_tc<<std::endl;
std::cout<<"b_cast_tc: "<<b_cast_tc<<std::endl;
std::cout<<"a_cast_bc: "<<a_cast_bc<<std::endl;
std::cout<<"b_cast_bc: "<<b_cast_bc<<std::endl;
std::cout<<"sc: "<<&sc<<std::endl;
std::cout<<"a_from_sc: "<<a_from_sc<<std::endl;
std::cout<<"b_from_sc: "<<b_from_sc<<std::endl;
std::cout<<"a_from_sb: "<<a_from_sb<<std::endl;
std::cout<<"b_from_sb: "<<b_from_sb<<std::endl;
return 0;
}

View File

@@ -0,0 +1,48 @@
#include<iostream>
#include<hgl/type/object/ObjectRelation.h>
using namespace hgl;
class Base
{
ObjectBaseInfo obj_base_info; ///<对象基本信息
ObjectRelation obj_relation; ///<对象引用关系
public:
const size_t GetTypeHash()const noexcept{return obj_base_info.hash_code;}
public:
};
template<typename T> class Inherit:public T
{
size_t hash_code;
public:
Inherit()
{
hash_code=GetTypeHash<Inherit>();
}
const size_t GetClassTypeHash()const noexcept{return hash_code;}
};
class TestA:public Inherit<Base>
{
public:
};
int main()
{
TestA test;
std::cout<<"Base1: "<<base1.StaticTypeHash()<<std::endl;
std::cout<<"Base2: "<<base2.StaticTypeHash()<<std::endl;
return 0;
}

View File

@@ -0,0 +1,50 @@
#include<hgl/type/DataType.h>
#include<hgl/math/Math.h>
#include<hgl/type/RectScope.h>
#include<iostream>
using namespace hgl;
#define OUTPUT_SIZEOF(type) std::cout<<" sizeof(" #type ") = "<<sizeof(type)<<std::endl;
void main()
{
OUTPUT_SIZEOF(char)
OUTPUT_SIZEOF(char8_t)
OUTPUT_SIZEOF(char16_t)
OUTPUT_SIZEOF(char32_t)
OUTPUT_SIZEOF(wchar_t)
OUTPUT_SIZEOF(short)
OUTPUT_SIZEOF(int)
OUTPUT_SIZEOF(long)
OUTPUT_SIZEOF(long long)
OUTPUT_SIZEOF(float)
OUTPUT_SIZEOF(double)
OUTPUT_SIZEOF(long double)
OUTPUT_SIZEOF(void *)
OUTPUT_SIZEOF(ptrdiff_t)
OUTPUT_SIZEOF(Vector2f)
OUTPUT_SIZEOF(Vector3f)
OUTPUT_SIZEOF(Vector4f)
OUTPUT_SIZEOF(RectScope2s)
OUTPUT_SIZEOF(RectScope2i)
OUTPUT_SIZEOF(RectScope2f)
OUTPUT_SIZEOF(RectScope2d)
OUTPUT_SIZEOF(Matrix2f)
OUTPUT_SIZEOF(Matrix3f)
OUTPUT_SIZEOF(Matrix4f)
OUTPUT_SIZEOF(Matrix2x4f)
OUTPUT_SIZEOF(Matrix3x4f)
OUTPUT_SIZEOF(Matrix4x2f)
OUTPUT_SIZEOF(Matrix4x3f)
OUTPUT_SIZEOF(Quatf)
OUTPUT_SIZEOF(Transform)
}