CArchive_input_iterator‹T› tmp = *this;

  ++*this;

  return tmp;

 }

 CArchive_input_iterator‹T› operator+(int delta) {

  CArchive_input_iterator‹T› tmp = *this;

  tmp.count += delta;

  return tmp;

 }

};

template ‹class T› inline bool operator==(const CArchive_input_iterator‹T›& x, const CArchive_input_iterator‹T›& y) {

 return x.archive == y.archive && x.count == y.count;

}

template ‹class T› inline bool operator!=(const CArchive_input_iterator‹T›& x, const CArchive_input_iterator‹T›& y) {

 return !(x == y);

}

2.4.5 Serializing CStrokeList and CStroke using Archive Iterators

The loops in CStrokeList:: serialize and CStroke:: serialize are then replaced by calls to the copy algorithm as follows:

Output of a CStrokeList:

CArchive_output_iterator‹CStroke*› oi(ar);

copy(begin(), end(), oi);

Input of a CStrokeList

CArchive_input_iterator‹CStroke*› ii(ar);

copy(ii, ii + s, back_inserter(*this));

Output of a CStroke

CArchive_output_iterator‹CPoint› oi(ar);

copy(m_pointArray.begin(), m_pointArray.end(), oi);

Input of a CStroke

CArchive_input_iterator‹CPoint› ii(ar);

copy(ii, ii + w, back_inserter(m_pointArray));

3 Conclusion

The STL containers can be easily adapted to be used in Windows® applications in place of the MFC collection classes. By adding the CArchive input and output iterators, the use of the standard algorithms can be applied to the serialization process.

The STL containers are more general than the MFC collection classes. By separating the iterators from the containers themselves, and by providing a common interface for all containers, the STL container choice is independent of the algorithm. The Scribble example uses a linked list of arrays to represent the strokes. This was replicated in the STL version. The STL version could easily be changed to use an array (vector) of lists, or a list of lists, or an array of arrays.

We first developed this example using Microsoft's Visual C++ version 5.0. We experienced numerous difficulties requiring us to define a class, MYCPoint to extend the CPoint class and developing modified versions of the function binders and adapters. Microsoft's Visual C++ version 6.0 does not require these workarounds.

4 References

[1] Shepherd, George and Wingo, Scot. MFC Internals. Addison-Wesley, 1996.

[2] Stepanov, A. A. and Lee, M. The Standard Template Library. Technical Report HPL-94-34, Hewlett-Packard Laboratories, April 1994.

[3] The Microsoft Developer Studio Help Files and Example Files.

[4] Stroustrup, Bjarne. The C++ Programming Language Third Edition. Addison-Wesley, 1997.

Paul Wolfgang is a Staff Engineer at Boeing and an Adjunct Professor at Temple University. He can be reached at Paul.Wolfgang@Boeing.com  or at wolfgang@falcon.cis.temple.edu.

Yang Song is a graduate student at Temple University. She can be reached at yangsong@astro.ocis.temple.edu.

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

0

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

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