demo_gps.hpp 8.8 KB

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