beautypg.com

HP Integrity NonStop J-Series User Manual

Page 201

background image

Note that there are three types of objects stored in collection, an RWCollectableDate, and
RWCollectableInt, and two RWCollectableStrings. The same RWCollectableString, george, is
inserted into collection twice.

Example Two: Restoring Polymorphically

The second example shows how the polymorphically saved collection of the first example can be
read back in and faithfully restored using the overloaded extraction operator:

Rwvistream& operator>>(RWvistream&, RWCollectable&);

In this example, persistence happens when the program executes the statement:

istr >> collection2;

This statement uses the overloaded extraction operator to isomorphically restore the collection
saved by the first example into collection2.

How does persistence happen? For each pointer to an

RWCollectable

-derived object restored into

collection2 from the input stream istr, the extraction operator operator>> calls a variety of
overloaded extraction operators and persistence functions. For each RWCollectable-derived
object pointer, collection2's extraction operators:

Read the stream istr to discover the type of the

RWCollectable

-derived object.

Read the stream istr to see if the

RWCollectable

-derived object that is pointed to has

already been restored and referenced in the restore table.

If the

RWCollectable

-derived object has not yet been restored, the extraction

operators create a pointer, create an object of the correct type from the heap, and
initialize the created object with data read from the stream. Then the operators
update the pointer with the address of the new object, and finally save a reference to
the object in the restore table.

If the

RWCollectable

-derived object has already been restored, the extraction

operators create a pointer and read the reference to the object from the stream. Then
the operators use the reference to get the object's address from the restore table, and
update the pointer with this address.

Finally, the restored pointer is inserted into the collection.

We'll look at the implementation details for the persistence mechanism again in the next section.
You should note, however, that when a heterogeneous collection (which must be based on

RWCollection

) is restored, the restoring process does not know the types of objects it will be

restoring. Hence, it must always allocate the objects off the heap. This means that you are
responsible for deleting the restored contents
. This happens at the end of the example, in the
expression collection2.clearAndDestroy.

Here is the listing of the example:

This manual is related to the following products: