HP Integrity NonStop J-Series User Manual
Page 224

The saveGuts(RWFile&) and saveGuts(RWvostream&) virtual functions are responsible for
polymorphically saving the internal state of an
RWCollectable
object on either a binary file, using
class
RWFile
, or on a virtual output stream, using class
RWvostream
.
[26]
This allows the object to
be restored at some later time, or in a different location. Here are some rules for defining a
saveGuts() function:
Save the state of your base class by calling its version of saveGuts().
1.
For each type of member data, save its state. How to do this depends upon the type of the
member data:
Primitives. For primitives, save the data directly. When saving to
RWFile
s, use
RWFile::Write(); when saving to virtual streams, use the insertion operator
RWvostream::operator<<().
❍
Rogue Wave classes. Most Rogue Wave classes offer an overloaded version of the
insertion operator. For example,
RWCString
offers:
RWvostream& operator<<(RWvostream&, const RWCString& str);
Hence, many Rogue Wave classes can simply be shifted onto the stream.
❍
Objects inheriting from
RWCollectable
. For most of these objects, use the global
function:
RWvostream& operator<<(RWvostream&,
const RWCollectable& obj);
This function will call saveGuts() recursively for the object.
❍
2.
With these rules in mind, let's look at a possible definition of the saveGuts() functions for the Bus
example:
void Bus::saveGuts(RWFile& f) const
{ RWCollectable::saveGuts(f); // Save base class
f.Write(busNumber_); // Write primitive directly
f << driver_ << customers_; // Use Rogue Wave
//provided versions
f << passengers_; // Will detect nil pointer
// automatically
}
void Bus::saveGuts(RWvostream& strm) const
{ RWCollectable::saveGuts(strm); // Save base class
strm << busNumber_; // Write primitives directly
strm << driver_ << customers_; // Use Rogue Wave
// provided versions
strm << passengers_; // Will detect nil pointer
// automatically
}