demo_xml.hpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. #ifndef BOOST_SERIALIZATION_EXAMPLE_DEMO_XML_HPP
  2. #define BOOST_SERIALIZATION_EXAMPLE_DEMO_XML_HPP
  3. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  4. //
  5. // demo_xml.hpp
  6. //
  7. // (C) Copyright 2002-4 Robert Ramey - http://www.rrsd.com .
  8. // Use, modification and distribution is subject to the Boost Software
  9. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  10. // http://www.boost.org/LICENSE_1_0.txt)
  11. #include <string>
  12. #include <iomanip>
  13. #include <iostream>
  14. #include <fstream>
  15. #include <boost/serialization/nvp.hpp>
  16. #include <boost/serialization/utility.hpp>
  17. #include <boost/serialization/list.hpp>
  18. #include <boost/serialization/version.hpp>
  19. // This illustration models the bus system of a small city.
  20. // This includes, multiple bus stops, bus routes and schedules.
  21. // There are different kinds of stops. Bus stops in general will
  22. // will appear on multiple routes. A schedule will include
  23. // muliple trips on the same route.
  24. /////////////////////////////////////////////////////////////
  25. // gps coordinate
  26. //
  27. // llustrates serialization for a simple type
  28. //
  29. class gps_position
  30. {
  31. friend class boost::serialization::access;
  32. friend std::ostream & operator<<(std::ostream &os, const gps_position &gp);
  33. int degrees;
  34. int minutes;
  35. float seconds;
  36. template<class Archive>
  37. void serialize(Archive & ar, const unsigned int /* file_version */){
  38. ar & BOOST_SERIALIZATION_NVP(degrees)
  39. & BOOST_SERIALIZATION_NVP(minutes)
  40. & BOOST_SERIALIZATION_NVP(seconds);
  41. }
  42. public:
  43. // every serializable class needs a constructor
  44. gps_position(){};
  45. gps_position(int _d, int _m, float _s) :
  46. degrees(_d), minutes(_m), seconds(_s)
  47. {}
  48. };
  49. std::ostream & operator<<(std::ostream &os, const gps_position &gp)
  50. {
  51. return os << ' ' << gp.degrees << (unsigned char)186 << gp.minutes << '\'' << gp.seconds << '"';
  52. }
  53. /////////////////////////////////////////////////////////////
  54. // One bus stop
  55. //
  56. // illustrates serialization of serializable members
  57. //
  58. class bus_stop
  59. {
  60. friend class boost::serialization::access;
  61. virtual std::string description() const = 0;
  62. gps_position latitude;
  63. gps_position longitude;
  64. template<class Archive>
  65. void serialize(Archive &ar, const unsigned int version)
  66. {
  67. ar & BOOST_SERIALIZATION_NVP(latitude);
  68. ar & BOOST_SERIALIZATION_NVP(longitude);
  69. }
  70. protected:
  71. bus_stop(const gps_position & _lat, const gps_position & _long) :
  72. latitude(_lat), longitude(_long)
  73. {}
  74. public:
  75. bus_stop(){}
  76. friend std::ostream & operator<<(std::ostream &os, const bus_stop &gp);
  77. virtual ~bus_stop(){}
  78. };
  79. BOOST_IS_ABSTRACT(bus_stop)
  80. std::ostream & operator<<(std::ostream &os, const bus_stop &bs)
  81. {
  82. return os << bs.latitude << bs.longitude << ' ' << bs.description();
  83. }
  84. /////////////////////////////////////////////////////////////
  85. // Several kinds of bus stops
  86. //
  87. // illustrates serialization of derived types
  88. //
  89. class bus_stop_corner : public bus_stop
  90. {
  91. friend class boost::serialization::access;
  92. std::string street1;
  93. std::string street2;
  94. virtual std::string description() const
  95. {
  96. return street1 + " and " + street2;
  97. }
  98. template<class Archive>
  99. void serialize(Archive &ar, const unsigned int version)
  100. {
  101. // save/load base class information
  102. ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(bus_stop);
  103. ar & BOOST_SERIALIZATION_NVP(street1);
  104. ar & BOOST_SERIALIZATION_NVP(street2);
  105. }
  106. public:
  107. bus_stop_corner(){}
  108. bus_stop_corner(const gps_position & _lat, const gps_position & _long,
  109. const std::string & _s1, const std::string & _s2
  110. ) :
  111. bus_stop(_lat, _long), street1(_s1), street2(_s2)
  112. {
  113. }
  114. };
  115. class bus_stop_destination : public bus_stop
  116. {
  117. friend class boost::serialization::access;
  118. std::string name;
  119. virtual std::string description() const
  120. {
  121. return name;
  122. }
  123. template<class Archive>
  124. void serialize(Archive &ar, const unsigned int version)
  125. {
  126. ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(bus_stop)
  127. & BOOST_SERIALIZATION_NVP(name);
  128. }
  129. public:
  130. bus_stop_destination(){}
  131. bus_stop_destination(
  132. const gps_position & _lat, const gps_position & _long, const std::string & _name
  133. ) :
  134. bus_stop(_lat, _long), name(_name)
  135. {
  136. }
  137. };
  138. /////////////////////////////////////////////////////////////
  139. // a bus route is a collection of bus stops
  140. //
  141. // illustrates serialization of STL collection templates.
  142. //
  143. // illustrates serialzation of polymorphic pointer (bus stop *);
  144. //
  145. // illustrates storage and recovery of shared pointers is correct
  146. // and efficient. That is objects pointed to by more than one
  147. // pointer are stored only once. In such cases only one such
  148. // object is restored and pointers are restored to point to it
  149. //
  150. class bus_route
  151. {
  152. friend class boost::serialization::access;
  153. friend std::ostream & operator<<(std::ostream &os, const bus_route &br);
  154. typedef bus_stop * bus_stop_pointer;
  155. std::list<bus_stop_pointer> stops;
  156. template<class Archive>
  157. void serialize(Archive &ar, const unsigned int version)
  158. {
  159. // in this program, these classes are never serialized directly but rather
  160. // through a pointer to the base class bus_stop. So we need a way to be
  161. // sure that the archive contains information about these derived classes.
  162. //ar.template register_type<bus_stop_corner>();
  163. ar.register_type(static_cast<bus_stop_corner *>(NULL));
  164. //ar.template register_type<bus_stop_destination>();
  165. ar.register_type(static_cast<bus_stop_destination *>(NULL));
  166. // serialization of stl collections is already defined
  167. // in the header
  168. ar & BOOST_SERIALIZATION_NVP(stops);
  169. }
  170. public:
  171. bus_route(){}
  172. void append(bus_stop *_bs)
  173. {
  174. stops.insert(stops.end(), _bs);
  175. }
  176. };
  177. std::ostream & operator<<(std::ostream &os, const bus_route &br)
  178. {
  179. std::list<bus_stop *>::const_iterator it;
  180. // note: we're displaying the pointer to permit verification
  181. // that duplicated pointers are properly restored.
  182. for(it = br.stops.begin(); it != br.stops.end(); it++){
  183. os << '\n' << std::hex << "0x" << *it << std::dec << ' ' << **it;
  184. }
  185. return os;
  186. }
  187. /////////////////////////////////////////////////////////////
  188. // a bus schedule is a collection of routes each with a starting time
  189. //
  190. // Illustrates serialization of STL objects(pair) in a non-intrusive way.
  191. // See definition of operator<< <pair<F, S> >(ar, pair)
  192. //
  193. // illustrates nesting of serializable classes
  194. //
  195. // illustrates use of version number to automatically grandfather older
  196. // versions of the same class.
  197. class bus_schedule
  198. {
  199. friend class boost::serialization::access;
  200. friend std::ostream & operator<<(std::ostream &os, const bus_schedule &bs);
  201. template<class Archive>
  202. void serialize(Archive &ar, const unsigned int version)
  203. {
  204. ar & BOOST_SERIALIZATION_NVP(schedule);
  205. }
  206. // note: this structure was made public. because the friend declarations
  207. // didn't seem to work as expected.
  208. public:
  209. struct trip_info
  210. {
  211. template<class Archive>
  212. void serialize(Archive &ar, const unsigned int file_version)
  213. {
  214. // in versions 2 or later
  215. if(file_version >= 2)
  216. // read the drivers name
  217. ar & BOOST_SERIALIZATION_NVP(driver);
  218. // all versions have the follwing info
  219. ar & BOOST_SERIALIZATION_NVP(hour)
  220. & BOOST_SERIALIZATION_NVP(minute);
  221. }
  222. // starting time
  223. int hour;
  224. int minute;
  225. // only after system shipped was the driver's name added to the class
  226. std::string driver;
  227. trip_info(){}
  228. trip_info(int _h, int _m, const std::string &_d) :
  229. hour(_h), minute(_m), driver(_d)
  230. {}
  231. ~trip_info(){
  232. }
  233. };
  234. // friend std::ostream & operator<<(std::ostream &os, const trip_info &ti);
  235. private:
  236. std::list<std::pair<trip_info, bus_route *> > schedule;
  237. public:
  238. void append(const std::string &_d, int _h, int _m, bus_route *_br)
  239. {
  240. schedule.insert(schedule.end(), std::make_pair(trip_info(_h, _m, _d), _br));
  241. }
  242. bus_schedule(){}
  243. };
  244. BOOST_CLASS_VERSION(bus_schedule::trip_info, 3)
  245. BOOST_CLASS_VERSION(bus_schedule, 2)
  246. std::ostream & operator<<(std::ostream &os, const bus_schedule::trip_info &ti)
  247. {
  248. return os << '\n' << ti.hour << ':' << ti.minute << ' ' << ti.driver << ' ';
  249. }
  250. std::ostream & operator<<(std::ostream &os, const bus_schedule &bs)
  251. {
  252. std::list<std::pair<bus_schedule::trip_info, bus_route *> >::const_iterator it;
  253. for(it = bs.schedule.begin(); it != bs.schedule.end(); it++){
  254. os << it->first << *(it->second);
  255. }
  256. return os;
  257. }
  258. #endif // BOOST_SERIALIZATION_EXAMPLE_DEMO_XML_HPP