File:  [WindowsNT SDKs] / mstools / mfc / samples / templdef / array.ctt
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs
Thu Aug 9 18:21:00 2018 UTC (7 years, 9 months ago) by root
Branches: msft, MAIN
CVS tags: ntsdk-oct-1992, ntsdk-jun-1992, ntsdk-jul-1993, HEAD
Microsoft Windows NT Build 297 06-28-1992

////////////////////////////////////////////////////////////////////////////
// class CArray<TYPE, ARG_TYPE, IS_SERIAL, HAS_CREATE> - an array containing 'TYPE' elements,
// passed in parameters as ARG_TYPE
//
// This is a part of the Microsoft Foundation Classes C++ library.
// Copyright (C) 1992 Microsoft Corporation
// All rights reserved.
//
// This source code is only intended as a supplement to the
// Microsoft Foundation Classes Reference and Microsoft
// QuickHelp documentation provided with the library.
// See these sources for detailed information regarding the
// Microsoft Foundation Classes product.
////////////////////////////////////////////////////////////////////////////

//$DECLARE_TEMPLATE

////////////////////////////////////////////////////////////////////////////

template<class TYPE, class ARG_TYPE, int IS_SERIAL, int HAS_CREATE>
class CArray : public CObject
{
#if IS_SERIAL
	DECLARE_SERIAL(CArray)
#else
	DECLARE_DYNAMIC(CArray)
#endif //!IS_SERIAL
public:

// Construction
	CArray();

// Attributes
	int     GetSize() const
				{ return m_nSize; }
	int     GetUpperBound() const
				{ return m_nSize-1; }
	void    SetSize(int nNewSize, int nGrowBy = -1);

// Operations
	// Clean up
	void    FreeExtra();
	void    RemoveAll()
				{ SetSize(0); }

	// Accessing elements
	TYPE    GetAt(int nIndex) const
				{ ASSERT(nIndex >= 0 && nIndex < m_nSize);
					return m_pData[nIndex]; }
	void    SetAt(int nIndex, ARG_TYPE newElement)
				{ ASSERT(nIndex >= 0 && nIndex < m_nSize);
					m_pData[nIndex] = newElement; }
	TYPE&   ElementAt(int nIndex)
				{ ASSERT(nIndex >= 0 && nIndex < m_nSize);
					return m_pData[nIndex]; }

	// Potentially growing the array
	void    SetAtGrow(int nIndex, ARG_TYPE newElement);
	int     Add(ARG_TYPE newElement)
				{ int nIndex = m_nSize;
					SetAtGrow(nIndex, newElement);
					return nIndex; }

	// overloaded operator helpers
	TYPE    operator[](int nIndex) const
				{ return GetAt(nIndex); }
	TYPE&   operator[](int nIndex)
				{ return ElementAt(nIndex); }

	// Operations that move elements around
	void    InsertAt(int nIndex, ARG_TYPE newElement, int nCount = 1);
	void    RemoveAt(int nIndex, int nCount = 1);
	void    InsertAt(int nStartIndex, CArray* pNewArray);

// Implementation
protected:
	TYPE*   m_pData;        // the actual array of data
	int     m_nSize;        // # of elements (upperBound - 1)
	int     m_nMaxSize;     // max allocated
	int     m_nGrowBy;      // grow amount

public:
	~CArray();
#if IS_SERIAL
	void    Serialize(CArchive&);
#endif //IS_SERIAL
#ifdef _DEBUG
	void    Dump(CDumpContext&) const;
	void    AssertValid() const;
#endif
};

//$IMPLEMENT_TEMPLATE

/////////////////////////////////////////////////////////////////////////////
//
// Implementation of Array of TYPEs
//
/////////////////////////////////////////////////////////////////////////////
// NOTE: we allocate an array of 'm_nMaxSize' elements, but only
//  the current size 'm_nSize' contains properly constructed
//  objects.

#include "afxcoll.h"
#pragma hdrstop

#ifdef AFX_COLL_SEG
#pragma code_seg(AFX_COLL_SEG)
#endif

#include <limits.h>
#define SIZE_T_MAX  UINT_MAX            /* max size for a size_t */

#if IS_SERIAL
IMPLEMENT_SERIAL(CArray, CObject, 0);
#else
IMPLEMENT_DYNAMIC(CArray, CObject);
#endif //!IS_SERIAL

#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif

#define new DEBUG_NEW

/////////////////////////////////////////////////////////////////////////////

#if HAS_CREATE
#include "elements.h"       // used for special creation

static void NEAR ConstructElements(register TYPE* pNewData, int nCount)
{
	ASSERT(nCount >= 0);

	while (nCount--)
	{
		ConstructElement(pNewData);
		pNewData++;
	}
}

static void NEAR DestructElements(register TYPE* pOldData, int nCount)
{
	ASSERT(nCount >= 0);

	while (nCount--)
	{
		pOldData->Empty();
		pOldData++;
	}
}
#endif //HAS_CREATE

/////////////////////////////////////////////////////////////////////////////

