Click here to Skip to main content
15,881,281 members
Articles / Web Development / HTML

A Comprehensive CE Class Library to Replace ATL and MFC

Rate me:
Please Sign up or sign in to vote.
4.48/5 (14 votes)
4 Oct 2000CPOL 277.9K   998   70  
A collection of classes for CE that do not use ATL or MFC, plus an FTP client, database viewer, and sample application that solves beam deflection equations.
#ifndef __CeArray_h__
#define __CeArray_h__

//
// Simplistic, even ineffecient dynamic array
//
template<class T>
class CeArray
{
private:
	int m_nSize;
	T* m_pArray;

public:
	CeArray()
		{ m_pArray = NULL; m_nSize = 0; }
	virtual ~CeArray()
		{ if (m_pArray) delete[] (BYTE*)m_pArray; }

	// Add to the end of the array
	bool Add(T* type);
	bool Add(T& type);
	bool Remove(int n);

	// remove from the array
//	T* Remove(int n);

	T& operator[] (int n) const
		{ return m_pArray[n]; }

	int GetUpperBound() const
		{ return (m_nSize - 1); }

	int GetCount() const
		{ return m_nSize; }

	int GetSize() const
		{ return m_nSize; }

	int SetSize(int nSize);

	T* GetData() const
		{ return m_pArray; }
};

template<class T>
inline int CeArray<T>::SetSize(int nSize)
{
	if (nSize <= m_nSize)
		// smaller, nothing
		return m_nSize;

	// larger
	BYTE* pArray = new BYTE[ nSize * sizeof(T) ];
	if (NULL == pArray)
		return m_nSize;

	if (m_pArray)
	{
		memcpy(pArray, m_pArray, m_nSize * sizeof(T));
		delete[] (BYTE*) m_pArray;
	}

	// copy in an initialized ojbect, allows objects that create
	// initialized objects (e.g., CeString) to function
	T obj;
	for (int ii = m_nSize; ii < nSize; ii++)
		memcpy(&(((T*)pArray)[ii]), &obj, sizeof obj);

	m_nSize = nSize;
	m_pArray = (T*) pArray;
	return m_nSize;
}

// Add to the end of the array
template<class T>
inline bool CeArray<T>::Add(T* pType)
{
	BYTE* pArray = new BYTE[ (m_nSize+1) * sizeof(T) ];
	if (NULL == pArray)
		return false;

	if (m_pArray)
	{
		memcpy(pArray, m_pArray, m_nSize * sizeof(T));
		delete[] (BYTE*) m_pArray;
	}

	m_pArray = (T*) pArray;

	// copy in an initialized ojbect, allows objects that create
	// initialized objects (e.g., CeString) to function
	T obj;
	memcpy(&m_pArray[m_nSize], &obj, sizeof obj);

	// copy object
	m_pArray[m_nSize] = *pType;

	// update the size
	m_nSize++;
	return true;
}


// Add to the end of the array
template<class T>
inline bool CeArray<T>::Add(T& rType)
{
	BYTE* pArray = new BYTE[ (m_nSize+1) * sizeof(T) ];
	if (NULL == pArray)
		return false;

	if (m_pArray)
	{
		memcpy(pArray, m_pArray, m_nSize * sizeof(T));
		delete[] (BYTE*) m_pArray;
	}

	m_pArray = (T*) pArray;

	// copy in an initialized ojbect, allows objects that create
	// initialized objects (e.g., CeString) to function
	T obj;
	memcpy(&m_pArray[m_nSize], &obj, sizeof obj);

	// copy the specified element
	m_pArray[m_nSize] = rType;

	// update the size
	m_nSize++;
	return true;
}


// remove from the array
template <class T>
inline bool CeArray<T>::Remove(int n)
{
	if (n >= m_nSize)
		return FALSE;

	BYTE* pArray = new BYTE[ (m_nSize-1) * sizeof(T) ];
	if (NULL == pArray)
		return FALSE;

	T* pType = &m_pArray[n];

	// call the destructor for the object
	pType->~T();

	if (m_nSize > 1)
	{
		if (0 != n)
			memcpy(pArray, m_pArray, (n+1) * sizeof(T));

		memcpy(
			pArray + n * sizeof(T),
			m_pArray + (n+1),
			(m_nSize - n - 1) * sizeof(T));
	}
	else
	{
		m_pArray = NULL;
	}

	delete[] (BYTE*) m_pArray;

	m_pArray = (T *) pArray;
	m_nSize--;
	return TRUE;
}


#endif // __CeArray_h__

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions