Boost logo

Boost Users :

Subject: Re: [Boost-users] Boost Serialization - Base Classes (very confused)
From: Robert Ramey (ramey_at_[hidden])
Date: 2009-03-19 13:02:59


See below.

Robert Ramey

Adrien Guillon wrote:
> Hi all,
>
> I don't understand why the following code does not work. I get an
> unregistered class exception.
>
> AJ
>
>
> #include <boost/serialization/serialization.hpp>
> #include <boost/archive/text_oarchive.hpp>
> #include <boost/archive/text_iarchive.hpp>
> #include <boost/serialization/export.hpp>
>
> #include <string>
> #include <sstream>
> #include <iostream>
>
> namespace pbb { // BEGIN NAMESPACE pbb
> namespace internal { // BEGIN NAMESPACE internal
>
> /**
> * The base class for all requests to be made of a server.
> */
> class server_request
> {
> friend class boost::serialization::access;
> public:
> virtual ~server_request();
> virtual std::string description() = 0;
> private:
> template <typename Archive>
> void serialize(Archive& ar, const unsigned int version)
> {
> }
> };
>
> class hello_request : public server_request
> {
> friend class boost::serialization::access;
> public:
> virtual ~hello_request();
> virtual std::string description();
> private:
> template <typename Archive>
> void serialize(Archive& ar, const unsigned int version)
> {
> ar & boost::serialization::base_object<server_request>(*this);
> }
> };
>
> } // END NAMESPACE internal
> } // END NAMESPACE pbb
>
>
> namespace pbb { // BEGIN NAMESPACE pbb
> namespace internal { // BEGIN NAMESPACE internal
>
>
> server_request::~server_request()
> {
> }
>
> hello_request::~hello_request()
> {
> }
>
> std::string hello_request::description()
> {
> return "HELLO REQUEST";
> }
>
> } // END NAMESPACE internal
> } // END NAMESPACE pbb
>
>
> BOOST_CLASS_EXPORT(pbb::internal::server_request);
> BOOST_CLASS_EXPORT(pbb::internal::hello_request);
>
> void msave(const pbb::internal::server_request& request,
> boost::archive::text_oarchive& o)
> {
> o << request;
> }
>
> pbb::internal::server_request* mload(boost::archive::text_iarchive&
> in) {
> pbb::internal::server_request* r;
> in >> r;
> return r;
> }
>
>
> int main()
> {
>
> // Create a string stream for I/O
> std::stringstream stream;
>
> pbb::internal::server_request* request = new
> pbb::internal::hello_request();
>
> // Write a server request
> {
>
> boost::archive::text_oarchive out_archive(stream);
> //msave(*request, out_archive); // uh oh - throwing away "pointerness"
      out_archve << request;
> }
>
> // Read a server request
> pbb::internal::server_request* incoming_request;>
> {
> boost::archive::text_iarchive in_archive(stream);
> incoming_request = mload(in_archive); // preserve symetry
      in_archve >> incoming request
> }
>
> std::cout << incoming_request->description() << std::endl;
>
>
>
> }


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