examples/SysLibs/Streams/StoreMap/StoreMap.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 // multiple streams with deferred loading.
00016 // Also illustrates the use of a store map (CStoreMap) 
00017 //
00018 
00019 #include "CommonStreamStore.h"
00020 #include <s32file.h>
00021 
00022 //
00023 // Format types and the newline character
00024 //
00025 
00026 _LIT(KTxtNewLine,"\n");
00027 _LIT(KFormatType1,"\n%d");
00028 _LIT(KFormatType2,"\n%S");
00029 _LIT(KFormatType3,"\n%u");
00030 _LIT(KFormatType4,"\n%f");
00031 
00032                                 // Constructs a CCompound object and externalizes
00033                                 // it and its components to separate streams.
00034 static void doMakeAndExternalizeL(const TDesC& aName);        
00035 
00036                                 // Demonstrates deferred loading of
00037                                 // component objects.
00038 static void doDeferredLoadingL(const TDesC& aName); 
00039 
00040                                 // Shows the content of the compound object 
00041 class CCompound;
00042 static void doShowAll(const TDesC& aHeading,const CCompound& aCompound);
00043 
00044                                 // Shows the content of the CLassA component
00045                                 // of the compound object.
00046 class CClassA;
00047 static void doShowComponent(const TDesC& aHeading,const CClassA& aComponent);
00048 
00049                                 // Shows the content of the CLassB component
00050                                 // of the compound object.
00051 class CClassB;  
00052 static void doShowComponent(const TDesC& aHeading,const CClassB& aComponent);
00053 
00054                                 // Shows the content of the CLassB component
00055                                 // of the compound object.
00056 class CClassC;
00057 static void doShowComponent(const TDesC& aHeading,const CClassC& aComponent);
00058 
00059                                 // Defintion of classes used by the example
00060 class CClassA
00061         {
00062 public :
00063         void            ExternalizeL(RWriteStream& aStream) const;
00064         void            InternalizeL(RReadStream& aStream);
00065         TStreamId       StoreL(CStreamStore& aStore) const;
00066         void            RestoreL(CStreamStore& aStore, TStreamId anId);
00067 public :
00068         TBuf<32>        iBufferA;
00069         TInt            iXA;
00070         TUint           iYA;
00071         };
00072 
00073 
00074 class CClassB
00075         {
00076 public :
00077         void            ExternalizeL(RWriteStream& aStream) const;
00078         void            InternalizeL(RReadStream& aStream);
00079         TStreamId       StoreL(CStreamStore& aStore) const;
00080         void            RestoreL(CStreamStore& aStore, TStreamId anId);
00081 public :
00082         TBuf<32>        iBufferB;
00083         };
00084 
00085 
00086 class CClassC
00087         {
00088 public :
00089         void            ExternalizeL(RWriteStream& aStream) const;
00090         void            InternalizeL(RReadStream& aStream);
00091         TStreamId       StoreL(CStreamStore& aStore) const;
00092         void            RestoreL(CStreamStore& aStore, TStreamId anId);
00093 public :
00094         TReal           iZC;
00095         };
00096 
00097         
00098 class CCompound : public CBase
00099         {
00100 public :
00101         static          CCompound* NewLC(CStreamStore& aStore);
00102         static          CCompound* NewLC(CStreamStore& aStore,TStreamId anId);
00103 public :
00104         CCompound(CStreamStore& aStore);
00105         CCompound(CStreamStore& aStore,TStreamId anId);
00106         ~CCompound();
00107         void            InternalizeL(RReadStream& aStream);
00108         void            ExternalizeL(RWriteStream& aStream) const;
00109         TStreamId       StoreL() const;
00110         void            RestoreL();
00111         void            StoreComponentsL(CStoreMap& aMap) const;
00112         void            DisplayAL(const TDesC& aCommentary);
00113 private:
00114     void                ConstructL();
00115 public :
00116         TSwizzle<CClassA> iA;
00117         TSwizzle<CClassB> iB;
00118         TSwizzle<CClassC> iC;
00119         CStreamStore&     iStore; // Store to/Restore from this store
00120         TStreamId         iId;    // Restore from/replace this stream
00121         };
00122 
00123 
00124 // The file name, extension and path for the file store
00125 _LIT(KFullNameOfFileStore,"\\epoc32ex\\data\\StoreMapUse.dat");
00126 
00127         // Do the example
00128 static void doExampleL()
00129     {
00130                                     // make sure directory exists
00131         fsSession.MkDirAll(KFullNameOfFileStore);
00132                                 // Construct a CCompound object and externalize
00133                                 // it and its components to separate streams.
00134         doMakeAndExternalizeL(KFullNameOfFileStore);
00135                         
00136                                 // Demonstrate deferred loading of component objects
00137         doDeferredLoadingL(KFullNameOfFileStore);
00138         }
00139 
00140 static void doMakeAndExternalizeL(const TDesC& aName)
00141         {
00142         TParse  filestorename;
00143         fsSession.Parse(aName,filestorename);
00144                                 // construct file store object - the file to contain the
00145                                 // the store replaces any existing file of the same name.
00146         CFileStore* store = CPermanentFileStore::ReplaceLC(fsSession,filestorename.FullName(),EFileWrite);
00147 
00148                                 // Must say what kind of file store
00149     store->SetTypeL(KPermanentFileStoreLayoutUid);
00150 
00151                                         // Construct an object of type CCompound ... 
00152         CCompound* thecompound = CCompound::NewLC(*store);
00153 
00154                                 // ... and put some data into it.
00155                                 // Note that "iA->" achieves the same
00156                                 // as "iA->AsPtr()->"
00157         _LIT(KTxtClassAText,"CClassA text");
00158         _LIT(KTxtClassBText,"CClassB text");
00159         thecompound->iA.AsPtr()->iBufferA = KTxtClassAText;
00160         thecompound->iA.AsPtr()->iXA      = -1; 
00161         thecompound->iA->iYA              = 2; // see note above
00162         thecompound->iB.AsPtr()->iBufferB = KTxtClassBText;
00163         thecompound->iC.AsPtr()->iZC      = 3.456;
00164 
00165                                 // Show contents of the CCompound object (and its
00166                                 // components).
00167         _LIT(KTxtInitialContent,"... Initial content of CCompound");
00168         doShowAll(KTxtInitialContent,*thecompound);
00169 
00170                                 // stores all components as separate streams and
00171                                 // then streams the store map
00172         TStreamId id = thecompound->StoreL();
00173 
00174                                 // Set the stream id as the root
00175         store->SetRootL(id);
00176 
00177                                 // Commit changes to the store
00178         store->CommitL();
00179 
00180                                 // Destroy:
00181                                 // 1. the CCompound object
00182                                 // 2. the store object (this also closes 
00183                                 //    the file containing the store)
00184                                 // Remove both from the cleanup stack
00185         CleanupStack::PopAndDestroy(2);
00186         }
00187 
00188 static void doDeferredLoadingL(const TDesC& aName)
00189         {
00190         TParse  filestorename;
00191         fsSession.Parse(aName,filestorename);
00192                                 // construct file store object - specifying the file
00193                                 // containing the store.
00194         CFileStore* store = CPermanentFileStore::OpenLC(fsSession,filestorename.FullName(),EFileRead);
00195         
00196                                 // Construct an object of type CCompound.
00197                                 // The loading of its components is deferred 
00198                                 // until needed.
00199         CCompound* thecompound = CCompound::NewLC(*store,store->Root());
00200         
00201                                 // Display component A.
00202                                 // The component is loaded in from the 
00203                                 // stream if not already loaded.
00204         _LIT(KTxtFirstDisplay," (first display)");
00205         thecompound->DisplayAL(KTxtFirstDisplay);
00206 
00207                                 // Re-Display component A
00208                                 // The component should now be in memory.
00209         _LIT(KTxtSecondDisplay," (second display)");
00210         thecompound->DisplayAL(KTxtSecondDisplay);
00211 
00212         console->Printf(KTxtNewLine);
00213 
00214                                 // Destroy:
00215                                 // 1. the CCompound object
00216                                 // 2. the store object (this also closes 
00217                                 //    the file containing the store)
00218                                 // Remove both from the cleanup stack
00219         CleanupStack::PopAndDestroy(2);
00220         }
00221 
00222 _LIT(KTxtComponentA,"... Component A");
00223 _LIT(KTxtComponentB,"... Component B");
00224 _LIT(KTxtComponentC,"... Component C");
00225 _LIT(KTxtPressKey,"\n (press any key to continue)");
00226 
00227 static void doShowAll(const TDesC& aHeading,const CCompound& aCompound)
00228         { 
00229         console->Printf(KTxtNewLine);
00230         console->Printf(aHeading);
00231         if (aCompound.iA.IsPtr())
00232                 doShowComponent(KTxtComponentA,*aCompound.iA.AsPtr());
00233         if (aCompound.iB.IsPtr())
00234                 doShowComponent(KTxtComponentB,*aCompound.iB.AsPtr());
00235         if (aCompound.iB.IsPtr())
00236                 doShowComponent(KTxtComponentC,*aCompound.iC.AsPtr());
00237         console->Printf(KTxtPressKey);
00238         console->Getch();
00239         }
00240 
00241 static void doShowComponent(const TDesC& aHeading,const CClassA& aComponent)
00242         {
00243         console->Printf(KTxtNewLine);
00244         console->Printf(aHeading);
00245         console->Printf(KFormatType2,&aComponent.iBufferA);
00246         console->Printf(KFormatType1,aComponent.iXA);
00247         console->Printf(KFormatType3,aComponent.iYA);
00248         }
00249 
00250 static void doShowComponent(const TDesC& aHeading,const CClassB& aComponent)
00251         {
00252         console->Printf(KTxtNewLine);
00253         console->Printf(aHeading);
00254         console->Printf(KFormatType2,&aComponent.iBufferB);
00255         }
00256 
00257 static void doShowComponent(const TDesC& aHeading,const CClassC& aComponent)
00258         {
00259         console->Printf(KTxtNewLine);
00260         console->Printf(aHeading);
00261         console->Printf(KFormatType4,aComponent.iZC);
00262         }
00263 
00264 //***************************************************************
00265 //***************************************************************
00266 CCompound::CCompound(CStreamStore& aStore)
00267         : iStore(aStore)
00268         {}
00269 
00270 CCompound::CCompound(CStreamStore& aStore,TStreamId anId)
00271         : iStore(aStore), iId(anId)
00272         {}
00273 
00274 
00275                                 // Construct a plain CCompound object and
00276                                 // place on the cleanup stack.
00277 CCompound* CCompound::NewLC(CStreamStore& aStore)
00278         {
00279         CCompound* self=new (ELeave) CCompound(aStore);
00280         CleanupStack::PushL(self);
00281         self->ConstructL();
00282         return self;
00283         }
00284 
00285 
00286                                 // Construct a CCompound object. The
00287                                 // components are restored when needed; however
00288                                 // the streamids of the component streams
00289                                 // ARE restored.
00290 CCompound* CCompound::NewLC(CStreamStore& aStore,TStreamId anId)
00291         {
00292         CCompound* self=new (ELeave) CCompound(aStore,anId);
00293         CleanupStack::PushL(self);
00294         self->RestoreL();
00295         return self;
00296         }
00297 
00298                                 // Complete the construction of the 
00299                                 // plain CCompound object. Makes implicit 
00300                                 // use of  TSwizzle operators.
00301 void CCompound::ConstructL()
00302         {
00303         iA = new (ELeave) CClassA;
00304         iB = new (ELeave) CClassB;
00305         iC = new (ELeave) CClassC;
00306         }
00307 
00308                                 // The CCompound destructor. Destroy the swizzled
00309                                 // contained objects only if they are in memory
00310 CCompound::~CCompound()
00311         {
00312         if (iA.IsPtr())
00313                 delete iA.AsPtr();
00314         if (iB.IsPtr())
00315                 delete iB.AsPtr();
00316         if (iC.IsPtr())
00317                 delete iC.AsPtr();
00318         }
00319 
00320 void CCompound::RestoreL()
00321         {
00322         RStoreReadStream stream;
00323         stream.OpenLC(iStore,iId);
00324         InternalizeL(stream);
00325         CleanupStack::PopAndDestroy();
00326         }
00327                                 
00328 void CCompound::InternalizeL(RReadStream& aStream)
00329         {
00330         aStream >> iA;  
00331         aStream >> iB;
00332         aStream >> iC;
00333         }
00334                                 // Display the content of the "A" component
00335                                 // object. The component is fully restored 
00336                                 // from the appropriate stream, if it has not
00337                                 // already been restored. 
00338 void CCompound::DisplayAL(const TDesC& aCommentary)
00339         {
00340         if (iA.IsId())
00341                 {
00342                 CClassA* ptrA = new (ELeave) CClassA;
00343                 CleanupStack::PushL(ptrA);
00344                 ptrA->RestoreL(iStore,iA.AsId());
00345                 iA = ptrA;
00346             CleanupStack::Pop(); 
00347                 }
00348         _LIT(KTxtRestoredComponent,"... Displaying restored component A");
00349         TBuf<96> heading(KTxtRestoredComponent);
00350         heading.Append(aCommentary); 
00351         doShowComponent(heading,*iA.AsPtr());
00352         }
00353 
00354 
00355                 
00356 void CCompound::ExternalizeL(RWriteStream& aStream) const
00357         {
00358         aStream << iA; // these are swizzles
00359         aStream << iB;
00360         aStream << iC;
00361         }
00362 
00363 TStreamId CCompound::StoreL() const
00364         {
00365         CStoreMap* map=CStoreMap::NewLC(iStore);
00366         StoreComponentsL(*map);
00367 //
00368         RStoreWriteStream stream(*map);
00369         TStreamId id=stream.CreateLC(iStore);
00370         ExternalizeL(stream);
00371         stream.CommitL();
00372 //
00373         map->Reset();
00374         CleanupStack::PopAndDestroy(2);
00375         return id;
00376         }
00377 
00378 void CCompound::StoreComponentsL(CStoreMap& aMap) const
00379         {
00380         TStreamId id;
00381         
00382         if (iA)
00383                 {
00384                 id = iA->StoreL(iStore);
00385                 aMap.BindL(iA,id);
00386                 }
00387 
00388         if (iB)
00389                 {
00390                 id = iB->StoreL(iStore);
00391                 aMap.BindL(iB,id);
00392                 }
00393 
00394         if (iC)
00395                 {
00396                 id = iC->StoreL(iStore);
00397                 aMap.BindL(iC,id);
00398                 }
00399         }
00400 
00401 //***************************************************************
00402 //***************************************************************
00403 
00404                                 // Read the data members of the CClassA 
00405                                 // object from the stream.
00406 void CClassA::InternalizeL(RReadStream& aStream)
00407         {
00408         aStream >> iBufferA;
00409         iXA = aStream.ReadInt32L();
00410         iYA = aStream.ReadUint32L();
00411         }  
00412                                 // Write the data members of the CClassA 
00413                                 // object to the stream.
00414 void CClassA::ExternalizeL(RWriteStream& aStream) const
00415         {
00416         aStream << iBufferA;
00417         aStream.WriteInt32L(iXA);
00418         aStream.WriteUint32L(iYA);
00419         }  
00420 
00421 TStreamId CClassA::StoreL(CStreamStore& aStore) const
00422         {
00423         RStoreWriteStream stream;
00424         TStreamId id=stream.CreateLC(aStore);
00425         ExternalizeL(stream);
00426         stream.CommitL();
00427         CleanupStack::PopAndDestroy();
00428         return id;
00429         }
00430 
00431 void CClassA::RestoreL(CStreamStore& aStore, TStreamId anId)
00432         {
00433         RStoreReadStream stream;
00434         stream.OpenLC(aStore,anId);
00435         InternalizeL(stream);
00436         CleanupStack::PopAndDestroy();
00437         }
00438 
00439 //***************************************************************
00440 //***************************************************************
00441 
00442                                 // Read the data member(s) of the CClassB 
00443                                 // object from the stream.
00444 void CClassB::InternalizeL(RReadStream& aStream)
00445         {
00446         aStream >> iBufferB;
00447         }
00448                                 // Write the data member(s) of the CClassB 
00449                                 // object to the stream.
00450 void CClassB::ExternalizeL(RWriteStream& aStream) const
00451         {
00452         aStream << iBufferB;
00453         }  
00454 
00455 TStreamId CClassB::StoreL(CStreamStore& aStore) const
00456         {
00457         RStoreWriteStream stream;
00458         TStreamId id=stream.CreateLC(aStore);
00459         ExternalizeL(stream);
00460         stream.CommitL();
00461         CleanupStack::PopAndDestroy();
00462         return id;
00463         }
00464 
00465 void CClassB::RestoreL(CStreamStore& aStore, TStreamId anId)
00466         {
00467         RStoreReadStream stream;
00468         stream.OpenLC(aStore,anId);
00469         InternalizeL(stream);
00470         CleanupStack::PopAndDestroy();
00471         }
00472 
00473 
00474 //***************************************************************
00475 //***************************************************************
00476 
00477                                 // Write the data member(s) of the CClassC 
00478                                 // ob ject to the stream.
00479 void CClassC::ExternalizeL(RWriteStream& aStream) const
00480         {
00481         aStream.WriteReal64L(iZC);
00482         }  
00483                                 // Read the data member(s) of the CClassC 
00484                                 // object from the stream.
00485 void CClassC::InternalizeL(RReadStream& aStream)
00486         {
00487         iZC = aStream.ReadReal64L();
00488         }  
00489                 
00490 TStreamId CClassC::StoreL(CStreamStore& aStore) const
00491         {
00492         RStoreWriteStream stream;
00493         TStreamId id=stream.CreateLC(aStore);
00494         ExternalizeL(stream);
00495         stream.CommitL();
00496         CleanupStack::PopAndDestroy();
00497         return id;
00498         }
00499 
00500 
00501 void CClassC::RestoreL(CStreamStore& aStore, TStreamId anId)
00502         {
00503         RStoreReadStream stream;
00504         stream.OpenLC(aStore,anId);
00505         InternalizeL(stream);
00506         CleanupStack::PopAndDestroy();
00507         }
00508 
00509 
00510 
00511         
00512         

Generated by  doxygen 1.6.2