You Are Here:

Community: Blogs

Paul Todd's Forum Nokia Blog

Serialize data

Paul.Todd | 24 May, 2007 00:21

I saw an intersting post today about what is the quickest way to serialize a structure.

To do this the proper way there are three alternatives:

Write out the structure member by member using the RWriteStream classes specialized for files - RFileWriteStream
Read them back with the relevant RReadStream classes - RFileReadStream

Write InternalizeL and ExternalizeL functions to serialize the data.

Thirdly there is actually however a much simpler solution which generates much less problems, using the TPckgC/TPckg classes.

These class wrap a struct into a TPtr8 which make make it much easier to read and write

Structure:
struct TTestStruct
    {
    TInt         iInt;
    TBuf<32>    iString;
    };

To write:
const TPckgC<TTestStruct> pkgOut(tmp);
User::LeaveIfError(file.Write(pkgOut));

To read:

TTestStruct nw;
TPckg<TTestStruct> pkgIn(nw);
User::LeaveIfError(file.Read(0, pkgIn));

OR

TPckgBuf<TTestStruct> pkgIn;
User::LeaveIfError(file.Read(0, pkgIn));
.... do something with pkgIn().iString etc.

I was going to point you folks to the descriptors blog on blogspot but strangly enough I could'nt find anything there about it.

Maybe Jo will add a section for these useful classes (TPckg, TPckgC and TPckgBuf)?

RSSComments

Re: Serialize data

TwmD | 31/05/2007, 00:15

These classes are used for transfering data structures over process boundaries, typically client/server.

I'm thinking that you need to be careful when externalising to disk since the packing of the structure could be compiler specific and so you could end up with different data files on the emulator to the device which could be difficult to debug.

Re: Serialize data

Paul.Todd | 01/06/2007, 18:10

Paul.Todd True, but then again you run up against the same thing when using gcce and arm on symbian servers.

i.e. One server is built in GCCE and one in ARM.

Re: Serialize data

TwmD | 02/06/2007, 03:48

I would expect sizeof(structure) and padding to be the same accross EABI compliant compilers of which codewarrior is not.

Re: Serialize data

TwmD | 05/06/2007, 02:35

Ah, but GCCE and ARM compilers use the EABI binary interface. i.e a structure has to be padded in the same way to work accross binary boundaries - which means copying by sizeof() should be fine.
Codewarrior uses a different binary interface to EABI.

If you write out several TTestStruct after each other then you might get different padding after each or indeed different padding between each member
You must login to post comments. Login
 

Rate This

 
 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia 
