Paul Wolfgang, Yang Song

Integration of the Standard Template Library and the Microsoft Foundation Class

1 Introduction

Both the Microsoft Foundation Class (MFC) [1] and the Standard Template Library (STL) [2] provide generalized containers and a facility to iterate over all of the objects within a container. However, the approach taken is different. Within the MFC the iteration mechanism is dependent upon the container, while in the STL there is a common iteration mechanism so that an algorithm can operate on each element of a container without knowledge of the container's type. The MFC containers support persistent storage, which is not a feature of the STL.

This paper presents a small example of a Windows® application using the STL containers in place of the corresponding MFC containers.

2 Example – Scribble.

2.1 Description of the Problem

The MFC Tutorial [3] includes a simple graphics application known as Scribble. The purpose of Scribble is to let the user draw a set of strokes with the mouse. The result is saved in a file (called a document) which can be opened and updated by adding additional strokes. (There is no method for deleting a stroke.) The user also has the option of specifying the thickness and color of the pen.

Scribble's data structure consists of one or more strokes. Each stroke is the record of the mouse position from the time when the user clicks on left mouse key to the time when the user releases the mouse button. In MFC approach, a new class CStroke, which is derived from class CObject, is defined. This contains a data member of CArray‹CPoint, CPoint› with other data members to record and work on each stroke. The document class, CScribbDoc, is derived from CDocument. It contains a list of stokes using the MFC template class CTypedPtrList‹CObList, CStroke*›.

Figure 1 illustrates the document data structure.

Figure 1 Scribble Document Structure

2.2 Type definitions

2.2.1 MFC Implementation

In the MFC implementation, the list of strokes is stored in the member m_strokeList which is defined as a CTypedPtrList‹CObject, CStroke*›. The class CStroke, in turn, contains a CArray‹CPoint, CPoint› to contain the array of points that constitute the stroke.

2.2.2 STL Implementation

The class CStrokeList is defined to replace the CTypedPtrList‹CObject, CStroke*› as follows:

class CStrokeList: public CObject, public std::list‹CStroke*› {

public:

 CStrokeList () {}

 CStrokeList(const CStrokeList&);

 DECLARE_SERIAL(CStrokeList)

public:

 virtual void Serialize(CArchive& ar);

};

The CArray‹CPoint, CPoint› in CStroke is replaced by a std::list‹CPoint›.

2.3 Using the STL algorithms and iterators

2.3.1 Inserting points into a stroke

In the original MFC implementation, points were added to a stroke by the following statement:

m_pStrokeCur-›m_pointArray.Add(point);

In the STL implementation this becomes:

m_pStrokeCur-›m_pointArray.push_back(point);

2.3.2 Drawing all strokes

In the original MFC implementation, the list of strokes was traversed and each stroke drawn by the following code:

POSITION pos = strokeList.front ();

while (pos!= NULL) {

 CStroke* pStroke = strokeList.GetNext(pos);

 pStroke-›DrawStroke(pDC);

}

We make two changes. The first obvious change is to replace the MFC list iteration with the corresponding STL iteration. The result is as follows:

for (std::list‹CStroke*›::iterator i = strokeList.begin(); i!= strokeList.end(); ++i)

 (*i)-›DrawStroke(pDC);

The second change is to apply the for_each algorithm. Unfortunately, the for_each algorithm takes as its third argument a function of one argument, that argument being the type obtained by de-referencing the iterator. Specifically, we must convert the expression:

(*i)-›DrawStroke(pDC);

Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

Вы можете отметить интересные вам фрагменты текста, которые будут доступны по уникальной ссылке в адресной строке браузера.

Отметить Добавить цитату
×