From 5a7ea1675f79f396a7faead8799e9d920ad99630 Mon Sep 17 00:00:00 2001 From: "HuYingzhuo(hugo/hyzboy)" Date: Wed, 21 Jun 2023 20:01:17 +0800 Subject: [PATCH] added RAM_TestClass --- CMakeLists.txt | 2 + RAM_TestClass.cpp | 4 ++ RAM_TestClass.h | 43 +++++++++++++++++ RuntimeAssetManager.h | 92 +++++++++++++++++++++++++++++++++++++ RuntimeAssetManagerTest.cpp | 61 ++++++++++++++++++++++++ 5 files changed, 202 insertions(+) create mode 100644 RAM_TestClass.cpp create mode 100644 RAM_TestClass.h create mode 100644 RuntimeAssetManager.h create mode 100644 RuntimeAssetManagerTest.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index e0e65e6..0e2fa2d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -52,3 +52,5 @@ add_executable(OSFontList OSFontList.cpp) cm_example_project(OSFontList) target_link_libraries(OSFontList PRIVATE CMUtil) +add_executable(RuntimeAssetManagerTest RuntimeAssetManagerTest.cpp RuntimeAssetManager.h RAM_TestClass.h RAM_TestClass.cpp) +cm_example_project(RuntimeAssetManagerTest) \ No newline at end of file diff --git a/RAM_TestClass.cpp b/RAM_TestClass.cpp new file mode 100644 index 0000000..69c326d --- /dev/null +++ b/RAM_TestClass.cpp @@ -0,0 +1,4 @@ +#include"RAM_TestClass.h" + +HGL_RUNTIME_ASSET_DECLARATION(InstanceID,Instance) +HGL_RUNTIME_ASSET_DECLARATION(PhysicalDeviceID,PhysicalDevice) diff --git a/RAM_TestClass.h b/RAM_TestClass.h new file mode 100644 index 0000000..2fd9a80 --- /dev/null +++ b/RAM_TestClass.h @@ -0,0 +1,43 @@ +#pragma once +#include"RuntimeAssetManager.h" +#include + +using namespace hgl; + +using InstanceID =uint64_t; +using PhysicalDeviceID =uint64_t; + +struct Instance:public RuntimeAsset +{ + SortedSets physical_devices; + +public: + + using RuntimeAsset::RuntimeAsset; + + void AddPhysicalDevice(PhysicalDeviceID pd_id) + { + physical_devices.Add(pd_id); + } + + const SortedSets &GetPhysicalDevices()const{return physical_devices;} +}; + +struct PhysicalDevice:public RuntimeAsset +{ + InstanceID inst_id; + + AnsiString device_name; + +public: + + using RuntimeAsset::RuntimeAsset; + + bool Init(const AnsiString &name,const InstanceID &iid) + { + device_name=name; + inst_id=iid; + + return(true); + } +}; diff --git a/RuntimeAssetManager.h b/RuntimeAssetManager.h new file mode 100644 index 0000000..761258e --- /dev/null +++ b/RuntimeAssetManager.h @@ -0,0 +1,92 @@ +#pragma once + +#include + +using namespace hgl; + +template struct RuntimeAssetManager:public ResManage +{ +public: + + bool Add(V *v) + { + if(!v)return(false); + + return ResManage::Add(v->GetID(),v); + } +}; + +template struct RuntimeAsset +{ +public: + + using RAMClass=RuntimeAssetManager; + +private: + + static RuntimeAssetManager RAM; + +private: + + K RuntimeAssetID; + +public: + + const K GetID()const{return RuntimeAssetID;} + +public: + + RuntimeAsset(K id) + { + RuntimeAssetID=id; + } + + virtual ~RuntimeAsset()=default; + + bool Init() + { + return(true); + } + +public: + + static uint GetInstanceCount() + { + return RAM.GetCount(); + } + + static RuntimeAssetManager &GetRAM() + { + return RAM; + } + + template + static V *CreateInstance(const K &id,ARGS...args) + { + V *obj=new V(id); + + if(!obj)return(nullptr); + + if(!obj->Init(args...)) + { + delete obj; + return(nullptr); + } + + RAM.Add(obj); + return obj; + } + + static V *GetInstance(const K &id) + { + return RAM.Get(id); + } + + void Release() + { + RAM.Release(RuntimeAssetID); + delete this; + } +}; + +#define HGL_RUNTIME_ASSET_DECLARATION(RA_ID_TYPE,RA_CLASS) RuntimeAssetManager RuntimeAsset::RAM; diff --git a/RuntimeAssetManagerTest.cpp b/RuntimeAssetManagerTest.cpp new file mode 100644 index 0000000..f35bb29 --- /dev/null +++ b/RuntimeAssetManagerTest.cpp @@ -0,0 +1,61 @@ +#include +#include +#include +#include"RAM_TestClass.h" + +using namespace hgl; + +void CreateTestObject() +{ + srand(time(nullptr)); + + uint32_t ic=rand()%10+1; + uint32_t pc=rand()%10+1; + + std::cout<<"Instance Count: "<AddPhysicalDevice(pd->GetID()); + } + + delete[] pd; + delete[] ii; +} + +void OutputTestObject() +{ + const auto &ram=Instance::GetRAM(); + + for(int i=0;iGetPhysicalDevices(); + + std::cout<<"Instance "<GetID()<<" have "<GetID()<<" name: "<device_name.c_str()<