|
Boost Users : |
Subject: Re: [Boost-users] Problems using boost serializingandcustomconstructors
From: Frank Birbacher (bloodymir.crap_at_[hidden])
Date: 2012-03-13 14:43:28
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hi!
Am 13.03.12 20:39, schrieb Robert Ramey:
> I posted a example - did this not work?
It worked for me using Boost 1.47. The code below can output or input
serialization data. It links to Boost.Serialization of course. I
tested it by running it twice and piping the output of the first into
the input of the second instance:
$dist/test 1 | dist/test 2
1
Frank
///// Main.cpp:
#include <vector>
struct A {
std::vector<unsigned> data;
};
struct B {
const std::vector<unsigned> * data_ptr;
};
struct C {
A a;
std::vector<B> elements;
};
template<class Archive>
void serialize(Archive &ar, A & a, const unsigned int version){
ar & a.data;
}
template<class Archive>
void serialize(Archive &ar, B & b, const unsigned int version){
ar & b.data_ptr;
}
template<class Archive>
void serialize(Archive &ar, C & c, const unsigned int version){
ar & c.a;
ar & c.elements;
}
struct Sample {
Sample();
C const& getC() { return c; }
C c;
} sample;
Sample::Sample()
{
c.a.data.push_back(5);
const B b = { &c.a.data };
c.elements.resize(2, b);
}
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/lexical_cast.hpp>
#include <iostream>
#include <ostream>
void output()
{
boost::archive::text_oarchive ar(std::cout);
ar << sample.c;
}
void input()
{
boost::archive::text_iarchive ar(std::cin);
ar >> sample.c;
std::cout << (!sample.c.elements.empty() && &sample.c.a.data ==
sample.c.elements.front().data_ptr)
<< std::endl;
}
int main(int argc, char *argv[])
{
if(argc < 2) {
std::cerr << "no argument\n";
return -3;
}
switch (boost::lexical_cast<unsigned>(argv[1])) {
case 1:
output();
break;
case 2:
input();
break;
default:
std::clog << "give option 1 or 2\n";
return -2;
}
}
-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.0.17 (Darwin)
Comment: GPGTools - http://gpgtools.org
Comment: keyserver x-hkp://pool.sks-keyservers.net
iEYEARECAAYFAk9flVAACgkQhAOUmAZhnmr53QCeNFO8S6Ph3ps9ZXemwoJsD5x/
jYwAn1/S+bKwVAXkHdYrti8LKBhtzLvc
=x19a
-----END PGP SIGNATURE-----
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