Boost logo

Boost Users :

Subject: [Boost-users] Boost::Serialization and MFC Doc/View architecture
From: Colin Caprani (cc.caprani_at_[hidden])
Date: 2011-07-02 20:31:41


Hi All,

I'm porting an existing MFC C++ application to use Boost::Serialization for
XML files. My CDocument object contains all the data for the app. I've
implemented the serialize function as:

    template<class Archive>
    void CMyDoc::serialize(Archive& ar, const unsigned int version)
    {
    ar & BOOST_SERIALIZATION_NVP(m_Param1)
        & BOOST_SERIALIZATION_NVP(m_Param2);
    }

To capture the save and load events, in the CMyDoc.cpp file I have:

    BOOL CMyDoc::OnOpenDocument(LPCTSTR lpszPathName)
    {
    clear(); // clear current params

    //if (!CDocument::OnOpenDocument(lpszPathName)) // Old MFC serialize
code
    // return FALSE;

    CEvolveTrafficDoc* pDoc = this; // pointers the same here
    std::ifstream ifs(lpszPathName);
    boost::archive::xml_iarchive ia(ifs);
    ia >> boost::serialization::make_nvp("MyDoc",pDoc); // pointer changes
here

    return TRUE;
    }

    BOOL CMyDoc::OnSaveDocument(LPCTSTR lpszPathName)
    {
    //if (!CDocument::OnSaveDocument(lpszPathName)) // Old MFC serialize
code
    // return FALSE;

    std::ofstream ofs(lpszPathName);
    boost::archive::xml_oarchive oa(ofs);
    oa << boost::serialization::make_nvp("MyDoc",this);

    return TRUE;
    }

Saving a document works fine. The problem is that loading a document doesn't
work. The boost library seems to copy the CMyDoc object because the pointer
comes back a different address. This means that the loaded file isn't loaded
into the current document. Can a CDoc overwrite itself with boost? It can
with MFC CArchive.

If I overload the CMyView to capture the file open and save events, the MRU
list management offered by the Doc/View architecture won't happen.

I'm sure this has been done a million times, but I can't find any
information online. Weird! Any help much appreciated.

Thanks,

Colin



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