⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 myobjcollection.cpp

📁 实现撤销和恢复功能得远吗
💻 CPP
字号:
// MyObjCollection.cpp: implementation of the CMyObjCollection class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "TestUndo.h"
#include "MyObjCollection.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CMyObjCollection::CMyObjCollection()
{

}

CMyObjCollection::~CMyObjCollection()
{
	Clear();
}

int CMyObjCollection::getCount()
{
	return m_vecMyObjects.size();
}

CMyObject* CMyObjCollection::getItem(int index)
{
	return m_vecMyObjects[index];
}

bool CMyObjCollection::IsEmpty()
{
	return (m_vecMyObjects.size() == 0);
}

void CMyObjCollection::Add(CMyObject* pItem)
{
	ASSERT(Find(pItem) == -1); // 不能多次加入同一对象
	m_vecMyObjects.push_back(pItem);
	pItem->AddRef();
}

void CMyObjCollection::Remove(CMyObject* pItem)
{
	for (vector<CMyObject*>::iterator it = m_vecMyObjects.begin(); 
		it != m_vecMyObjects.end(); it++)
	{
		if (*it == pItem)
		{
			m_vecMyObjects.erase(it);
			pItem->Release();
			return;
		}
	}
}

void CMyObjCollection::RemoveAt(int index)
{
	CMyObject* pItem = m_vecMyObjects[index];
	m_vecMyObjects.erase(m_vecMyObjects.begin() + index);
	pItem->Release();
}

void CMyObjCollection::Clear()
{
	for (int i = 0; i < m_vecMyObjects.size(); i++)
		m_vecMyObjects[i]->Release();
	m_vecMyObjects.erase(m_vecMyObjects.begin(), m_vecMyObjects.end());
}

bool CMyObjCollection::Contains(CMyObject* pItem)
{
	return (Find(pItem) != -1);
}

int CMyObjCollection::Find(CMyObject* pItem)
{
	for (int i = 0; i < m_vecMyObjects.size(); i++)
	{
		if (m_vecMyObjects[i] == pItem)
			return i;
	}
	return -1;
}

CMyObject* CMyObjCollection::operator [] (int index)
{
	return m_vecMyObjects[index];
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -