From 12cc93806051d1565ed6a6aaad6453d4f8b2d1f4 Mon Sep 17 00:00:00 2001 From: "HuYingzhuo(hugo/hyzboy)" Date: Thu, 10 Aug 2023 17:51:25 +0800 Subject: [PATCH] improved ObjectMap, use DataLifetimeCallback --- inc/hgl/type/Map.h | 58 ++++++++++++++++------------------------------ 1 file changed, 20 insertions(+), 38 deletions(-) diff --git a/inc/hgl/type/Map.h b/inc/hgl/type/Map.h index 4abce99..dec528d 100644 --- a/inc/hgl/type/Map.h +++ b/inc/hgl/type/Map.h @@ -167,13 +167,15 @@ namespace hgl typedef _Map SuperClass; - virtual void DeleteObject(const K &,V *)=0; ///<删除一个数据 + DefaultObjectLifetimeCallback default_olc; + DataLifetimeCallback *olc; + void DeleteObject(KVData *ds) { if(!ds)return; - if(ds->value) ///<存在数据就是nullptr的可能 - DeleteObject(ds->key,ds->value); + if(olc) + olc->Clear(&(ds->value)); } void DeleteObject(int index) @@ -183,14 +185,19 @@ namespace hgl public: - _ObjectMap()=default; + _ObjectMap() + { + olc=&default_olc; + } + virtual ~_ObjectMap() { - if(SuperClass::GetCount()>0) - { - //LOG_ERROR(u"这是一个严重的程序设计错误,会产生纯虚函数调用,请在派生类的析构函数中调用Clear函数以清除数据。"); - //LOG_ERROR(OS_TEXT("This is a serious design errors, will produce the pure virtual function call, please call in the destructor of the derived class the function to clear the data.")); - } + Clear(); + } + + virtual void SetDataLifetimeCallback(DataLifetimeCallback *cb) ///<设定数据生命周期回调函数 + { + olc=cb?cb:&default_olc; } /** @@ -280,7 +287,7 @@ namespace hgl while(n--) DeleteObject(n); - _Map::Clear(); + SuperClass::Clear(); } /** @@ -334,33 +341,6 @@ namespace hgl } void Clear(){DeleteAll();} - };//class _ObjectMap - - template class CustomObjectMap:public _ObjectMap - { - protected: - - //virtual T * CreateObject(const F &){return(new T);} ///<创建一个数据 - virtual void DeleteObject(const K &,V *obj){delete obj;} ///<删除一个数据 - - public: - - CustomObjectMap()=default; - virtual ~CustomObjectMap() - { - _ObjectMap::Clear(); - } - };//class CustomObjectMap - - template class ObjectMap:public CustomObjectMap > - { - public: - - ObjectMap()=default; - virtual ~ObjectMap() - { - CustomObjectMap >::Clear(); - } V *operator[](const K &index)const { @@ -371,7 +351,9 @@ namespace hgl else return nullptr; }; - };//class ObjectMap + };//class _ObjectMap + + template using ObjectMap=_ObjectMap >; }//namespace hgl #include #endif//HGL_MAP_INCLUDE