# C++ Serialization Woes

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

## Recommended Posts

##### Share on other sites
Well the serialastion can be achieved in a pretty standard way by overloading the << and >> operators to save and restore object state

//.h#pragma once#include <iostream>#include <iomanip>	enum BoardAlignment	{		BA_FIGHTING=-1,		//Position could go either way		BA_NEUTRAL=0,		//Position owned by no one		BA_LEFT_PLAYER=1,	//Player on the left owns position		BA_RIGHT_PLAYER=2,	//Player on the right owns position		BA_BOTH=3	//Player on the right owns position	};// For classes you would need to declare these "friend" in the class definition.//... this would allow the functions to get at data members as if they are part //of the class despite being seperate.	std::ostream& operator << (std::ostream& out, BoardAlignment& posType);	std::istream& operator >> (std::istream& in, BoardAlignment& posType);// .cpp#include "..\include\BoardAlignment.h"	std::ostream& operator << (std::ostream& out, BoardAlignment& boardAlignment)	{		return out << (int)boardAlignment << " ";	}	std::istream& operator >> (std::istream& in, BoardAlignment& boardAlignment)	{		int i = 0;		in >> i;		boardAlignment = (BoardAlignment)i;		return in;	}

I realise that's probably not anything that you do not already know from the way you are talking - but hey ho perhaps some one else will be able to reply with something more useful, plus it might be useful to any newbies reading the topic.

Useful search terms could be along the lines of "persistant objects" as well as the whole serialisation thing.
Here's what [google] just came up with for me:
http://s11n.net/

[Edited by - paulecoyote on June 29, 2005 4:54:14 AM]

##### Share on other sites
If you're at all willing to entertain the notion of doing it yourself, here is the best reference I can recommend for you.

##### Share on other sites
Or check out Boost::serialization

##### Share on other sites
Quote:
 Or check out Boost::serialization

Quote:
 So for example there are the boost libraries ... One of those is a serialization library.

ffx; read the post next time.

OP - You're probably going to have to write it yourself. For saving graphs and trees, there is a simple technigue. Iterate over the object, and assign each one an integer ID. Iterate again, writing them to file, but wherever you have to record a link, record the target's ID instead of the pointer. When you load, you read in every object, then iterate over them replacing ID's with addresses. It takes an intermediatary data structure, but it works.
For STL containers, it's trivial. There are no standard graph structures, so you don't have to worry about links. Just save everything in the container in iteration order, even for tree based structures, and the container can easily be rebuilt when loaded.

##### Share on other sites
I was under the impression that RTTI was a project level setting that didn't break compatibility, and that for example having it disabled for your main application, and enabled inside a dll plugin would still be compatible. Is that not the case?

##### Share on other sites
Quote:
 Original post by DrEvilI was under the impression that RTTI was a project level setting that didn't break compatibility, and that for example having it disabled for your main application, and enabled inside a dll plugin would still be compatible. Is that not the case?

As I understand it, adding RTTI in changes the size/layout of the structure, making it difficult to link RTTI/non-RTTI code.

This is done purposely, too, according to This ABI spec, section 3.4.4.2

##### Share on other sites

The C++ Lite Faq covers how to serialize graphs if you are rolling your own but I was hoping to find a solution that didn't require me to write my own. The whole point is to save time and writing my own means spending more time (at least in the short term) then doing it the old fashion way so if I could find an existing solution that would be great but if I have to roll my own I could already tell it was going to take me longer than expected (things like needed special constuctors for filling in constant fields, references, etc....)

For example if you have a class

class Something{    const string _name;    SomethingElse& _somethingElse;public:    Something(const string& name, SomethingElse& se)    : _name(name)    , _somethingElse(se)    { }    void serialize(Archive& ar);};

this is NOT going to work

void Something::serialize(Archive& ar){   if (ar.isWriting())   {      ar << _name;      ar << _somethingElse;   }   else   {      ar >> _name; // ERROR! _name is const!      ar >> _somethingElse; // ERROR! you can't set a reference   }}

And there are a bunch of other little things like that which I'm sure there are solutions for but that's why I knew it would take me quite a while to roll my own.

Yesterday I was hoping there was an option in VC++ that would let my specify no RTTI for certain classes. Kind of like you can do this

extern "C" {
void myfunc (int c);
}

well if I could do this

extern "NORTTI" {
#include <plugin/api.h>
}

then the problem would be solved but I didn't find any such solution :-(

##### Share on other sites
Sometimes you just have to write your own, but if you can write it in a generic way then you should only have to do it once.

I created my own framework based on the Composition design pattern plus a couple of others which I use in all my new projects.

##### Share on other sites

You might consider www.webEbenezer.net. It does some of what you
mentioned.

We support STL containers, don't require RTTI, and in my opinion
it is easy to use.

We don't support XML, boost::shared_ptr or versioning. I don't
think s11n.net supports versioning either.

Brian

##### Share on other sites
"One of those is a serialization library. Unfortnately it requires a relatively newer C++ feature called Runtime Type Indentification or RTTI. RTTI code is incompatible with non RTTI code and the stuff I'm writing is a plugin to another program. That program didn't use RTTI so I can't you RTTI in my plugin. So much for using the boost library. Supposedly they have some support for non RTTI but I tried the examples and they would not compile until I turned RTTI on. (Yes, I removed the class that was using RTTI but the compiler still complained that it couldn't include the header files for serialization unless I turned on RTTI). Worse, I tried some simple example with RTTI just to see if it worked and as far as I could tell it didn't actually work. I made a simple structure with a couple of floats, serialized the struct as per the docs and looked at the data, the floats were not in there. Maybe that's an issue with VC++ 2003 although the docs claimed it works with no problems."

I am the author of the Boost Serialization Library. The library includes dozens of tests and numerous examples which all compile and run with VC 7.1 (VC 2003). The library can run in an environment that does not support rtti.

Robert Ramey