Advanced Tools Module User’s Guide : PART II Advanced Tools Module Packages : Chapter 6 Using Object Serialization : Getting Started
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 “Serialization Requirements.”)
2. Declare the streaming operators by putting macros in the header file for each class. (See “Declaring the Streaming Operators.”)
3. Define the streamContents() function by putting macros in the source file for each class. (See “Defining the streamContents() Function.”)
4. Write your object to an object stream and read it back. (See “Streaming the Serialized Object.”)
Serialization Requirements
Before you begin, look at your classes in terms of the requirements for serialization support:
Identify all classes that need to be serialized.
For a polymorphic class hierarchy to be serializable, it must derive from a class that supports serialization, so include both concrete classes and abstract base classes.
In order to support serialization, classes must be compiled with run-time type identification (RTTI) enabled. If your compiler/system requires a flag or setting in order to support RTTI, make sure this flag is on. If your class is compiled into a separate library, and you want to provide it with serialization support, make sure RTTI is enabled during compilation of that library as well.
NOTE >> Derived classes must have a base class that supports serialization if any inaccessible members of the base class need to be serialized, or if you wish to serialize a derived object as a pointer to base.
Some of these base classes may already support serialization. For a list of such classes in the Essential Tools Module, including most Rogue Wave containers, see “Polymorphic Persistence” in the Essential Tools Module User’s Guide. If you are using these classes, see “Making RWCollectable Objects That Are Also Serializable.”
Make sure the classes have the structure to support serialization.
Object streaming normally requires a public default constructor. If your class does not have one, see “Default Constructors.”
Consider whether some classes will require external serialization.
Normally, you should use intrusive serialization for any class that you can change. If its base class requires external serialization, however, you must use external serialization for the derived class as well. For examples, see “External Serialization.”
NOTE >> Classes within a hierarchy must either use external serialization or use the intrusive style. Mixed hierarchies are not supported.