Boost logo

Boost Users :

From: John O-Connor (john.o-connor_at_[hidden])
Date: 2007-11-22 08:15:17


I have the following code serialising shared_ptr's to fstream and reading
them back correctly. When I switch to using a stringstream (invert
comments) I get a null value in "str" following serialisation. I noticed
someone had a similar issue previously and solved it (but didn't explain
how). Serialising/de-serialising between stringstream works fine when the
iostreams are avoided.
Could someone point out what I'm doing wrong please?
Regards,
John

Using boost_1_33_1 on vc++2005 (8.0)

// test.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <boost/archive/xml_oarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>
/*//*/#include <fstream>
//#include <sstream>
#include <iostream>
#include <boost/serialization/vector.hpp>
#include <boost/serialization/shared_ptr.hpp>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/filter/gzip.hpp>

struct simple
{
        simple() : i_(1){}
        simple(int i) : i_(i){}
        int i_;
        template<class Archive>
        void serialize(Archive & ar, const unsigned int version)
        {
                ar & BOOST_SERIALIZATION_NVP(i_);
        }
};

int _tmain(int argc, _TCHAR* argv[])
{
        //std::string str;
        {
                boost::shared_ptr<simple> s(new simple(2));

                std::vector<boost::shared_ptr<simple> > vec;
                for (int j = 0; j < 100; j++)
                {
                        vec.push_back(s);
                }
                /*//*/std::ofstream os("c:/test_ser_shared_ptr.xml",
std::ios_base::binary);
                //std::ostringstream os;
                boost::iostreams::filtering_ostream out;
                out.push(boost::iostreams::gzip_compressor());
                out.push(os);
                boost::archive::xml_oarchive oa(out);
                oa & BOOST_SERIALIZATION_NVP(vec);
                //str = os.str();
        }

        {
                std::vector<boost::shared_ptr<simple> > vec;
                /*//*/std::ifstream is("c:/test_ser_shared_ptr.xml",
std::ios_base::binary);
                //std::istringstream is(str);
                boost::iostreams::filtering_istream in;
                in.push(boost::iostreams::gzip_decompressor());
                in.push(is);
                boost::archive::xml_iarchive ia(in);
                ia & BOOST_SERIALIZATION_NVP(vec);
                int i = (*(vec.begin()))->i_;
        }

        return 0;
}

---
This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and delete this e-mail. Any unauthorized copying, disclosure or distribution of the material in this e-mail is strictly forbidden.
Please refer to http://www.db.com/en/content/eu_disclosures.htm for additional EU corporate and regulatory disclosures.


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