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
00036
00037
00038
00039
00040
00041
00042
00043
00044 #include "WriteToEmbedded.h"
00045
00046
00047
00048
00049
00050
00051 _LIT(KFullNameOfFileStore,"\\epoc32ex\\data\\WriteToEmbedded.dat");
00052
00053
00054 LOCAL_C void doExampleL()
00055 {
00056
00057 fsSession.MkDirAll(KFullNameOfFileStore);
00058 doMakeAndStoreL(KFullNameOfFileStore);
00059 doDeleteComponentL(KFullNameOfFileStore);
00060 }
00061
00062 LOCAL_C void doMakeAndStoreL(const TDesC& aName)
00063 {
00064
00065 TParse filestorename;
00066 fsSession.Parse(aName,filestorename);
00067 CFileStore* store = CPermanentFileStore::ReplaceLC(fsSession,filestorename.FullName(),EFileWrite);
00068
00069
00070 store->SetTypeL(KPermanentFileStoreLayoutUid);
00071
00072
00073
00074
00075
00076 CMainClass* theMain = CMainClass::NewLC(*store);
00077
00078
00079
00080 TStreamId theId = theMain->StoreL();
00081
00082
00083 store->SetRootL(theId);
00084
00085
00086 store->CommitL();
00087
00088
00089
00090
00091 CleanupStack::PopAndDestroy(2);
00092 }
00093
00094 LOCAL_C void doDeleteComponentL(const TDesC& aName)
00095 {
00096
00097 TParse filestorename;
00098 fsSession.Parse(aName,filestorename);
00099 CFileStore* store = CPermanentFileStore::OpenLC(fsSession,filestorename.FullName(),EFileRead|EFileWrite);
00100
00101
00102 CMainClass* theMain = CMainClass::NewLC(*store,store->Root());
00103
00104
00105
00106 CClassABC* abc = theMain->PtrAbc();
00107
00108 _LIT(KTxtClassAContent,"CClassA content ...");
00109 _LIT(KTxtClassBContent,"CClassB content ...");
00110 _LIT(KTxtClassCContent,"CClassC content ...");
00111 _LIT(KTxtClassABCDeleted,"CClassABC component deleted");
00112
00113 if (abc)
00114 {
00115 doShow(KTxtClassAContent,*abc->PtrA());
00116 doShow(KTxtClassBContent,*abc->PtrBL());
00117 doShow(KTxtClassCContent,*abc->PtrC());
00118 }
00119 else
00120 doShow(KTxtClassABCDeleted);
00121
00122
00123
00124 theMain->RemoveAbcL();
00125
00126
00127
00128 abc = theMain->PtrAbc();
00129 if (abc)
00130 {
00131 doShow(KTxtClassAContent,*abc->PtrA());
00132 doShow(KTxtClassBContent,*abc->PtrBL());
00133 doShow(KTxtClassCContent,*abc->PtrC());
00134 }
00135 else
00136 doShow(KTxtClassABCDeleted);
00137
00138
00139
00140
00141 CleanupStack::PopAndDestroy(2);
00142 }
00143
00144 _LIT(KTxtNewLine,"\n");
00145 _LIT(KFormatType1,"\n%S, ");
00146 _LIT(KFormatType2,"%d, ");
00147 _LIT(KFormatType3,"%u ");
00148 _LIT(KFormatType4,"%u, ");
00149 _LIT(KFormatType5,"%f ");
00150
00151 LOCAL_C void doShow(const TDesC& aComment)
00152 {
00153 console->Printf(KTxtNewLine);
00154 console->Printf(aComment);
00155 console->Printf(KTxtNewLine);
00156 }
00157
00158 LOCAL_C void doShow(const TDesC& aHeading,const CClassA& anA)
00159 {
00160 console->Printf(KTxtNewLine);
00161 console->Printf(aHeading);
00162 console->Printf(KFormatType1,anA.iVarBuf);
00163 console->Printf(KFormatType2,anA.iIntValue);
00164 console->Printf(KFormatType3,anA.iUintValue);
00165 console->Printf(KTxtNewLine);
00166 }
00167
00168 LOCAL_C void doShow(const TDesC& aHeading,const CClassB& aB)
00169 {
00170 console->Printf(KTxtNewLine);
00171 console->Printf(aHeading);
00172 console->Printf(KFormatType1,&aB.iFixBuf);
00173 console->Printf(KFormatType2,aB.iIntValue);
00174 console->Printf(KFormatType4,aB.iUintValue);
00175 console->Printf(KFormatType5,aB.iRealValue);
00176 console->Printf(KTxtNewLine);
00177 }
00178
00179 LOCAL_C void doShow(const TDesC& aHeading,const CClassC& aC)
00180 {
00181 console->Printf(KTxtNewLine);
00182 console->Printf(aHeading);
00183 console->Printf(KFormatType1,&aC.iFixBuf);
00184 console->Printf(KTxtNewLine);
00185 }
00186
00187
00188
00189
00190
00191
00192 CMainClass* CMainClass::NewLC(CStreamStore& aStore)
00193 {
00194 CMainClass* self = new (ELeave) CMainClass(aStore);
00195 CleanupStack::PushL(self);
00196 self->ConstructL();
00197 return self;
00198 }
00199
00200 CMainClass* CMainClass::NewLC(CStreamStore& aStore,TStreamId anId)
00201 {
00202 CMainClass* self = new (ELeave) CMainClass(aStore,anId);
00203 CleanupStack::PushL(self);
00204 self->RestoreL();
00205 return self;
00206 }
00207
00208 CMainClass::CMainClass(CStreamStore& aStore)
00209 : iStore(aStore)
00210 {}
00211
00212 CMainClass::CMainClass(CStreamStore& aStore,TStreamId anId)
00213 : iStore(aStore), iId(anId)
00214 {}
00215
00216 _LIT(KTxtMainData,"Main data");
00217 _LIT(KTxtClassAData,"Data for the CClassA - AAAAA");
00218 _LIT(KTxtClassBData,"Data for the CClassB - BBBBB");
00219 _LIT(KTxtClassCData,"Data for the CClassC - CCCCC");
00220
00221 void CMainClass::ConstructL()
00222 {
00223 iSomeData = KTxtMainData;
00224
00225 RStoreWriteStream childStream;
00226
00227 iEmbeddedStoreId = childStream.CreateLC(iStore);
00228
00229 CPersistentStore* embeddedStore = CEmbeddedStore::NewLC(childStream);
00230
00231
00232 iAbc = CClassABC::NewL(*embeddedStore);
00233 iAbc->ConstructAL(KTxtClassAData,-1,2);
00234 iAbc->ConstructB(KTxtClassBData,-3,4,5.6);
00235 iAbc->ConstructC(KTxtClassCData);
00236
00237
00238 TStreamId idForAbc = iAbc->StoreL();
00239
00240
00241 embeddedStore->SetRootL(idForAbc);
00242
00243 embeddedStore->CommitL();
00244
00245
00246 childStream.CommitL();
00247
00248
00249 CleanupStack::PopAndDestroy(2);
00250 }
00251
00252 CMainClass::~CMainClass()
00253 {
00254 delete iEmbeddedStore;
00255 iChildStream.Release();
00256 delete iAbc;
00257 }
00258
00259 TStreamId CMainClass::StoreL()
00260 {
00261 RStoreWriteStream stream;
00262 TStreamId id = stream.CreateLC(iStore);
00263 ExternalizeL(stream);
00264 stream.CommitL();
00265 CleanupStack::PopAndDestroy();
00266 return id;
00267 }
00268
00269 void CMainClass::ExternalizeL(RWriteStream& aStream)
00270 {
00271 aStream << iSomeData;
00272 aStream << iEmbeddedStoreId;
00273 }
00274
00275 void CMainClass::RestoreL()
00276 {
00277 RStoreReadStream stream;
00278 stream.OpenLC(iStore,iId);
00279
00280 InternalizeL(stream);
00281
00282 CleanupStack::PopAndDestroy();
00283
00284
00285 if (iEmbeddedStoreId != KNullStreamId)
00286 {
00287 iChildStream.OpenL(iStore,iEmbeddedStoreId);
00288
00289
00290 iEmbeddedStore = CEmbeddedStore::FromL(iChildStream);
00291
00292
00293
00294
00295 iAbc = CClassABC::NewL(*iEmbeddedStore,iEmbeddedStore->Root());
00296 }
00297 }
00298
00299 void CMainClass::InternalizeL(RReadStream& aStream)
00300 {
00301 aStream >> iSomeData;
00302 aStream >> iEmbeddedStoreId;
00303 }
00304
00305 CClassABC* CMainClass::PtrAbc()
00306 {
00307 return iAbc;
00308 }
00309
00310 void CMainClass::RemoveAbcL()
00311 {
00312
00313
00314
00315
00316
00317
00318 if (!iAbc)
00319 return;
00320 delete iAbc;
00321 iAbc = NULL;
00322
00323 delete iEmbeddedStore;
00324 iEmbeddedStore = NULL;
00325
00326
00327 iChildStream.Release();
00328
00329 iStore.DeleteL(iEmbeddedStoreId);
00330
00331
00332 iStore.CommitL();
00333 iEmbeddedStoreId = KNullStreamId;
00334 }
00335
00336
00337
00338
00339
00340
00341 CClassABC::CClassABC(CStreamStore& aStore)
00342 : iStore(aStore)
00343 {}
00344
00345 CClassABC::CClassABC(CStreamStore& aStore,TStreamId anId)
00346 : iStore(aStore), iId(anId)
00347 {}
00348
00349 CClassABC* CClassABC::NewLC(CStreamStore& aStore)
00350 {
00351 CClassABC* self = new (ELeave) CClassABC(aStore);
00352 CleanupStack::PushL(self);
00353 self->ConstructL();
00354 return self;
00355 }
00356
00357 CClassABC* CClassABC::NewL(CStreamStore& aStore)
00358 {
00359 CClassABC* self = CClassABC::NewLC(aStore);
00360 CleanupStack::Pop();
00361 return self;
00362 }
00363
00364 CClassABC* CClassABC::NewLC(CStreamStore& aStore, TStreamId anId)
00365 {
00366 CClassABC* self = new (ELeave) CClassABC(aStore,anId);
00367 CleanupStack::PushL(self);
00368 self->RestoreL();
00369 return self;
00370 }
00371
00372 CClassABC* CClassABC::NewL(CStreamStore& aStore, TStreamId anId)
00373 {
00374 CClassABC* self = CClassABC::NewLC(aStore,anId);
00375 CleanupStack::Pop();
00376 return self;
00377 }
00378
00379 void CClassABC::ConstructL()
00380 {
00381 iA = CClassA::NewL();
00382 iB = CClassB::NewL();
00383 iC = CClassC::NewL();
00384 }
00385
00386 void CClassABC::ConstructAL(const TDesC& aData,TInt anInt,TUint aUint)
00387 {
00388 iA->iVarBuf = aData.AllocL();
00389 iA->iIntValue = anInt;
00390 iA->iUintValue = aUint;
00391 }
00392
00393 void CClassABC::ConstructB(const TDesC& aData,TInt anInt,TUint aUint,TReal aReal)
00394 {
00395 iB->iFixBuf = aData;
00396 iB->iIntValue = anInt;
00397 iB->iUintValue = aUint;
00398 iB->iRealValue = aReal;
00399 }
00400
00401 void CClassABC::ConstructC(const TDesC& aData)
00402 {
00403 iC->iFixBuf = aData;
00404 }
00405
00406
00407
00408
00409
00410
00411
00412 CClassABC::~CClassABC()
00413 {
00414 delete iA;
00415 delete iC;
00416 if (iB.IsPtr())
00417 delete iB.AsPtr();
00418 }
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430 TStreamId CClassABC::StoreL()
00431 {
00432
00433
00434 RStoreWriteStream outstream;
00435 TStreamId idForB = outstream.CreateLC(iStore);
00436
00437
00438
00439 outstream << *iB;
00440
00441 outstream.CommitL();
00442
00443 CleanupStack::PopAndDestroy();
00444
00445
00446
00447
00448
00449 TStreamId id = outstream.CreateLC(iStore);
00450
00451 outstream << *iA;
00452
00453 outstream << idForB;
00454
00455 outstream << *iC;
00456
00457 outstream.CommitL();
00458
00459 CleanupStack::PopAndDestroy();
00460
00461 return id;
00462 }
00463
00464
00465 const CClassA* CClassABC::PtrA()
00466 {
00467 return iA;
00468 }
00469
00470
00471 const CClassB* CClassABC::PtrBL()
00472 {
00473 if (iB.IsId())
00474 RestoreBL();
00475 return iB.AsPtr();
00476 }
00477
00478
00479 const CClassC* CClassABC::PtrC()
00480 {
00481 return iC;
00482 }
00483
00484
00485
00486
00487
00488
00489
00490
00491 void CClassABC::RestoreL()
00492 {
00493
00494 RStoreReadStream instream;
00495 instream.OpenLC(iStore,iId);
00496
00497 iA = CClassA::NewL();
00498 instream >> *iA;
00499
00500
00501
00502
00503
00504
00505 instream >> iB;
00506
00507 iC = CClassC::NewL();
00508 instream >> *iC;
00509
00510 CleanupStack::PopAndDestroy();
00511 }
00512
00513
00514
00515
00516
00517
00518 void CClassABC::RestoreBL()
00519 {
00520
00521
00522 RStoreReadStream instream;
00523 instream.OpenLC(iStore,iB.AsId());
00524
00525
00526
00527
00528 CClassB* ptrB = CClassB::NewLC();
00529 instream >> *ptrB;
00530 CleanupStack::Pop();
00531 iB = ptrB;
00532
00533 CleanupStack::PopAndDestroy();
00534 }
00535
00536
00537
00538
00539
00540
00541 CClassA* CClassA::NewL()
00542 {
00543 CClassA* self = CClassA::NewLC();
00544 CleanupStack::Pop();
00545 return self;
00546 }
00547
00548 CClassA* CClassA::NewLC()
00549 {
00550 CClassA* self = new (ELeave) CClassA;
00551 CleanupStack::PushL(self);
00552 return self;
00553 }
00554
00555 CClassA::~CClassA()
00556 {
00557 delete iVarBuf;
00558 }
00559
00560 void CClassA::ExternalizeL(RWriteStream& aStream) const
00561 {
00562 aStream.WriteInt32L(iVarBuf->Des().MaxLength());
00563 aStream << *iVarBuf;
00564 aStream.WriteInt32L(iIntValue);
00565 aStream.WriteUint32L(iUintValue);
00566 }
00567
00568 void CClassA::InternalizeL(RReadStream& aStream)
00569 {
00570 TInt maxlen;
00571 maxlen = aStream.ReadInt32L();
00572 iVarBuf = HBufC::NewL(aStream,maxlen);
00573 iIntValue = aStream.ReadInt32L();
00574 iUintValue = aStream.ReadUint32L();
00575 }
00576
00577
00578
00579
00580
00581
00582
00583 CClassB* CClassB::NewLC()
00584 {
00585 CClassB* self = new (ELeave) CClassB;
00586 CleanupStack::PushL(self);
00587 return self;
00588 }
00589
00590 CClassB* CClassB::NewL()
00591 {
00592 CClassB* self = CClassB::NewLC();
00593 CleanupStack::Pop();
00594 return self;
00595 }
00596
00597 void CClassB::ExternalizeL(RWriteStream& aStream) const
00598 {
00599 aStream << iFixBuf;
00600 aStream.WriteInt32L(iIntValue);
00601 aStream.WriteUint32L(iUintValue);
00602 aStream.WriteReal64L(iRealValue);
00603 }
00604
00605 void CClassB::InternalizeL(RReadStream& aStream)
00606 {
00607 aStream >> iFixBuf;
00608 iIntValue = aStream.ReadInt32L();
00609 iUintValue = aStream.ReadUint32L();
00610 iRealValue = aStream.ReadReal64L();
00611 }
00612
00613
00614
00615
00616
00617
00618 CClassC* CClassC::NewL()
00619 {
00620 CClassC* self = CClassC::NewLC();
00621 CleanupStack::Pop();
00622 return self;
00623 }
00624
00625 CClassC* CClassC::NewLC()
00626 {
00627 CClassC* self = new (ELeave) CClassC;
00628 CleanupStack::PushL(self);
00629 return self;
00630 }
00631
00632 void CClassC::ExternalizeL(RWriteStream& aStream) const
00633 {
00634 aStream << iFixBuf;
00635 }
00636
00637 void CClassC::InternalizeL(RReadStream& aStream)
00638 {
00639 aStream >> iFixBuf;
00640 }