Boost logo

Boost Users :

From: at forum (at1forum_at_[hidden])
Date: 2008-02-06 12:09:18


Hi
I am testing boost serialization to integrate it in our software.
Actually I am using mfc

I want to save an object to an older version?
I using the split method (save and load) and I don't find how to pass
the version to the save function?

"BOOST_SERIALIZATION_NVP(this)" is using the current object version.

code :

class CMyPoint
{
        friend class boost::serialization::access;

protected:
        int m_nX, m_nY;
        std::string m_sName;

public:
        CMyPoint()
        {
                m_nX = 0;
                m_nY = 0;
                m_sName = "";
        }
        CMyPoint(int nX, int nY, std::string sName = "")
        {
                m_nX = nX;
                m_nY = nY;
                m_sName = sName;
        }

        void SetX(int nX)
        {
                m_nX = nX;
        }

        void SetY(int nY)
        {
                m_nY = nY;
        }

        virtual void Assign(const CMyPoint &pt)
        {
                m_nX = pt.m_nX;
                m_nY = pt.m_nY;
        }

        CMyPoint& operator =(const CMyPoint &pt)
        {
                Assign(pt);
                return *this;
        }

        template<class Archive>
    void save(Archive & ar, const unsigned int version) const
    {
        ar & BOOST_SERIALIZATION_NVP(m_nX)
            & BOOST_SERIALIZATION_NVP(m_nY);
                if(2 <= version)
                        ar & BOOST_SERIALIZATION_NVP(m_sName);
    }
    template<class Archive>
    void load(Archive & ar, const unsigned int version)
    {
       ar & BOOST_SERIALIZATION_NVP(m_nX)
            & BOOST_SERIALIZATION_NVP(m_nY);
           if(2 <= version)
                        ar & BOOST_SERIALIZATION_NVP(m_sName);
    }
    BOOST_SERIALIZATION_SPLIT_MEMBER()

        void Save(const char * filename)
        {
                std::ofstream ofs(filename);
                assert(ofs.good());
                boost::archive::xml_oarchive oa(ofs);
                oa << BOOST_SERIALIZATION_NVP(this);
        }

        void Load(const char * filename)
        {
                std::ifstream ifs(filename);
                assert(ifs.good());
                boost::archive::xml_iarchive ia(ifs);
                ia >> BOOST_SERIALIZATION_NVP(this);
        }
};
BOOST_CLASS_VERSION(CMyPoint, 2)
BOOST_CLASS_EXPORT_GUID(CMyPoint, "CMyPoint")

Thank you


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