serialize.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright (C) 2006 Trustees of Indiana University
  2. //
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // (See accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. #include <boost/config.hpp>
  7. #include <iostream>
  8. #include <fstream>
  9. #include <string>
  10. #include <boost/tuple/tuple.hpp>
  11. #include <boost/graph/adjacency_list.hpp>
  12. #include <boost/graph/visitors.hpp>
  13. #include <boost/graph/breadth_first_search.hpp>
  14. #include <map>
  15. #include <boost/graph/adj_list_serialize.hpp>
  16. #include <boost/archive/xml_iarchive.hpp>
  17. #include <boost/archive/xml_oarchive.hpp>
  18. struct vertex_properties {
  19. std::string name;
  20. template<class Archive>
  21. void serialize(Archive & ar, const unsigned int /*version*/) {
  22. ar & BOOST_SERIALIZATION_NVP(name);
  23. }
  24. };
  25. struct edge_properties {
  26. std::string name;
  27. template<class Archive>
  28. void serialize(Archive & ar, const unsigned int /*version*/) {
  29. ar & BOOST_SERIALIZATION_NVP(name);
  30. }
  31. };
  32. using namespace boost;
  33. typedef adjacency_list<vecS, vecS, undirectedS,
  34. vertex_properties, edge_properties> Graph;
  35. typedef graph_traits<Graph>::vertex_descriptor vd_type;
  36. typedef adjacency_list<vecS, vecS, undirectedS,
  37. vertex_properties> Graph_no_edge_property;
  38. int main()
  39. {
  40. {
  41. std::ofstream ofs("./kevin-bacon2.dat");
  42. archive::xml_oarchive oa(ofs);
  43. Graph g;
  44. vertex_properties vp;
  45. vp.name = "A";
  46. vd_type A = add_vertex( vp, g );
  47. vp.name = "B";
  48. vd_type B = add_vertex( vp, g );
  49. edge_properties ep;
  50. ep.name = "a";
  51. add_edge( A, B, ep, g);
  52. oa << BOOST_SERIALIZATION_NVP(g);
  53. Graph_no_edge_property g_n;
  54. oa << BOOST_SERIALIZATION_NVP(g_n);
  55. }
  56. {
  57. std::ifstream ifs("./kevin-bacon2.dat");
  58. archive::xml_iarchive ia(ifs);
  59. Graph g;
  60. ia >> BOOST_SERIALIZATION_NVP(g);
  61. if (!( g[*(vertices( g ).first)].name == "A" )) return -1;
  62. Graph_no_edge_property g_n;
  63. ia >> BOOST_SERIALIZATION_NVP(g_n);
  64. }
  65. return 0;
  66. }