Boost logo

Boost Users :

From: Ruediger Berlich (ruediger.berlich_at_[hidden])
Date: 2007-03-31 16:33:21


Dear all,

I'd like to serialize a number of classes using the free-standing version of
the serialize function. As I do not want to access internals of the classes
directly, I have to work with set/get functions, which in turn implies
having to write load/save functions.

This seems to work for simple types, like in the following example. But will
it circumvent any internal mechanisms of Boost.Serialization , if I do not
give the library direct access to class data ? Will this work for complex
types, e.g. vectors of shared_ptr objects ?

/*************************************************************************/

class base{
public:
    base(void){
        setSecret(1);
    }
    base(int val){
        setSecret(val);
    }
    virtual ~base(){ /* nothing */ }
    
    virtual int getSecret(void) const{
        return secret;
    }

    virtual void printInfo(void) = 0;

    void setSecret(int val){
        secret = val;
    }

private:
    int secret;
};

BOOST_IS_ABSTRACT(base);

namespace boost {
    namespace serialization {
        template<class Archive>
        void save(Archive & ar, const base & t,
                  unsigned int version)
        {
            int secret = t.getSecret();
            ar & make_nvp("secret", secret);
        }
        
        template<class Archive>
        void load(Archive & ar, base & t, unsigned int version)
        {
            int secret;
            ar & make_nvp("secret", secret);
            t.setSecret(secret);
        }

        template<class Archive>
        inline void serialize(Archive & ar, base & t;
                              const unsigned int version)
        {
            split_free(ar, t, version);
        }
    }
}

/*************************************************************************/

Thanks and have a good day,
Ruediger


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