|
Boost : |
From: FATH Francois (francois.fath-renexter_at_[hidden])
Date: 2004-05-07 04:24:16
I have difficulties when serializing two objects pointing to each other.
The generated archive is fine (I can see both references in it), but
when deserializing, only one is restored. In the attached example, I
have a collection of nodes, each one is pointing to the previous and to
the next node. When deserializing, only the ->next references are
restored, all the ->previous are NULL.
The compiler is VC7.1
Thanks,
Francois Fath
-- Disclaimer ------------------------------------
Ce message ainsi que les eventuelles pieces jointes constituent une correspondance privee et confidentielle a l'attention exclusive du destinataire designe ci-dessus. Si vous n'etes pas le destinataire du present message ou une personne susceptible de pouvoir le lui delivrer, il vous est signifie que toute divulgation, distribution ou copie de cette transmission est strictement interdite. Si vous avez recu ce message par erreur, nous vous remercions d'en informer l'expediteur par telephone ou de lui retourner le present message, puis d'effacer immediatement ce message de votre systeme.
***
This e-mail and any attachments is a confidential correspondence intended only for use of the individual or entity named above. If you are not the intended recipient or the agent responsible for delivering the message to the intended recipient, you are hereby notified that any disclosure, distribution or copying of this communication is strictly prohibited. If you have received this communication in error, please notify the sender by phone or by replying this message, and then delete this message from your system.
#include "stdafx.h"
#include <fstream>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>
using namespace std;
class Node
{
public:
Node(Node *previous = NULL, Node *next = NULL)
{
id = ++count;
this->previous = previous;
if (previous) previous->next = this;
this->next = next;
if (next) next->previous = this;
}
~Node()
{
if (previous) previous->next = next;
if (next) next->previous = previous;
}
private:
static unsigned int count;
unsigned int id;
Node *previous;
Node *next;
friend class boost::serialization::access;
template<class Archive> void serialize(Archive & ar, const unsigned int version)
{
ar & BOOST_SERIALIZATION_NVP(id);
ar & BOOST_SERIALIZATION_NVP(previous);
ar & BOOST_SERIALIZATION_NVP(next);
}
};
unsigned int Node::count = 0;
int _tmain(int argc, _TCHAR* argv[])
{
Node *head = new Node();
Node *node = new Node(head);
node = new Node(node);
node = new Node(node);
// serialize
{
ofstream ofs("cyclic.xml");
boost::archive::xml_oarchive oa(ofs);
oa << BOOST_SERIALIZATION_NVP(head);
}
// deserialize
Node *other_node = NULL;
{
ifstream ifs("cyclic.xml");
boost::archive::xml_iarchive ia(ifs);
ia >> BOOST_SERIALIZATION_NVP(other_node);
}
//quit
return 0;
}
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk