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

@@ -35,8 +35,12 @@ cm_example_project("DataType/RAM" RuntimeAssetManagerTest datatype/ram/RuntimeA
datatype/ram/RAM_TestClass.cpp)
####################################################################################################
cm_example_project("DataType" TypeSizeof datatype/TypeSizeof.cpp)
cm_example_project("DataType" TypeCastTest datatype/TypeCastTest.cpp)
cm_example_project("DataType/TypeInfo" TypeSizeof datatype/TypeInfo/TypeSizeof.cpp)
cm_example_project("DataType/TypeInfo" TypeCastTest datatype/TypeInfo/TypeCastTest.cpp)
cm_example_project("DataType/TypeInfo" TypeCheck datatype/TypeInfo/TypeCheck.cpp)
cm_example_project("DataType/TypeInfo" ObjectRelationTest datatype/TypeInfo/ObjectRelationTest.cpp)
####################################################################################################
cm_example_project("DataType" HalfFloatTest datatype/HalfFloatTest.cpp)
cm_example_project("DataType" SplitStringTest datatype/SplitStringTest.cpp)
cm_example_project("DataType" StrChrTest datatype/strchr_test.cpp)
@@ -64,6 +68,7 @@ cm_example_project("DataType/Collection" StackPoolTest datatype/collection/Sta
cm_example_project("DataType/Collection" DataChainTest datatype/collection/DataChainTest.cpp)
cm_example_project("DataType/Collection" DataChainTest2 datatype/collection/DataChainTest2.cpp)
cm_example_project("DataType/Collection" ResManagerTest datatype/collection/ResourceManagerTest.cpp)
cm_example_project("DataType/Collection" HashSetTest datatype/collection/HashSetTest.cpp)
####################################################################################################
cm_example_project("Pick" Pick2DTest pick/Pick2DTest.cpp)

View File

@@ -1,7 +1,7 @@
#include<iostream>
#include<stdlib.h>
#include<time.h>
#include<hgl/type/SortedSets.h>
#include<hgl/type/SortedSet.h>
#include<hgl/type/Map.h>
#include<hgl/type/String.h>
#include"UserInfo.h"
@@ -39,7 +39,7 @@ void IntMapTest()
int index;
int value;
SortedSets<int> int_sets;
SortedSet<int> int_sets;
Map<int,int> int_map;
srand(time(nullptr));
@@ -78,22 +78,27 @@ void IntMapTest()
void StringMapTest()
{
Map<UTF8String,UserInfo> ui_map;
Map<AnsiString,UserInfo> ui_map;
for(auto &ui:user_info_array)
ui_map.Add(ui.name,ui);
ui_map.Enum([](const UTF8String &key,UserInfo &ui)
ui_map.Enum([](const AnsiString &key,UserInfo &ui)
{
cout<<"["<<key<<","<<(ui.sex?"male":"female")<<","<<ui.age<<"]"<<endl;
});
for(auto it:ui_map) //测试一下迭代器
{
cout<<"Iterator["<<it->key.c_str()<<","<<(it->value.sex?"male":"female")<<","<<it->value.age<<"]"<<endl;
}
cout<<endl;
}
void StringObjectMapTest()
{
ObjectMap<UTF8String,UserInfoClass> ui_map;
ObjectMap<AnsiString,UserInfoClass> ui_map;
for(auto &ui:user_info_array)
{
@@ -104,7 +109,7 @@ void StringObjectMapTest()
ui_map.Add(ui.name,uic);
}
ui_map.Enum([](const UTF8String &key,UserInfoClass *&ui)
ui_map.Enum([](const AnsiString &key,UserInfoClass *&ui)
{
cout<<"["<<key<<","<<(ui->GetSex()?"male":"female")<<","<<ui->GetAge()<<"]"<<endl;
});

View File

@@ -1,7 +1,7 @@
#pragma once
#include"RuntimeAssetManager.h"
#include<hgl/type/String.h>
#include<hgl/type/SortedSets.h>
#include<hgl/type/SortedSet.h>
#include<iostream>
using namespace hgl;
@@ -11,7 +11,7 @@ using PhysicalDeviceID =uint64;
struct Instance:public RuntimeAsset<InstanceID,Instance>
{
SortedSets<PhysicalDeviceID> physical_devices;
SortedSet<PhysicalDeviceID> physical_devices;
public:
@@ -27,7 +27,7 @@ public:
physical_devices.Add(pd_id);
}
const SortedSets<PhysicalDeviceID> &GetPhysicalDevices()const{return physical_devices;}
const SortedSet<PhysicalDeviceID> &GetPhysicalDevices()const{return physical_devices;}
};
struct PhysicalDevice:public RuntimeAsset<PhysicalDeviceID,PhysicalDevice>

View File

@@ -104,7 +104,7 @@ public:
* {
* public:
*
* using RuntimeAsset::RuntimeAsset;
* using RuntimeAsset<MyAssetID,MyRuntimeAsset>::RuntimeAsset;
* };
*
* MyRuntimeAsset.cpp

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,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;
}