HP Integrity NonStop J-Series User Manual

Page 279

Advertising
background image

p = p->nextTangle;
i++;
}
}

RWDEFINE_COLLECTABLE(Tangle, 100)

main(){
Tangle *head = 0, *head2 = 0;

for (int i=9; i >= 0; i--)
head = new Tangle(head,i);

checkList(head); // Check the original list

{
RWFile file("junk.dat");
file << head;
}

RWFile file2("junk.dat");
file2 >> head2;

checkList(head2); // Check the restored list
return 0;
}

In the above example, the class Tangle implements a circularly linked list. What happens? When
function operator<<() is called for the first time for an instance of Tangle, it sets up the identity
dictionary as described above, then calls Tangle's saveGuts(), whose definition is shown above.
This definition stores any member data of Tangle, then calls operator<<() for the next link. This
recursion continues on around the chain.

If the chain ends with a nil object (that is, if nextTangle is zero), then operator<<() notes this
internally and stops the recursion.

On the other hand, if the list is circular, then a call to operator<<() is eventually made again for
the first instance of Tangle, the one that started this whole chain. When this happens,
operator<<() will recognize that it has already seen this instance before and, rather than call
saveGuts() again, will just make a reference to the previously written link. This stops the series
of recursive calls and the stack unwinds.

Restoration of the chain is done in a similar manner. A call to:

RWFile& operator>>(RWFile&, RWCollectable*&);

can create a new object off the heap and return a pointer to it, return the address of a previously

Advertising
This manual is related to the following products: