beautypg.com

HP Integrity NonStop J-Series User Manual

Page 226

background image

If the original passengers_ is non-nil, then this function restores a new

RWSet

off the heap and

returns a pointer to it. Otherwise, it returns a nil pointer. Either way, the old contents of
passengers_ are replaced. Hence, we must call delete passengers_ first.

Multiply-referenced Objects

A passenger name can exist in the set pointed to by customers_ and in the set pointed to by
passengers_; that is, both collections contain the same string. When the Bus is restored, we want to
make sure that the pointer relationship is maintained, and that our restoration does not create
another copy of the string.

Fortunately, we don't have to do anything special to insure that the pointer relationship stays as it
should be. Consider the call:

Bus aBus;
RWFile aFile("busdata.dat");

aBus.addPassenger("John");
aFile << aBus;

Because passenger_is a subset of customer_, the function addPassenger puts the name on both the
customer list and the passenger list. When we save aBus to aFile, both lists are saved in a single
call: first the customer list, then the passenger list. The polymorphic persistence machinery saves
the first reference to John, but for the second reference it merely stores a reference to the first copy.
During the restore, both references will resolve to the same object, replicating the original
morphology of the collection.

Virtual Function binaryStoreSize()

The binaryStoreSize() virtual function calculates the number of bytes necessary to store an object
using

RWFile

. The function is:

virtual Rwspace binaryStoreSize() const;

This function is useful for classes

RWFileManager

and

RWBTreeOnDisk

, which require allocation

of space for an object before it can be stored. The non-virtual function recursiveStoreSize() returns
the number of bytes that is actually stored. Recursive store size uses binaryStoreSize() to do its
work.

Writing a version of binaryStoreSize() is usually straightforward. You just follow the pattern set by
saveGuts(RWFile&), except that instead of saving member data, you add up their sizes. The only
real difference is a syntactic one: instead of insertion operators, you use sizeof() and the member
functions indicated below:

For primitives, use sizeof();

For objects that inherit from

RWCollectable

, if the pointer is non-nil, use member function:

RWspace RWCollectable::recursiveStoreSize();

For objects that inherit from

RWCollectable

, if the pointer is nil, use the static member

This manual is related to the following products: