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

6.2 Getting Started

These are the basic steps required to add serialization support to your classes:

  1. Identify all classes that need to be serialized. Include all abstract base and concrete classes. (See Section 6.2.1, "Serialization Requirements.")

  2. Declare the streaming operators by putting macros in the header file for each class. (See Section 6.2.2.1, "Declaring the Streaming Operators.")

  3. Define the streamContents() function by putting macros in the source file for each class. (See Section 6.2.2.2, "Defining the streamContents() Function.")

  4. Write your object to an object stream and read it back. (See Section 6.2.2.3, "Streaming the Serialized Object.")

6.2.1 Serialization Requirements

Before you begin, look at your classes in terms of the requirements for serialization support:

6.2.2 Simple Examples

This section includes a few simple examples on how to use the Serialization package. The code in Section 6.2.2.1 through Section 6.2.2.3 shows how to add basic serialization support to the class real_property. The code is taken from real_property.h, real_property.cpp, and real_estate.cpp in the examples\serial\simple directory. The examples in Section 6.2.2.4 and Section 6.2.2.5 show two ways to stream a serialized object: compact object streams working with data streams from the Streams package, and compact object streams working with virtual streams from the Essential Tools Module.

6.2.2.1 Declaring the Streaming Operators

First, the streaming operators are declared by putting the RW_DECLARE_VIRTUAL_STREAM_FNS macro at the beginning of the class declaration.

//1

This macro declares streaming operators for the real_property class.

6.2.2.2 Defining the streamContents() Function

Next, the streamContents() function is defined for this class by putting macros in the source file for the class.

//1

This macro begins the definition of the streamContents() function.

//2

This macro defines the code for inserting and extracting data to and from the address_ and size_ data members of the real_property class.

//3

The streamContents() function is closed out with this macro.

6.2.2.3 Streaming the Serialized Object

Now an instance of class real_property can be written to a file and read back.

//1

Create a real_property object.

//2

Create a local scope so the output file closes automatically when it's no longer needed.

//3

Open a file for output.

//4

Create a data stream using the standard file stream just opened and then create a compact object output stream from the data stream.

//5

Stream out the contents of real1 to the file in compact format.

//6

Now open the file for input.

//7

Create a data stream and then create a compact object input stream from the data stream.

//8

Read the data from the first variable (real1) into the second (real2).

A more complete version of this example with more explanation is presented in Section 6.4.1, "Saving and Restoring an Object by Value." If you wish to stream objects as pointers, see Section 6.4.2, "Saving and Restoring an Object by Pointer."

6.2.2.4 Writing to and Reading from Compact Object Streams

This example outputs an object in compact format, using streams from the Streams package.

This example reads an object in that was output in compact form.

6.2.2.5 Writing to and Reading from Compact Object Streams With Virtual Streams

This example outputs an object in compact form using virtual streams.

This example reads in an object, using virtual streams, that was output in compact form.



Previous fileTop of DocumentContentsIndex pageNext 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.
Contact Rogue Wave about documentation or support issues.