Guidelines for Writing rwRestoreGuts
The global overloaded functions:
rwRestoreGuts(RWFile& f, YourClass& t)
rwRestoreGuts(RWvistream& 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 input stream (an
RWvistream).
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>>).
Since
size_t is normally a typedef for one of the basic types, it cannot be handled by
operator<< (it would cause an ambiguity error), so you will need to save it specifically by calling
getSizeT(aSizeTValue);.
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:
RWvistream& operator>>(RWvistream&, 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_
stream.getSizeT(gut.aSizetValue_);
stream
>> gut.aRogueWaveObject_
>> gut.anotherRogueWaveObject_
>> gut.pointerToAnRWCollectable_;
// The tricky restoring of a pointer
// to a non-RWCollectable object.
bool isValid;
stream >> isValid; // Is it a nil pointer?
if (isValid) // No,
stream >> gut.pointerToAnObject_; // restore the pointer.
else // Yes,
gut.pointerToAnObject_ = rwnil; // set pointer to nil.
}
void rwRestoreGuts(RWFile& stream, Gut& gut) {
// The body of this function is identical to
// rwRestoreGuts(RWvostream& stream, Gut& gut).
}
NOTE: The order of the data is not important as long as the rwSaveGuts and rwRestoreGuts are isomorphic (See
A Few Friendly Warnings for guidelines).