SourcePro : Essential Tools Module User's Guide : Persistence : Isomorphic Persistence : Isomorphic Persistence of an Essential Tools Module Class
Isomorphic Persistence of an Essential Tools Module Class
The following example shows the isomorphic persistence of a templatized collection of RWCollectable integers, RWTPtrDlist<RWCollectableInt>. RWTPtrDlist is a templatized, reference-based, doubly-linked list that uses isomorphic persistence to store pointer references to values in memory.
This example uses RWCollectableInt instead of int because int uses simple persistence. By using RWCollectableInts, we can implement isomorphic persistence.
When RWTPtrDlist is saved and then restored, the pointer relationships of the restored list has the same morphology as the original list.
 
#include <assert.h>
#include <rw/tpdlist.h> // RWTPtrDlist
#include <rw/collint.h> // RWCollectableInt
#include <rw/rwfile.h> // RWFile
int main (){
RWTPtrDlist<RWCollectableInt> dlist1;
RWCollectableInt *one = new RWCollectableInt(1);
dlist1.insert(one);
dlist1.insert(one);
{
RWFile f("dlist.dat");
f << dlist1; // Isomorphic persistence of dlist1.
}
assert(dlist1[0] == one && dlist1[0] == dlist1[1]);
// dlist1[0], dlist[1] and "one" all point to the
// same place.
RWTPtrDlist<RWCollectableInt> dlist2;
{
RWFile f("dlist.dat");
f >> dlist2;
// restore dlist2 from f
// dlist2 now contains 2 pointers
// to the same RWCollectableInt of value 1.
// However, this RWCollectableInt isn't at
// the same address as the value
// that "one" points to.
}
// See the figure following this example to see what dlist1 and
// dlist2 now look like in memory.
assert(dlist2[0] == dlist2[1] && (*dlist2[0]) == *one);
// dlist2[0] and dlist2[1] point to the same place
// and that place has the same value as "one".
delete dlist2[0];
delete one;
// The developer must allocate and delete objects.
// The templatized collection member function
// clearAndDestroy() doesn't check that a given
// pointer is deleted only once.
// So in this case, delete the shared
// pointer manually.
return 0;
}
Figure 6 – After isomorphic save and restore of RWTPtrDlist<RWCollectableInt>