template<class TYPE, class ARG_TYPE, int IS_SERIAL, int HAS_CREATE>
CArray<TYPE, ARG_TYPE, IS_SERIAL, HAS_CREATE>::CArray()
{
	m_pData = NULL;
	m_nSize = m_nMaxSize = m_nGrowBy = 0;
}

template<class TYPE, ARG_TYPE, int IS_SERIAL, int HAS_CREATE>
CArray<TYPE, ARG_TYPE, IS_SERIAL, HAS_CREATE>::~CArray()
{
	ASSERT_VALID(this);

#if HAS_CREATE
	DestructElements(m_pData, m_nSize);
#endif //HAS_CREATE
	delete [] (BYTE*)m_pData;
}

template <class TYPE, class ARG_TYPE, int IS_SERIAL, int HAS_CREATE>
void CArray<TYPE, ARG_TYPE, IS_SERIAL, HAS_CREATE>::SetSize(int nNewSize, int nGrowBy /* = -1 */)
{
	ASSERT_VALID(this);
	ASSERT(nNewSize >= 0);

	if (nGrowBy != -1)
		m_nGrowBy = nGrowBy;    // set new size

	if (nNewSize == 0)
	{
		// shrink to nothing
#if HAS_CREATE
		DestructElements(m_pData, m_nSize);
#endif //HAS_CREATE
		delete [] (BYTE*)m_pData;
		m_pData = NULL;
		m_nSize = m_nMaxSize = 0;
	}
	else if (m_pData == NULL)
	{
		// create one with exact size
#ifdef SIZE_T_MAX
		ASSERT((long)nNewSize * sizeof(TYPE) <= SIZE_T_MAX);    // no overflow
#endif
		m_pData = (TYPE*) new BYTE[nNewSize * sizeof(TYPE)];
#if HAS_CREATE
		ConstructElements(m_pData, nNewSize);
#else
		memset(m_pData, 0, nNewSize * sizeof(TYPE));        // zero fill
#endif
		m_nSize = m_nMaxSize = nNewSize;
	}
	else if (nNewSize <= m_nMaxSize)
	{
		// it fits
		if (nNewSize > m_nSize)
		{
			// initialize the new elements
#if HAS_CREATE
			ConstructElements(&m_pData[m_nSize], nNewSize-m_nSize);
#else
			memset(&m_pData[m_nSize], 0, (nNewSize-m_nSize) * sizeof(TYPE));
#endif
		}
#if HAS_CREATE
		else if (m_nSize > nNewSize) // destroy the old elements
			DestructElements(&m_pData[nNewSize], m_nSize-nNewSize);
#endif
		m_nSize = nNewSize;
	}
	else
	{
		// Otherwise grow array
		int nNewMax;
		if (nNewSize < m_nMaxSize + m_nGrowBy)
			nNewMax = m_nMaxSize + m_nGrowBy;   // granularity
		else
			nNewMax = nNewSize; // no slush

#ifdef SIZE_T_MAX
		ASSERT((long)nNewMax * sizeof(TYPE) <= SIZE_T_MAX); // no overflow
#endif
		TYPE* pNewData = (TYPE*) new BYTE[nNewMax * sizeof(TYPE)];

		// copy new data from old
		memcpy(pNewData, m_pData, m_nSize * sizeof(TYPE));

		// construct remaining elements
		ASSERT(nNewSize > m_nSize);
#if HAS_CREATE
		ConstructElements(&pNewData[m_nSize], nNewSize-m_nSize);
#else
		memset(&pNewData[m_nSize], 0, (nNewSize-m_nSize) * sizeof(TYPE));
#endif

		// get rid of old stuff (note: no destructors called)
		delete [] (BYTE*)m_pData;
		m_pData = pNewData;
		m_nSize = nNewSize;
		m_nMaxSize = nNewMax;
	}
}

template <class TYPE, class ARG_TYPE, int IS_SERIAL, int HAS_CREATE>
void CArray<TYPE, ARG_TYPE, IS_SERIAL, HAS_CREATE>::FreeExtra()
{
	ASSERT_VALID(this);

	if (m_nSize != m_nMaxSize)
	{
		// shrink to desired size
#ifdef SIZE_T_MAX
		ASSERT((long)m_nSize * sizeof(TYPE) <= SIZE_T_MAX); // no overflow
#endif
		TYPE* pNewData = (TYPE*) new BYTE[m_nSize * sizeof(TYPE)];
		// copy new data from old
		memcpy(pNewData, m_pData, m_nSize * sizeof(TYPE));

		// get rid of old stuff (note: no destructors called)
		delete [] (BYTE*)m_pData;
		m_pData = pNewData;
		m_nMaxSize = m_nSize;
	}
}

/////////////////////////////////////////////////////////////////////////////

template <class TYPE, class ARG_TYPE, int IS_SERIAL, int HAS_CREATE>
void CArray<TYPE, ARG_TYPE, IS_SERIAL, HAS_CREATE>::SetAtGrow(int nIndex, ARG_TYPE newElement)
{
	ASSERT(nIndex >= 0);
	if (nIndex >= m_nSize)
		SetSize(nIndex+1);
	m_pData[nIndex] = newElement;
}

template <class TYPE, class ARG_TYPE, int IS_SERIAL, int HAS_CREATE>
void CArray<TYPE, ARG_TYPE, IS_SERIAL, HAS_CREATE>::InsertAt(int nIndex, ARG_TYPE newElement, int nCount /*=1*/)
{
	ASSERT_VALID(this);
	ASSERT(nIndex >= 0);        // will expand to meet need
	ASSERT(nCount > 0);     // zero or negative size not allowed

	if (nIndex >= m_nSize)
	{
		// adding after the end of the array
		SetSize(nIndex + nCount);       // grow so nIndex is valid
	}
	else
	{
		// inserting in the middle of the array
		int nOldSize = m_nSize;
		SetSize(m_nSize + nCount); // grow it to new size
		// shift old data up to fill gap
		memmove(&m_pData[nIndex+nCount], &m_pData[nIndex],
			(nOldSize-nIndex) * sizeof(TYPE));

		// re-init slots we copied from
#if HAS_CREATE
		ConstructElements(&m_pData[nIndex], nCount);
#else
		memset(&m_pData[nIndex], 0, nCount * sizeof(TYPE));
#endif
	}

	// insert new value in the gap
	ASSERT(nIndex + nCount <= m_nSize);
	while (nCount--)
		m_pData[nIndex++] = newElement;
}

template <class TYPE, class ARG_TYPE, int IS_SERIAL, int HAS_CREATE>
void CArray<TYPE, ARG_TYPE, IS_SERIAL, HAS_CREATE>::RemoveAt(int nIndex, int nCount /* = 1 */)
{
	ASSERT_VALID(this);
	ASSERT(nIndex >= 0);
	ASSERT(nCount >= 0);
	ASSERT(nIndex + nCount <= m_nSize);

	// just remove a range
	int nMoveCount = m_nSize - (nIndex + nCount);
#if HAS_CREATE
	DestructElements(&m_pData[nIndex], nCount);
#endif
	if (nMoveCount)
		memcpy(&m_pData[nIndex], &m_pData[nIndex + nCount],
			nMoveCount * sizeof(TYPE));
	m_nSize -= nCount;
}

template <class TYPE, class ARG_TYPE, int IS_SERIAL, int HAS_CREATE>
void CArray<TYPE, ARG_TYPE, IS_SERIAL, HAS_CREATE>::InsertAt(int nStartIndex, CArray* pNewArray)
{
	ASSERT_VALID(this);
	ASSERT(pNewArray != NULL);
	ASSERT(pNewArray->IsKindOf(RUNTIME_CLASS(CArray)));
	ASSERT_VALID(pNewArray);
	ASSERT(nStartIndex >= 0);

	if (pNewArray->GetSize() > 0)
	{
		InsertAt(nStartIndex, pNewArray->GetAt(0), pNewArray->GetSize());
		for (int i = 0; i < pNewArray->GetSize(); i++)
			SetAt(nStartIndex + i, pNewArray->GetAt(i));
	}
}

/////////////////////////////////////////////////////////////////////////////
// Serialization

template<class TYPE, class ARG_TYPE, int IS_SERIAL, int HAS_CREATE>
#if IS_SERIAL
void CArray<TYPE, ARG_TYPE, IS_SERIAL, HAS_CREATE>::Serialize(CArchive& ar)
{
	ASSERT_VALID(this);

	CObject::Serialize(ar);

	if (ar.IsStoring())
	{
		ar << (WORD) m_nSize;
		for (int i = 0; i < m_nSize; i++)
			ar << m_pData[i];
	}
	else
	{
		WORD    nOldSize;
		ar >> nOldSize;
		SetSize(nOldSize);

		for (int i = 0; i < m_nSize; i++)
			ar >> m_pData[i];
	}
}
#endif //IS_SERIAL

/////////////////////////////////////////////////////////////////////////////
// Diagnostics

#ifdef _DEBUG
template <class TYPE, class ARG_TYPE, int IS_SERIAL, int HAS_CREATE>
void CArray<TYPE, ARG_TYPE, IS_SERIAL, HAS_CREATE>::Dump(CDumpContext& dc) const
{
	ASSERT_VALID(this);

#define MAKESTRING(x) #x
	dc << "a " MAKESTRING(CArray) " with " << m_nSize << " elements";
#undef MAKESTRING
	if (dc.GetDepth() > 0)
	{
		dc << "\n";
		for (int i = 0; i < m_nSize; i++)
			dc << "\n\t[" << i << "] = " << m_pData[i];
	}
}

template <class TYPE, class ARG_TYPE, int IS_SERIAL, int HAS_CREATE>
void CArray<TYPE, ARG_TYPE, IS_SERIAL, HAS_CREATE>::AssertValid() const
{
	CObject::AssertValid();
	if (m_pData == NULL)
	{
		ASSERT(m_nSize == 0);
		ASSERT(m_nMaxSize == 0);
	}
	else
	{
		ASSERT(m_nSize <= m_nMaxSize);
	}
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////


unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.