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