Boost logo

Boost Users :

From: Robert Ramey (ramey_at_[hidden])
Date: 2006-05-05 01:35:33


LOL - I liked the title.

I've taken a very quick look at your code and files.

a) I'm a little suspicious of including a ":" character as the GUID for
a class. I think that might be an issue. Try replacing

BOOST_CLASS_EXPORT(fem::CTCSoil);

with

BOOST_CLASS_EXPORT_GUID(fem::CTCSoil, "fem_CTCSoil");

You'll have to double check the exact syntax.

b) You've set version numbers to 1 but I don't see the version numbers
used in the deserializations. Version number assignment is only necessary
if you're going to use the version numbers and they are not equal to 0.

c) A minor suggestion - not important here but interesting anyway.
In fem_boost_serialize.cpp Use templates to shorten the code.

template<class Archive>
void CTCDataBase::boost_serialize_open(const char* filename)
{
    try {
        std::ifstream ifs(filename);
        assert(ifs.good());
        Archive ia(ifs);

        ia >> BOOST_SERIALIZATION_NVP(m_lstSoils);
        ia >> BOOST_SERIALIZATION_NVP(m_lstBrickWalls);
        ia >> BOOST_SERIALIZATION_NVP(CTCFooting::s_bspSoil);
        ia >> BOOST_SERIALIZATION_NVP(CTCFrame::s_bspBrickWall);
    }
    catch (const boost::archive::archive_exception& e) {
        e;
    }
}

and
template<class Archive>
void CTCDataBase::boost_serialize_saveconst char* filename)
{
    try {
        std::ofstream ofs(filename);
        assert(ofs.good());
        Archive oa(ofs);

        oa << BOOST_SERIALIZATION_NVP(m_lstSoils);
        oa << BOOST_SERIALIZATION_NVP(m_lstBrickWalls);
        oa << BOOST_SERIALIZATION_NVP(CTCFooting::s_bspSoil);
        oa << BOOST_SERIALIZATION_NVP(CTCFrame::s_bspBrickWall);
    }
    catch (const boost::archive::archive_exception& e) {
        e;
    }
}

void main()
{
fill_with_build_in();
boost_serialize_save<boost::archive::text_oarchive>("boost_serialize.txt");
boost_serialize_save<boost::archive::binary_oarchive>("boost_serialize.bin");
boost_serialize_save<boost::archive::xml_oarchive>("boost_serialize.xml");

// some time later ...
boost_serialize_open<boost::archive::text_oarchive>("boost_serialize.txt");
boost_serialize_open<boost::archive::binary_oarchive>("boost_serialize.bin");
boost_serialize_open<boost::archive::xml_oarchive>("boost_serialize.xml");
}

Robert Ramey

TOL mtsagara wrote:
>> Hi to all,
>>
>> This is my very first post to boost-users! I've been using a couple
>> of boost libraries for a while (shared_ptr & bind mostly). Write now
>> we are in the process of transforming a project based on MFC
>> collections, (with objects derived from CObject), to STL, and doing
>> some search for a library that will allow object persistence the way
>> MFC does, the obvious solution was boost::serialize. As I'm a newbie
>> in mpl style code the first impression was terrifying! But after
>> reading the documentation and experimenting a lot, I did manage to
>> start using boost::serialize. In fact there is no need to be
>> familiar with mpl. The library is excellent - an exemplary work,
>> thank you Robert Ramey!
>>
>> Now my problem is with xml files. I can write a couple of classes
>> both in txt and binary files. But I can't write the very same data
>> in xml format. I believe I followed all guidelines but I had no luck
>> ... :(
>>
>> The scenario in pseudo code is this:
>>
>>
>> //////////////////////////////////////////////////////////////////////
>> // file fem.h
>> #include <boost/archive/...>
>> #include <boost/serialize/...>
>>
>> namespace fem {
>>
>> class CTCBase
>> {
>> ...
>> };
>> typedef boost::shared_ptr<CTCBase> CBSPBase
>> typedef std::list<CBSPBase> CBSPBaseList
>>
>>
>>
>> class CTCSoil
>> {
>> ...
>> };
>> typedef boost::shared_ptr<CTCSoil> CBSPSoil
>> typedef std::list<CBSPSoil> CBSPSoilList
>>
>>
>>
>> class CTCBrickWall
>> {
>> ...
>> };
>> typedef boost::shared_ptr<CTCBrickWall> CBSPBrickWall
>> typedef std::list<CBSPBrickWall> CBSPBrickWallList
>>
>>
>>
>> // list of soils
>> CBSPSoilList s_lstSoils;
>> // list of Brick Walls
>> CBSPBrickWallList s_lstBrickWalls;
>> // initial soil for each new foundation beam (also contained in
>> above list) CBSPSoil s_bspSoil;
>> // initial brick wall on each beam (also contained in above list)
>> CBSPBrickWall s_bspBrick;
>> }
>>
>> BOOST_CLASS_VERSION(fem::CTCBase, 1);
>> BOOST_CLASS_VERSION(fem::CTCSoil, 1);
>> BOOST_CLASS_VERSION(fem::CTCBrickWall, 1);
>>
>>
>>
>>
>>
>>
>> //////////////////////////////////////////////////////////////////////
>> // file fem.cpp
>>
>> #include "fem.h"
>> #include <boost/serialization/export.hpp>
>>
>>
>> BOOST_IS_ABSTRACT(fem::CTCBaseObj);
>> BOOST_CLASS_EXPORT(fem::CTCSoil);
>> BOOST_CLASS_EXPORT(fem::CTCBrickWall);
>>
>> void fill_with_build_in()
>> {
>> ...
>> }
>>
>>
>> void main()
>> {
>> fill_with_build_in();
>> boost_serialize_save_txt("boost_serialize.txt");
>> boost_serialize_save_bin("boost_serialize.bin");
>> boost_serialize_save_xml("boost_serialize.xml");
>>
>>
>> // some time later ...
>> boost_serialize_open_txt("boost_serialize.txt");
>> boost_serialize_open_bin("boost_serialize.bin");
>> boost_serialize_open_xml("boost_serialize.xml");
>> }
>>
>>
>>
>> The xml save & open functions are in file fem_boost_serialize.cpp
>>
>> I attached the 3 files produced this way. I can also read back txt &
>> binary versions. I'm wondering if somebody - hopefully Robert Ramey!
>> - can tell me just by examining the output files, where should I
>> look for the problem. Or do I have to debug the code line by line
>> and see what wend wrong? It seems something is wrong with the very
>> first shared_ptr serialization .
>>
>> I include some - not stand alone I'm afraid - source code files with
>> this message just in case someone might take a look at them. I can
>> tell that during the transition period from MFC to STL (possibly
>> after that too), our objects derive from MFC CObject. Also at the
>> moment, we still use MFC CString and not std::string. I had to copy
>> CString to std::string for the serialization to work properly. Could
>> there be a conflict somewhere there? Could it be the order in hpp
>> files are included?
>>
>>
>> Finally I use MS Visual C++ .net 2003 and boost v1.33.1.
>>
>>
>> Thank you in advanced,
>>
>>
>>
>> Emmanuil Tsagarakis
>>
>>
>
>
>
>> _______________________________________________
>> Boost-users mailing list
>> Boost-users_at_[hidden]
>> http://lists.boost.org/mailman/listinfo.cgi/boost-users


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