1 //=======================================================================
2 // Copyright 2005 Jeremy G. Siek
3 // Authors: Jeremy G. Siek
5 // Distributed under the Boost Software License, Version 1.0. (See
6 // accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
8 //=======================================================================
9 #ifndef BOOST_GRAPH_ADJ_LIST_SERIALIZE_HPP
10 #define BOOST_GRAPH_ADJ_LIST_SERIALIZE_HPP
12 #include <boost/graph/adjacency_list.hpp>
13 #include <boost/graph/iteration_macros.hpp>
14 #include <boost/pending/property_serialize.hpp>
15 #include <boost/config.hpp>
16 #include <boost/detail/workaround.hpp>
18 #include <boost/serialization/collections_save_imp.hpp>
19 #include <boost/serialization/collections_load_imp.hpp>
20 #include <boost/serialization/split_free.hpp>
25 namespace serialization
28 // Turn off tracking for adjacency_list. It's not polymorphic, and we
29 // need to do this to enable saving of non-const adjacency lists.
30 template < class OEL, class VL, class D, class VP, class EP, class GP,
32 struct tracking_level< boost::adjacency_list< OEL, VL, D, VP, EP, GP, EL > >
34 typedef mpl::integral_c_tag tag;
35 typedef mpl::int_< track_never > type;
36 BOOST_STATIC_CONSTANT(int, value = tracking_level::type::value);
39 template < class Archive, class OEL, class VL, class D, class VP, class EP,
41 inline void save(Archive& ar,
42 const boost::adjacency_list< OEL, VL, D, VP, EP, GP, EL >& graph,
43 const unsigned int /* file_version */
46 typedef adjacency_list< OEL, VL, D, VP, EP, GP, EL > Graph;
47 typedef typename graph_traits< Graph >::vertex_descriptor Vertex;
49 int V = num_vertices(graph);
50 int E = num_edges(graph);
51 ar << BOOST_SERIALIZATION_NVP(V);
52 ar << BOOST_SERIALIZATION_NVP(E);
54 // assign indices to vertices
55 std::map< Vertex, int > indices;
57 BGL_FORALL_VERTICES_T(v, graph, Graph)
60 ar << serialization::make_nvp(
61 "vertex_property", get(vertex_all_t(), graph, v));
65 BGL_FORALL_EDGES_T(e, graph, Graph)
67 ar << serialization::make_nvp("u", indices[source(e, graph)]);
68 ar << serialization::make_nvp("v", indices[target(e, graph)]);
69 ar << serialization::make_nvp(
70 "edge_property", get(edge_all_t(), graph, e));
73 ar << serialization::make_nvp(
74 "graph_property", get_property(graph, graph_all_t()));
77 template < class Archive, class OEL, class VL, class D, class VP, class EP,
80 Archive& ar, boost::adjacency_list< OEL, VL, D, VP, EP, GP, EL >& graph,
81 const unsigned int /* file_version */
84 typedef adjacency_list< OEL, VL, D, VP, EP, GP, EL > Graph;
85 typedef typename graph_traits< Graph >::vertex_descriptor Vertex;
86 typedef typename graph_traits< Graph >::edge_descriptor Edge;
89 ar >> BOOST_SERIALIZATION_NVP(V);
91 ar >> BOOST_SERIALIZATION_NVP(E);
93 std::vector< Vertex > verts(V);
97 Vertex v = add_vertex(graph);
99 ar >> serialization::make_nvp(
100 "vertex_property", get(vertex_all_t(), graph, v));
106 ar >> BOOST_SERIALIZATION_NVP(u);
107 ar >> BOOST_SERIALIZATION_NVP(v);
110 boost::tie(e, inserted) = add_edge(verts[u], verts[v], graph);
111 ar >> serialization::make_nvp(
112 "edge_property", get(edge_all_t(), graph, e));
114 ar >> serialization::make_nvp(
115 "graph_property", get_property(graph, graph_all_t()));
118 template < class Archive, class OEL, class VL, class D, class VP, class EP,
120 inline void serialize(Archive& ar,
121 boost::adjacency_list< OEL, VL, D, VP, EP, GP, EL >& graph,
122 const unsigned int file_version)
124 boost::serialization::split_free(ar, graph, file_version);
130 #endif // BOOST_GRAPH_ADJ_LIST_SERIALIZE_HPP