Rogue Wave banner
Previous fileTop of DocumentContentsIndex pageNext file
Essential Tools Module User's Guide

9.4 Simple Persistence

Simple persistence is the storage and retrieval of an object to and from a stream. Table 18 lists the classes in the Essential Tools Module that use simple persistence.

Table 18: Classes with simple persistence 

Category Description

C++ fundamental types

int , char , float , ...

Rogue Wave date and time classes

RWDateTime, RWDate, RWTime

Rogue Wave string classes

RWCString, RWWString

Miscellaneous Rogue Wave classes

RWBitVec

Because it is straightforward, simple persistence is a quick and easy way to save and restore objects that have neither pointers to other objects nor virtual member functions.

However, when objects that refer to each other are saved and then restored with simple persistence, the pointer relationships, or morphology, among the objects can change. This is because simple persistence assumes that every pointer reference to an object in memory refers to a unique object. Thus, when an object is saved with simple persistence, two references to the same memory location will cause two copies of the contents of that memory location to be saved. Not only does this use extra space in the stream, but it also causes the restored object to point to two distinct copies of the referenced object.

9.4.1 Two Examples of Simple Persistence

Here are two examples of simple persistence. The first example illustrates successful persistence of fundamental data types, and demonstrates the Essential Tools Module overloaded operators operator<< and operator>>, which save and restore persistent objects. The second example illustrates one of the problems with simple persistence -- its inability to maintain pointer relationships among objects.

9.4.1.1 Example One: Simple Persisting Objects of Fundamental Type

This example uses simple persistence to save two integers to an output stream po, which saves the integers to the file int.dat. Then the example restores the two integers from the stream pi, which reads the integers from the file int.dat.

The example uses the overloaded insertion operator operator<< to save the objects, and the overloaded extraction operator operator>> to restore the objects, much the same way as you use these operators to output and input objects in C++ streams.

Note that the saving stream and the restoring stream are put into separate blocks. This is so that opening pi will cause it to be positioned at the beginning of the file.

Here is the code:

The preceding example shows how easy it is to use overloaded operators to implement this level of persistence. So, what are some of the problems with using simple persistence? As mentioned above, one problem is that simple persistence will not maintain the pointer relationships among objects. We'll take a look at this problem in the next example.

9.4.1.2 Example Two: Simple Persistence and Pointers

This example shows one of the shortcomings of simple persistence: its inability to maintain the pointer relationships among persisted objects. First, you have a class Developer that contains a pointer to other Developer objects:

Now, you have another class, Team, that is an array of pointers to Developer s:

Note that Team::member_ does not actually contain Developers, but only pointers to Developers.

Assume that you have written overloaded extraction and insertion operators that use simple persistence to save and restore Developers and Teams. The example code for this is omitted to keep the explanation clear.

When you save and restore a Team with simple persistence, what you restore may be different from what you saved. Look at the following code, which creates a team, then saves and restores it with simple persistence.

Because this example uses simple persistence, which does not maintain pointer relationships, the restored team has different pointer relationships than the original team. Figure 3 shows what the created and restored teams look like in memory if you run the program.

Figure 3: Simple Persistence

Figure 3 shows that when objects that refer to each other are saved and then are restored with simple persistence, the morphology among the objects can change. This is because simple persistence assumes that every pointer reference to an object in memory refers to a unique object. Thus, when such objects are saved, two references to the same memory location will cause two copies of the contents of that memory location to be saved, and later restored.



Previous fileTop of DocumentContentsIndex pageNext file

©2004 Copyright Quovadx, Inc. All Rights Reserved.
Rogue Wave and SourcePro are registered trademarks of Quovadx, Inc. in the United States and other countries. All other trademarks are the property of their respective owners.
Contact Rogue Wave about documentation or support issues.