examples/SysLibs/Streams/WriteToEmbedded/WriteToEmbedded.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 uses an embedded store.
00015 // This example uses a permanent file store. It constructs a CMainClass object
00016 // which has a CClassABC object as a component. The CClassABC object itself
00017 // has other components and its structure is the same as found in the
00018 // WriteToMany example.
00019 // Here, the CClassABC component object network is stored in an embedded store.
00020 // The example:
00021 // creates a permanent file store
00022 // constructs a CMainClass object. This results in the construction
00023 // of a CClassABC objet network which is stored in its own embedded
00024 // store (within the main store). 
00025 // stores the CMainClass object in its own stream in the main store
00026 // and makes that stream the root stream.
00027 // closes the store and deletes the CMainClass object from memory
00028 // re-opens the permanent file store
00029 // restores the CMainClass object and opens the embedded store. As the 
00030 // the contained CClassABC object itself has components which are only 
00031 // loaded in when needed, then the embedded store is kept open.
00032 // Displays the content of the CClassA, CClassB and CClassC objects.
00033 // Deletes the CClassABC object from CMainClass and deletes its representation
00034 // from the permanent file store. Note that knowledge of the detailed
00035 // structure of the CClassABC object network is not needed. 
00036 // Attempts to redisplay the content of the CClassA, CClassB and CClassC 
00037 // objects; this should fail as they have been deleted.
00038 // closes the store and deletes the CMainClass object from memory.
00039 // Notes:
00040 // The file name and extension of the direct file store is "WriteToEmbedded.dat".
00041 // and will be created in the "\data\" folder of the writable drive.
00042 //
00043 
00044 #include "WriteToEmbedded.h"
00045 //***************************************************************
00046 //
00047 // Implementations
00048 //***************************************************************
00049 
00050 // The file name, extension and path for the file store
00051 _LIT(KFullNameOfFileStore,"\\epoc32ex\\data\\WriteToEmbedded.dat");
00052 
00053                                 //  Do the example
00054 LOCAL_C void doExampleL()
00055     {
00056                             // make sure directory exists
00057         fsSession.MkDirAll(KFullNameOfFileStore);
00058         doMakeAndStoreL(KFullNameOfFileStore);
00059         doDeleteComponentL(KFullNameOfFileStore);
00060         }
00061  
00062 LOCAL_C void doMakeAndStoreL(const TDesC& aName)
00063         {
00064                                 // Create (replace, if it exists) the permanent file store
00065         TParse  filestorename;
00066         fsSession.Parse(aName,filestorename);
00067         CFileStore* store = CPermanentFileStore::ReplaceLC(fsSession,filestorename.FullName(),EFileWrite);
00068 
00069                                 // Must say what kind of file store.
00070         store->SetTypeL(KPermanentFileStoreLayoutUid);
00071         
00072                                 // Construct a main object and its component
00073                                 // CClassABC object; the CClassABC component object
00074                                 // and its components are stored in an embedded store
00075                                 // within the main store.  
00076         CMainClass* theMain = CMainClass::NewLC(*store);
00077 
00078                                 // Store the CMainClass object in its own stream 
00079                                 // in the top level store
00080         TStreamId theId = theMain->StoreL();
00081 
00082                                 // Set this stream id as the root stream
00083         store->SetRootL(theId);
00084 
00085                                 // Commit theses changes to the top level store
00086         store->CommitL();
00087 
00088                                 // Destroy:
00089                                 //              the CMainClass object,
00090                                 //              the permanent file store object (closes the file)
00091         CleanupStack::PopAndDestroy(2); 
00092         }
00093 
00094 LOCAL_C void doDeleteComponentL(const TDesC& aName)
00095         {
00096                                 // Open the permanent file store
00097         TParse  filestorename;
00098         fsSession.Parse(aName,filestorename);
00099         CFileStore* store = CPermanentFileStore::OpenLC(fsSession,filestorename.FullName(),EFileRead|EFileWrite);
00100                                 
00101                                 // Restore the main object
00102         CMainClass* theMain = CMainClass::NewLC(*store,store->Root());
00103         
00104                                 // Show the content of the classes contained by CClassABC,
00105                                 // loading as needed
00106         CClassABC* abc = theMain->PtrAbc();
00107         
00108         _LIT(KTxtClassAContent,"CClassA content ...");
00109         _LIT(KTxtClassBContent,"CClassB content ...");
00110         _LIT(KTxtClassCContent,"CClassC content ...");
00111         _LIT(KTxtClassABCDeleted,"CClassABC component deleted");
00112 
00113         if (abc)
00114                 {
00115                 doShow(KTxtClassAContent,*abc->PtrA());
00116                 doShow(KTxtClassBContent,*abc->PtrBL());                                        
00117                 doShow(KTxtClassCContent,*abc->PtrC());
00118                 }
00119         else
00120                 doShow(KTxtClassABCDeleted);
00121                 
00122                                 // remove the CClassABC component from CMainClass and
00123                                 // and remove its representation from the store
00124         theMain->RemoveAbcL();
00125 
00126                                 // Try and show the contents again; the pointer
00127                                 // abc should now be NULL
00128         abc = theMain->PtrAbc();
00129         if (abc)
00130                 {
00131                 doShow(KTxtClassAContent,*abc->PtrA());
00132                 doShow(KTxtClassBContent,*abc->PtrBL());                                        
00133                 doShow(KTxtClassCContent,*abc->PtrC());
00134                 }
00135         else
00136                 doShow(KTxtClassABCDeleted);
00137 
00138                                 // Destroy:
00139                                 //              the CMainClass object, 
00140                                 //      the permanent file store object (closes the file) 
00141         CleanupStack::PopAndDestroy(2);
00142         }
00143 
00144 _LIT(KTxtNewLine,"\n");
00145 _LIT(KFormatType1,"\n%S, ");
00146 _LIT(KFormatType2,"%d, ");
00147 _LIT(KFormatType3,"%u  ");
00148 _LIT(KFormatType4,"%u, ");
00149 _LIT(KFormatType5,"%f  ");
00150  
00151 LOCAL_C void doShow(const TDesC& aComment)
00152         {
00153         console->Printf(KTxtNewLine);
00154         console->Printf(aComment);
00155         console->Printf(KTxtNewLine);
00156         }
00157         
00158 LOCAL_C void doShow(const TDesC& aHeading,const CClassA& anA)
00159         {
00160         console->Printf(KTxtNewLine);
00161         console->Printf(aHeading);
00162         console->Printf(KFormatType1,anA.iVarBuf);
00163         console->Printf(KFormatType2,anA.iIntValue);
00164         console->Printf(KFormatType3,anA.iUintValue);
00165         console->Printf(KTxtNewLine);
00166         }
00167 
00168 LOCAL_C void doShow(const TDesC& aHeading,const CClassB& aB)
00169         {
00170         console->Printf(KTxtNewLine);
00171         console->Printf(aHeading);
00172         console->Printf(KFormatType1,&aB.iFixBuf);
00173         console->Printf(KFormatType2,aB.iIntValue);
00174         console->Printf(KFormatType4,aB.iUintValue);
00175         console->Printf(KFormatType5,aB.iRealValue);
00176         console->Printf(KTxtNewLine);
00177         }
00178 
00179 LOCAL_C void doShow(const TDesC& aHeading,const CClassC& aC)
00180         {
00181         console->Printf(KTxtNewLine);
00182         console->Printf(aHeading);
00183         console->Printf(KFormatType1,&aC.iFixBuf);
00184         console->Printf(KTxtNewLine);
00185         }
00186 
00187 //***************************************************************
00188 //
00189 // CMainClass Implementation
00190 //
00191 //***************************************************************
00192 CMainClass* CMainClass::NewLC(CStreamStore& aStore)
00193         {
00194         CMainClass* self = new (ELeave) CMainClass(aStore);
00195         CleanupStack::PushL(self);
00196         self->ConstructL();
00197         return self;
00198         }
00199 
00200 CMainClass* CMainClass::NewLC(CStreamStore& aStore,TStreamId anId)
00201         {
00202         CMainClass* self = new (ELeave) CMainClass(aStore,anId);
00203         CleanupStack::PushL(self);
00204         self->RestoreL();
00205         return self;
00206         }
00207 
00208 CMainClass::CMainClass(CStreamStore& aStore)
00209         : iStore(aStore)
00210         {}
00211 
00212 CMainClass::CMainClass(CStreamStore& aStore,TStreamId anId)
00213         : iStore(aStore), iId(anId)
00214         {}
00215 
00216 _LIT(KTxtMainData,"Main data");
00217 _LIT(KTxtClassAData,"Data for the CClassA - AAAAA");
00218 _LIT(KTxtClassBData,"Data for the CClassB - BBBBB");
00219 _LIT(KTxtClassCData,"Data for the CClassC - CCCCC");
00220 
00221 void CMainClass::ConstructL()
00222         {
00223         iSomeData = KTxtMainData;
00224                                 // create the stream to contain the embedded store
00225         RStoreWriteStream childStream;
00226                                 // we need to keep track of this stream id
00227         iEmbeddedStoreId = childStream.CreateLC(iStore);
00228                                 // construct the embedded store
00229         CPersistentStore* embeddedStore = CEmbeddedStore::NewLC(childStream);
00230                                 // construct the CClassABC and its containees. Note that
00231                                 // the store it uses is the embedded store.
00232         iAbc = CClassABC::NewL(*embeddedStore); 
00233         iAbc->ConstructAL(KTxtClassAData,-1,2);
00234         iAbc->ConstructB(KTxtClassBData,-3,4,5.6);
00235         iAbc->ConstructC(KTxtClassCData);
00236                                 // store the CClassABC and its containees in the
00237                                 // embedded store ...
00238         TStreamId idForAbc = iAbc->StoreL();
00239                                 // ... and set this stream id as the root 
00240                                 // stream of the embedded store
00241         embeddedStore->SetRootL(idForAbc);      
00242                                 // Commit changes to the embedded store
00243         embeddedStore->CommitL();
00244                                 // Commit changes to the stream containing the 
00245                                 // embedded store.
00246         childStream.CommitL();
00247                                 // Destroy the embedded store object
00248                                 // Cleanup the stream containing the emmbedded store 
00249         CleanupStack::PopAndDestroy(2);
00250         }
00251 
00252 CMainClass::~CMainClass()
00253         {
00254         delete iEmbeddedStore;
00255         iChildStream.Release(); //stream cleanup
00256         delete iAbc;
00257         }
00258 
00259 TStreamId CMainClass::StoreL()
00260         {
00261         RStoreWriteStream stream;
00262         TStreamId id = stream.CreateLC(iStore);
00263         ExternalizeL(stream);
00264         stream.CommitL();
00265         CleanupStack::PopAndDestroy();
00266         return id;
00267         }
00268         
00269 void CMainClass::ExternalizeL(RWriteStream& aStream)
00270         {
00271         aStream << iSomeData; 
00272         aStream << iEmbeddedStoreId;
00273         }
00274 
00275 void CMainClass::RestoreL()
00276         {
00277         RStoreReadStream stream;
00278         stream.OpenLC(iStore,iId);
00279                                 // restore the CMainClass object
00280         InternalizeL(stream);
00281                                 // clean up the stream
00282         CleanupStack::PopAndDestroy();
00283                                 // open stream containing the embedded store
00284                                 // (and keep it open)
00285         if (iEmbeddedStoreId != KNullStreamId)
00286                 {
00287                 iChildStream.OpenL(iStore,iEmbeddedStoreId);
00288                                 // construct the embedded store 
00289                                 // (and keep it open)
00290                 iEmbeddedStore = CEmbeddedStore::FromL(iChildStream);
00291                                 // restore the CClassABC object network from the
00292                                 // embedded store. Note that not all of CClassABC's
00293                                 // containees are loaded in, hence the need to keep
00294                                 // the embedded store open 
00295                 iAbc = CClassABC::NewL(*iEmbeddedStore,iEmbeddedStore->Root());
00296                 }
00297         }
00298 
00299 void CMainClass::InternalizeL(RReadStream& aStream)
00300         {
00301         aStream >> iSomeData; 
00302         aStream >> iEmbeddedStoreId;
00303         }
00304 
00305 CClassABC* CMainClass::PtrAbc()
00306         {
00307         return iAbc;
00308         }
00309 
00310 void CMainClass::RemoveAbcL()
00311         {
00312                                 // Remove the CClassABC component from memory an
00313                                 // from the store. 
00314                                 // To remove the CClassABC representation from the 
00315                                 // store, just delete the stream containing the
00316                                 // embedded store which contains the representation of
00317                                 // the CClassABC object network.
00318         if (!iAbc)
00319                 return;
00320         delete iAbc;
00321         iAbc = NULL;
00322                                 // close the embedded store
00323         delete iEmbeddedStore;
00324         iEmbeddedStore = NULL;
00325                                 // cleanup the stream (this is in the top 
00326                                 // level store) containing the embedded store
00327         iChildStream.Release();
00328                                 // delete the stream from the top level store ...
00329         iStore.DeleteL(iEmbeddedStoreId);
00330                                 // ... and commit these changes to the top
00331                                 // level store
00332         iStore.CommitL();                       
00333         iEmbeddedStoreId = KNullStreamId;
00334         }
00335 
00336 //***************************************************************
00337 //
00338 // CClassABC Implementation
00339 //
00340 //***************************************************************
00341 CClassABC::CClassABC(CStreamStore& aStore)
00342         : iStore(aStore)
00343         {}
00344 
00345 CClassABC::CClassABC(CStreamStore& aStore,TStreamId anId)
00346         : iStore(aStore), iId(anId)
00347         {}
00348 
00349 CClassABC* CClassABC::NewLC(CStreamStore& aStore)
00350         {
00351         CClassABC* self = new (ELeave) CClassABC(aStore);
00352         CleanupStack::PushL(self);
00353         self->ConstructL();
00354         return self;
00355         }
00356 
00357 CClassABC* CClassABC::NewL(CStreamStore& aStore)
00358         {
00359         CClassABC* self = CClassABC::NewLC(aStore);
00360         CleanupStack::Pop();
00361         return self;
00362         }
00363 
00364 CClassABC* CClassABC::NewLC(CStreamStore& aStore, TStreamId anId)
00365         {                                                                                                                       
00366         CClassABC* self = new (ELeave) CClassABC(aStore,anId);
00367         CleanupStack::PushL(self);
00368         self->RestoreL();
00369         return self;
00370         }
00371 
00372 CClassABC* CClassABC::NewL(CStreamStore& aStore, TStreamId anId)
00373         {
00374         CClassABC* self = CClassABC::NewLC(aStore,anId);
00375         CleanupStack::Pop();
00376         return self;
00377         }
00378                 
00379 void CClassABC::ConstructL()
00380         {
00381         iA = CClassA::NewL();
00382         iB = CClassB::NewL();
00383         iC = CClassC::NewL();
00384         }
00385 
00386 void CClassABC::ConstructAL(const TDesC& aData,TInt anInt,TUint aUint)
00387         {
00388         iA->iVarBuf    = aData.AllocL();        
00389         iA->iIntValue  = anInt;
00390         iA->iUintValue = aUint;
00391         }
00392                         
00393 void CClassABC::ConstructB(const TDesC& aData,TInt anInt,TUint aUint,TReal aReal)
00394         {
00395         iB->iFixBuf    = aData;
00396         iB->iIntValue  = anInt; 
00397         iB->iUintValue = aUint;
00398         iB->iRealValue = aReal;
00399         }
00400                         
00401 void CClassABC::ConstructC(const TDesC& aData)
00402         {
00403         iC->iFixBuf = aData;
00404         }
00405 
00406 
00407                         // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
00408                         // Destructor deletes the CClassB object only if it is in memory.
00409                         // The IsPtr() function can be used to determine whether the 
00410                         // CClassB object is memory. 
00411                         // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
00412 CClassABC::~CClassABC()
00413         {
00414         delete iA;
00415         delete iC;
00416         if (iB.IsPtr())
00417                 delete iB.AsPtr(); // can also "delete iB;" makes implicit call to "operator T*()"
00418         }
00419 
00420         
00421                                 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
00422                                 // Stores the CClassB object in its own stream and
00423                                 // then stores:
00424                                 //              the CClassA object,
00425                                 //              the streamid of the CClassB object,
00426                                 //              the CClassC object
00427                                 // in a separate stream.
00428                                 // Returns the streamid of this last stream
00429                                 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
00430 TStreamId CClassABC::StoreL()
00431         {
00432                                 // Construct the output stream which is intended
00433                                 // to hold the CClassB object only.
00434         RStoreWriteStream outstream;
00435         TStreamId idForB = outstream.CreateLC(iStore);
00436                                 // Stream out the CClassB object.
00437                                 // Note that the right hand side returns a reference
00438                                 // to CClassB
00439         outstream  << *iB; 
00440                                 // Commit changes to the stream
00441         outstream.CommitL();
00442                                 // Cleanup the stream object
00443         CleanupStack::PopAndDestroy();  
00444                                 // Now construct the output stream which is intended
00445                                 // to hold:
00446                                 //              the CClassA object,
00447                                 //              the streamid of the CClassB object,
00448                                 //              the CClassC object
00449         TStreamId id = outstream.CreateLC(iStore);
00450                                 // Write out the CClassA object.
00451         outstream  << *iA; 
00452                                 // Write out the stream id of the CClassB object
00453         outstream  << idForB;
00454                                 // Write out the CClassC object.
00455         outstream  << *iC;
00456                                 // Commit changes to the stream
00457         outstream.CommitL();
00458                                 // Cleanup the stream object,
00459         CleanupStack::PopAndDestroy();  
00460                                 // Return this stream id
00461         return id;
00462         }
00463 
00464                                 
00465 const CClassA* CClassABC::PtrA()
00466         {
00467         return iA;      // Return a pointer to the contained CClassA object
00468         }
00469 
00470                                 
00471 const CClassB* CClassABC::PtrBL()
00472         {
00473         if (iB.IsId())          // If the contained CClassB object is not in memory, it must
00474                 RestoreBL();    // be loaded in before a pointer can be returned to the caller
00475         return iB.AsPtr();
00476         }
00477 
00478                                 
00479 const CClassC* CClassABC::PtrC()
00480         {
00481         return iC;      //Returns a pointer to the contained CClassC object
00482         }
00483                                 
00484                                 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
00485                                 // Restores the CClassA and CClassC objects and the streamid for the
00486                                 // CClassB object.
00487                                 // The swizzle for the CClassB object is constructed from the
00488                                 // streamid but the CClassB object itself is NOT restored at 
00489                                 // this time.
00490                                 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
00491 void CClassABC::RestoreL()
00492         {
00493                                 // Construct the input stream.
00494         RStoreReadStream instream;
00495         instream.OpenLC(iStore,iId);
00496                                 // Construct a CClassA object and restore from the stream
00497         iA = CClassA::NewL();
00498         instream >> *iA;
00499                                 // Construct the swizzle for the CClassB object. This
00500                                 // stream contains the id of the stream which 
00501                                 // actually contains the full CClassB object. The loading of
00502                                 // the CClassB object into memory is deferred until later.
00503                                 // The resulting swizzle represents the CClassB object as 
00504                                 // a streamid 
00505         instream >> iB;
00506                                 // Construct a CClassC object and restrore from the stream
00507         iC = CClassC::NewL();
00508         instream >> *iC;
00509                                 // Cleanup the stream object
00510         CleanupStack::PopAndDestroy();
00511         }
00512 
00513 
00514                                 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
00515                                 // Loads the CClassB object into memory and changes the swizzle's
00516                                 // representation from "streamid" to "pointer".
00517                                 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
00518 void CClassABC::RestoreBL()
00519         {
00520                                 // Construct the input stream. Assumes we have the correct store (this
00521                                 // emphasizes the need to ensure that the store remain open)
00522         RStoreReadStream instream;
00523         instream.OpenLC(iStore,iB.AsId());
00524                                 // Construct a CClassB object and restore from the stream. The 
00525                                 // assignment: CClassB* to TSwizzle<CClassB> changes the
00526                                 // swizzle's representation of the CClassB object 
00527                                 // from "streamid" to "pointer"
00528         CClassB* ptrB = CClassB::NewLC();
00529         instream >> *ptrB;
00530         CleanupStack::Pop();
00531         iB = ptrB;
00532                                 // Cleanup the stream object                    
00533         CleanupStack::PopAndDestroy();
00534         }       
00535 
00536 //***************************************************************
00537 //
00538 // CClassA Implementation
00539 //
00540 //***************************************************************
00541 CClassA* CClassA::NewL()
00542         {
00543         CClassA* self = CClassA::NewLC();
00544         CleanupStack::Pop();
00545         return self;
00546         }
00547 
00548 CClassA* CClassA::NewLC()
00549         {
00550         CClassA* self = new (ELeave) CClassA;
00551         CleanupStack::PushL(self);
00552         return self;
00553         }
00554 
00555 CClassA::~CClassA()
00556         {
00557         delete iVarBuf;
00558         }
00559 
00560 void CClassA::ExternalizeL(RWriteStream& aStream) const
00561         {
00562         aStream.WriteInt32L(iVarBuf->Des().MaxLength());
00563         aStream << *iVarBuf;
00564         aStream.WriteInt32L(iIntValue);
00565         aStream.WriteUint32L(iUintValue);
00566         }  
00567  
00568 void CClassA::InternalizeL(RReadStream& aStream)
00569         {
00570         TInt maxlen;
00571         maxlen     = aStream.ReadInt32L();
00572         iVarBuf    = HBufC::NewL(aStream,maxlen);
00573         iIntValue  = aStream.ReadInt32L();
00574         iUintValue = aStream.ReadUint32L();
00575         }  
00576                 
00577 
00578 //***************************************************************
00579 //
00580 // CClassB Implementation
00581 //
00582 //***************************************************************
00583 CClassB* CClassB::NewLC()
00584         {
00585         CClassB* self = new (ELeave) CClassB;
00586         CleanupStack::PushL(self);
00587         return self;
00588         }
00589 
00590 CClassB* CClassB::NewL()        
00591         {
00592         CClassB* self = CClassB::NewLC();
00593         CleanupStack::Pop();    
00594         return self;
00595         }
00596 
00597 void CClassB::ExternalizeL(RWriteStream& aStream) const
00598         {
00599         aStream << iFixBuf;
00600         aStream.WriteInt32L(iIntValue);
00601         aStream.WriteUint32L(iUintValue);
00602         aStream.WriteReal64L(iRealValue);
00603         }  
00604  
00605 void CClassB::InternalizeL(RReadStream& aStream)
00606         {
00607         aStream >> iFixBuf;
00608         iIntValue  = aStream.ReadInt32L();
00609         iUintValue = aStream.ReadUint32L();
00610         iRealValue = aStream.ReadReal64L();
00611         }  
00612 
00613 //***************************************************************
00614 //
00615 // CClassC Implementation
00616 //
00617 //***************************************************************
00618 CClassC* CClassC::NewL()
00619         {
00620         CClassC* self = CClassC::NewLC();
00621         CleanupStack::Pop();
00622         return self;
00623         }
00624 
00625 CClassC* CClassC::NewLC()
00626         {
00627         CClassC* self = new (ELeave) CClassC;
00628         CleanupStack::PushL(self);
00629         return self;
00630         }
00631 
00632 void CClassC::ExternalizeL(RWriteStream& aStream) const
00633         {
00634         aStream << iFixBuf;
00635         }  
00636  
00637 void CClassC::InternalizeL(RReadStream& aStream)
00638         {
00639         aStream >> iFixBuf;
00640         }  

Generated by  doxygen 1.6.2