examples/SysLibs/Streams/StoreMap/StoreMap.cpp

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

Generated by  doxygen 1.6.2