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

5.2 Object Serialization Concepts

Object serialization enables user-defined C++ objects to be written to a stream and then read back while preserving dynamic type information (polymorphism) and the topology of pointers between objects (isomorphism). The object serialization mechanism is independent of:

For instance, an object could be transmitted over a TCP socket using a binary encoding or stored in a disk file with an ASCII format.

5.2.1 Writing and Reading the Object State

The Serialization package reads and writes the object state, including associations with other objects.

The object state is a logical representation of primitive data elements, not the bits in memory that represent the object. For example, the state of a linked list can be expressed as the length of the list and the elements in the list. The state of an ordered vector is expressed in the same way, though the internal representation is different.

5.2.2 Type Information (Polymorphism)

Type information (polymorphism) is required so that objects of the proper type are created when the object stream is read back.

The Serialization package uses the standard C++ runtime type identification (RTTI) mechanism to provide type information. Because the names given to the various types differ depending on the compiler, the type name is captured as it appears in the source and then transformed into a canonical type name common to all compilers. This canonical name is used in the object stream interfaces.

5.2.3 Object Identity (Isomorphism)

The Serialization package uses isomorphic persistence, so that the pointer relationships between objects are preserved when you store and retrieve objects to and from a stream. See the Essential Tools Module User's Guide for a comparison of isomorphic versus simple persistence.

5.2.4 Intrusive and External Serialization

The simplest and most efficient way to serialize your classes is intrusive serialization, where you build serialization directly into your objects. Deriving from a single root class is not required.

For existing classes that you cannot modify, you can implement external serialization. This kind of serialization uses pseudo-polymorphism to add polymorphic streaming operators to a preexisting class hierarchy.

The Serialization package includes macros for both styles of serialization. See Section 6.3, "Using the Stream Contents Macros."

5.2.5 Symmetry of Object Input and Output

The essence of object serialization is symmetry between output and input operations. This symmetry is provided by the various data stream classes for sequences of primitive types (int, float, string); but for a class, the symmetry must be provided by the input and output operations for that class.

For example, if the output operation for a class writes two int types followed by a float, and the input operation expects to read three int types, then incorrect data is read. In most cases, this kind of error halts processing and invalidates the stream.

The Serialization package solves this problem by using the same function for both object input and output. Each serializable class has a streamContents() member function (or global function for externally serializable classes) that is responsible for output and input of that class's members.

5.2.6 Annotated Streams and Embedded Schemas

With the Serialization package, you can build self-describing serialized data known as embedded schemas. The package supports annotated streams, where the name of each object precedes the value of that object in the stream. Annotated streams include enough meta-information (schema) to process objects of unknown types using generic code, while preserving the original object's structure.

When an object is output to an object stream, the streamContents() function for that object makes a series of calls on the object stream to save the object's state. Besides state information, these calls include information about the object, such as the names of the fields. Specific object streams may either ignore this information or include it in their output. A stream that includes this information is known as an annotated stream.



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.