Boost logo

Boost Users :

Subject: Re: [Boost-users] [Serialization][BGL] problem serializing an edge
From: Ireneusz Szcześniak (irek.szczesniak_at_[hidden])
Date: 2010-07-20 13:03:23


Hi Cedric,

Thanks for your response. My graph in my code is actually a bit more
complicated than what I gave in my example. This is what I'm actually
using:

typedef
adjacency_list <vecS, vecS, undirectedS,
                 property<vertex_name_t, string,
                 property<vertex_distance_t, vector<int>,
                 property<vertex_predecessor_t, vector<Vertex>,
                 property<vertex_bundle_t, vector<my_class> > > > >,
                 property<edge_weight_t, int,
                 property<edge_weight2_t, int> > >
Graph;

I have many data structures that have edge_descriptors as their
fields, and I wouldn't like to refactor them only to make them vertex
or edge properties.

I agree that the reason for the compilation error is that the
Serialization library doesn't know how to serialize an
edge_descriptor. I don't know why the serialization code for the
adjacency list doesn't deliver the serialization code for the
edge_description. Should I write the serialization code for the
edge_descriptor? If so, then how?

I would appreciate it, if someone could fix the initial example, so
that it compiles:

#include <iostream>
#include <boost/archive/text_oarchive.hpp>
#include <boost/graph/adj_list_serialize.hpp>

using namespace std;
using namespace boost;

int
main()
{
     typedef adjacency_list<listS, vecS, undirectedS> Graph;
     typedef graph_traits<Graph>::edge_descriptor Edge;

     Graph g;
     Edge e;

     boost::archive::text_oarchive oa(cout);
     oa << g;
     oa << e;

     return 0;
}

Thanks,
Irek

-- 
Ireneusz (Irek) Szczesniak
http://www.irkos.org
Cedric Laczny wrote:
> Hi Irek,
> 
> as far as I can see from the example and the error message, the error seems to 
> be that there is no operator<< implemented for the edge_descriptor. So the 
> serialization does not know how it should save this instance of an edge 
> _descriptor. Same problem as when working with std::cout and custom classes 
> that have no operator<< defined.
> I remember having encountered issues when first working with boost::serialize 
> and boost graph lib. However, I don't exactly remember what the problems 
> where. In any case, I managed to sucessfully serialize a graph with custom  
> Vertex and Edge-Properties. You're adjacency_list is a rather straight-forward 
> example and thus should work well. At least as long as you don't want to 
> do/store more complex things with your vertices/edges ;)
> What I used there was the operator& instead of operator<<.
> So perhaps you might give it a try.
> 
> Also you might be interested in the code:
> #include <iostream>
> #include <fstream> 
> #include "Graph.hpp"
> #include "Properties.hpp"
> #include <boost/graph/adj_list_serialize.hpp>
> #include <boost/archive/text_iarchive.hpp>
> #include <boost/archive/text_oarchive.hpp> 
> 
> using namespace boost;
> 
> int main(int argc, char** argv)
> {
> 	typedef Graph<VertexProperties, EdgeProperties> MyGraph;
> 	std::list< std::pair< VertexProperties, VertexProperties> > v_props;
> 	v_props.push_back(std::make_pair(VertexProperties("a", "type_of_a"), 
> VertexProperties("b", "type_of_b")));
> 	MyGraph G(v_props);
> 	
> 	//Serialize it!
> 	std::ofstream ofs("./serialize_out.dat");
> 	archive::text_oarchive oa(ofs);
> 	oa & G; 
> 
> 	//DEserialize it!!!
> 	std::ifstream ifs("./serialize_in.dat");
> 	archive::text_iarchive ia(ifs);
> 	MyGraph g;
> 	ia & g; 
> 	Graph<VertexProperties, EdgeProperties>::Vertex v = g.getFirstVertex();
> 	std::cout << ((g.properties(v)).type) << std::endl;
> 
> 
> 	return 0;
> }
> 
> There's not much of a difference to your example about it, except for the 
> operator& and the custom Graph<>-class. I have not defined the operator& 
> anywhere, so it might very well work for your problem out-of-the-box also.
> If it doesn't please let the mailinglist, and thus me, know.
> 
> Best,
> 
> Cedric

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