diff --git a/CMakeLists.txt b/CMakeLists.txt index 3123863..a1ed93e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/datatype/collection/MapTest.cpp b/datatype/collection/MapTest.cpp index b788d7f..ab87801 100644 --- a/datatype/collection/MapTest.cpp +++ b/datatype/collection/MapTest.cpp @@ -1,7 +1,7 @@ #include #include #include -#include +#include #include #include #include"UserInfo.h" @@ -39,7 +39,7 @@ void IntMapTest() int index; int value; - SortedSets int_sets; + SortedSet int_sets; Map int_map; srand(time(nullptr)); @@ -78,22 +78,27 @@ void IntMapTest() void StringMapTest() { - Map ui_map; + Map 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.c_str()<<","<<(it->value.sex?"male":"female")<<","<value.age<<"]"< ui_map; + ObjectMap 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<<"["<GetSex()?"male":"female")<<","<GetAge()<<"]"< -#include +#include #include using namespace hgl; @@ -11,7 +11,7 @@ using PhysicalDeviceID =uint64; struct Instance:public RuntimeAsset { - SortedSets physical_devices; + SortedSet physical_devices; public: @@ -27,7 +27,7 @@ public: physical_devices.Add(pd_id); } - const SortedSets &GetPhysicalDevices()const{return physical_devices;} + const SortedSet &GetPhysicalDevices()const{return physical_devices;} }; struct PhysicalDevice:public RuntimeAsset diff --git a/datatype/ram/RuntimeAssetManager.h b/datatype/ram/RuntimeAssetManager.h index aa68dc4..b5f271b 100644 --- a/datatype/ram/RuntimeAssetManager.h +++ b/datatype/ram/RuntimeAssetManager.h @@ -104,7 +104,7 @@ public: * { * public: * - * using RuntimeAsset::RuntimeAsset; + * using RuntimeAsset::RuntimeAsset; * }; * * MyRuntimeAsset.cpp diff --git a/datatype/typeinfo/ObjectRelationTest.cpp b/datatype/typeinfo/ObjectRelationTest.cpp index fa8586a..2d1dbef 100644 --- a/datatype/typeinfo/ObjectRelationTest.cpp +++ b/datatype/typeinfo/ObjectRelationTest.cpp @@ -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; - -/** -* 数据指针引用
-* 类似于std::shared_ptr,SharedPtr。但这个类强大在于它会记录谁引用了它,以及它引用了谁。 -*/ -template 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")<; + +template class RefPtr; + +/** +* 引用对象 +*/ +template class RefObject +{ + T *obj; + + ObjectSimpleInfoSet ref_me_set; ///<引用我的对象 + ObjectSimpleInfoSet me_ref_set; ///<我引用的对象 + + template friend class RefPtr; + +public: + + RefObject(T *o) + { + obj=o; + } + + ~RefObject() + { + if(obj) + obj->Destory(); + } + + /** + * 申请一个引用 + */ + RefPtr Acquire(const ObjectSimpleInfo *osi,const SourceCodeLocation &scl); + + void Release(RefPtr *rp,const SourceCodeLocation &) + { + if(!rp)return; + + + } +};//template class RefObject + +template class RefPtr +{ + RefObject *ref_obj; + +public: + + RefPtr() + { + ref_obj=nullptr; + } + + RefPtr(RefObject *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 texture; + +public: + + void Init(RefObject *ref_tex) + { + texture=ACQUIRE_REF(ref_tex,this); + } +}; + +void test2() +{ + RefObject ref_tex1=new TestTexture; + + TestMaterial *mtl=new TestMaterial; + + mtl->Init(&ref_tex1); +} + +int main(int,char **) +{ + return 0; } \ No newline at end of file diff --git a/datatype/TypeCastTest.cpp b/datatype/typeinfo/TypeCastTest.cpp similarity index 100% rename from datatype/TypeCastTest.cpp rename to datatype/typeinfo/TypeCastTest.cpp diff --git a/datatype/typeinfo/TypeCheck.cpp b/datatype/typeinfo/TypeCheck.cpp new file mode 100644 index 0000000..0e73f8a --- /dev/null +++ b/datatype/typeinfo/TypeCheck.cpp @@ -0,0 +1,48 @@ +#include +#include + +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 class Inherit:public T +{ + size_t hash_code; + +public: + + Inherit() + { + hash_code=GetTypeHash(); + } + + const size_t GetClassTypeHash()const noexcept{return hash_code;} +}; + +class TestA:public Inherit +{ + +public: + +}; + +int main() +{ + TestA test; + + std::cout<<"Base1: "<