examples/SysLibs/Stores/StreamInStore/StreamInStore.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 stream creation in a non persistent store. 
00029 The example:
00030 constructs two different types of object,
00031 writes them to a single stream contained in
00032 the in-memory store (a CBufStore),
00033 displays the contents of the objects,
00034 deletes them from memory,
00035 restores them from the store,
00036 displays the contents of the restored objects 
00037 */
00038 
00039 
00040 
00041 
00042 
00043 #include "CommonStreamStore.h"
00044 #include <s32file.h>
00045 #include <s32mem.h>
00046 
00047 //
00048 // Format types and the newline character
00049 //
00050 _LIT(KTxtNewLine,"\n");
00051 _LIT(KFormatType1,"\n%S, ");
00052 _LIT(KFormatType2,"%d, ");
00053 _LIT(KFormatType3,"%u, ");
00054 _LIT(KFormatType4,"%u  ");
00055 _LIT(KFormatType5,"%f  ");
00056 
00057                                 // Constructs an object of type CClassA and an object
00058                                 // of type CClassB, externalizes them to a
00059                                 // non-persistent store and then destroys the
00060                                 // CClassA object. 
00061 static void doMakeAndStoreL(CStreamStore& aStore,TStreamId& anId);
00062 
00063                                 // Constructs "empty" CClassA and CClassB objects and
00064                                 // restores them from the in-memory store.
00065 static void doRestoreL(CStreamStore& aStore,const TStreamId& anId);
00066 
00067                                 // Displays the contents of a CClassA object
00068 class CClassA;
00069 static void doShow(const TDesC& aHeading,const CClassA& anA);
00070 
00071                                 // Displays the contents of a CClassB object
00072 class CClassB;
00073 static void doShow(const TDesC& aHeading,const CClassB& aB);
00074 
00075                                 // definition of CClassA
00076 class CClassA : public CBase
00077         {
00078 public :
00079         static CClassA* NewLC();
00080         ~CClassA();
00081         void     SetTextL(const TDesC& aData);
00082         void     ExternalizeL(RWriteStream& aStream) const;
00083         void     InternalizeL(RReadStream& aStream);
00084 public :
00085         HBufC*   iVarBuffer;
00086         TInt     iIntValue;
00087         TUint    iUintValue;
00088         };
00089 
00090                                 // definition of CClassB
00091 class CClassB : public CBase
00092         {
00093 public :
00094         static CClassB* NewLC();
00095         void ExternalizeL(RWriteStream& aStream) const;
00096         void InternalizeL(RReadStream& aStream);
00097 public :
00098         TBuf<32> iFixBuffer;
00099         TUint    iUintValue;
00100         TInt     iIntValue;
00101         TReal    iRealValue;
00102         };
00103 
00104 //  Do the example
00105 static void doExampleL()
00106     {
00107                                 // The stream id, kept in memory and used
00108                                 // to access the (one and only stream) in the
00109                                 // non-persistent store
00110         TStreamId theId;
00111 
00112                                 // construct a CBufStore, an in-memory 
00113                                 // non-persistent store.
00114         CStreamStore* store = CBufStore::NewLC(2 /*granularity*/);
00115 
00116         doMakeAndStoreL(*store,theId);
00117         doRestoreL(*store,theId);
00118 
00119                                 // destroy the CBufStore object
00120         CleanupStack::PopAndDestroy();
00121         }
00122 
00123 static void doMakeAndStoreL(CStreamStore& aStore,TStreamId& anId)
00124         {
00125                                 // Construct an object of type CClassA and put some 
00126                                 // data into it
00127         _LIT(KTxtForClassA,"Text for CClassA");
00128         CClassA* theA = CClassA::NewLC();
00129         theA->SetTextL(KTxtForClassA);  
00130         theA->iIntValue  = -1;
00131         theA->iUintValue = 2;
00132 
00133                                 // Construct an object of type CClassB and put some 
00134                                 // data into it
00135         _LIT(KTxtForClassB,"Text for CClassB");
00136         CClassB* theB = CClassB::NewLC();
00137         theB->iFixBuffer = KTxtForClassB;
00138         theB->iIntValue  = -3;
00139         theB->iUintValue = 4; 
00140         theB->iRealValue = 5.6;
00141 
00142                                 // Show contents of the CClassA object
00143         _LIT(KTxtClassAContent,"CClassA content ...");
00144         doShow(KTxtClassAContent,*theA);
00145 
00146                                 // Show contents of the CClassB object
00147         _LIT(KTxtClassBContent,"CClassB content ...");
00148         doShow(KTxtClassBContent,*theB);                                        
00149 
00150                                 // Construct the output stream.
00151                                 // The stream id (there is only one) is kept
00152                                 // in memory.
00153         RStoreWriteStream outstream;
00154         anId = outstream.CreateLC(aStore);
00155 
00156                                 // Stream out the CClassA object first
00157         outstream  << *theA; 
00158                                 
00159                                 // Stream out the CClassB object second
00160         outstream  << *theB;
00161 
00162                                 // Equally we could have done:
00163                                 //              outstream << *theA << *theB;
00164                                 // to the same effect
00165 
00166                                 // Commit changes to the stream
00167         outstream.CommitL();
00168 
00169                                 // Cleanup the stream object.
00170                                 // Destroy the CClassB object,
00171                                 // Destroy the CClassA object,
00172         CleanupStack::PopAndDestroy(3);
00173         }
00174 
00175 static void doRestoreL(CStreamStore& aStore,const TStreamId& anId)
00176         {
00177                                 // Construct "empty" CClassA and CClassB objects
00178         CClassA* theA = CClassA::NewLC();
00179         CClassB* theB = CClassB::NewLC();
00180         
00181                                 // Construct and open the input stream.
00182                                 // We want to access the one and only 
00183                                 // stream from the in-memory store.
00184         RStoreReadStream instream;
00185         instream.OpenLC(aStore,anId);
00186 
00187                                 // Stream in the CClassA object first and 
00188                                 // then stream in the CClassB object. This is the order
00189                                 // in which the objects were streamed out.
00190                                 //
00191                                 // NB the order in which the objects are streamed OUT to 
00192                                 // a single stream is arbitrary BUT, whatever the order 
00193                                 // chosen, the objects must be streamed in, in the SAME order
00194                                 // 
00195                                 // In this example, streaming in has assignment semantics.
00196 
00197         instream >> *theA;
00198         instream >> *theB; 
00199 
00200                                 // Equally we could have done:
00201                                 //              outstream >> *theA >> *theB;
00202                                 // to the same effect
00203         
00204                                 // Cleanup the stream object
00205         CleanupStack::PopAndDestroy();
00206 
00207                                 // Show restored contents of the CClassA object
00208         _LIT(KTxtRestoredClassA,"Restored CClassA content ...");
00209         doShow(KTxtRestoredClassA,*theA);
00210 
00211                                 // Show restored contents of the CClassB object
00212         _LIT(KTxtRestoredClassB,"Restored CClassB content ...");
00213         doShow(KTxtRestoredClassB,*theB);                                       
00214 
00215                                 // Destroy the CClassB object, 
00216                                 // Destroy the CClassA object, 
00217         CleanupStack::PopAndDestroy(2);
00218         }
00219                                                                         
00220 static void doShow(const TDesC& aHeading,const CClassA& anA)
00221         {
00222         console->Printf(KTxtNewLine);
00223         console->Printf(aHeading);
00224         console->Printf(KFormatType1,anA.iVarBuffer);
00225         console->Printf(KFormatType2,anA.iIntValue);
00226         console->Printf(KFormatType4,anA.iUintValue);
00227         console->Printf(KTxtNewLine);
00228         }
00229 
00230 static void doShow(const TDesC& aHeading,const CClassB& aB)
00231         {
00232         console->Printf(KTxtNewLine);
00233         console->Printf(aHeading);
00234         console->Printf(KFormatType1,&aB.iFixBuffer);
00235         console->Printf(KFormatType2,aB.iIntValue);
00236         console->Printf(KFormatType3,aB.iUintValue);
00237         console->Printf(KFormatType5,aB.iRealValue);
00238         console->Printf(KTxtNewLine);
00239         }
00240 
00241 
00242 //***************************************************************
00243 //***************************************************************
00244 CClassA* CClassA::NewLC()
00245         {
00246         CClassA* self = new (ELeave) CClassA;
00247         CleanupStack::PushL(self);
00248         return self;
00249         }
00250         
00251 CClassA::~CClassA()
00252         {
00253         delete iVarBuffer;
00254         }
00255 
00256 void CClassA::SetTextL(const TDesC& aData)
00257         {
00258         iVarBuffer = aData.AllocL();    
00259         }
00260 
00261 void CClassA::ExternalizeL(RWriteStream& aStream) const
00262         {
00263         aStream.WriteInt32L(iVarBuffer->Des().MaxLength());
00264         aStream << *iVarBuffer;
00265         aStream.WriteInt32L(iIntValue);
00266         aStream.WriteUint32L(iUintValue);
00267         }  
00268  
00269 void CClassA::InternalizeL(RReadStream& aStream)
00270         {
00271         TInt maxlen;
00272         maxlen     = aStream.ReadInt32L();
00273         iVarBuffer = HBufC::NewL(aStream,maxlen);
00274         iIntValue  = aStream.ReadInt32L();
00275         iUintValue = aStream.ReadUint32L();
00276         }  
00277                 
00278 
00279 //***************************************************************
00280 //***************************************************************
00281 CClassB* CClassB::NewLC()
00282         {
00283         CClassB* self = new (ELeave) CClassB;
00284         CleanupStack::PushL(self);
00285         return self;
00286         }
00287 
00288 void CClassB::ExternalizeL(RWriteStream& aStream) const
00289         {
00290         aStream << iFixBuffer;
00291         aStream.WriteInt32L(iIntValue);
00292         aStream.WriteUint32L(iUintValue);
00293         aStream.WriteReal64L(iRealValue);
00294         }  
00295  
00296 void CClassB::InternalizeL(RReadStream& aStream)
00297         {
00298         aStream >> iFixBuffer;
00299         iIntValue  = aStream.ReadInt32L();
00300         iUintValue = aStream.ReadUint32L();
00301         iRealValue = aStream.ReadReal64L();
00302         }  
00303                 
00304 
00305 
00306 
00307         
00308         

Generated by  doxygen 1.6.2