CSer - C++ Serialization


What is serialization?

Serialization, also called 'persistence', is the ability to write out a network of objects to a file, then recreate that network later by reading it back in.  This isn't quite a simple as you might think, since one object may be pointed to by multiple other objects.

How does it work?

Writing out integers and character strings is fairly straightforward.  The trick is dealing with objects, and pointers to them.  Serialization of these involves assigning a unique ID # to each object as it is written, and in keeping a lookup table that maps each object to its ID.  That way, when an object is encountered a second time (because something else is pointing to it), only its ID is written.  And when the network is read back in, CSer knows to only recreate the object once, and to use its pointer when its ID is encountered thereafter.

How do I use it?

Sorry for the lack of documentation.  For a quick start, see the 'test1.cc' example included in the source.  Here is a brief list of the steps you need to take (also listed in the README):

1.  #include "CSer.h"
2.  Derive from "Serializable".
3.  Add a "CSER_DECLARE(Myclass);" in the public part of class 'MyClass'.
4.  Add a "CSER_REGISTER(Myclass);" in the .cc file for your class.
5.  Add methods "void write(CSer_out&) and void read(CSer_in&) to your class.
    NOTE:  If an object instance is part of another object
    ('composition'), then you must write/read the object itself before
    writing/reading any pointers to it.  See ClassC in 'test1.cc' as
    an example.
6.  Your class must also have a constructor taking no arguments.    

Are there other libraries that do this?

Of course; I'm not that clever.  The most well-known is MFC.  But it's not of much use to those of us writing code for non-Windows platforms.  A more portable library is Common C++, which has many other features as well.

So why do another one?

I wanted a  package that just did serialization, with no additional overhead and no external requirements (other than STL).

Links

CSer on SourceForge
CVS Source Browser

SourceForge Logo