|
Boost Users : |
From: Robert Ramey (ramey_at_[hidden])
Date: 2006-03-24 21:53:36
I made a change in your program so it now compiles. For an explanation of
what I did
see the "Rationale" section of the documentation
Robert Ramey
/////////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/tmpdir.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/serialization/base_object.hpp>
#include <boost/serialization/utility.hpp>
#include <boost/serialization/list.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)
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)
{
}
};
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)
{
}
};
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 mailing list
Boost-users_at_[hidden]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
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