Hi,
This is how I do it:
Derive every class from an ISerialize class.
This ISerialize class has one member accepting an IArchive i.e Serialize(
IArchive& Archive ).
The ISerialize class has a virtual function that returns it name (overridden
in the derived classes to return the class name). The name is used to
deserialize the correct class.
The IArchive function knows whether it is open for reading or writing.
The IArchive class has two virtual members read( char *, long length ) and a
similar write().
The IArchive also contains non virtual functions for all regular types like
string long int (use templates etc to reduce these to a few functions).
In addition this IArchive can serialize/deserialize ISerialize derived
classes by first storing there name and then calling the class'es Serialize
function passing itself. On deserialization it first reads the class name
(it knows this is a ISerialize class and not another type like long int etc.
because you pass a pointer or reference to it.)
Then it creates the object (lookup 'object factory' this is pretty standard
C++ way of creating objects by name or id) and calls the serialize member of
the object passing itself.
The Serialize member of a ISerialize derived typically (piece of my code)
void MCursor::MCursorInfo::Serialize( MArchive& Archive )
{
Archive.Serialize ( Event );
Archive.Serialize ( OffsetX );
Archive.Serialize ( OffsetY );
Archive.Serialize ( Animation );
}
Note1 that since the archive knows whether it is reading or writing it can
choose between the read or write function.
Note2 Because you can overload the read and write function you can create
derived function to store to memory disks, sockets etc easily. You only have
to overload the very simplistic read and write function.
Note3 Because (de)/serialization is done with one function it always is in
sync (no possibility to make a mismatch between read and write).
When this works extend your archive to keep track of written pointers to
ISerailize objects (so it only creates one object when it there are several
pointers around to one object on deserialization).
and add some stuff to automatically save STL vectors/sets/maps etc of
ISerialize etc
It might take some time to setup but ones you have it, it really works like
a charm :-) and loads of fun to see how easy it works then. I use it to save
my 2D game engines internal state (savegame) and load the same state back in
memory (deserialization).
Things to study:
Object factories
Lookup microsofts way of serialization on MSDN ( I stole the duplicate
pointer idea from them :-) )
Make sure your are reasonably aquainted with templates
Regards, Ron AF Greve
http://www.InformationSuperHighway.eu
'William' <Wi*******@gmail.comwrote in message
news:11**********************@d57g2000hsg.googlegr oups.com...
I'm looking for an example that would show how to serialize a c++
object at it's simplest w/o using any other api's. I have a class that
I want to serialize and then pass to my obj-c class so I can send it
over the wire.
I'm just looking for how to serialize it, then pack it back up on the
other end.
Any help much appreciated.