RDF Facets: qdcZdescriptionQSxThoughtsE20onE20makingE20theE20mobileE20eE78perienceE20uniE71ueE20doctordwarfE20E7cE2009E20MarchE2cE202007E2011E3a40E20AE20colleagueE20ofE20mineE20latelyE20askedE20meE20whatE20wouldE20beE20theE20mostE20importantE20SymbianE20OSE20CE2bE2bE20IE44E45E20featuresE20forE20supportingE20theE20agileE20softwareE20developmentE2eE20WhileE20agileE20processesE20areE20moreE20aboutE20peopleE20andE20interactionE2cE20thanE20aboutE20theE20toolsE2cE20aE20decentE20toolE20supportE20certainlyE20makesE20thingsE20easierE2eE20HereE20isE20aE20listE20ofE20thingsE20IE20wouldE20valueE20inE20theE20agileE2dawareE20CE2bE2bE20IE44E45E20inE20theE20orderE20ofE20decreasingE20priorityE2eE201E2eE20CommandE2dlineE20repeatabilityE20AgilE2eE2eE2eX qdcZidentifierQSxhttpE3aE2fE2fblogsE2eforumE2enokiaE2ecomE2fblogE2fartemE2dmarchenkosE2dforumE2dnokiaE2dblogE2f2007E2f03E2f09E2fagileE2dawareE2dsymbianE2dcE2dideE2fpageE2f2X qdcZpublisherQUxhttpE3aE2fE2fswE2enokiaE2ecomE2fidE2fc764fd1cE2d8b06E2d499aE2d9a6aE2d17c3903d5a65E2fforumE5fnokiaE5fcrawlerE5fagentX qdcZtitleQSxArtemE20MarchenkoE27sE20ForumE20NokiaE20BlogE20E7cE20AgileE2dawareE20SymbianE20CE2bE2bE20IE44E45X qdcZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qdcZtypeQUqfntypeZBlogContentQ qdcZtypeQUqfntypeZBlogE45ntryQ qdcZtypeQUqfntypeZCommunityContentQ qdcZtypeQUqfntypeZE52esourceQ qdcZtypeQUqfntypeZWebpageQ qdcZtypeQUqmarsZManagedE52esourceQ qdcZtypeQUqwebZInformationE52esourceQ qdcZtypeQUqwebZPageQ qdcZtypeQUqwebZE52esourceQ qdcZtypeQUqrdfsZE52esourceQ qrssZdescriptionQSxThoughtsE20onE20makingE20theE20mobileE20eE78perienceE20uniE71ueE20doctordwarfE20E7cE2009E20MarchE2cE202007E2011E3a40E20AE20colleagueE20ofE20mineE20latelyE20askedE20meE20whatE20wouldE20beE20theE20mostE20importantE20SymbianE20OSE20CE2bE2bE20IE44E45E20featuresE20forE20supportingE20theE20agileE20softwareE20developmentE2eE20WhileE20agileE20processesE20areE20moreE20aboutE20peopleE20andE20interactionE2cE20thanE20aboutE20theE20toolsE2cE20aE20decentE20toolE20supportE20certainlyE20makesE20thingsE20easierE2eE20HereE20isE20aE20listE20ofE20thingsE20IE20wouldE20valueE20inE20theE20agileE2dawareE20CE2bE2bE20IE44E45E20inE20theE20orderE20ofE20decreasingE20priorityE2eE201E2eE20CommandE2dlineE20repeatabilityE20AgilE2eE2eE2eX qfnZdistributionQUxhttpE3aE2fE2fblogsE2eforumE2enokiaE2ecomE2fX qfnZtopicQUqfnTopicZcppQRqdcZtypeQUqrdfsZE52esourceQRqmarsZrelevanceQNx100X qfnZtypeQUqfntypeZBlogContentQ qfnZtypeQUqfntypeZBlogE45ntryQ qfnZtypeQUqfntypeZCommunityContentQ qfnZtypeQUqfntypeZE52esourceQ qfnZtypeQUqfntypeZWebpageQ qfnZupdatedQDx2007E2d11E2d19X qfnZuserE5ftagQSxsymbianE2dcE2bE2bX qmarsZdescriptionQSxThoughtsE20onE20makingE20theE20mobileE20eE78perienceE20uniE71ueE20doctordwarfE20E7cE2009E20MarchE2cE202007E2011E3a40E20AE20colleagueE20ofE20mineE20latelyE20askedE20meE20whatE20wouldE20beE20theE20mostE20importantE20SymbianE20OSE20CE2bE2bE20IE44E45E20featuresE20forE20supportingE20theE20agileE20softwareE20developmentE2eE20WhileE20agileE20processesE20areE20moreE20aboutE20peopleE20andE20interactionE2cE20thanE20aboutE20theE20toolsE2cE20aE20decentE20toolE20supportE20certainlyE20makesE20thingsE20easierE2eE20HereE20isE20aE20listE20ofE20thingsE20IE20wouldE20valueE20inE20theE20agileE2dawareE20CE2bE2bE20IE44E45E20inE20theE20orderE20ofE20decreasingE20priorityE2eE201E2eE20CommandE2dlineE20repeatabilityE20AgilE2eE2eE2eX qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX qrdfZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qrdfZtypeQUqfntypeZBlogContentQ qrdfZtypeQUqfntypeZBlogE45ntryQ qrdfZtypeQUqfntypeZCommunityContentQ qrdfZtypeQUqfntypeZE52esourceQ qrdfZtypeQUqfntypeZWebpageQ qrdfZtypeQUqmarsZManagedE52esourceQ qrdfZtypeQUqwebZInformationE52esourceQ qrdfZtypeQUqwebZPageQ qrdfZtypeQUqwebZE52esourceQ qrdfZtypeQUqrdfsZE52esourceQ