Advanced Tools Module User’s Guide : PART II Advanced Tools Module Packages : Chapter 5 The Serialization Package
Chapter 5 The Serialization Package
Introducing the Serialization Package
The Serialization package provides a way to flatten objects, so they can be easily transported across communication boundaries or stored to disk. This object serialization mechanism includes two processes:
Translation of an object or set of related objects in memory into a linear series of atomic events or tokens
Translation of such a series back into an equivalent object or set of related objects
The classes, macros, and functions in the Serialization package help you to define serializable objects with a minimum of custom code, while retaining control over the formats and policies used. To make your C++ classes serializable, you prepare them with a small set of streaming macros. These macros encapsulate the details associated with streaming protocols, shielding your application from the implementation details but otherwise not affecting your application code. You have a choice of intrusive serialization for your own classes or external serialization for classes that you can’t modify, such as classes from a third-party library. The package also includes a set of utility functions for streaming objects to and from various formats.
Classes Supported by the Serialization Package
The Serialization package has built-in support for the following classes:
The C++ standard collection classes (vector, list, map, set, deque)
The Rogue Wave RWCollectable-based collections classes (RWBag, RWHashDictionary, and so on)
The Rogue Wave template collection classes, both pointer-based and value-based
RWBasicUString
RWCString
RWDate
RWDateTime
RWDecimalPortable
RWInteger
RWCollectableString
RWCollectableWString
RWCollectableInt
RWCollectableDate
RWWString
RWSymbol
All other Rogue Wave collectable classes with Rogue Wave persistence format
Collectable classes can be streamed out in the Rogue Wave persistence format, or in the format of a particular object stream. To stream a collectable class in Rogue Wave persistence format you only need to include the RWCollectableIO.h header file and define the macro RW_DEFINE_COLLECTABLE_AS_STREAMABLE(className) for the class (see “Using Object Streams with RWCollectable-Derived Objects.”). Now an object of this class will be embedded within the stream using the Rogue Wave persistence format (regardless of the format of the object stream itself). To stream out a collectable in the format for the object stream you are using you must declare RW_DECLARE_STREAMABLE_COLLECTABLE and then use the external serialization macros just as you would for a class from any other library (see “Making RWCollectable Objects That Are Also Serializable.”).
Object Serialization for Saving Program State
The most common use for object serialization is to save the state of a program to disk, so that the state can be restored when the program starts again. Rather than developing custom code and custom file formats to add this function to your application, you can use the Serialization package’s tools and industry-standard formats.
Object Serialization and Distributed Applications
Object serialization is also useful for distributed applications. Often you run into the trade-off between passing a reference to a remote object from one process to another or passing the object itself between processes. If the objects are not large, have static state, and will be accessed often, you may decide to send them by value between processes. Rather than writing custom network protocols to transmit application objects and writing code to read and write these protocols, you can use the Serialization package, which transmits serialized objects as blocks of raw data using generic protocols.
Interpackage Dependencies
The Serialization package depends on the Streams package, as well as the Smart Pointer, Synchronization, Execution Tracing and Thread-compatible Exception packages of the Threads Module.