Boost logo

Boost Users :

Subject: Re: [Boost-users] boost::serialization - track_never, load and save in cpp file?
From: Robert Ramey (ramey_at_[hidden])
Date: 2009-02-24 11:24:50


Martin Trappel wrote:
> Hi.
>
> To support the serialization of a class I don't want to change
> anything, I have added free save and load function. (See the test
> code below.) It compiles and appears to work.
>
> I have the following questions:
> * Is it OK to put the save & load definitions in a separate
> implementation file?

Personally I think this is good practice. It shortens compile times
and permits all of the code for class "X" to be in a *.lib file.

> * Is it OK to specify track_never in a cpp file?

I don't think this is a good idea. A lot of serialization magic
occurs in templates at compile time. Doing this will short-circuit
this and break things.

>Will this cause problems if someone else tries to serialize a map<int,
>string> ?

not that I know of.

Robert Ramey

>
> thanks!
> - Martin
>
>
> - global_serialization.h -
> // ...
> class MyComplexClass;
> // ...
> namespace boost {
> namespace serialization {
> // Declare save and load for MyComplexClass
> template<class Archive>
> void save(Archive & ar, const MyComplexClass& t, unsigned int
> version); template<class Archive>
> void load(Archive & ar, MyComplexClass& t, unsigned int version);
> }
> }
> // Split:
> BOOST_SERIALIZATION_SPLIT_FREE(MyComplexClass);
>
> *****
>
> - complex_class_serialization.cpp -
> // ...
> #include "ComplexClass.h"
> // ...
> typedef std::map<int, std::string ser_map_t;
> BOOST_CLASS_TRACKING(ser_map_t, boost::serialization::track_never);
>
> namespace boost {
> namespace serialization {
> template<class Archive>
> void save(Archive & ar, const MyComplexClass& t, unsigned int version)
> {
> // the relevant information to be serialized for
> // instances of MyComplexClass can be represented
> // by a map<int, string>
>
> ser_map_t m;
> // ... fill m object from t object
>
> ar << m; // Serialize
> }
>
> template<class Archive>
> void load(Archive & ar, CBoxHandler & t, unsigned int version)
> {
> ser_map_t m;
> ar >> m;
>
> // ... init t object from m
> }
>
> } // namespace serialization
> } // namespace boost


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net