Boost logo

Boost Users :

From: wayne.x.liu_at_[hidden]
Date: 2006-03-27 10:47:00


Thanks for the help. The new code works.

For my application, I would like to use BOOST_CLASS_EXPORT_GUID to
simplify the registration process. Changing the demo code to use EXPORT
causes it to core dump. Perhaps I'm not using correctly... Again, help
would be appreciated.

Wayne Liu

#0 0x004a2575 in
boost::archive::detail::basic_iarchive_impl::load_preamble ()
   from
/cbb/public/wayne/build/lib/libboost_serialization-gcc-1_33_1.so.1.33.1
#1 0x004a30b9 in
boost::archive::detail::basic_iarchive_impl::load_pointer ()
   from
/cbb/public/wayne/build/lib/libboost_serialization-gcc-1_33_1.so.1.33.1
#2 0x004a2934 in boost::archive::detail::basic_iarchive::load_pointer ()
   from
/cbb/public/wayne/build/lib/libboost_serialization-gcc-1_33_1.so.1.33.1
#3 0x0804d989 in
boost::archive::detail::load_pointer_type<boost::archive::text_iarchive,
bus_stop*>::invoke (ar=@0xbfff7fd0, t=@0xbfff800c)
    at
/cbb/public/wayne/build/include/boost-1_33_1/boost/archive/detail/iserializer.hpp:486
#4 0x0804d8f6 in load<boost::archive::text_iarchive, bus_stop*>
(ar=@0xbfff7fd0, t=@0xbfff800c)
    at
/cbb/public/wayne/build/include/boost-1_33_1/boost/archive/detail/iserializer.hpp:572
#5 0x0804d80b in
boost::archive::basic_text_iarchive<boost::archive::text_iarchive>::load_override<bus_stop*>
(this=0xbfff7fd0, t=@0xbfff800c)
    at
/cbb/public/wayne/build/include/boost-1_33_1/boost/archive/basic_text_iarchive.hpp:64
#6 0x0804d718 in
boost::archive::text_iarchive_impl<boost::archive::text_iarchive>::load_override<bus_stop*>
(this=0xbfff7fd0, t=@0xbfff800c)
    at
/cbb/public/wayne/build/include/boost-1_33_1/boost/archive/text_iarchive.hpp:64
#7 0x0804d65d in operator>><bus_stop*> (this=0xbfff7fd0, t=@0xbfff800c)
    at
/cbb/public/wayne/build/include/boost-1_33_1/boost/archive/detail/interface_iarchive.hpp:76
#8 0x0804c807 in restore (ss=@0xbfff8070) at test/d3.cpp:138
#9 0x0804cf69 in main (argc=1, argv=0xbfff8274) at test/d3.cpp:180

/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
// demo.cpp
//
// (C) Copyright 2002-4 Robert Ramey - http://www.rrsd.com .
// Use, modification and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>

#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>

#include <boost/serialization/access.hpp>
#include <boost/serialization/base_object.hpp>
#include <boost/serialization/utility.hpp>

#include <boost/serialization/export.hpp>

/////////////////////////////////////////////////////////////
// One bus stop
//
// illustrates serialization of serializable members
//

class bus_stop
{
    friend class boost::serialization::access;
    friend std::ostream & operator<<(std::ostream &os, const bus_stop
&gp);
    virtual std::string description() const = 0;
    int latitude;
    int longitude;
    template<class Archive>
    void serialize(Archive &ar, const unsigned int version)
    {
        ar & latitude;
        ar & longitude;
    }
protected:
    bus_stop(const int & _lat, const int & _long) :
        latitude(_lat), longitude(_long)
    {}
public:
    bus_stop(){}
    virtual ~bus_stop(){}
};

BOOST_IS_ABSTRACT(bus_stop)
BOOST_CLASS_EXPORT_GUID(bus_stop, "bus_stop")

std::ostream & operator<<(std::ostream &os, const bus_stop &bs)
{
    return os << bs.latitude << bs.longitude << ' ' << bs.description();
}

/////////////////////////////////////////////////////////////
// Several kinds of bus stops
//
// illustrates serialization of derived types
//
class bus_stop_corner : public bus_stop
{
    friend class boost::serialization::access;
    std::string street1;
    std::string street2;
    virtual std::string description() const
    {
        return street1 + " and " + street2;
    }
    template<class Archive>
    void serialize(Archive &ar, const unsigned int version)
    {
        // save/load base class information
        ar & boost::serialization::base_object<bus_stop>(*this);
        ar & street1 & street2;
    }

public:
    bus_stop_corner(){}
    bus_stop_corner(const int & _lat, const int & _long,
        const std::string & _s1, const std::string & _s2
    ) :
        bus_stop(_lat, _long), street1(_s1), street2(_s2)
    {
    }
};
BOOST_CLASS_EXPORT_GUID(bus_stop_corner, "bus_stop_corner")

class bus_stop_destination : public bus_stop
{
    friend class boost::serialization::access;
    std::string name;
    virtual std::string description() const
    {
        return name;
    }
    template<class Archive>
    void serialize(Archive &ar, const unsigned int version)
    {
        ar & boost::serialization::base_object<bus_stop>(*this) & name;
    }
public:

    bus_stop_destination(){}
    bus_stop_destination(
        const int & _lat, const int & _long, const std::string & _name
    ) :
        bus_stop(_lat, _long), name(_name)
    {
    }
};
BOOST_CLASS_EXPORT_GUID(bus_stop_destination, "bus_stop_destination")

void save(const bus_stop * const s, std::stringstream &ss) /* NOTE extra
const here !! */
{
    std::cout << "original";
    std::cout << *s;
    boost::archive::text_oarchive oa(ss);
//oa.register_type(static_cast<bus_stop_corner *>(NULL));
//oa.register_type(static_cast<bus_stop_destination *>(NULL));

    oa << s;
}

void
restore( std::stringstream &ss)
{
    bus_stop *s;
    boost::archive::text_iarchive ia(ss);
//ia.register_type(static_cast<bus_stop_corner *>(NULL));
//ia.register_type(static_cast<bus_stop_destination *>(NULL));
    ia >> s;
    std::cout << "restored";
    std::cout << *s;
}

int main(int argc, char *argv[])
{
    // fill in the data
    // make a few stops
    bus_stop *bs0 = new bus_stop_corner(
        34,
        134,
        "24th Street", "10th Avenue"
    );
    bus_stop *bs1 = new bus_stop_corner(
        35,
        133,
        "State street", "Cathedral Vista Lane"
    );
    bus_stop *bs2 = new bus_stop_destination(
        35,
        133,
        "White House"
    );
    bus_stop *bs3 = new bus_stop_destination(
        35,
        133,
        "Lincoln Memorial"
    );

{
    std::stringstream ss;
    // display the complete schedule
    save(bs0,ss);
    restore(ss);
}

{
    std::stringstream ss;
    // display the complete schedule
    save(bs1,ss);
    restore(ss);
}

{
    std::stringstream ss;
    // display the complete schedule
    save(bs2,ss);
    restore(ss);
}

{
    std::stringstream ss;
    // display the complete schedule
    save(bs3,ss);
    restore(ss);
}

    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