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

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

工具類庫系列(十一)-ObjectMap

2019-11-08 03:17:06
字體:
來源:轉載
供稿:網友

第十一個工具類:ObjectMap

ObjectMap 是一個上篇提到的 objectid64 的一個id<->id映射關系表,由map實現

該類用于需求中時常會出現的 1對1  或者 1對多 映射關系,

比如:

1對1:一個網絡鏈接對象 對應 一個玩家數據對象, 一個玩家數據對象 也對應一個 網絡鏈接對象

1對多:一個玩家對象 對應 一個游戲公會對象

同時:

1、繼承boost::noncopyable,禁止ObjectMap對象間拷貝賦值

2、ObjectMap加了鎖,確保操作是線程安全了

3、實現了當一個線程在對ObjectMap進行迭代,其他線程在對ObjectMap進行添加/刪除,確保迭代不失效

如何實現保證迭代不失效:

ObjectMap中保存了迭代時當前的迭代器std::map::iterator對象

當做刪除操作時,先檢查下要刪除的這個對象是否就是當前正在遍歷的那個對象

是的話,則保存的迭代器后移,再刪除

提供的主要接口:

1、增加一個id<->id的映射關系:void AddObject(objectid64 key, objectid64 value);

2、查找一個id<->id映射關系(沒找到返回NULL_ID):objectid64 FindObject(objectid64 key);

3、判斷一個id<->id映射關系是否存在:bool IsExistsObject(objectid64 key);

4、刪除一個id<->id映射關系:void FreeObject(objectid64 key);

5、獲取id<->id映射關系的數量:unsigned int GetSize();

6、迭代:objectid64 BeginObject();  objectid64 NextObject();

上代碼:

ObjectMap.h

#ifndef __ObjectMap_h__#define __ObjectMap_h__#include <boost/noncopyable.hpp>#include <map>#include "ToolDefine.h"namespace common{	namespace tool{		// 純uint64的鍵值對池		// 主要用于id對id的索引		class ObjectMap : PRivate boost::noncopyable		{		public:			ObjectMap();			~ObjectMap();			// 使用一個空閑結點			void AddObject(objectid64 key, objectid64 value);			// 隨機一個結點(分配第一個結點,空列表返回空)			objectid64 RandObject();			// 查找一個結點(沒找到返回空)			objectid64 FindObject(objectid64 key);			bool IsExistsObject(objectid64 key);			// 釋放一個結點			void FreeObject(objectid64 key);			// 獲取對象個數			unsigned int GetSize();			// 迭代			objectid64 BeginObject();			objectid64 NextObject();		private:			rw_mutex m_object_list_lock;			typedef std::map<objectid64, objectid64> MapType;			typedef std::map<objectid64, objectid64>::iterator MapTypeIterator;			MapType m_object_list;			MapTypeIterator m_object_it;		};	}}#endifObjectMap.cpp

#include "ObjectMap.h"namespace common{	namespace tool{		ObjectMap::ObjectMap()		{			write_lock lock(m_object_list_lock);			m_object_list.clear();			m_object_it = m_object_list.begin();		}		ObjectMap::~ObjectMap()		{			write_lock lock(m_object_list_lock);			m_object_list.clear();			m_object_it = m_object_list.begin();		}		void ObjectMap::AddObject(objectid64 key, objectid64 value)		{			write_lock lock(m_object_list_lock);			m_object_list[key] = value;		}		objectid64 ObjectMap::RandObject()		{			read_lock lock(m_object_list_lock);			MapTypeIterator it = m_object_list.begin();			if (it != m_object_list.end())			{				return it->second;			}			else			{				return NULL_ID;			}		}		objectid64 ObjectMap::FindObject(objectid64 key)		{			read_lock lock(m_object_list_lock);			MapTypeIterator it = m_object_list.find(key);			if (it != m_object_list.end())			{				return it->second;			}			else			{				return NULL_ID;			}		}		bool ObjectMap::IsExistsObject(objectid64 key)		{			read_lock lock(m_object_list_lock);			MapTypeIterator it = m_object_list.find(key);			if (it != m_object_list.end())			{				return true;			}			else			{				return false;			}		}		void ObjectMap::FreeObject(objectid64 key)		{			write_lock lock(m_object_list_lock);			MapTypeIterator it = m_object_list.find(key);			if (it != m_object_list.end())			{				// 刪除結點				if (m_object_it == it)				{					m_object_list.erase(m_object_it++);				}				else				{					m_object_list.erase(it);				}			}		}		unsigned int ObjectMap::GetSize()		{			read_lock lock(m_object_list_lock);			return m_object_list.size();		}		objectid64 ObjectMap::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;			}			else			{				return NULL_ID;			}		}		objectid64 ObjectMap::NextObject()		{			write_lock lock(m_object_list_lock);			if (m_object_it != m_object_list.end())			{				return (m_object_it++)->second;			}			else			{				return NULL_ID;			}		}	}}

其中ToolDefine.h增加了定義讀寫鎖

//讀寫鎖typedef boost::shared_mutex rw_mutex;typedef boost::shared_lock<rw_mutex> read_lock;typedef boost::unique_lock<rw_mutex> write_lock;


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 辽阳市| 大安市| 江阴市| 甘孜| 张家川| 黔西| 潮州市| 白城市| 古浪县| 福州市| 彩票| 含山县| 思茅市| 清水县| 青神县| 凯里市| 贵阳市| 长葛市| 玉溪市| 绩溪县| 五家渠市| 防城港市| 余江县| 米泉市| 济源市| 且末县| 杭州市| 新邵县| 广汉市| 宁强县| 黑龙江省| 渑池县| 宁海县| 望奎县| 文登市| 湖南省| 抚松县| 南丹县| 宁蒗| 图木舒克市| 张家口市|