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:

//1

This is the explicit default constructor required in serializable classes.

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

//1

The derived classes also require an explicit default constructor.

//2

The 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:

The definitions generated through these macros guarantee symmetry between the serialization output and input operations.

If you want to allow for the possibility that these objects might be serialized as pointers directly to themselves, you need to apply RW_DEFINE_STREAMABLE_AS_SELF as well. In the code that follows, both RW_DEFINE_STREAMABLE_AS_BASE and RW_DEFINE_STREAMABLE_AS_SELF are applied.

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

//1

Begins the streamContents() function definition for the class real_property.

//2

Generates 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.

//3

Ends the streamContents() function definition.

//4

Registers class factory

//1

Generates the code for the streaming of variables inherited from the parent class. This macro must appear immediately after the RW_BEGIN_STREAM_CONTENTS macro.

//2

Generates the code for the streaming of the footage_ variable, which is an extension to the variables in the base class.

//3

This macro call supports the streaming of pointers directly to the residential class.

//4

This macro call supports the streaming of pointers to the base class that are forwarded to this derived class.

//5

Required for all classes when streaming pointers to objects.

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.

//1

Create an output file stream to the file properties.xml.

//2

Create an XML object output stream, passing the output file stream as a parameter.

//3

As the second parameter, specify sequence, denoting that the object to be serialized out is a collection.

//4

Stream out the RWTPtrOrderedVector collection properties.

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

//1

Create an input file stream to read in the file properties.xml.

//2

Create an XML object input stream, passing the input file stream as a parameter.

//3

As the second parameter, specify sequence, denoting that the object to be serialized in is a collection.

//4

Create a collection properties to hold the restored collection.

//5

Stream the data into the properties collection.



Previous fileTop of DocumentContentsNo linkNext file

Copyright © Rogue Wave Software, Inc. All Rights Reserved.

The Rogue Wave name and logo, and SourcePro, are registered trademarks of Rogue Wave Software. All other trademarks are the property of their respective owners.
Provide feedback to Rogue Wave about its documentation.