Brief Full Advanced Search Search Tips
|  |  | To access the contents, click the chapter and section titles. Fast Track Visual C++ 6.0 Programming (Publisher: John Wiley & Sons, Inc.) Author(s): Steve Holzner ISBN: 0471312908 Publication Date: 09/01/98 Search this book:
Previous | Table of Contents | Next
Finally, we add the required get and set functions: GetText(), SetText(), GetX(), SetX(), and so on.
    class CData{
    private:
        CString m_text;
        int m_x, m_y;
    public:
        CData() {m_text = CString(””); m_x = m_y = 0;}
        CString GetText() {return m_text;}                          ⇐
        void SetText(CString new_text) {m_text = new_text;}         ⇐
        int GetX() {return m_x;}                                    ⇐
        void SetX(int new_x) {m_x = new_x;}                         ⇐
        int GetY() {return m_y;}                                    ⇐
        void SetY(int new_y) {m_y = new_y;}                         ⇐
    };
Then we add the object of this class, m_data, that we use to store the data in the Editor program to the document’s header.
    class CEditorDoc : public CDocument
    {
    protected: // create from serialization only
        CEditorDoc();
        DECLARE_DYNCREATE(CEditorDoc)
    // Attributes
    public:
        CData m_data;                       ⇐
         .
         .
         .
The storage for our data is set up in the document. We have yet to allow the user to save the data to disk, so we do that now. ### Working with Files: Serialization In MFC programs, files are saved and retrieved through serialization. Serialization is based on overloading the << and >> operators for the CArchive class. We are passed an object of that class in the document’s Serialize() function. If the object we’re serializing is based on the MFC CObject class, we can serialize it using the << and >> operators. For example, if our Editor program stored only the text in a CString object named m_text, we could add this code to the document’s Serialize() function to serialize that object and so provide file handling.
    void CEditorDoc::Serialize(CArchive& ar)
    {
        if (ar.IsStoring())
        {
         ar << m_text;                       ⇐
        }
        else
        {
         ar >> m_text;                       ⇐
        }
    }
All the file-handling menu items such as Open, Save, and Save As are already active. Now that we’ve enabled serialization, our program would handle files—if our program stored data in a simple CString object. However, in Editor, we use the CData class, which stores not only a CString object, but two integers representing the screen location of the string as well. We can serialize CData objects with a little work, and we do so now. #### Serializing Our Data Object We modify the CData class to enable serialization, and there are several simple steps to follow here. We start by making the MFC CObject class into CData’s base class.
    class CData : public CObject {          ⇐
        .
        .
        .
Next, we include the declarations needed for the serialization process with the MFC macro DECLARE_SERIAL.
    class CData : public CObject {
    private:
        CString m_text;
        int m_x, m_y;
        DECLARE_SERIAL(CData);              ⇐
         .
         .
         .
We also need a new function in the CData class: Serialize().
    class CData : public CObject {
    private:
        CString m_text;
        int m_x, m_y;
        DECLARE_SERIAL(CData);
    public:
        CData() {m_text = CString(””); m_x = m_y = 0;}
        CString GetText() {return m_text;}
        void SetText(CString new_text) {m_text = new_text;}
        int GetX() {return m_x;}
        void SetX(int new_x) {m_x = new_x;}
        int GetY() {return m_y;}
        void SetY(int new_y) {m_y = new_y;}
        void Serialize(CArchive& archive);               ⇐
    };
The last step here is to write the Serialize() function. To do that, we add a new file to the project with the New item in the File menu — Cdata.cpp—which we start with the following code:
    #include “stdafx.h”
    #include “EditorDoc.h”

    void CData::Serialize(CArchive &archive)
    {

    }
First, we call the CObject base class’s Serialize function.
    IMPLEMENT_SERIAL(CData, CObject, 0)
    #include “stdafx.h”
    #include “EditorDoc.h”

    void CData::Serialize(CArchive &archive)
    {
        CObject::Serialize(archive);                ⇐
         .
         .
         .
    }
Then, we add the code to serialize the three data members we want to work with here: m_x, m_y, and m_text.
    #include “stdafx.h”
    #include “EditorDoc.h”

    void CData::Serialize(CArchive &archive)
    {
        CObject::Serialize(archive);
        if(archive.IsStoring()){                   ⇐
         archive << m_x;                           ⇐
         archive << m_y;                           ⇐
         archive << m_text;                        ⇐
        }                                          ⇐
        else{                                      ⇐
         archive >> m_x;                           ⇐
         archive >> m_y;                           ⇐
         archive >> m_text;                        ⇐
        }                                          ⇐
    }
The final step is to add the code for the MFC serialization support, which we do with the IMPLEMENT_SERIAL macro.
    #include “stdafx.h”
    #include “EditorDoc.h”

    void CData::Serialize(CArchive &archive)
    {
        CObject::Serialize(archive);
        if(archive.IsStoring()){
         archive << m_x;
         archive << m_y;
         archive << m_text;
        }
        else{
         archive >> m_x;
         archive >> m_y;
         archive >> m_text;
        }
    }

    IMPLEMENT_SERIAL(CData, CObject, 0)             ⇐
Version Numbering The last parameter in the IMPLEMENT_SERIAL macro is a version number. Each time you update the version of your data object, you can increment this value if you wish. All that remains is to add the correct code to the document’s Serialize() function, and we do that as follows:
    void CEditorDoc::Serialize(CArchive& ar)
    {
        m_data.Serialize(ar);                       ⇐
    }
We’ve enabled file handling in our program; all the file-handling menu items are now active. #### Setting the Editor Data File’s Default Extension We can set the default file extension associated with our data files by editing the IDR_EDITORTYPE resource string.
    \nEditor\nEditor\n\n\nEditor.Document\nEditor Document
To make the default file extension for our files, say, .ed, we change that string this way:
    \nEditor\nEditor\n\n.ed\nEditor.Document\nEditor Document     ⇐
Now the first document created by our program will be Editor1.ed, the second Editor2.ed, and so on. We can do more here: We can register our file type with Windows so that when the user double-clicks a file with the .ed extension, it opens automatically in the Editor program.
Previous | Table of Contents | Next

| Products | Contact Us | About Us | Privacy | Ad Info | Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.