beautypg.com

HP Integrity NonStop J-Series User Manual

Page 190

background image

Wave classes, including

RWCollectable

, use the overloaded insertion operator operator<<.

Saving members that are pointers to non-

RWCollectable

objects can be a bit tricky. This is

because it is possible that a pointer does not point to any object at all. One way of dealing with
the possibility of nil pointers is to check whether a pointer points to a valid object. If the pointer
is valid, save a Boolean true, then save the dereferenced pointer. If the pointer is invalid, save a
Boolean false but don't save the pointer.

When you restore the pointer, rwRestoreGuts first restores the Boolean. If the Boolean is true,
then rwRestoreGuts restores the valid pointer. If the Boolean is false, then rwRestoreGuts sets
the pointer to nil.

Saving pointers to objects derived from

RWCollectable

is easier. It is still possible that a

pointer is nil. But if you use:

RWvostream& operator<<(RWvostream&, const RWCollectable*);

to save the pointer, the nil pointer will be detected automatically.

Using these guidelines, you can write rwSaveGuts functions for the example class Gut as follows:

void rwSaveGuts(RWvostream& stream, const Gut& gut) {

// Use insertion operators to save fundamental objects,
// Rogue Wave objects and pointers to
// RWCollectable-derived objects.

stream
<< gut.fundamentalType_
<< gut.aRogueWaveObject_
<< gut.anotherRogueWaveObject_
<< gut.pointerToAnRWCollectable_;

// The tricky saving of a pointer
// to a non-RWCollectable object.

if (gut.pointerToAnObject_ == 0) // Is it a nil pointer?
stream << false; // Yes, don't save.
else {
stream << true; // No, it's valid
stream << *(gut.pointerToAnObject_); // so save it.
}
}

void rwSaveGuts(RWFile& stream, const Gut& gut) {
// The body of this function is identical to
// rwSaveGuts(RWvostream& stream, const Gut& gut).
}

Guidelines for Writing rwRestoreGuts

This manual is related to the following products: