More on storing and retrieving rwcollectables – HP Integrity NonStop J-Series User Manual
Page 277

Click on the banner to return to the user guide home page.
©Copyright 1996 Rogue Wave Software
More on Storing and Retrieving RWCollectables
In
, we saw how to save and restore the morphology or pointer
relationships of a class using the following global functions:
Rwvostream& operator<<(RWvostream&, const RWCollectable&);
RWFile& operator<<(RWFile&, const RWCollectable&);
Rwvostream& operator<<(RWvostream&, const RWCollectable*);
RWFile& operator<<(RWFile&, const RWCollectable*);
Rwvistream& operator>>(RWvistream&, RWCollectable&);
RWFile& operator>>(RWFile&, RWCollectable&);
Rwvistream& operator>>(RWvistream&, RWCollectable*&);
RWFile& operator>>(RWFile&, RWCollectable*&);
When working with
RWCollectable
s, it is useful to understand how these functions work. Here is
a brief description.
When you call one of the left-shift << operators for any collectable object for the first time, an
identity dictionary is created internally. The object's address is put into the dictionary, along with
its ordinal position in the output file_for example, first, second, sixth, etc.
Once this is done, a call is made to the object's virtual function saveGuts(). Because this is a
virtual function, the call will go to the definition of saveGuts() used by the derived class. As we
have seen, the job of saveGuts() is to store the internal components of the object. If the object
contains other objects inheriting from
RWCollectable
, the object's saveGuts()calls operator<<()
recursively for each of these objects.
Subsequent invocations of operator<<() do not create a new identity dictionary, but store the
object's address in the already existing dictionary. If an address is encountered which is identical
to a previously written object's address, then saveGuts() is not called. Instead, a reference is
written that this object is identical to some previous object.
When the entire collection is traversed and the initial call to saveGuts() returns, the identity
dictionary is deleted and the initial call to operator<<() returns.
The function operator>>() essentially reverses this whole process by calling restoreGuts to
restore objects into memory from a stream or file. When encountering a reference to an object
that has already been created, it merely returns the address of the old object rather than asking
the
RWFactory
to create a new one.