How to store and restore a non-compound object

Storing

The basic protocol for storing is the StoreL() function and should be prototyped as:

       
        
       
       TStreamId StoreL(CStreamStore& aStore) const;
      

The function takes a reference to the store in which the stream is to be stored and returns the stream ID through which the object can be restored.

In some cases, the design of a class may be such that it maintains a pointer or reference to the store. In this event, the CStreamStore argument would be redundant.

To store an object that does not contain any other objects:

  1. construct a write stream object, i.e. an instance of RStoreWriteStream

  2. externalise the object

  3. return the ID of the stream

The following code fragment shows the canonical form for some general class CClassA :

       
        
       
       TStreamId CClassA::StoreL(CStreamStore& aStore) const
    {
    RStoreWriteStream stream;
    TStreamId id=stream.CreateLC(aStore);
    ExternalizeL(stream);
    stream.CommitL();
    CleanupStack::PopAndDestroy();
    return id;
    }
      

Restoring

The basic protocol for restoring is the <code>RestoreL()</code> function and should be prototyped as:

       void RestoreL(CStreamStore& aStore,TStreamId anId);
      

The function takes a reference to the store in which the stream is stored and the ID that identifies that stream is stored.

To restore an object that does not contain any other objects:

  • construct a read stream object, i.e. an instance of

    RStoreReadStream
  • internalise the object.

The following code fragment shows an example for some general class

CClassA
       void CClassA::RestoreL(CStreamStore& aStore, TStreamId anId)
     {
          RStoreReadStream stream;
          stream.OpenLC(aStore,anId);
          InternalizeL(stream);
          CleanupStack::PopAndDestroy();
     }