MFC-like Containers

If you have a cool piece of software to share, but you are not hosting it officially yet, please dump it in here. If you have code snippets that are useful, please donate!
Post Reply
dirkmausf
In need of some credit
In need of some credit
Posts: 7
Joined: Tue May 15, 2007 9:05 pm
Location: Freiburg
Contact:

MFC-like Containers

Post by dirkmausf »

Hi all!

First of all i have to say: I love wxWidgets!

Im using it everyday and in nearly all of my projects and i am very happy with it.

Since i have many old MFC related projects that i want to convert with the fewest possible effort, i have written two little container classes that look a bit like the MFC couterpart.


The first is a Remake of CArray called wxCArray (wxArray still exists). Second is the remake of CMap class (called wxMap), that i used very often.

Both of them are utilizing STL classes (vector<> and map<>).

Have fun with it... its yours now



Code: Select all

#pragma once

#include <vector>


template<class TYPE, class ARG_TYPE = const TYPE&>
class wxCArray : public wxObject
{
public:
// Construction
	wxCArray() {};

// Attributes
	int GetSize()		{ return (int)m_oArray.capacity(); }
	int GetCount()		{ return (int)m_oArray.size(); }
	bool IsEmpty()		{ return m_oArray.empty(); }
	int GetUpperBound() { return (int)m_oArray.max_size(); }
	void SetSize(int nNewSize)  { m_oArray.resize(nNewSize); }

// Operations
	// Clean up
	void FreeExtra()		{ /*return m_oArray.();*/ }
	void RemoveAll()		{ m_oArray.clear(); }	

	// Accessing elements
	TYPE& GetAt(int nIndex) { return m_oArray[nIndex]; }
	void SetAt(int nIndex, ARG_TYPE newElement) { m_oArray[nIndex] = newElement; }
	TYPE& ElementAt(int nIndex)  { return m_oArray[nIndex]; }

	int Add(ARG_TYPE newElement) { m_oArray.push_back(newElement); return (int)m_oArray.size()-1; }
	int Append(const wxCArray& src) { for (int i=0; i<src.Count(); i++) m_oArray.push_back(src[i]);  }
	void Copy(const wxCArray& src)  { m_oArray.assign(src); }

	TYPE& operator[](int nIndex)  { return m_oArray[nIndex]; }

	// Operations that move elements around
	void InsertAt(int nIndex, ARG_TYPE newElement) { m_oArray.insert(m_oArray.begin()+nIndex, newElement); } 
	void RemoveAt(int nIndex) { m_oArray.erase(m_oArray.begin()+nIndex); }

	int Lookup(ARG_TYPE CompElement) { for (int i=0; i<GetCount(); i++) { if (GetAt(i)==CompElement) return i;} return -1; }

// Implementation
protected:
	std::vector<TYPE> m_oArray;

public:
	~wxCArray() {};

};


and

Code: Select all

#pragma once

#include <map>
#include <iterator>

template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>
class wxCMap : public wxObject
{
public:
// Construction
	wxCMap(int nBlockSize = 10) { };

	typedef std::map<KEY,VALUE> BaseMap;

// Attributes
	// number of elements
	int GetCount() { return m_oMap.size(); };
	int GetSize() { return m_oMap.size(); };
	BOOL IsEmpty() { return m_oMap.empty(); };

	// Lookup
	void Lookup(ARG_KEY key, VALUE& rValue) { rValue = m_oMap[key]; }
	VALUE Lookup(ARG_KEY key) { return m_oMap[key]; }

	// Operations
	// Lookup and add if not there
	VALUE& operator[](ARG_KEY key)  { return m_oMap[key]; }; 

	// add a new (key, value) pair
	void SetAt(ARG_KEY key, ARG_VALUE newValue) { m_oMap[key] = newValue; }

	// removing existing (key, ?) pair
	BOOL RemoveKey(ARG_KEY key) { m_oMap.erase(key); return true; }
	void RemoveAll() { m_oMap.clear(); }

	int GetStartPosition() { return m_oMap.begin(); }  

	VALUE& GetAt(int Position)
	{
		return m_oMap.find(m_oMap.begin()+Position);
	}

	void GetNextAssoc(int Position, KEY& rKey, VALUE& rValue)
	{
		rValue = m_oMap.find(m_oMap.begin()+Position);
	}

// Implementation
protected:
	BaseMap m_oMap;

public:
	~wxCMap() {};

};


Byebye, Dirk

[/code]
Post Reply