Rogue Wave banner
Previous fileTop of DocumentContentsIndex pageNext file
XML Streams Module User's Guide
Rogue Wave web site:  Home Page  |  Main Documentation Page

3.4 Examining the Code

The code for this example is presented in three parts:

3.4.1 Declaring the Basic Classes

The code below shows the base class real_property and its two derived classes residential and rural_land.

Here is the declaration for the base class real_property:

//1This is the explicit default constructor required in serializable classes.

Here are the declarations for the two derived classes residential and rural_land:

//1The derived classes also require an explicit default constructor.
//2The derived classes must implement the pure virtual function printMe().

3.4.2 Making the Basic Classes Serializable

The real estate application references the residential and rural_land objects as pointers to the real_property base class. As stated in Section 3.3 above, to make these classes serializable as pointers you must:

3.4.2.1 Header File Changes

Starting with the header files, add the following macros:

Here is the revised code for the header files:

3.4.2.2 Source File Changes

Moving on to the source files, you must now:

Here is the source code for your three classes, modified as needed with the macros described above:

//1Begins the streamContents() function definition for the class real_property.
//2Generates the code for the streaming of the address_ variable. The first argument can be anything, but clearly something close to the variable name is preferable. The second argument must be the exact variable name.
//3Ends the streamContents() function definition.
//4Registers class factory
    // from residential.cpp
    
    RW_BEGIN_STREAM_CONTENTS(residential)
       RW_STREAM_PARENT(real_property)                             //1
       RW_STREAM_ATTR_MEMBER(footage,footage_)                     //2
    RW_END_STREAM_CONTENTS
    
    RW_DEFINE_STREAMABLE_AS_SELF(residential)                      //3
    RW_DEFINE_STREAMABLE_AS_BASE(residential, real_property)       //4
    RW_DEFINE_STREAMABLE_POINTER(residential)                      //5
    
//5Generates the code for the streaming of variables inherited from the parent class. This macro must appear immediately after the RW_BEGIN_STREAM_CONTENTS macro.
//6Generates the code for the streaming of the footage_ variable, which is an extension to the variables in the base class.
//7This macro call supports the streaming of pointers directly to the residential class.
//8This macro call supports the streaming of pointers to the base class that are forwarded to this derived class.
//9Required for all classes when streaming pointers to objects.
    // from rural_land.cpp
    

Note that the code below parallels the code for residential.

3.4.3 Persisting the Serializable Objects

Now that the classes have been prepared with serialization support, consider the code that could be used to persist the residential and rural_land objects -- as pointers to real_property -- at application shutdown, and restore these objects when the application is restarted. The main logic for the example application is in listing.cpp.

But before looking at the main logic, there is one more setup requirement. Recall from the use case description that at application shutdown the RWTPtrOrderedVector collection of real_property objects is written out to a file as a single operation. When the application is restarted, it reads in the file and restores the RWTPtrOrderedVector collection with all its real_property objects.

For this to work, you must make the RWTPtrOrderedVector collection serializable. To do this, apply the macro RW_DECLARE_STREAMABLE_PTR_SEQUENCE() on the collection. This macro declares the global input and output operators needed for streaming a collection of pointers. This macro can occur anywhere in the code so long as it is guaranteed to occur after the #include of the header file for RWTPtrOrderedVector, which is tpordvec.h. Since this include occurs in listing.cpp, this is the safest place to apply the macro.

Here is the code that includes the collection and prepares it for serialization:

Now you are ready to look at the main logic, beginning with the code for persisting the collection of real_property objects at application shutdown.

//1Create an output file stream to the file properties.xml.
//2Create an XML object output stream, passing the output file stream as a parameter.
//3As the second parameter, specify sequence, denoting that the object to be serialized out is a collection.
//4Stream out the RWTPtrOrderedVector collection properties.

Next, look at the code for restoring the collection at application startup.

//1Create an input file stream to read in the file properties.xml.
//2Create an XML object input stream, passing the input file stream as a parameter.
//3As the second parameter, specify sequence, denoting that the object to be serialized in is a collection.
//4Create a collection properties to hold the restored collection.
//5Stream the data into the properties collection.


Previous fileTop of DocumentContentsIndex pageNext file

© Copyright Rogue Wave Software, Inc. All Rights Reserved.
Rogue Wave and SourcePro are registered trademarks of Rogue Wave Software, Inc. in the United States and other countries. All other trademarks are the property of their respective owners.
Contact Rogue Wave about documentation or support issues.