added RAM_TestClass
This commit is contained in:
parent
9148d1c0be
commit
5a7ea1675f
@ -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)
|
4
RAM_TestClass.cpp
Normal file
4
RAM_TestClass.cpp
Normal file
@ -0,0 +1,4 @@
|
||||
#include"RAM_TestClass.h"
|
||||
|
||||
HGL_RUNTIME_ASSET_DECLARATION(InstanceID,Instance)
|
||||
HGL_RUNTIME_ASSET_DECLARATION(PhysicalDeviceID,PhysicalDevice)
|
43
RAM_TestClass.h
Normal file
43
RAM_TestClass.h
Normal file
@ -0,0 +1,43 @@
|
||||
#pragma once
|
||||
#include"RuntimeAssetManager.h"
|
||||
#include<hgl/type/SortedSets.h>
|
||||
|
||||
using namespace hgl;
|
||||
|
||||
using InstanceID =uint64_t;
|
||||
using PhysicalDeviceID =uint64_t;
|
||||
|
||||
struct Instance:public RuntimeAsset<InstanceID,Instance>
|
||||
{
|
||||
SortedSets<PhysicalDeviceID> physical_devices;
|
||||
|
||||
public:
|
||||
|
||||
using RuntimeAsset::RuntimeAsset;
|
||||
|
||||
void AddPhysicalDevice(PhysicalDeviceID pd_id)
|
||||
{
|
||||
physical_devices.Add(pd_id);
|
||||
}
|
||||
|
||||
const SortedSets<PhysicalDeviceID> &GetPhysicalDevices()const{return physical_devices;}
|
||||
};
|
||||
|
||||
struct PhysicalDevice:public RuntimeAsset<PhysicalDeviceID,PhysicalDevice>
|
||||
{
|
||||
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);
|
||||
}
|
||||
};
|
92
RuntimeAssetManager.h
Normal file
92
RuntimeAssetManager.h
Normal file
@ -0,0 +1,92 @@
|
||||
#pragma once
|
||||
|
||||
#include<hgl/type/ResManage.h>
|
||||
|
||||
using namespace hgl;
|
||||
|
||||
template<typename K,typename V> struct RuntimeAssetManager:public ResManage<K,V>
|
||||
{
|
||||
public:
|
||||
|
||||
bool Add(V *v)
|
||||
{
|
||||
if(!v)return(false);
|
||||
|
||||
return ResManage<K,V>::Add(v->GetID(),v);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename K,typename V> struct RuntimeAsset
|
||||
{
|
||||
public:
|
||||
|
||||
using RAMClass=RuntimeAssetManager<K,V>;
|
||||
|
||||
private:
|
||||
|
||||
static RuntimeAssetManager<K,V> 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<K,V> &GetRAM()
|
||||
{
|
||||
return RAM;
|
||||
}
|
||||
|
||||
template<typename ...ARGS>
|
||||
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<RA_ID_TYPE,RA_CLASS> RuntimeAsset<RA_ID_TYPE,RA_CLASS>::RAM;
|
61
RuntimeAssetManagerTest.cpp
Normal file
61
RuntimeAssetManagerTest.cpp
Normal file
@ -0,0 +1,61 @@
|
||||
#include<iostream>
|
||||
#include<iomanip>
|
||||
#include<random>
|
||||
#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: "<<ic<<std::endl;
|
||||
std::cout<<"PhysicalDevice Count: "<<pc<<std::endl;
|
||||
|
||||
Instance **ii=new Instance *[ic];
|
||||
PhysicalDevice **pd=new PhysicalDevice *[pc];
|
||||
|
||||
for(uint32_t i=0;i<ic;i++)
|
||||
ii[i]=Instance::CreateInstance(i);
|
||||
|
||||
for(uint32_t i=0;i<pc;i++)
|
||||
{
|
||||
uint32_t iid=rand()%ic;
|
||||
|
||||
PhysicalDevice *pd=PhysicalDevice::CreateInstance(i,"PD"+AnsiString::numberOf(i),iid);
|
||||
|
||||
ii[iid]->AddPhysicalDevice(pd->GetID());
|
||||
}
|
||||
|
||||
delete[] pd;
|
||||
delete[] ii;
|
||||
}
|
||||
|
||||
void OutputTestObject()
|
||||
{
|
||||
const auto &ram=Instance::GetRAM();
|
||||
|
||||
for(int i=0;i<ram.GetCount();i++)
|
||||
{
|
||||
Instance *inst=Instance::GetInstance(i);
|
||||
const auto &pd_set=inst->GetPhysicalDevices();
|
||||
|
||||
std::cout<<"Instance "<<inst->GetID()<<" have "<<pd_set.GetCount()<<" physical device."<<std::endl;
|
||||
|
||||
for(const auto &pd_id:pd_set)
|
||||
{
|
||||
PhysicalDevice *pd=PhysicalDevice::GetInstance(pd_id);
|
||||
|
||||
std::cout<<" PhysicalDevice "<<pd->GetID()<<" name: "<<pd->device_name.c_str()<<std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void main()
|
||||
{
|
||||
CreateTestObject();
|
||||
OutputTestObject();
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user