Boost logo

Boost :

Subject: Re: [boost] [boost.SerializationV1.42.0]QuestionaboutBOOST_SERIALIZATION_SPLIT_FREE andnon-public members
From: gmkdroid (gmkdroid_at_[hidden])
Date: 2010-08-10 22:30:54


On Tue, Aug 10, 2010 at 3:30 AM, Robert Ramey <ramey_at_[hidden]> wrote:

> 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.
>

Robert,

I tried your suggestion, but VS2008 didn't compile. I made a few minor
tweaks and its working now. Here's what I ended up using:

*MyObject.h*
//////////////////////////////////////////////////////////////////////////////////////
#pragma once
#include "Serializer_MyObject.h"
class CMyObject
{
private:
   template<class Archive>
   friend void boost::serialization::save(Archive & ar, const CMyObject& t,
unsigned int version);

   template<class Archive>
   friend void boost::serialization::load(Archive & ar, CMyObject& t,
unsigned int version);

   int m_nPrivateData;

public:
   CMyObject(void) : m_nPrivateData(123) { }
   ~CMyObject(void) { }
};

*Serializer_MyObject.h*
//////////////////////////////////////////////////////////////////////////////////////
#pragma once
#include <boost/config.hpp>
#include <boost/serialization/split_free.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>
class CMyObject;
BOOST_SERIALIZATION_SPLIT_FREE(CMyObject)

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);
}

}} // end boost::serialization namespaces

**
**
*main.cpp*
//////////////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include <fstream>
#include <iostream>
#include "MyObject.h"

int _tmain(int argc, _TCHAR* argv[])
{
   std::ofstream ofs("C:\\test.xml");
   boost::archive::xml_oarchive oa(ofs);
   CMyObject obj;
   oa << BOOST_SERIALIZATION_NVP(obj);
   ofs.close();

   CMyObject newobj;
   std::ifstream ifs("C:\\test.xml");
   boost::archive::xml_iarchive ia(ifs);
   ia >> BOOST_SERIALIZATION_NVP(newobj);;
   ifs.close();
}

Thanks for your help.

Glen


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