Boost logo

Boost Users :

Subject: [Boost-users] Problem serializing a std::vector of a user-defined class containing a std::string
From: Ivan Gonzalez (iglpdc_at_[hidden])
Date: 2009-01-21 06:13:25


Dear all,

I'm having problems to serialize a std::vector of a small class I use
in my code. The class contains 3 members: a std::string, a size_t, and
an another class passed as a template parameter (see the code below.)
The class passed as a template is basically a std::map<int,
blitz::Array<double, 2> >(see this post
http://lists.boost.org/boost-users/2008/05/36282.php). I'm using the
serialization library with the last class elsewhere in my code and
works with no problems, so I guess the error must be in the code
below. My program compiles and finishes OK, but the results I'm
getting are slightly different from what I should get: it seems that
somewhere in the serialization process the numbers stored in the third
class get corrupted.

I'm using boost 1.35, I need the binary file (I'm taking advantage of
the serialization stuff for arrays contiguous in memory), and
everything happens in the same machine in a single run of my code (so
it is not a problem of different binary archives for different
machines.)

Thanks in advance,

Ivan

//--------------------------------------------------------------------------------------------
#include <iostream>
#include <sstream> // for std::istringstream
#include<boost/serialization/string.hpp>
#include<boost/serialization/vector.hpp>
#include<boost/archive/binary_oarchive.hpp>
#include<boost/archive/binary_iarchive.hpp>

template<class DataType>
class ThingToStore{

    public:

        typedef DataType data_type;

        ThingToStore() :
            site_(), name_(), data_() {}

        ThingToStore(const data_type& data,
                const std::string& name, int site) :
            site_(site), name_(name), data_(data) {}

        const std::string & name() const { return name_; }
        size_t site() const { return site_; }
        const data_type& map() const { return data_; }

    private:

        friend class boost::serialization::access;

        template<class Archive>
            inline void serialize(Archive& ar,
                    const unsigned int file_version)
            {
                ar & BOOST_SERIALIZATION_NVP(site_);
                ar & BOOST_SERIALIZATION_NVP(name_);
                ar & data_;
            }

        size_t site_;
        std::string name_;
        data_type data_;
};

template<class DataType>
class StoredThingList {

    private:

        typedef std::vector<ThingToStore<DataType> > this_type;

    public:

        StoredThingList() {}

        template<class Index>
            void save(const Index& i,
                    const std::string& name="blockOps_") const
            {
                // make a string for the name file
                std::ostringstream oss;
                oss<<i<<"_"<<".aux";
                std::string s=name+oss.str();
                // make a output stream to the file above and open it
                std::ofstream ofile(s, std::ios::binary);
                if ( ofile.is_open() )
                {
                    boost::archive::binary_oarchive oa(ofile);
                    oa & (*this);
                }
            }

        template<class Index>
            void load(const Index& i,
                    const std::string& name="blockOps_")
            {
                // make a string for the name file
                std::ostringstream oss;
                oss<<i<<"_"<<".aux";
                std::string s=name+oss.str();
                // make a input stream to the file above and open it
                std::ifstream ifile(s, std::ios::binary);
                if ( ifile.is_open() )
                {
                    vec_.clear();
                    boost::archive::binary_iarchive ia(ifile);
                    ia & (*this);
                }
            }

    private:

        friend class boost::serialization::access;

        template<class Archive>
            inline void serialize(Archive& ar, const unsigned int version)
            {
                ar & vec_;
            }

        this_type vec_;
};
//--------------------------------------------------------------------------------------------


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