00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "CommonStreamStore.h"
00020 #include <s32file.h>
00021
00022
00023
00024
00025
00026 _LIT(KTxtNewLine,"\n");
00027 _LIT(KFormatType1,"\n%d");
00028 _LIT(KFormatType2,"\n%S");
00029 _LIT(KFormatType3,"\n%u");
00030 _LIT(KFormatType4,"\n%f");
00031
00032
00033
00034 static void doMakeAndExternalizeL(const TDesC& aName);
00035
00036
00037
00038 static void doDeferredLoadingL(const TDesC& aName);
00039
00040
00041 class CCompound;
00042 static void doShowAll(const TDesC& aHeading,const CCompound& aCompound);
00043
00044
00045
00046 class CClassA;
00047 static void doShowComponent(const TDesC& aHeading,const CClassA& aComponent);
00048
00049
00050
00051 class CClassB;
00052 static void doShowComponent(const TDesC& aHeading,const CClassB& aComponent);
00053
00054
00055
00056 class CClassC;
00057 static void doShowComponent(const TDesC& aHeading,const CClassC& aComponent);
00058
00059
00060 class CClassA
00061 {
00062 public :
00063 void ExternalizeL(RWriteStream& aStream) const;
00064 void InternalizeL(RReadStream& aStream);
00065 TStreamId StoreL(CStreamStore& aStore) const;
00066 void RestoreL(CStreamStore& aStore, TStreamId anId);
00067 public :
00068 TBuf<32> iBufferA;
00069 TInt iXA;
00070 TUint iYA;
00071 };
00072
00073
00074 class CClassB
00075 {
00076 public :
00077 void ExternalizeL(RWriteStream& aStream) const;
00078 void InternalizeL(RReadStream& aStream);
00079 TStreamId StoreL(CStreamStore& aStore) const;
00080 void RestoreL(CStreamStore& aStore, TStreamId anId);
00081 public :
00082 TBuf<32> iBufferB;
00083 };
00084
00085
00086 class CClassC
00087 {
00088 public :
00089 void ExternalizeL(RWriteStream& aStream) const;
00090 void InternalizeL(RReadStream& aStream);
00091 TStreamId StoreL(CStreamStore& aStore) const;
00092 void RestoreL(CStreamStore& aStore, TStreamId anId);
00093 public :
00094 TReal iZC;
00095 };
00096
00097
00098 class CCompound : public CBase
00099 {
00100 public :
00101 static CCompound* NewLC(CStreamStore& aStore);
00102 static CCompound* NewLC(CStreamStore& aStore,TStreamId anId);
00103 public :
00104 CCompound(CStreamStore& aStore);
00105 CCompound(CStreamStore& aStore,TStreamId anId);
00106 ~CCompound();
00107 void InternalizeL(RReadStream& aStream);
00108 void ExternalizeL(RWriteStream& aStream) const;
00109 TStreamId StoreL() const;
00110 void RestoreL();
00111 void StoreComponentsL(CStoreMap& aMap) const;
00112 void DisplayAL(const TDesC& aCommentary);
00113 private:
00114 void ConstructL();
00115 public :
00116 TSwizzle<CClassA> iA;
00117 TSwizzle<CClassB> iB;
00118 TSwizzle<CClassC> iC;
00119 CStreamStore& iStore;
00120 TStreamId iId;
00121 };
00122
00123
00124
00125 _LIT(KFullNameOfFileStore,"\\epoc32ex\\data\\StoreMapUse.dat");
00126
00127
00128 static void doExampleL()
00129 {
00130
00131 fsSession.MkDirAll(KFullNameOfFileStore);
00132
00133
00134 doMakeAndExternalizeL(KFullNameOfFileStore);
00135
00136
00137 doDeferredLoadingL(KFullNameOfFileStore);
00138 }
00139
00140 static void doMakeAndExternalizeL(const TDesC& aName)
00141 {
00142 TParse filestorename;
00143 fsSession.Parse(aName,filestorename);
00144
00145
00146 CFileStore* store = CPermanentFileStore::ReplaceLC(fsSession,filestorename.FullName(),EFileWrite);
00147
00148
00149 store->SetTypeL(KPermanentFileStoreLayoutUid);
00150
00151
00152 CCompound* thecompound = CCompound::NewLC(*store);
00153
00154
00155
00156
00157 _LIT(KTxtClassAText,"CClassA text");
00158 _LIT(KTxtClassBText,"CClassB text");
00159 thecompound->iA.AsPtr()->iBufferA = KTxtClassAText;
00160 thecompound->iA.AsPtr()->iXA = -1;
00161 thecompound->iA->iYA = 2;
00162 thecompound->iB.AsPtr()->iBufferB = KTxtClassBText;
00163 thecompound->iC.AsPtr()->iZC = 3.456;
00164
00165
00166
00167 _LIT(KTxtInitialContent,"... Initial content of CCompound");
00168 doShowAll(KTxtInitialContent,*thecompound);
00169
00170
00171
00172 TStreamId id = thecompound->StoreL();
00173
00174
00175 store->SetRootL(id);
00176
00177
00178 store->CommitL();
00179
00180
00181
00182
00183
00184
00185 CleanupStack::PopAndDestroy(2);
00186 }
00187
00188 static void doDeferredLoadingL(const TDesC& aName)
00189 {
00190 TParse filestorename;
00191 fsSession.Parse(aName,filestorename);
00192
00193
00194 CFileStore* store = CPermanentFileStore::OpenLC(fsSession,filestorename.FullName(),EFileRead);
00195
00196
00197
00198
00199 CCompound* thecompound = CCompound::NewLC(*store,store->Root());
00200
00201
00202
00203
00204 _LIT(KTxtFirstDisplay," (first display)");
00205 thecompound->DisplayAL(KTxtFirstDisplay);
00206
00207
00208
00209 _LIT(KTxtSecondDisplay," (second display)");
00210 thecompound->DisplayAL(KTxtSecondDisplay);
00211
00212 console->Printf(KTxtNewLine);
00213
00214
00215
00216
00217
00218
00219 CleanupStack::PopAndDestroy(2);
00220 }
00221
00222 _LIT(KTxtComponentA,"... Component A");
00223 _LIT(KTxtComponentB,"... Component B");
00224 _LIT(KTxtComponentC,"... Component C");
00225 _LIT(KTxtPressKey,"\n (press any key to continue)");
00226
00227 static void doShowAll(const TDesC& aHeading,const CCompound& aCompound)
00228 {
00229 console->Printf(KTxtNewLine);
00230 console->Printf(aHeading);
00231 if (aCompound.iA.IsPtr())
00232 doShowComponent(KTxtComponentA,*aCompound.iA.AsPtr());
00233 if (aCompound.iB.IsPtr())
00234 doShowComponent(KTxtComponentB,*aCompound.iB.AsPtr());
00235 if (aCompound.iB.IsPtr())
00236 doShowComponent(KTxtComponentC,*aCompound.iC.AsPtr());
00237 console->Printf(KTxtPressKey);
00238 console->Getch();
00239 }
00240
00241 static void doShowComponent(const TDesC& aHeading,const CClassA& aComponent)
00242 {
00243 console->Printf(KTxtNewLine);
00244 console->Printf(aHeading);
00245 console->Printf(KFormatType2,&aComponent.iBufferA);
00246 console->Printf(KFormatType1,aComponent.iXA);
00247 console->Printf(KFormatType3,aComponent.iYA);
00248 }
00249
00250 static void doShowComponent(const TDesC& aHeading,const CClassB& aComponent)
00251 {
00252 console->Printf(KTxtNewLine);
00253 console->Printf(aHeading);
00254 console->Printf(KFormatType2,&aComponent.iBufferB);
00255 }
00256
00257 static void doShowComponent(const TDesC& aHeading,const CClassC& aComponent)
00258 {
00259 console->Printf(KTxtNewLine);
00260 console->Printf(aHeading);
00261 console->Printf(KFormatType4,aComponent.iZC);
00262 }
00263
00264
00265
00266 CCompound::CCompound(CStreamStore& aStore)
00267 : iStore(aStore)
00268 {}
00269
00270 CCompound::CCompound(CStreamStore& aStore,TStreamId anId)
00271 : iStore(aStore), iId(anId)
00272 {}
00273
00274
00275
00276
00277 CCompound* CCompound::NewLC(CStreamStore& aStore)
00278 {
00279 CCompound* self=new (ELeave) CCompound(aStore);
00280 CleanupStack::PushL(self);
00281 self->ConstructL();
00282 return self;
00283 }
00284
00285
00286
00287
00288
00289
00290 CCompound* CCompound::NewLC(CStreamStore& aStore,TStreamId anId)
00291 {
00292 CCompound* self=new (ELeave) CCompound(aStore,anId);
00293 CleanupStack::PushL(self);
00294 self->RestoreL();
00295 return self;
00296 }
00297
00298
00299
00300
00301 void CCompound::ConstructL()
00302 {
00303 iA = new (ELeave) CClassA;
00304 iB = new (ELeave) CClassB;
00305 iC = new (ELeave) CClassC;
00306 }
00307
00308
00309
00310 CCompound::~CCompound()
00311 {
00312 if (iA.IsPtr())
00313 delete iA.AsPtr();
00314 if (iB.IsPtr())
00315 delete iB.AsPtr();
00316 if (iC.IsPtr())
00317 delete iC.AsPtr();
00318 }
00319
00320 void CCompound::RestoreL()
00321 {
00322 RStoreReadStream stream;
00323 stream.OpenLC(iStore,iId);
00324 InternalizeL(stream);
00325 CleanupStack::PopAndDestroy();
00326 }
00327
00328 void CCompound::InternalizeL(RReadStream& aStream)
00329 {
00330 aStream >> iA;
00331 aStream >> iB;
00332 aStream >> iC;
00333 }
00334
00335
00336
00337
00338 void CCompound::DisplayAL(const TDesC& aCommentary)
00339 {
00340 if (iA.IsId())
00341 {
00342 CClassA* ptrA = new (ELeave) CClassA;
00343 CleanupStack::PushL(ptrA);
00344 ptrA->RestoreL(iStore,iA.AsId());
00345 iA = ptrA;
00346 CleanupStack::Pop();
00347 }
00348 _LIT(KTxtRestoredComponent,"... Displaying restored component A");
00349 TBuf<96> heading(KTxtRestoredComponent);
00350 heading.Append(aCommentary);
00351 doShowComponent(heading,*iA.AsPtr());
00352 }
00353
00354
00355
00356 void CCompound::ExternalizeL(RWriteStream& aStream) const
00357 {
00358 aStream << iA;
00359 aStream << iB;
00360 aStream << iC;
00361 }
00362
00363 TStreamId CCompound::StoreL() const
00364 {
00365 CStoreMap* map=CStoreMap::NewLC(iStore);
00366 StoreComponentsL(*map);
00367
00368 RStoreWriteStream stream(*map);
00369 TStreamId id=stream.CreateLC(iStore);
00370 ExternalizeL(stream);
00371 stream.CommitL();
00372
00373 map->Reset();
00374 CleanupStack::PopAndDestroy(2);
00375 return id;
00376 }
00377
00378 void CCompound::StoreComponentsL(CStoreMap& aMap) const
00379 {
00380 TStreamId id;
00381
00382 if (iA)
00383 {
00384 id = iA->StoreL(iStore);
00385 aMap.BindL(iA,id);
00386 }
00387
00388 if (iB)
00389 {
00390 id = iB->StoreL(iStore);
00391 aMap.BindL(iB,id);
00392 }
00393
00394 if (iC)
00395 {
00396 id = iC->StoreL(iStore);
00397 aMap.BindL(iC,id);
00398 }
00399 }
00400
00401
00402
00403
00404
00405
00406 void CClassA::InternalizeL(RReadStream& aStream)
00407 {
00408 aStream >> iBufferA;
00409 iXA = aStream.ReadInt32L();
00410 iYA = aStream.ReadUint32L();
00411 }
00412
00413
00414 void CClassA::ExternalizeL(RWriteStream& aStream) const
00415 {
00416 aStream << iBufferA;
00417 aStream.WriteInt32L(iXA);
00418 aStream.WriteUint32L(iYA);
00419 }
00420
00421 TStreamId CClassA::StoreL(CStreamStore& aStore) const
00422 {
00423 RStoreWriteStream stream;
00424 TStreamId id=stream.CreateLC(aStore);
00425 ExternalizeL(stream);
00426 stream.CommitL();
00427 CleanupStack::PopAndDestroy();
00428 return id;
00429 }
00430
00431 void CClassA::RestoreL(CStreamStore& aStore, TStreamId anId)
00432 {
00433 RStoreReadStream stream;
00434 stream.OpenLC(aStore,anId);
00435 InternalizeL(stream);
00436 CleanupStack::PopAndDestroy();
00437 }
00438
00439
00440
00441
00442
00443
00444 void CClassB::InternalizeL(RReadStream& aStream)
00445 {
00446 aStream >> iBufferB;
00447 }
00448
00449
00450 void CClassB::ExternalizeL(RWriteStream& aStream) const
00451 {
00452 aStream << iBufferB;
00453 }
00454
00455 TStreamId CClassB::StoreL(CStreamStore& aStore) const
00456 {
00457 RStoreWriteStream stream;
00458 TStreamId id=stream.CreateLC(aStore);
00459 ExternalizeL(stream);
00460 stream.CommitL();
00461 CleanupStack::PopAndDestroy();
00462 return id;
00463 }
00464
00465 void CClassB::RestoreL(CStreamStore& aStore, TStreamId anId)
00466 {
00467 RStoreReadStream stream;
00468 stream.OpenLC(aStore,anId);
00469 InternalizeL(stream);
00470 CleanupStack::PopAndDestroy();
00471 }
00472
00473
00474
00475
00476
00477
00478
00479 void CClassC::ExternalizeL(RWriteStream& aStream) const
00480 {
00481 aStream.WriteReal64L(iZC);
00482 }
00483
00484
00485 void CClassC::InternalizeL(RReadStream& aStream)
00486 {
00487 iZC = aStream.ReadReal64L();
00488 }
00489
00490 TStreamId CClassC::StoreL(CStreamStore& aStore) const
00491 {
00492 RStoreWriteStream stream;
00493 TStreamId id=stream.CreateLC(aStore);
00494 ExternalizeL(stream);
00495 stream.CommitL();
00496 CleanupStack::PopAndDestroy();
00497 return id;
00498 }
00499
00500
00501 void CClassC::RestoreL(CStreamStore& aStore, TStreamId anId)
00502 {
00503 RStoreReadStream stream;
00504 stream.OpenLC(aStore,anId);
00505 InternalizeL(stream);
00506 CleanupStack::PopAndDestroy();
00507 }
00508
00509
00510
00511
00512