Boost logo

Boost :

Subject: Re: [boost] [boost.SerializationV1.42.0]QuestionaboutBOOST_SERIALIZATION_SPLIT_FREE andnon-public members
From: Robert Ramey (ramey_at_[hidden])
Date: 2010-08-10 03:30:08


I looked into this a little bit.

First I can see the the friend ...access will only work for
intrusive serialization.

But I can see that it's straight forward to just make
the free save/load functions friend templates - EXCEPT
that not all compilers do this. Sooo here is what I had
to do to sneak it past my msvc 7.1 compiler.

Robert Ramey

//////////////////////////////////////////////////////////////////////////////////////
#include <boost/config.hpp>
#include <boost/serialization/split_free.hpp>

class CMyObject
{
#ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
private:
    friend template<class Archive>
    void save(Archive & ar, const CMyObject& t, unsigned int version);
    friend template<class Archive>
    void load(Archive & ar, CMyObject& t, unsigned int version);
#else
public:
#endif
    int m_nPrivateData;
public:
    CMyObject(void) : m_nPrivateData(123) { }
    ~CMyObject(void) { }
};
BOOST_SERIALIZATION_SPLIT_FREE(CMyObject)

//////////////////////////////////////////////////////////////////////////////////////
#include <boost/archive/xml_oarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>

namespace boost { namespace serialization {
template<class Archive>
void save(Archive & ar, const CMyObject& t, unsigned int version)
{
    ar << boost::serialization::make_nvp("CMyObject", t.m_nPrivateData);
}

template<class Archive>
void load(Archive & ar, CMyObject& t, unsigned int version)
{
    ar >> boost::serialization::make_nvp("CMyObject", t.m_nPrivateData);
}

}} // boost and serialization namespaces

#include <fstream>

int
main(int argc, char *argv[]){
    std::ifstream is;
    boost::archive::xml_iarchive ia(is);
    CMyObject obj;
    ia >> BOOST_SERIALIZATION_NVP(obj);
}


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