Boost logo

Boost Users :

From: Terence Wilson (tez_at_[hidden])
Date: 2005-12-24 02:52:47


I am an MFC veteran that wants to use a more modern mechanism for archival,
in MFC a base pointer can be written and the object recreated automatically
upon the read. I've been reading the serialization documentation and I am
very confused about what I need to do to make this work using Boost, the
code I have so far fails to compile with a static assert in the save
function in oserializer.hpp.
 
class CMyClassBase
{
public:
         CMyClassBase():m_TagBase(0){}
        virtual ~CMyClassBase(){}

        int m_TagBase;

        friend class boost::serialization::access;
        template<class Archive> void serialize(Archive & ar, const unsigned
int /* file_version */)
        {
                ar & m_TagBase;
        }
};

class CMyClassDerived : public CMyClassBase {
public:
        CMyClassDerived():m_TagDerived(1){}
        virtual ~CMyClassDerived(){}

        int m_TagDerived;

        friend class boost::serialization::access;
        template<class Archive> void serialize(Archive & ar, const unsigned
int /* file_version */)
        {
                boost::serialization::base_object<CMyClassBase>(*this);

                ar & m_TagDerived;
        }
};

BOOST_CLASS_EXPORT_GUID(CMyClassDerived, "CMyClassDerived")

void save_myclass(CMyClassBase* pMyClassBase, const char * filename){
        // make an archive
        std::ofstream ofs(filename);
        boost::archive::text_oarchive oa(ofs);
        oa << pMyClassBase;
}

void
restore_myclass(CMyClassBase* pMyClassBase, const char * filename) {
        // open the archive
        std::ifstream ifs(filename);
        boost::archive::text_iarchive ia(ifs);

        // restore the schedule from the archive
        ia >> pMyClassBase;
}


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