Boost logo

Boost Users :

From: Robert Ramey (ramey_at_[hidden])
Date: 2006-09-04 11:18:15


How about the following ( using the serialization of std::vector as a model)

void save_mib(const vector<Row>& mib, const string& filename)
{
    ofstream ofs(filename.c_str());
    boost::archive::xml_oarchive oa(ofs);

    oa << mib.size(); // might need a "const" cast here

    const vector<Row>::const_iterator mibEnd(mib.end());
    for (vector<Row>::const_iterator i(mib.begin()); i != mibEnd; ++i)
    {
        const Row& row(*i);
        oa << BOOST_SERIALIZATION_NVP(row);
    }
}

void load_mib(vector<Row>& mib, const string& filename)
{
    ifstream ifs(filename.c_str());
    boost::archive::xml_iarchive ia(ifs);

    unsigned int count;
    ia >> count;
    while(count-- > 0) {
            ia >> BOOST_SERIALIZATION_NVP(row);
            mib.push_back(row);
        }
    }
}

But this is the same as using a std::vector. So you might want something
that doesn't require an itermediate data structure:

// since we might use a temporary object upon saving and are often going to
"throw away" the temporary object upon loading
// make sure that it doesn't optimize saving/loading of pointed to objects.

BOOST_SERIALIZATION_TRACKING(Row, no_tracking);

typedef ... row_iterator;

void save_mib(const unsigned int count, const row_iterator it, const string&
filename)
{
    ofstream ofs(filename.c_str());
    boost::archive::xml_oarchive oa(ofs);

    oa << count;

    const vector<Row>::const_iterator mibEnd(mib.end());
    for (vector<Row>::const_iterator i(mib.begin()); i != mibEnd; ++i)
    for(unsigned int i = count; i-- > 0;){
        oa << *it;
    }
}

typedef ... function_object;

void load_mib(function_object & fo, const string& filename)
{
    ifstream ifs(filename.c_str());
    boost::archive::xml_iarchive ia(ifs);

    unsigned int count;
    ia >> count;
    while(count-- > 0) {
            Row row;
            ia >> BOOST_SERIALIZATION_NVP(row);
            fo(row);
        }
    }
}

Good luck with this.

Robert Ramey


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