Link to home
Start Free TrialLog in
Avatar of rukiman
rukiman

asked on

porting java to C++ tackling serialization

Ok I have a game that is written in Java and need to port it to C++. How can I tackle serilization? In java it is really easy, simply make your objects implement Serializable and when serializing the object, java is smart enough to transverse through all the objects in memory and serialize them taking into account that there might be multiple references to the same object so not to serialize multiplle times.

How can I handle this in C++ ? Is there a library that works simlilar to the java serilization? Ofcourse I can manually serialize all the objects myself, but I want to avoid doing that as that will take time and not so elegant. And does these libraries handle multiple pointers to the same objects as Java does?
Avatar of Let_Me_Be
Let_Me_Be
Flag of United States of America image

Boost::Serialization http://www.boost.org/doc/libs/1_39_0/libs/serialization/doc/index.html

But don't expect such comfort/features as in Java or .NET.
Avatar of ahao
ahao

You can also try Google Protocol Buffers
http://code.google.com/p/protobuf/
Is this really necessary?  In Java, I thought Serialization is really just so that you can read/write your classes to memory/files, but this is already available in C++:

MyClass myClass;
std::fstream file("game.lvl");
file.read((char *)&myClass,sizeof(myClass));
file.write((char *)&myClass,sizeof(myClass));
file.close();

If you need the more advanced features of being able to read old/new files, then you could just read each data member individually, and when you add a new data member, you just add that new line of code.  However, I'm not sure why you would need this for reading game objects.  Why are you porting the game to C++?  Can you not keep it in Java?
> Is this really necessary?  In Java, I thought Serialization is really just so that you can read/write your classes to memory/files, but this is already available in C++:

C++ "serialization" as you present it isn't portable at all, while Java serialization isn't portable only between different versions.
Oh ok, that's right; I forgot about that, thanks.  If you need that type of portability, then I hope you're also using a portable game engine, else if this is just being made for Windows, then I wouldn't see the need (as much).  Still if you are making a cross-platform game, just stick with your java code.  You can make Java applets and Java web start applications.  The games on these two websites (made by Jagex) were written in Java (as Java applets):


http://www.runescape.com/
http://www.funorb.com/
WhiteMage: this is not only about platform but also about compiler versions (and of course system version). Not to mention that your example won't work with 98% of classes.
Avatar of rukiman

ASKER

Yes that example will not work, you cannot assume you will have the same memory space availabel to you  when you deserialize, all your pointers will be pointing to no man's world after you deserialize.


Boost library, I already came across that, but seemed too complicated to use. I was hoping for something easier.
Avatar of rukiman

ASKER

The game must be ported to C++, iphone doesn't run Java unfortunately.
ASKER CERTIFIED SOLUTION
Avatar of Let_Me_Be
Let_Me_Be
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial