第十二个工具类: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
新闻热点
疑难解答