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

Generated by  doxygen 1.6.2