第十二個工具類: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
新聞熱點
疑難解答