date_serialization_demo.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #include "boost/date_time/gregorian/gregorian.hpp"
  2. #include "boost/date_time/gregorian/greg_serialize.hpp"
  3. #include "boost/serialization/set.hpp"
  4. #include "boost/serialization/list.hpp"
  5. #include "boost/archive/text_oarchive.hpp"
  6. #include "boost/archive/text_iarchive.hpp"
  7. #include <iostream>
  8. #include <fstream>
  9. using namespace boost::gregorian;
  10. typedef std::set<date> date_set;
  11. typedef std::list<date> date_list;
  12. void print(std::ostream& os, const date_set& ds)
  13. {
  14. os << "******** Date Set *********" << std::endl;
  15. date_set::const_iterator itr = ds.begin();
  16. while (itr != ds.end())
  17. {
  18. os << (*itr) << " ";
  19. itr++;
  20. }
  21. os << "\n***************************" << std::endl;
  22. }
  23. class foo {
  24. public:
  25. foo(date d = date(not_a_date_time),
  26. int i = 0) :
  27. my_date(d),
  28. my_int(i)
  29. {}
  30. void insert_date(date d)
  31. {
  32. my_dates.push_back(d);
  33. }
  34. void print(std::ostream& os) const
  35. {
  36. os << "foo= my_date is: " << my_date
  37. << " my_int is: " << my_int;
  38. date_list::const_iterator i = my_dates.begin();
  39. os << " Important dates: ";
  40. while (i != my_dates.end()) {
  41. os << (*i) << " ";
  42. i++;
  43. }
  44. os << std::endl;
  45. }
  46. private:
  47. friend class boost::serialization::access;
  48. // is a type of input archive the & operator is defined similar to >>.
  49. template<class Archive>
  50. void serialize(Archive & ar, const unsigned int version)
  51. {
  52. ar & my_date;
  53. ar & my_int;
  54. ar & my_dates;
  55. }
  56. date my_date;
  57. int my_int;
  58. date_list my_dates;
  59. };
  60. int
  61. main()
  62. {
  63. try {
  64. date d(2004, Apr, 5);
  65. std::cout << "Date: " << to_iso_string(d) << std::endl;
  66. std::cout << "Date: " << d << std::endl;
  67. std::ofstream ofs("date_demo.txt");
  68. boost::archive::text_oarchive oa(ofs);
  69. oa << d;
  70. std::cout << "Construct a foo" << std::endl;
  71. foo f(d, 1);
  72. f.insert_date(d+days(1));
  73. f.insert_date(d+days(2));
  74. f.insert_date(d+days(3));
  75. f.print(std::cout);
  76. oa << f;
  77. date_set dates;
  78. dates.insert(date(2004, Apr,1));
  79. dates.insert(date(2004, Apr,10));
  80. dates.insert(date(2004, Apr,15));
  81. print(std::cout, dates);
  82. oa << dates;
  83. ofs.close();
  84. std::cout << "Now do the input streaming" << std::endl;
  85. date d2(not_a_date_time);
  86. std::ifstream ifs("date_demo.txt");
  87. boost::archive::text_iarchive ia(ifs);
  88. ia >> d2;
  89. std::cout << "New date is: " << d2 << std::endl;
  90. foo f2;
  91. ia >> f2;
  92. f2.print(std::cout);
  93. date_set dates2;
  94. ia >> dates2; //exception here
  95. print(std::cout, dates2);
  96. }
  97. catch(std::exception& e) {
  98. std::cout << "Caught Exception: " << e.what() << std::endl;
  99. }
  100. }
  101. /* Copyright 2001-2004: CrystalClear Software, Inc
  102. * http://www.crystalclearsoftware.com
  103. *
  104. * Subject to the Boost Software License, Version 1.0.
  105. * (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
  106. */