Paul Wolfgang - Integration of the Standard Template Library and the Microsoft Foundation Class

Здесь есть возможность читать онлайн «Paul Wolfgang - Integration of the Standard Template Library and the Microsoft Foundation Class» весь текст электронной книги совершенно бесплатно (целиком полную версию без сокращений). В некоторых случаях можно слушать аудио, скачать через торрент в формате fb2 и присутствует краткое содержание. Жанр: Программирование, на английском языке. Описание произведения, (предисловие) а так же отзывы посетителей доступны на портале библиотеки ЛибКат.

Integration of the Standard Template Library and the Microsoft Foundation Class: краткое содержание, описание и аннотация

Предлагаем к чтению аннотацию, описание, краткое содержание или предисловие (зависит от того, что написал сам автор книги «Integration of the Standard Template Library and the Microsoft Foundation Class»). Если вы не нашли необходимую информацию о книге — напишите в комментариях, мы постараемся отыскать её.

Integration of the Standard Template Library and the Microsoft Foundation Class — читать онлайн бесплатно полную книгу (весь текст) целиком

Ниже представлен текст книги, разбитый по страницам. Система сохранения места последней прочитанной страницы, позволяет с удобством читать онлайн бесплатно книгу «Integration of the Standard Template Library and the Microsoft Foundation Class», без необходимости каждый раз заново искать на чём Вы остановились. Поставьте закладку, и сможете в любой момент перейти на страницу, на которой закончили чтение.

Тёмная тема
Сбросить

Интервал:

Закладка:

Сделать

2.4.2 The serialize function

The generalize scheme for the serialize function is as follows:

void CMyClass::Serialize(CArchive& ar) {

CObject::Serialize(ar);

if (ar.IsStoring()) {

// insert the member objects into ar

} else {

// extract the member objects from ar

}

}

2.4.3 Serializing the stroke list and the stroke

The straightforward implementation of serialize for CStrokeList and CStroke are as follows:

void CStrokeList::Serialize( CArchive& ar) {

CObject::Serialize(ar);

if (ar.IsStoring()) {

ar ‹‹ (WORD) size ();

for(list‹CStroke*›::iterator it = begin(); it!= end(); ++it) ar ‹‹ *it;

} else {

WORD s;

ar ›› s; // get size

clear();

for (int i = 0; i!=s; i ++)

{

ar ›› temp; push_back(temp);

}

}

}

void CStroke::Serialize( CArchive& ar) {

CObject::Serialize(ar);

if (ar.IsStoring()) {

ar ‹‹ (WORD)m_nPenWidth;

ar ‹‹ (WORD) m_pointArray.size();

for (vector‹CPoint›::iterator i = m_pointArray.begin(); i!= m_pointArray.end(); ++i)

ar ‹‹ *i;

} else {

WORD w;

ar ›› w; // pen width

m_nPenWidth = w;

m_pointArray.clear();

ar ›› w; // array size

CPoint point;

m_pointArray.reserve(w);

for (int i = 0; i ‹ w; ++i) {

ar ›› point; m_pointArray.push_back( point);

}

}

}

2.4.4 Archive iterators

Since CArchive is defined to work like istream and ostream, it is desirable to define a CArchive_input_iterator and a CArchive_output_iterator that will work like the istream_iterator and ostream_iterator. The loops can then be replaced by a call to the copy algorithm.

2.4.4.1 CArchive_output_iterator

Adapting the output_iterator to become the CArchive_output_iterator is very straightforward. Merely replace ostream with CArchive. The result is as follows:

template ‹class T›

class CArchive_output_iterator: public std::iterator‹std::output_iterator_ tag, void, void› {

protected:

CArchive* archive;

public:

CArchive_output_iterator( CArchive& s): archive(&s) {}

CArchive_output_iterator‹T›& operator=(const T& value) {

*archive ‹‹ value;

return *this;

}

CArchive_output_iterator‹T›& operator*() {return *this;}

CArchive_output_iterator‹T›& operator++() {return *this;}

CArchive_output_iterator‹T›& operator++(int) {return *this;}

};

2.4.4.2 CArchive_input_iterator

The loop to read a stroke must terminate after all of the points for that stroke have been read. This is not at the end of the input stream. We will need to pass to the copy algorithm a CArchive_input_iterator that represents the current file position as the first argument, and a CArchive_input_iterator that represents the position at which the copy operator should stop. If we were copying from a standard array, the copy call would look something like this:

copy(start, start+n, destination);

Therefore, we overload the + operator so that a CArchive_input_iterator plus an int will result in a CArchive_input_iterator object that can be used by the equality operator called by the copy algorithm to terminate the loop.

In istream_iterator the data value is extracted from the istream when the iterator object is first created and whenever the ++ operator is called. The dereferencing operator (*) then retrieves the saved value. Since we do not want to extract an object from the CArchive once the specified number of objects have been retrieved, we set a flag to indicate the need to retrieve a new object, and have the actual extraction performed by the * operator. The resulting implementation of CArchive_input_iterator is as follows:

template ‹class T›

class CArchive_input_iterator: std::iterator‹std::input_iterator_t ag, T, ptrdiff_t› {

friend bool operator==(const CArchive_input_iterator‹T›& x, const CArchive_input_iterator‹T›& y);

friend bool operator!=(const CArchive_input_iterator‹T›& x, const CArchive_input_iterator‹T›& y);

protected:

CArchive* archive;

T value;

bool flag; // True to indicate that value is defined

int count; // Count of the number of times ++ has been

// applied Or the target value for the number of advances

public:

CArchive_input_iterator(): archive(0), flag(false), count(0) {}

CArchive_input_iterator(CArchive& s): archive(& s), count(0), flag(false) {}

const T& operator*() {

if (flag) { return value; }

else {

*archive ›› value;

flag = true;

return value;

}

}

CArchive_input_iterator‹T›& operator++() {

++count;

flag = false;

return *this;

}

CArchive_input_iterator‹T› operator++(int) {

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);

Читать дальше
Тёмная тема
Сбросить

Интервал:

Закладка:

Сделать

Похожие книги на «Integration of the Standard Template Library and the Microsoft Foundation Class»

Представляем Вашему вниманию похожие книги на «Integration of the Standard Template Library and the Microsoft Foundation Class» списком для выбора. Мы отобрали схожую по названию и смыслу литературу в надежде предоставить читателям больше вариантов отыскать новые, интересные, ещё непрочитанные произведения.


Отзывы о книге «Integration of the Standard Template Library and the Microsoft Foundation Class»

Обсуждение, отзывы о книге «Integration of the Standard Template Library and the Microsoft Foundation Class» и просто собственные мнения читателей. Оставьте ваши комментарии, напишите, что Вы думаете о произведении, его смысле или главных героях. Укажите что конкретно понравилось, а что нет, и почему Вы так считаете.

x