Making Unusable Objects Usable
Sometimes an unusable object can be made usable. For example, objects produced under the Producer/Product paradigm of the DB Interface Module, called
product objects, are unusable if they are produced by an invalid object. (See
Producers and Products.) However, producing a product object again, this time by a valid object, can yield a usable and valid object. This technique is shown in the following example:
RWDBConnection aConnection = badDB.connection(); // 1
if(!aConnection.isValid()) {
cout << “aConnection isn’t usable right now.” << endl;
}
aConnection = goodDB.connection(); // 2
if(aConnection.isValid()) {
cout << “aConnection is valid and usable now.” << endl;
}
In
//1, an
aConnection object is produced from an
RWDBDatabase instance that is unusable. The method
isValid() on
aConnection correctly reports that the connection is unusable. In
//2, an
aConnection object is produced again, this time from a good
RWDBDatabase instance. The invalid
aConnection object is discarded, and the new object takes its place. Because the connection is successfully produced, the method
isValid() on
aConnection reports that the connection is in a usable, valid state.
This example is intended to show how reproducing an unusable object can make it usable, but there is a corollary as well: it is always a good idea to understand the validity requirements for the particular class you are using. In this case, it is important to know that usable product objects must be produced by valid objects. Some qualifications apply in other classes as well.
NOTE: Always check the
SourcePro API Reference Guide for the object validity requirements and the description of
isValid() for the class you are using.