How to externalise and internalise descriptors

Use the templated stream operators to externalise and internalise a descriptor. Taking a class, TSimple, as an example:

class TSimple
      {
public :
      void ExternalizeL(RWriteStream& aStream) const;
      ...
      HBufC*        iBufPtr;
      TBuf<32>      iBuffer;
      };

Externalising Descriptors

The ExternalizeL() member function of TSimple can be implemented as:

void TSimple::ExternalizeL(RWriteStream& aStream) const
      {
      aStream << *iBufPtr;
      aStream << iBuffer;
      }

In some circumstances, it may be desirable to externalise the maximum length value of the heap descriptor to the stream and to use this value when allocating and internalising that descriptor. To do this, change the ExternalizeL() member function to:

void TSimple::ExternalizeL(RWriteStream& aStream) const
    {
    aStream.WriteInt32L(iBufPtr->Des().MaxLength());
    aStream << *iBufPtr;
    aStream << iBuffer;
    }

Internalising descriptors

The InternalizeL() member function of TSimple can be implemented as:

void TSimple::InternalizeL(RReadStream& aStream)
    {
    iBufPtr =  HBufC::NewL(aStream,KMaxlen);
    aStream >> iBuffer;
    }

where KMaxlen is some constant defining the maximum length for the heap descriptor.

If the maximum length value of the heap descriptor had been externalised, then the InternalizeL() member function could be changed to:

void TSimple::InternalizeL(RReadStream& aStream)
    {
    TInt maxlen;
    maxlen  =  aStream.ReadInt32L();
    iBufPtr =  HBufC::NewL(aStream,maxlen);
    aStream >> iBuffer;
    }