Polymorphic persistence example – HP Integrity NonStop J-Series User Manual
Page 199

Rwvostream& operator<<(RWvostream&, const RWCollectable*);
RWFile& operator<<(RWFile&, const RWCollectable*);
Each pointer to an object is saved isomorphically with a class ID that uniquely identifies the
object's class. Even nil pointers can be saved.
●
Operators that restore already-existing
RWCollectable
objects:
Rwvistream& operator>>(RWvistream&, RWCollectable&);
RWFile& operator>>(RWFile&, RWCollectable&);
Each
RWCollectable
-derived object is restored isomorphically. The persistence mechanism
determines the object type at run time by examining the class ID that was stored with the object.
●
Operators that restore pointers to
RWCollectable
objects:
Rwvistream& operator>>(RWvistream&, RWCollectable*&);
RWFile& operator>>(RWFile&, RWCollectable*&);
Each object derived from
RWCollectable
is restored isomorphically and the pointer reference is
updated to point to the restored object. The persistence mechanism determines the object type at
run time by examining the class ID that was stored with the object. Since the restored objects are
allocated from the heap, you are responsible for deleting them when you are done with them.
Designing your Class to Use Polymorphic Persistence
Note that the ability to restore the pointer relationships of a polymorphic object is a property of
the base class,
RWCollectable
. Polymorphic persistence can be used by any object that inherits
from
RWCollectable
polymorphic persistence in the classes that you create by inheriting from RWCollectable.
Polymorphic Persistence Example
This example of polymorphic persistence contains two distinct programs. The first example
polymorphically saves the contents of a collection to standard output (stdout). The second
example polymorphically restores the contents of the saved collection from standard input
(stdin). We divided the example to demonstrate that you can use persistence to share objects
between two different processes.
If you compile and run the first example, the output is an object as it would be stored to a file.
However, you can pipe the output of the first example into the second example:
firstExample | secondExample
Example One: Saving Polymorphically
This example constructs an empty collection, inserts objects into that collection, then saves the
collection polymorphically to standard output.