国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

工具類庫系列(十二)-ObjectSharedPtrMap

2019-11-08 02:05:00
字體:
來源:轉載
供稿:網友

第十二個工具類:ObjectSharedPtrMap

這是繼ObjectMap之后,第二個Object對象管理類,管理的是對象的智能指針對象

并且:

1、線程安全。

2、多線程環境下:有一個線程在迭代,其他線程在刪除時,迭代不失效

提供的主要接口:

1、增加一個Object的SharedPtr對象:void AddObject(K id, boost::shared_ptr<T> obj);

2、查找一個Object對象,返回該對象的SharedPtr對象(沒找到返回SharedPtr(NULL)):boost::shared_ptr<T> FindObject(K id);

3、判斷一個Object對象是否存在:bool IsExistsObject(K id);

4、刪除一個Object對象:void FreeObject(K id);

5、刪除所有(清空)Object對象:void FreeObject();

6、取出并刪除一個Object對象:boost::shared_ptr<T> PopObject(K id);

7、獲取Object對象的數量:unsigned int GetSize();

8、迭代:boost::shared_ptr<T> BeginObject();  boost::shared_ptr<T> NextObject();

上代碼:

ObjectSharedPtrMap.h

#ifndef __ObjectSharedPtrMap_h__#define __ObjectSharedPtrMap_h__#include <boost/noncopyable.hpp>#include <boost/shared_ptr.hpp>#include <map>#include "ToolDefine.h"namespace common{	namespace tool{		// 帶鍵值對的對象管理池		// 多線程環境下:有一個線程在迭代,其他線程在刪除時,迭代不失效		template <class K, class T>		class ObjectSharedPtrMap : PRivate boost::noncopyable		{		public:			ObjectSharedPtrMap();			~ObjectSharedPtrMap();			// 使用一個空閑結點			void AddObject(K id, boost::shared_ptr<T> obj);			// 隨機一個結點(分配第一個結點,空列表返回空)			boost::shared_ptr<T> RandObject();			// 隨機取出一個結點(該節點從map刪除)			boost::shared_ptr<T> PopObject();			// 取指定結點(該節點從map刪除)			boost::shared_ptr<T> PopObject(K id);			// 查找一個結點			boost::shared_ptr<T> FindObject(K id);			bool IsExistsObject(K id);			// 釋放一個結點			void FreeObject(K id);			// 釋放所有結點			void FreeObject();			// 獲取對象個數			unsigned int GetSize();			// 迭代			boost::shared_ptr<T> BeginObject();			boost::shared_ptr<T> NextObject();		private:			rw_mutex m_object_list_lock;			typedef typename std::map<K, boost::shared_ptr<T> > MapType;			typedef typename std::map<K, boost::shared_ptr<T> >::iterator MapTypeIterator;			MapType m_object_list;			MapTypeIterator m_object_it;		};		template <class K, class T>		ObjectSharedPtrMap<K, T>::ObjectSharedPtrMap()		{			write_lock lock(m_object_list_lock);			m_object_list.clear();			m_object_it = m_object_list.begin();		}		template <class K, class T>		ObjectSharedPtrMap<K, T>::~ObjectSharedPtrMap()		{			write_lock lock(m_object_list_lock);			MapTypeIterator it = m_object_list.begin();			while (it != m_object_list.end())			{				(it->second).reset();				it++;			}			m_object_list.clear();			m_object_it = m_object_list.begin();		}		template <class K, class T>		void ObjectSharedPtrMap<K, T>::AddObject(K id, boost::shared_ptr<T> obj)		{			if (NULL != obj.get())			{				write_lock lock(m_object_list_lock);				// id已存在的需要提前刪除				MapTypeIterator it = m_object_list.find(id);				if (it != m_object_list.end())				{					// 釋放實際數據對象的引用					(it->second).reset();					// 刪除結點					if (m_object_it == it)					{						m_object_list.erase(m_object_it++);					}					else					{						m_object_list.erase(it);					}				}				// 添加新對象				m_object_list[id] = obj;			}		}		template <class K, class T>		boost::shared_ptr<T> ObjectSharedPtrMap<K, T>::RandObject()		{			read_lock lock(m_object_list_lock);			MapTypeIterator it = m_object_list.begin();			if (it != m_object_list.end())			{				return it->second;			}			else			{				boost::shared_ptr<T> temp((T*)NULL);				return temp;			}		}		template <class K, class T>		boost::shared_ptr<T> ObjectSharedPtrMap<K, T>::PopObject()		{			write_lock lock(m_object_list_lock);			MapTypeIterator it = m_object_list.begin();			if (it != m_object_list.end())			{				boost::shared_ptr<T> temp = it->second;				// 刪除結點				if (m_object_it == it)				{					m_object_list.erase(m_object_it++);				}				else				{					m_object_list.erase(it);				}				return temp;			}			else			{				boost::shared_ptr<T> temp((T*)NULL);				return temp;			}		}		template <class K, class T>		boost::shared_ptr<T> ObjectSharedPtrMap<K, T>::PopObject(K id)		{			write_lock lock(m_object_list_lock);			MapTypeIterator it = m_object_list.find(id);			if (it != m_object_list.end())			{				boost::shared_ptr<T> temp = it->second;				// 刪除結點				if (m_object_it == it)				{					m_object_list.erase(m_object_it++);				}				else				{					m_object_list.erase(it);				}				return temp;			}			else			{				boost::shared_ptr<T> temp((T*)NULL);				return temp;			}		}		template <class K, class T>		boost::shared_ptr<T> ObjectSharedPtrMap<K, T>::FindObject(K id)		{			read_lock lock(m_object_list_lock);			MapTypeIterator it = m_object_list.find(id);			if (it != m_object_list.end())			{				return it->second;			}			else			{				boost::shared_ptr<T> temp;				return temp;			}		}		template <class K, class T>		bool ObjectSharedPtrMap<K, T>::IsExistsObject(K id)		{			read_lock lock(m_object_list_lock);			MapTypeIterator it = m_object_list.find(id);			if (it != m_object_list.end())			{				return true;			}			else			{				return false;			}		}		template <class K, class T>		void ObjectSharedPtrMap<K, T>::FreeObject(K id)		{			write_lock lock(m_object_list_lock);			MapTypeIterator it = m_object_list.find(id);			if (it != m_object_list.end())			{				// 釋放實際數據對象的引用				(it->second).reset();				// 刪除結點				if (m_object_it == it)				{					m_object_list.erase(m_object_it++);				}				else				{					m_object_list.erase(it);				}			}		}		template <class K, class T>		void ObjectSharedPtrMap<K, T>::FreeObject()		{			write_lock lock(m_object_list_lock);			MapTypeIterator it = m_object_list.begin();			while (it != m_object_list.end())			{				(it->second).reset();				it++;			}			m_object_list.clear();			m_object_it = m_object_list.begin();		}		template <class K, class T>		unsigned int ObjectSharedPtrMap<K, T>::GetSize()		{			read_lock lock(m_object_list_lock);			return m_object_list.size();		}		template <class K, class T>		boost::shared_ptr<T> ObjectSharedPtrMap<K, T>::BeginObject()		{			{				write_lock lock(m_object_list_lock);				m_object_it = m_object_list.begin();				if (m_object_it != m_object_list.end())				{					return (m_object_it++)->second;				}			}			boost::shared_ptr<T> temp((T*)NULL);			return temp;		}		template <class K, class T>		boost::shared_ptr<T> ObjectSharedPtrMap<K, T>::NextObject()		{			{				write_lock lock(m_object_list_lock);				if (m_object_it != m_object_list.end())				{					return (m_object_it++)->second;				}			}			boost::shared_ptr<T> temp((T*)NULL);			return temp;		}	}}#endif


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 珠海市| 灵石县| 澄迈县| 兴和县| 朝阳县| 措勤县| 榆社县| 洛阳市| 遂平县| 平安县| 许昌县| 达日县| 大港区| 台中市| 中卫市| 安龙县| 藁城市| 卫辉市| 依安县| 福海县| 札达县| 东丽区| 青冈县| 昭苏县| 霍州市| 石嘴山市| 鞍山市| 丽水市| 榆中县| 湖南省| 惠东县| 鄯善县| 台中市| 饶平县| 武陟县| 洪雅县| 房产| 依安县| 通江县| 乌兰浩特市| 乡城县|