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