Archived

This topic is now archived and is closed to further replies.

Serlizable

This topic is 5125 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

In Java there is a interface Serializable which if you let a class implement it then that class can be written/read to/from a file through a stream easily. Would there be a way to do this in c++. I mean how would you go about to implement the classes and the interface Serializable so that after you are done you can just inherit from the class ISerializableand then write your class to a binary file? LizardCPP

Share this post


Link to post
Share on other sites
quote:
Original post by LizardCPP
In Java there is a interface Serializable which if you let a class implement it then that class can be written/read to/from a file through a stream easily. Would there be a way to do this in c++.

No. C++ lacks the reflective capabilities to implement a suitably generic way of doing that. As a general-purpose programming language, C++ is largely a steaming pile of badly thought-out crap that you are best off steering clear of.
quote:

I mean how would you go about to implement the classes and the interface Serializable so that after you are done you can just inherit from the class ISerializableand then write your class to a binary file?

You''ll have to "do it manually".

Share this post


Link to post
Share on other sites
quote:
Original post by SabreMan
As a general-purpose programming language, C++ is largely a steaming pile of badly thought-out crap that you are best off steering clear of.


A bit feisty today are we?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by SabreMan
No. C++ lacks the reflective capabilities to implement a suitably generic way of doing that. As a general-purpose programming language, C++ is largely a steaming pile of badly thought-out crap that you are best off steering clear of.



In all fairness, those features haven''t really had a chance to GET added, just yet. The last C++ standard was released in 1998, and C++0x isn''t going to add any significantly new language features, just library ones.

Share this post


Link to post
Share on other sites

struct Streamable {
std::istream& Read(std::istream& is) {
return DoRead(is);
}
std::ostream& Write(std::ostream& os) const {
return DoWrite(os);
}
protected:
virtual std::istream& DoRead(std::istream& is) {}
virtual std::ostream& DoWrite(std::ostream& os) const {}
};

std::istream& operator>>(std::istream& is, Streamable& obj) {
return obj.Read(is);
}

std::ostream& operator<<(std::ostream& os, const Streamable& obj) {
return obj.Write(os);
}


You can work out the rest. It's pretty straight forward. Anything that wants to be Streamable just has to inherit Streamable and override DoRead and DoWrite.

One of the Conversations on Gotw might be helpful: Virtually Yours. Basically it's the Template Method Design Pattern.

Hope that all helps

Pete

[edited by - petewood on November 28, 2003 2:49:42 PM]

Share this post


Link to post
Share on other sites
You need to write serialization code for each unique type.

If you are not familar with serialization protocols, usually a header is written for each object conisting of at least an object id and a data chunk size.

COM has a set of interfaces for serialization, you could look at it for ideas (IPersist).

struct ISerializable
{
virtual read(IStream*)=0;
virtual write(IStream*)=0;

virtual ~ISerializable(){}
};

...
(A reflection mechanism will most likely be added in C++0x.)


Share this post


Link to post
Share on other sites
quote:
Original post by Magmai Kai Holmlor
A reflection mechanism will most likely be added in C++0x.

Magmai, do you remember where you read this?

I''ve read about the features they are looking to add in C++0x but I don''t remember reflection being one of them. That would be great if they did!


- Houdini

Share this post


Link to post
Share on other sites
quote:
Original post by antareus
A bit feisty today are we?

Whatever makes you think that?
quote:
Original post by Houdini
I''ve read about the features they are looking to add in C++0x but I don''t remember reflection being one of them.

Its called "eXtensible Type Information" (XTI), kind of an extension of RTTI. You can read about it here. I''d really expected to find an April 1st date on the document.

Share this post


Link to post
Share on other sites
quote:
Original post by petewood
You can work out the rest. It''s pretty straight forward.

The problem is it isn''t straightforward. Its simple enough to decide there are serialise and deserialise functions and to write the stubs, but then you have to do some thinking. The sorts of questions that get thrown up are:


  • What scheme to use for external representation.

  • How to handle composite objects.

  • What happens when an external representation is wrong for the indicated type.

  • How to handle polymorphic creation and subsequent usage of deserialised objects.


Amongst other things.

Effectively, you have to write your Meta-Object Protocol, since the wisdom of C++ is to throw away nearly all type information during the compilation process. You then have to write layers of code to reintroduce any type information you deem useful, effectively recreating your own half-assed dynamic type-system. Its amazing the number of people who will do just that without thinking twice (or maybe even once).

Share this post


Link to post
Share on other sites