Boost logo

Boost :

From: Dennis (dennisf486_at_[hidden])
Date: 2003-11-28 01:07:15


I see references in the mailing list to developing
code for serialization to go with STL, but I don't see
it in the 1.30 release zip. I'm assuming it's still
under development.

I'm working on a home project that uses STL
containers-within-other-containers. It needs to be
able save and load these structures to/from a file.
Is serialization close to being released, or should I
write my own code to do it?

I've done some experimentation of my own on developing
code to save STL containers. It may help you with
developing the serialization functions or you might be
able to give me some tips.

I have determined that it *is* possible to write a
suite of template functions to overload a WriteX()
function so that it can handle both built-in C++ types
and STL containers. Because the template is
quasi-recursive (it calls it's own name, but not
necessarily itself, rather it might be a different
overload) it can handle nested containers.

To test the idea, I made two functions:

void WriteX(const void*, size_type, fstream); // (not
a template), takes any type

and a template overload of the same name:

template <class T> WriteX(const vector<T>*, size_type,
fstream); // takes a vector of any type

If you call WriteX on a vector of vectors, it will
call the vector version of WriteX for each element of
each nested container until it comes to the "regular"
data type in the center for which it calls the void*
version on each element. I couldn't get my MSVC++ 6.0
compiler to differentiate between const T* and const
vector<T>*, even if I changed the order of the
declarations/definitions. (const T* hid every other
possible const * combination). So instead I used a
void* for the default case; this requires me to pass
the size of the object in size_type, because you can't
determine it from a void*.

There are some considerations:

1) Is there a way to be able to use const T* instead
of const void*?

2) This will compile for any type, even types it
doesn't actually know how to save correctly. If there
isn't a template written for the type, it will do a
bitwise save of it. This behavior is desirable when
it works (you don't need to define a new template if
the type really can be bitwise copied) and undesirable
when it hides nasty bugs (it can save a pointer to a
list accidently, instead of the contents).

3) I believe there is a way to write one WriteX
function that is so general that it can save *any* STL
type, by calling for_each() to catch all the elements
when writing, and likewise to write one ReadX function
that uses input iterators to add the elements to the
container when reading. Am I wrong?

4) For text streams, the standard library uses
operators >> and <<. I'd like to have it work that
way for my binary serialization, but would it be bad
practice to use the same operators for binary
serialization that the standard library already
defines for text serialization?

__________________________________
Do you Yahoo!?
Free Pop-Up Blocker - Get it now
http://companion.yahoo.com/


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk