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