position.cxx 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // file : examples/persistence/position.cxx
  2. // copyright : not copyrighted - public domain
  3. #include <iostream>
  4. #include <xml/parser>
  5. #include <xml/serializer>
  6. #include "position.hxx"
  7. using namespace std;
  8. using namespace xml;
  9. // position
  10. //
  11. position::
  12. position (parser& p)
  13. : lat_ (p.attribute<float> ("lat")),
  14. lon_ (p.attribute<float> ("lon"))
  15. {
  16. p.content (content::empty);
  17. }
  18. void position::
  19. serialize (serializer& s) const
  20. {
  21. s.attribute ("lat", lat_);
  22. s.attribute ("lon", lon_);
  23. }
  24. // object
  25. //
  26. object::
  27. object (parser& p)
  28. {
  29. // Note that for the root of the object model we parse the start/end
  30. // element ourselves instead of expecting the caller to do so. This
  31. // makes the client code nice and simple.
  32. //
  33. p.next_expect (parser::start_element, "object", content::complex);
  34. // Because of the above special case, this constructor is called
  35. // without start_element yet being parsed. As a result, we cannot
  36. // parse attributes or nested elements in the initializer list.
  37. //
  38. name_ = p.attribute ("name");
  39. type_ = p.attribute<object_type> ("type");
  40. id_ = p.attribute<unsigned int> ("id");
  41. do
  42. {
  43. p.next_expect (parser::start_element, "position");
  44. positions_.push_back (position (p));
  45. p.next_expect (parser::end_element);
  46. } while (p.peek () == parser::start_element);
  47. p.next_expect (xml::parser::end_element); // object
  48. }
  49. void object::
  50. serialize (serializer& s) const
  51. {
  52. // Note that for the root of the object model we serialize the
  53. // start/end element ourselves instead of expecting the caller
  54. // to do so. This makes the client code nice and simple.
  55. //
  56. s.start_element ("object");
  57. s.attribute ("name", name_);
  58. s.attribute ("type", type_);
  59. s.attribute ("id", id_);
  60. for (positions_type::const_iterator i (positions_.begin ());
  61. i != positions_.end ();
  62. ++i)
  63. {
  64. s.start_element ("position");
  65. i->serialize (s);
  66. s.end_element ();
  67. }
  68. s.end_element (); // object
  69. }
  70. // object_type
  71. //
  72. ostream&
  73. operator<< (ostream& os, object_type x)
  74. {
  75. if (x == building)
  76. os << "building";
  77. else
  78. os << "mountain";
  79. return os;
  80. }
  81. istream&
  82. operator>> (istream& is, object_type& x)
  83. {
  84. string s;
  85. if (is >> s)
  86. {
  87. if (s == "building")
  88. x = building;
  89. else if (s == "mountain")
  90. x = mountain;
  91. else
  92. is.setstate (istream::failbit);
  93. }
  94. return is;
  95. }