beautypg.com

HP Integrity NonStop J-Series User Manual

Page 191

background image

The global overloaded functions:

rwRestoreGuts(RWFile& f, YourClass& t)
rwRestoreGuts(RWvostream& s, YourClass& t)

are responsible for restoring the internal state of a YourClass object from either a binary file (using
class

RWFile

) or from a virtual output stream (an

RWvostream

).

The rwRestoreGuts functions that you write must restore the state of each member in YourClass,
including the members of the class that you inherited from. The functions must restore member data
in the order that it was saved.

How you write the functions depends upon the type of the member data:

To restore member data that are either C++ fundamental types (int, char, float,_) or most Rogue
Wave classes, including

RWCollectable

, use the overloaded extraction operators (operator>>).

Restoring members that are pointers to non-

RWCollectable

objects can be a bit tricky. This is

because it is possible that a saved pointer did not point to any object at all. But if rwSaveGuts
saved a Boolean flag before saving the pointer, as we described in the previous section, then it
is a relatively simple matter for the rwRestoreGuts to restore valid and nil pointers.

Assuming that the members were saved with a compatible rwSaveGuts, 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.

Restoring pointers to objects derived from

RWCollectable

is easier. It is still possible that the

pointer is nil. But if you use:

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

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

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

void rwRestoreGuts(RWvistream& stream, const Gut& gut) {

// Use extraction operators to restore fundamental objects,
// Rogue Wave objects and pointers to
// RWCollectable-derived objects.

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

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

bool isValid;

This manual is related to the following products: