Boost logo

Boost Users :

From: Youhei Murakami (ym555_at_[hidden])
Date: 2007-11-29 07:18:13


Hello everyone. I tried to serialize vector<shared_ptr<T> > that is held
by pimpl but
I met to a lot of error. If someone who knows the resason for it exists,
please teach
me the reason. source code is below. Compiled using visual studio 2005
sp1 in WindowsXP64bit.
boost-1_34_1
source code has been attached.


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

//#include "stdafx.h"

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <typeinfo>
#include <memory>
#include <algorithm>
#include <vector>
#include <map>


#include <boost/any.hpp>

#include <boost/shared_ptr.hpp>
#include <boost/intrusive_ptr.hpp>

#include <boost/ptr_container/ptr_vector.hpp>

#include <boost/thread.hpp>

#include <boost/tuple/tuple.hpp>
#include <boost/tuple/tuple_comparison.hpp>
#include <boost/tuple/tuple_io.hpp>
#include <boost/type_traits.hpp>
#include <boost/optional.hpp>
#include <boost/serialization/serialization.hpp>
#include <boost/serialization/string.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/serialization/list.hpp>
#include <boost/serialization/map.hpp>
#include <boost/serialization/set.hpp>
#include <boost/serialization/deque.hpp>
#include <boost/serialization/shared_ptr.hpp>
#include <boost/serialization/scoped_ptr.hpp>
#include <boost/serialization/split_member.hpp>
#include <boost/serialization/version.hpp>
#include <boost/serialization/base_object.hpp>
#include <boost/serialization/tracking.hpp>
#include <boost/serialization/is_abstract.hpp>
#include <boost/serialization/nvp.hpp>
#define BOOST_LIB_NAME boost_serialization
#include <boost/config/auto_link.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>
#include <boost/archive/text_woarchive.hpp>
#include <boost/archive/text_wiarchive.hpp>

using namespace std;
using namespace boost;
using namespace boost::serialization;

namespace test{
        class holder
        {
                struct Impl
                {
                        Impl() : x(0.0), y(0.0), message(wstring(L"blank")) {
                                vec.push_back(shared_ptr<int>( new int(1) ));
                                vec.push_back(shared_ptr<int>( new int(2) ));
                                vec.push_back(shared_ptr<int>( new int(3) ));
                        }

                        Impl(
                                double ax,
                                double ay,
                                const wstring &am,
                                const vector<shared_ptr<int> > &av )
                                : x(ax), y(ay), message(am), vec(av) {
                        }

                        double x, y;
                        wstring message;
                        vector<shared_ptr<int> > vec;

                private:
                        friend class serialization::access;
                        template <class Archive>
                        void serialize( Archive &ar, const unsigned long version );
                };
                shared_ptr<Impl> impl;

        public:
                holder() : impl( new Impl ) {}
                holder(
                        double ax,
                        double ay,
                        const wstring &am,
                        const vector<shared_ptr<int> > &av )
                        : impl( new Impl(ax, ay, am, av) ) {}
        private:
                friend class serialization::access;
                template <class Archive>
                void serialize( Archive &ar, const unsigned long version )
                {
                        ar & impl;
                }
        };

        template <class Archive>
        void holder::Impl::serialize(Archive &ar, const unsigned long version ) {
                ar & x; // ok
                ar & y; // ok
                ar & message; // ok
                ar & vec; // error : if remove this line, this source code will move file.
        }

        template void holder::serialize<archive::text_woarchive>(
                archive::text_woarchive &ar,
                const unsigned long version );
        template void holder::serialize<archive::text_wiarchive>(
                archive::text_wiarchive &ar,
                const unsigned long version );

        template void holder::Impl::serialize<archive::text_woarchive>(
                archive::text_woarchive &ar,
                const unsigned long version );
        template void holder::Impl::serialize<archive::text_wiarchive>(
                archive::text_wiarchive &ar,
                const unsigned long version );
}; // end namespace test2



int _tmain(int argc, _TCHAR* argv[])
{
        using namespace test;
        {
                wofstream ofile( L"hierarchy.txt" );
                archive::text_woarchive oa(ofile);
                holder h;
                oa << (const holder&)h;
                ofile.close();
                holder ih;
                wifstream ifile( L"hierarchy.txt" );
                archive::text_wiarchive ia(ifile);
                ia >> ih;
                ifile.close();
        };
        getchar();
        return 0;
}


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