#pragma once #include using namespace hgl; template struct RuntimeAssetManager:public ObjectManage { public: bool Add(V *v) { if(!v)return(false); return ObjectManage::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 ReleaseInstance() { RAM.Release(RuntimeAssetID); delete this; } }; #define HGL_RUNTIME_ASSET_CLASS(RA_ID_TYPE,_RA_CLASS) #define HGL_RUNTIME_ASSET_DECLARATION(RA_ID_TYPE,RA_CLASS) RuntimeAssetManager RuntimeAsset::RAM; /** * Example: * * MyRuntimeAsset.h * * using MyAssetID=uint; * * struct MyRuntimeAsset:public RuntimeAsset * { * public: * * using RuntimeAsset::RuntimeAsset; * }; * * MyRuntimeAsset.cpp * * #include"MyRuntimeAsset.h" * * HGL_RUNTIME_ASSET_DECLARATION(MyAssetID,MyRuntimeAsset); * */