?? basecollection.h
字號:
/******************************************************************************
CopyRight (c) 2000-2005 南京南自信息技術有限公司
All rights reserved.
文件名稱:BaseCollection.h
文件標識:對象集合基類
文件摘要:對象集合基類實現文件
作 者:王強
建立日期:2005.11.09
完成日期:
當前版本:1.0
修訂版本:未修訂
作 者:
完成日期:
******************************************************************************/
/******************************************************************************
CopyRight (c) 2000-2005 南京南自信息技術有限公司
All rights reserved.
類 名:TBaseCollection
父 類:無
作 者:王強
當前版本:1.1
建立日期:2005.11.09
完成日期:2006.06.14
修訂版本:1.0
作 者:王強
完成日期:2005.11.09
目 的:實現通用的對象集合類功能,即添加、刪除、查找等操作
接 口:見文檔
******************************************************************************/
#ifndef BaseCollectionH
#define BaseCollectionH
#include "bcb_comm.h"
#include <vector>
#include <map>
using namespace std;
//說明:
//1:K類型表示關鍵字索引類型,此類型需要重載<和==操作符
//2:T類型表示集合中元素的指針類型,此類型需要有拷貝構造函數
template<class K, class T> class TBaseCollection
{
typedef map< K, T* > CollectionType;
typedef CollectionType::iterator Position;
public:
//構造
TBaseCollection( void )
{
}
//拷貝構造
TBaseCollection(const TBaseCollection &rhs)
{
Position iteb(m_map.begin());
Position itee(m_map.end());
for(;iteb!=itee;++iteb)
{
T *pObj(new T(*iteb->second));
m_map.insert(make_pair(iteb->first,pObj));
}
}
//重載的=操作符
TBaseCollection &operator=(const TBaseCollection &rhs)
{
if(this!=&rhs)
{
Clear();
Position iteb(m_map.begin());
Position itee(m_map.end());
for(;iteb!=itee;++iteb)
{
T *pObj(new T(*iteb->second));
m_map.insert(make_pair(iteb->first,pObj));
}
}
return *this;
}
//析構
virtual ~TBaseCollection( void )
{
Clear();
}
//添加成員到集合中
T* Add(const K &key, const T *object, const bool bUpdate =true)
{
if(object==NULL)
{
return NULL;
}
//如果在集合中找到相同關鍵字的元素,則根據bUpdate參數的值來決定是否更新
//集合中的元素,如果bUpdate為true,則表示要更新,否則不更新
Position itePos(m_map.find(key));
//查找指定關鍵字的元素是否存在
if(itePos==m_map.end())
{
//添加進集合中的元素需要有拷貝構造函數
//這里集合中保存的是要添加元素的副本
T *newobj(new T(*object));
m_map.insert(make_pair(key,newobj));
return newobj;
}
else
{
if(bUpdate)
{
*(itePos->second) = *object;
}
return itePos->second;
}
}
//從集合中刪除元素,返回true表示指定元素被刪除,返回false表示未找到指定元素
bool Remove(const K &key)
{
Position itePos(m_map.find(key));
//查找指定關鍵字的元素是否存在
if(itePos!=m_map.end())
{
T *obj(itePos->second);
SAFE_DELETE(obj);
m_map.erase(itePos);
return true;
}
return false;
}
//從集合中查找元素
T* Find(const K &key)
{
Position itePos(m_map.find(key));
//查找指定關鍵字的元素是否存在
if(itePos!=m_map.end())
{
return itePos->second;
}
return NULL;
}
//返回集合中元素個數
int Count(void)
{
return m_map.size();
}
//根據索引返回指定位置的元素
T* GetByIndex(int idx)
{
Position iteb(m_map.begin());
Position itee(m_map.end());
for(int i=0;iteb!=itee;++iteb,++i)
{
T *obj(iteb->second);
if(i==idx)
{
return obj;
}
}
return NULL;
}
//清空集合中元素,并釋放元素占用的內容空間
void Clear(void)
{
//釋放集合中元素對象占用的內存
Position iteb(m_map.begin());
Position itee(m_map.end());
for(;iteb!=itee;++iteb)
{
T *obj(iteb->second);
SAFE_DELETE(obj);
}
m_map.clear();
}
//返回集合中所有對象
int GetAllObjects(vector<T*> &vct,bool bClear=true)
{
if(bClear)
{
vct.clear();
}
Position iteb(m_map.begin());
Position itee(m_map.end());
for(int i=0;iteb!=itee;++iteb,++i)
{
vct.push_back(iteb->second);
}
return vct.size();
}
//返回某個對象的副本
//2006.06.20 add by wq
bool GetObjectCopy(const K &key,T *&pObject)
{
T *pFoo(Find(key));
if(pFoo)
{
pObject = new T(*pFoo);
return true;
}
return false;
}
protected:
CollectionType m_map;
};
#endif //BaseCollectionH
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -