examples/SysLibs/Streaming/CompoundClass/CompoundClass.cpp

00001 // Copyright (c) 2000-2009 Nokia Corporation and/or its subsidiary(-ies).
00002 // All rights reserved.
00003 // This component and the accompanying materials are made available
00004 // under the terms of "Eclipse Public License v1.0"
00005 // which accompanies this distribution, and is available
00006 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
00007 //
00008 // Initial Contributors:
00009 // Nokia Corporation - initial contribution.
00010 //
00011 // Contributors:
00012 //
00013 // Description:
00014 // Example to demonstrate the streaming of a compound object to
00015 // a single stream.
00016 //
00017 
00018 
00019 #include "CommonStreamStore.h"
00020 #include <s32file.h>
00021 
00022                                 // Constructs a CCompound object and externalizes
00023                                 // it to a single stream.
00024 LOCAL_C void doMakeAndExternalizeL(const TDesC& aName);
00025 
00026                                 // Internalizes a CCompound object from
00027                                 // the stream
00028 LOCAL_C void doInternalizeL(const TDesC& aName);
00029 
00030                                 // Displays content of a CCompound object
00031 class CCompound;
00032 LOCAL_C void doShow(const TDesC& aHeading,const CCompound& theSimple);
00033 
00034                                 // Defintion of classes used by the example
00035 class CClassA
00036         {
00037 public :
00038         void ExternalizeL(RWriteStream& aStream) const;
00039         void InternalizeL(RReadStream& aStream);
00040 public :
00041         TBuf<32> iBufferA;
00042         TInt     iXA;
00043         TUint    iYA;
00044         };
00045 
00046 
00047 class CClassB
00048         {
00049 public :
00050         void ExternalizeL(RWriteStream& aStream) const;
00051         void InternalizeL(RReadStream& aStream);
00052 public :
00053         TBuf<32> iBufferB;
00054         };
00055 
00056 
00057 class TClassC
00058         {
00059 public :
00060         void ExternalizeL(RWriteStream& aStream) const;
00061         void InternalizeL(RReadStream& aStream);
00062 public :
00063         TReal iZC;
00064         };
00065 
00066         
00067 class CCompound : public CBase
00068         {
00069 public :
00070         ~CCompound();
00071         static          CCompound* NewLC();
00072         static          CCompound* NewLC(CStreamStore& aStore,TStreamId anId);
00073         static          CCompound* NewL(CStreamStore& aStore,TStreamId anId);
00074         TStreamId       StoreL(CStreamStore& store);
00075         void            RestoreL(CStreamStore& aStore,TStreamId anId);
00076         void            InternalizeL(RReadStream& aStream);
00077         void            ExternalizeL(RWriteStream& aStream) const;
00078 private:
00079     void                ConstructL();
00080         void            ConstructL(CStreamStore& aStore,TStreamId anId);
00081 public :
00082         CClassA* iCa;
00083         CClassB* iCb;
00084         TClassC  iTc;
00085         };
00086          
00087 // The file name, extension and path for the file store
00088         _LIT(KFullNameOfFileStore,"\\epoc32ex\\data\\stcompnd.dat");
00089 
00090 
00091 // Do the example
00092 LOCAL_C void doExampleL()
00093     {
00094                                             // make sure directory exists
00095         fsSession.MkDirAll(KFullNameOfFileStore);
00096         doMakeAndExternalizeL(KFullNameOfFileStore);
00097         doInternalizeL(KFullNameOfFileStore);
00098         }
00099 
00100 LOCAL_C void doMakeAndExternalizeL(const TDesC& aName)
00101         {
00102         TParse  filestorename;
00103 
00104         fsSession.Parse(aName,filestorename);
00105                                 // construct file store object - the file to contain the
00106                                 // the store replaces any existing file of the same name.
00107         CFileStore* store = CDirectFileStore::ReplaceLC(fsSession,filestorename.FullName(),EFileWrite);
00108 
00109                                 // Must say what kind of file store
00110     store->SetTypeL(KDirectFileStoreLayoutUid);
00111 
00112                                 // Construct an object of type CCompound 
00113                                 // and put some data into it.
00114         CCompound* thecompound = CCompound::NewLC();
00115 
00116         _LIT(KTxtClassAText,"CClassA text");
00117         _LIT(KTxtClassBText,"CClassB text");
00118 
00119         thecompound->iCa->iBufferA = KTxtClassAText;
00120         thecompound->iCa->iXA      = -1;
00121         thecompound->iCa->iYA      = 2;
00122         thecompound->iCb->iBufferB = KTxtClassBText;
00123         thecompound->iTc.iZC       = 3.456;
00124 
00125                                 // Show contents of the CCompound object (and its
00126                                 // components)
00127         _LIT(KTxtInitialContent,"... Initial content of CCompound");
00128         doShow(KTxtInitialContent,*thecompound);
00129                                         
00130                                 // Store the compound object to a single stream
00131                                 // and save the stream id as the root id. 
00132         TStreamId  id = thecompound->StoreL(*store);
00133 
00134                                 // Set the stream id as the root
00135         store->SetRootL(id);
00136 
00137                                 // Commit changes to the store
00138         store->CommitL();
00139 
00140                                 // Destroy:
00141                                 // 1. the CCompound object
00142                                 // 2. the store object (this also closes 
00143                                 //    the file containing the store)
00144                                 // Remove both from the cleanup stack
00145         CleanupStack::PopAndDestroy(2);
00146         }
00147 
00148 LOCAL_C void doInternalizeL(const TDesC& aName)
00149         {
00150         TParse  filestorename;
00151         
00152         fsSession.Parse(aName,filestorename);
00153                                 // construct file store object - specifying the file
00154                                 // containing the store.
00155         CFileStore* store = CDirectFileStore::OpenLC(fsSession,filestorename.FullName(),EFileRead);
00156         
00157                                 // Construct a CCompound object  
00158                                 // from the root stream created earlier.
00159         CCompound* thecompound = CCompound::NewL(*store,store->Root());
00160 
00161                                 // Show contents of the CCompound object (and its
00162                                 // components)
00163         _LIT(KTxtRestoredContent,"... Restored CCompound content.");
00164         doShow(KTxtRestoredContent,*thecompound);
00165                                 
00166                                 // destroy the store object (this also closes the file
00167                                 // containing the store) 
00168         CleanupStack::PopAndDestroy();
00169 
00170                                 // Now destroy the CCompound object
00171         delete thecompound;
00172         }
00173 
00174 _LIT(KTxtNewLine,"\n");
00175 _LIT(KFormatType1,"\n%d");
00176 _LIT(KFormatType2,"\n%S");
00177 _LIT(KFormatType3,"\n%u");
00178 _LIT(KFormatType4,"\n%f");
00179 
00180 LOCAL_C void doShow(const TDesC& aHeading,const CCompound& aCompound)
00181         {
00182         console->Printf(KTxtNewLine);
00183         console->Printf(aHeading);
00184         console->Printf(KFormatType2,&aCompound.iCa->iBufferA);
00185         console->Printf(KFormatType1,aCompound.iCa->iXA);
00186         console->Printf(KFormatType3,aCompound.iCa->iYA);
00187         console->Printf(KFormatType2,&aCompound.iCb->iBufferB);
00188         console->Printf(KFormatType4,aCompound.iTc.iZC);
00189         console->Printf(KTxtNewLine);
00190         }
00191 
00192 //***************************************************************
00193 //***************************************************************
00194 
00195                                 // The CCompound destructor
00196 CCompound::~CCompound()
00197         {
00198         delete iCa;
00199         delete iCb;
00200         }
00201 
00202                                 // Construct a new plain CCompound object and
00203                                 // place on the cleanup stack.
00204 CCompound* CCompound::NewLC()
00205         {
00206         CCompound* self=new (ELeave) CCompound;
00207         CleanupStack::PushL(self);
00208         self->ConstructL();
00209         return self;
00210         }
00211 
00212                                 // Complete the construction of the 
00213                                 // plain CCompound object
00214 void CCompound::ConstructL()
00215         {
00216         iCa = new (ELeave) CClassA;
00217         iCb = new (ELeave) CClassB;
00218         }
00219 
00220                                 // Construct a new CCompound object from 
00221                                 // the input stream.
00222 CCompound* CCompound::NewL(CStreamStore& aStore,TStreamId anId)
00223         {
00224         CCompound* self=CCompound::NewLC(aStore,anId);
00225         CleanupStack::Pop();
00226         return self;
00227         }
00228 
00229                                 // Construct a new CCompound object from 
00230                                 // the root stream of the store and 
00231                                 // place on the cleanup stack.
00232 CCompound* CCompound::NewLC(CStreamStore& aStore,TStreamId anId)
00233         {
00234         CCompound* self=new (ELeave) CCompound;
00235         CleanupStack::PushL(self);
00236         self->ConstructL(aStore,anId);
00237         return self;
00238         }
00239 
00240                                 // Complete the construction of the 
00241                                 // CCompound object
00242 void CCompound::ConstructL(CStreamStore& aStore,TStreamId anId)
00243         {
00244         iCa = new (ELeave) CClassA;
00245         iCb = new (ELeave) CClassB;
00246         RestoreL(aStore,anId);
00247         }
00248 
00249 void CCompound::RestoreL(CStreamStore& aStore,TStreamId anId)
00250         {
00251         RStoreReadStream instream;
00252         instream.OpenLC(aStore,anId);
00253         InternalizeL(instream);
00254                                         // Cleanup the stream object.
00255         CleanupStack::PopAndDestroy();                  
00256         }
00257 
00258                                 // Read the components and data members 
00259                                 // of the CCompound object from the stream.
00260 void CCompound::InternalizeL(RReadStream& aStream)
00261         {
00262         aStream >> *iCa;        
00263         aStream >> *iCb;
00264         aStream >> iTc;
00265         }
00266 
00267 TStreamId CCompound::StoreL(CStreamStore& aStore)
00268         {
00269         RStoreWriteStream outstream;
00270         TStreamId id = outstream.CreateLC(aStore);
00271                                 // Stream out this CCompound object
00272         ExternalizeL(outstream);
00273                                 // Commit changes to the stream
00274         outstream.CommitL();
00275                                 // Cleanup the stream object.
00276         CleanupStack::PopAndDestroy();
00277         return id;
00278         }
00279                                 // Write the components and data members 
00280                                 // of the CCompound object to the stream
00281 void CCompound::ExternalizeL(RWriteStream& aStream) const
00282         {
00283         aStream << *iCa;
00284         aStream << *iCb;
00285         aStream << iTc;
00286         }
00287 
00288 //***************************************************************
00289 //***************************************************************
00290 
00291                                 // Read the data members of the CClassA 
00292                                 // object from the stream.
00293 void CClassA::InternalizeL(RReadStream& aStream)
00294         {
00295         aStream >> iBufferA;
00296         iXA = aStream.ReadInt32L();
00297         iYA = aStream.ReadUint32L();
00298         }  
00299                                 // Write the data members of the CClassA 
00300                                 // object to the stream.
00301 void CClassA::ExternalizeL(RWriteStream& aStream)const
00302         {
00303         aStream << iBufferA;
00304         aStream.WriteInt32L(iXA);
00305         aStream.WriteUint32L(iYA);
00306         }  
00307 
00308 //***************************************************************
00309 //***************************************************************
00310 
00311                                 // Read the data member(s) of the CClassB 
00312                                 // object from the stream.
00313 void CClassB::InternalizeL(RReadStream& aStream)
00314         {
00315         aStream >> iBufferB;
00316         }
00317                                 // Write the data member(s) of the CClassB 
00318                                 // object to the stream.
00319 void CClassB::ExternalizeL(RWriteStream& aStream) const
00320         {
00321         aStream << iBufferB;
00322         }  
00323 
00324 //***************************************************************
00325 //***************************************************************
00326 
00327                                 // Write the data member(s) of the TClassC 
00328                                 // object to the stream.
00329 void TClassC::ExternalizeL(RWriteStream& aStream) const
00330         {
00331         aStream.WriteReal64L(iZC);
00332         }  
00333                                 // Read the data member(s) of the TClassC 
00334                                 // object from the stream.
00335 void TClassC::InternalizeL(RReadStream& aStream)
00336         {
00337         iZC = aStream.ReadReal64L();
00338         }  
00339                 
00340 
00341 
00342 
00343         
00344         

Generated by  doxygen 1.6.2