Tools.h++ Manual

104011 Tandem Computers Incorporated 17-11
17
17.10 How to add persistence
To add persistence to your
RWCollectable
class, you must override the
saveGuts()
and
restoreGuts()
virtual member functions such that they
write out all of your object’s member data.
Virtual functions saveGuts(RWFile&) and saveGuts(RWvostream&)
These virtual functions are responsible for storing the internal state of an
RWCollectable
object on either a binary file (using class
RWFile
) or on a
virtual output stream (an
RWvostream
). This allows the object to be recovered
at some later time.
Start by saving the state of your base class by calling its version of
saveGuts()
.
Then, for each type of member data, save its state.
How to do this will depend on the type of the member data:
For primitives, save the data directly. This means when storing to
RWFiles
use
RWFile::Write()
; for virtual streams, use the l-shift operator
RWvostream::operator<<()
.
For Tools.h++ classes, most offer an overloaded version of the l-shift
operator. For example, for
RWCStrings
:
RWvostream& operator<<(RWvostream&, const
RWCString& str);
Hence, these can simply be shifted onto the stream.
For objects inheriting from
RWCollectable
this means using the global
function
RWvostream& operator<<(RWvostream&, const
RWCollectable& obj);
This function will call
saveGuts()
recursively for the object.