demo_xml.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  2. //
  3. // demo_xml.cpp
  4. //
  5. // (C) Copyright 2002-4 Robert Ramey - http://www.rrsd.com .
  6. // Use, modification and distribution is subject to the Boost Software
  7. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. #include <iomanip>
  10. #include <iostream>
  11. #include <fstream>
  12. #include <string>
  13. #include <cstdio> // remove
  14. #include <boost/config.hpp>
  15. #if defined(BOOST_NO_STDC_NAMESPACE)
  16. namespace std{
  17. using ::remove;
  18. }
  19. #endif
  20. #include <boost/archive/tmpdir.hpp>
  21. #include <boost/archive/xml_iarchive.hpp>
  22. #include <boost/archive/xml_oarchive.hpp>
  23. #include "demo_gps.hpp"
  24. void save_schedule(const bus_schedule &s, const char * filename){
  25. // make an archive
  26. std::ofstream ofs(filename);
  27. assert(ofs.good());
  28. boost::archive::xml_oarchive oa(ofs);
  29. oa << BOOST_SERIALIZATION_NVP(s);
  30. }
  31. void
  32. restore_schedule(bus_schedule &s, const char * filename)
  33. {
  34. // open the archive
  35. std::ifstream ifs(filename);
  36. assert(ifs.good());
  37. boost::archive::xml_iarchive ia(ifs);
  38. // restore the schedule from the archive
  39. ia >> BOOST_SERIALIZATION_NVP(s);
  40. }
  41. int main(int argc, char *argv[])
  42. {
  43. // make the schedule
  44. bus_schedule original_schedule;
  45. // fill in the data
  46. // make a few stops
  47. bus_stop *bs0 = new bus_stop_corner(
  48. gps_position(34, 135, 52.560f),
  49. gps_position(134, 22, 78.30f),
  50. "24th Street", "10th Avenue"
  51. );
  52. bus_stop *bs1 = new bus_stop_corner(
  53. gps_position(35, 137, 23.456f),
  54. gps_position(133, 35, 54.12f),
  55. "State street", "Cathedral Vista Lane"
  56. );
  57. bus_stop *bs2 = new bus_stop_destination(
  58. gps_position(35, 136, 15.456f),
  59. gps_position(133, 32, 15.300f),
  60. "White House"
  61. );
  62. bus_stop *bs3 = new bus_stop_destination(
  63. gps_position(35, 134, 48.789f),
  64. gps_position(133, 32, 16.230f),
  65. "Lincoln Memorial"
  66. );
  67. // make a routes
  68. bus_route route0;
  69. route0.append(bs0);
  70. route0.append(bs1);
  71. route0.append(bs2);
  72. // add trips to schedule
  73. original_schedule.append("bob", 6, 24, &route0);
  74. original_schedule.append("bob", 9, 57, &route0);
  75. original_schedule.append("alice", 11, 02, &route0);
  76. // make aother routes
  77. bus_route route1;
  78. route1.append(bs3);
  79. route1.append(bs2);
  80. route1.append(bs1);
  81. // add trips to schedule
  82. original_schedule.append("ted", 7, 17, &route1);
  83. original_schedule.append("ted", 9, 38, &route1);
  84. original_schedule.append("alice", 11, 47, &route1);
  85. // display the complete schedule
  86. std::cout << "original schedule";
  87. std::cout << original_schedule;
  88. std::string filename(boost::archive::tmpdir());
  89. filename += "/demo.xml";
  90. // save the schedule
  91. save_schedule(original_schedule, filename.c_str());
  92. // ... some time later
  93. // make a new schedule
  94. bus_schedule new_schedule;
  95. restore_schedule(new_schedule, filename.c_str());
  96. // and display
  97. std::cout << "\nrestored schedule";
  98. std::cout << new_schedule;
  99. // should be the same as the old one. (except for the pointer values)
  100. std::remove(filename.c_str());
  101. delete bs0;
  102. delete bs1;
  103. delete bs2;
  104. delete bs3;
  105. return 0;
  106. }