fix doc example typo
[boost.git] / boost / date_time / gregorian / greg_serialize.hpp
blob3d17b15170d22c39fb894477af74d135225debbd
1 #ifndef GREGORIAN_SERIALIZE_HPP___
2 #define GREGORIAN_SERIALIZE_HPP___
4 /* Copyright (c) 2004-2005 CrystalClear Software, Inc.
5 * Use, modification and distribution is subject to the
6 * Boost Software License, Version 1.0. (See accompanying
7 * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
8 * Author: Jeff Garland, Bart Garst
9 * $Date$
12 #include "boost/date_time/gregorian/gregorian_types.hpp"
13 #include "boost/date_time/gregorian/parsers.hpp"
14 #include "boost/serialization/split_free.hpp"
17 // macros to split serialize functions into save & load functions
18 // An expanded version is below for gregorian::date
19 // NOTE: these macros define template functions in the boost::serialization namespace.
20 // They must be expanded *outside* of any namespace
21 BOOST_SERIALIZATION_SPLIT_FREE(::boost::gregorian::date_duration)
22 BOOST_SERIALIZATION_SPLIT_FREE(::boost::gregorian::date_duration::duration_rep)
23 BOOST_SERIALIZATION_SPLIT_FREE(::boost::gregorian::date_period)
24 BOOST_SERIALIZATION_SPLIT_FREE(::boost::gregorian::greg_month)
25 BOOST_SERIALIZATION_SPLIT_FREE(::boost::gregorian::greg_day)
26 BOOST_SERIALIZATION_SPLIT_FREE(::boost::gregorian::greg_weekday)
27 BOOST_SERIALIZATION_SPLIT_FREE(::boost::gregorian::partial_date)
28 BOOST_SERIALIZATION_SPLIT_FREE(::boost::gregorian::nth_kday_of_month)
29 BOOST_SERIALIZATION_SPLIT_FREE(::boost::gregorian::first_kday_of_month)
30 BOOST_SERIALIZATION_SPLIT_FREE(::boost::gregorian::last_kday_of_month)
31 BOOST_SERIALIZATION_SPLIT_FREE(::boost::gregorian::first_kday_before)
32 BOOST_SERIALIZATION_SPLIT_FREE(::boost::gregorian::first_kday_after)
34 namespace boost {
35 namespace serialization {
37 /*! Method that does serialization for gregorian::date -- splits to load/save
39 template<class Archive>
40 inline void serialize(Archive & ar,
41 ::boost::gregorian::date & d,
42 const unsigned int file_version)
44 split_free(ar, d, file_version);
47 //! Function to save gregorian::date objects using serialization lib
48 /*! Dates are serialized into a string for transport and storage.
49 * While it would be more efficient to store the internal
50 * integer used to manipulate the dates, it is an unstable solution.
52 template<class Archive>
53 void save(Archive & ar,
54 const ::boost::gregorian::date & d,
55 unsigned int /* version */)
57 std::string ds = to_iso_string(d);
58 ar & make_nvp("date", ds);
61 //! Function to load gregorian::date objects using serialization lib
62 /*! Dates are serialized into a string for transport and storage.
63 * While it would be more efficient to store the internal
64 * integer used to manipulate the dates, it is an unstable solution.
66 template<class Archive>
67 void load(Archive & ar,
68 ::boost::gregorian::date & d,
69 unsigned int /*version*/)
71 std::string ds;
72 ar & make_nvp("date", ds);
73 try{
74 d = ::boost::gregorian::from_undelimited_string(ds);
75 }catch(bad_lexical_cast&) {
76 gregorian::special_values sv = gregorian::special_value_from_string(ds);
77 if(sv == gregorian::not_special) {
78 throw; // no match found, rethrow original exception
80 else {
81 d = gregorian::date(sv);
87 //!override needed b/c no default constructor
88 template<class Archive>
89 inline void load_construct_data(Archive & ar,
90 ::boost::gregorian::date* dp,
91 const unsigned int /*file_version*/)
93 // retrieve data from archive required to construct new
94 // invoke inplace constructor to initialize instance of date
95 ::new(dp) ::boost::gregorian::date(::boost::gregorian::not_a_date_time);
98 /**** date_duration ****/
100 //! Function to save gregorian::date_duration objects using serialization lib
101 template<class Archive>
102 void save(Archive & ar, const gregorian::date_duration & dd,
103 unsigned int /*version*/)
105 typename gregorian::date_duration::duration_rep dr = dd.get_rep();
106 ar & make_nvp("date_duration", dr);
108 //! Function to load gregorian::date_duration objects using serialization lib
109 template<class Archive>
110 void load(Archive & ar, gregorian::date_duration & dd, unsigned int /*version*/)
112 typename gregorian::date_duration::duration_rep dr(0);
113 ar & make_nvp("date_duration", dr);
114 dd = gregorian::date_duration(dr);
116 //!override needed b/c no default constructor
117 template<class Archive>
118 inline void load_construct_data(Archive & ar, gregorian::date_duration* dd,
119 const unsigned int /*file_version*/)
121 ::new(dd) gregorian::date_duration(gregorian::not_a_date_time);
124 /**** date_duration::duration_rep (most likely int_adapter) ****/
126 //! helper unction to save date_duration objects using serialization lib
127 template<class Archive>
128 void save(Archive & ar, const gregorian::date_duration::duration_rep & dr,
129 unsigned int /*version*/)
131 typename gregorian::date_duration::duration_rep::int_type it = dr.as_number();
132 ar & make_nvp("date_duration_duration_rep", it);
134 //! helper function to load date_duration objects using serialization lib
135 template<class Archive>
136 void load(Archive & ar, gregorian::date_duration::duration_rep & dr, unsigned int /*version*/)
138 typename gregorian::date_duration::duration_rep::int_type it(0);
139 ar & make_nvp("date_duration_duration_rep", it);
140 dr = gregorian::date_duration::duration_rep::int_type(it);
142 //!override needed b/c no default constructor
143 template<class Archive>
144 inline void load_construct_data(Archive & ar, gregorian::date_duration::duration_rep* dr,
145 const unsigned int /*file_version*/)
147 ::new(dr) gregorian::date_duration::duration_rep(0);
150 /**** date_period ****/
152 //! Function to save gregorian::date_period objects using serialization lib
153 /*! date_period objects are broken down into 2 parts for serialization:
154 * the begining date object and the end date object
156 template<class Archive>
157 void save(Archive & ar, const gregorian::date_period& dp,
158 unsigned int /*version*/)
160 gregorian::date d1 = dp.begin();
161 gregorian::date d2 = dp.end();
162 ar & make_nvp("date_period_begin_date", d1);
163 ar & make_nvp("date_period_end_date", d2);
165 //! Function to load gregorian::date_period objects using serialization lib
166 /*! date_period objects are broken down into 2 parts for serialization:
167 * the begining date object and the end date object
169 template<class Archive>
170 void load(Archive & ar, gregorian::date_period& dp, unsigned int /*version*/)
172 gregorian::date d1(gregorian::not_a_date_time);
173 gregorian::date d2(gregorian::not_a_date_time);
174 ar & make_nvp("date_period_begin_date", d1);
175 ar & make_nvp("date_period_end_date", d2);
176 dp = gregorian::date_period(d1,d2);
178 //!override needed b/c no default constructor
179 template<class Archive>
180 inline void load_construct_data(Archive & ar, gregorian::date_period* dp,
181 const unsigned int /*file_version*/)
183 gregorian::date d(gregorian::not_a_date_time);
184 gregorian::date_duration dd(1);
185 ::new(dp) gregorian::date_period(d,dd);
188 /**** greg_month ****/
190 //! Function to save gregorian::greg_month objects using serialization lib
191 template<class Archive>
192 void save(Archive & ar, const gregorian::greg_month& gm,
193 unsigned int /*version*/)
195 unsigned short us = gm.as_number();
196 ar & make_nvp("greg_month", us);
198 //! Function to load gregorian::greg_month objects using serialization lib
199 template<class Archive>
200 void load(Archive & ar, gregorian::greg_month& gm, unsigned int /*version*/)
202 unsigned short us;
203 ar & make_nvp("greg_month", us);
204 gm = gregorian::greg_month(us);
206 //!override needed b/c no default constructor
207 template<class Archive>
208 inline void load_construct_data(Archive & ar, gregorian::greg_month* gm,
209 const unsigned int /*file_version*/)
211 ::new(gm) gregorian::greg_month(1);
214 /**** greg_day ****/
216 //! Function to save gregorian::greg_day objects using serialization lib
217 template<class Archive>
218 void save(Archive & ar, const gregorian::greg_day& gd,
219 unsigned int /*version*/)
221 unsigned short us = gd.as_number();
222 ar & make_nvp("greg_day", us);
224 //! Function to load gregorian::greg_day objects using serialization lib
225 template<class Archive>
226 void load(Archive & ar, gregorian::greg_day& gd, unsigned int /*version*/)
228 unsigned short us;
229 ar & make_nvp("greg_day", us);
230 gd = gregorian::greg_day(us);
232 //!override needed b/c no default constructor
233 template<class Archive>
234 inline void load_construct_data(Archive & ar, gregorian::greg_day* gd,
235 const unsigned int /*file_version*/)
237 ::new(gd) gregorian::greg_day(1);
240 /**** greg_weekday ****/
242 //! Function to save gregorian::greg_weekday objects using serialization lib
243 template<class Archive>
244 void save(Archive & ar, const gregorian::greg_weekday& gd,
245 unsigned int /*version*/)
247 unsigned short us = gd.as_number();
248 ar & make_nvp("greg_weekday", us);
250 //! Function to load gregorian::greg_weekday objects using serialization lib
251 template<class Archive>
252 void load(Archive & ar, gregorian::greg_weekday& gd, unsigned int /*version*/)
254 unsigned short us;
255 ar & make_nvp("greg_weekday", us);
256 gd = gregorian::greg_weekday(us);
258 //!override needed b/c no default constructor
259 template<class Archive>
260 inline void load_construct_data(Archive & ar, gregorian::greg_weekday* gd,
261 const unsigned int /*file_version*/)
263 ::new(gd) gregorian::greg_weekday(1);
266 /**** date_generators ****/
268 /**** partial_date ****/
270 //! Function to save gregorian::partial_date objects using serialization lib
271 /*! partial_date objects are broken down into 2 parts for serialization:
272 * the day (typically greg_day) and month (typically greg_month) objects
274 template<class Archive>
275 void save(Archive & ar, const gregorian::partial_date& pd,
276 unsigned int /*version*/)
278 gregorian::greg_day gd(pd.day());
279 gregorian::greg_month gm(pd.month().as_number());
280 ar & make_nvp("partial_date_day", gd);
281 ar & make_nvp("partial_date_month", gm);
283 //! Function to load gregorian::partial_date objects using serialization lib
284 /*! partial_date objects are broken down into 2 parts for serialization:
285 * the day (greg_day) and month (greg_month) objects
287 template<class Archive>
288 void load(Archive & ar, gregorian::partial_date& pd, unsigned int /*version*/)
290 gregorian::greg_day gd(1);
291 gregorian::greg_month gm(1);
292 ar & make_nvp("partial_date_day", gd);
293 ar & make_nvp("partial_date_month", gm);
294 pd = gregorian::partial_date(gd,gm);
296 //!override needed b/c no default constructor
297 template<class Archive>
298 inline void load_construct_data(Archive & ar, gregorian::partial_date* pd,
299 const unsigned int /*file_version*/)
301 gregorian::greg_month gm(1);
302 gregorian::greg_day gd(1);
303 ::new(pd) gregorian::partial_date(gd,gm);
306 /**** nth_kday_of_month ****/
308 //! Function to save nth_day_of_the_week_in_month objects using serialization lib
309 /*! nth_day_of_the_week_in_month objects are broken down into 3 parts for
310 * serialization: the week number, the day of the week, and the month
312 template<class Archive>
313 void save(Archive & ar, const gregorian::nth_kday_of_month& nkd,
314 unsigned int /*version*/)
316 typename gregorian::nth_kday_of_month::week_num wn(nkd.nth_week());
317 typename gregorian::nth_kday_of_month::day_of_week_type d(nkd.day_of_week().as_number());
318 typename gregorian::nth_kday_of_month::month_type m(nkd.month().as_number());
319 ar & make_nvp("nth_kday_of_month_week_num", wn);
320 ar & make_nvp("nth_kday_of_month_day_of_week", d);
321 ar & make_nvp("nth_kday_of_month_month", m);
323 //! Function to load nth_day_of_the_week_in_month objects using serialization lib
324 /*! nth_day_of_the_week_in_month objects are broken down into 3 parts for
325 * serialization: the week number, the day of the week, and the month
327 template<class Archive>
328 void load(Archive & ar, gregorian::nth_kday_of_month& nkd, unsigned int /*version*/)
330 typename gregorian::nth_kday_of_month::week_num wn(gregorian::nth_kday_of_month::first);
331 typename gregorian::nth_kday_of_month::day_of_week_type d(gregorian::Monday);
332 typename gregorian::nth_kday_of_month::month_type m(gregorian::Jan);
333 ar & make_nvp("nth_kday_of_month_week_num", wn);
334 ar & make_nvp("nth_kday_of_month_day_of_week", d);
335 ar & make_nvp("nth_kday_of_month_month", m);
337 nkd = gregorian::nth_kday_of_month(wn,d,m);
339 //!override needed b/c no default constructor
340 template<class Archive>
341 inline void load_construct_data(Archive & ar,
342 gregorian::nth_kday_of_month* nkd,
343 const unsigned int /*file_version*/)
345 // values used are not significant
346 ::new(nkd) gregorian::nth_kday_of_month(gregorian::nth_kday_of_month::first,
347 gregorian::Monday,gregorian::Jan);
350 /**** first_kday_of_month ****/
352 //! Function to save first_day_of_the_week_in_month objects using serialization lib
353 /*! first_day_of_the_week_in_month objects are broken down into 2 parts for
354 * serialization: the day of the week, and the month
356 template<class Archive>
357 void save(Archive & ar, const gregorian::first_kday_of_month& fkd,
358 unsigned int /*version*/)
360 typename gregorian::first_kday_of_month::day_of_week_type d(fkd.day_of_week().as_number());
361 typename gregorian::first_kday_of_month::month_type m(fkd.month().as_number());
362 ar & make_nvp("first_kday_of_month_day_of_week", d);
363 ar & make_nvp("first_kday_of_month_month", m);
365 //! Function to load first_day_of_the_week_in_month objects using serialization lib
366 /*! first_day_of_the_week_in_month objects are broken down into 2 parts for
367 * serialization: the day of the week, and the month
369 template<class Archive>
370 void load(Archive & ar, gregorian::first_kday_of_month& fkd, unsigned int /*version*/)
372 typename gregorian::first_kday_of_month::day_of_week_type d(gregorian::Monday);
373 typename gregorian::first_kday_of_month::month_type m(gregorian::Jan);
374 ar & make_nvp("first_kday_of_month_day_of_week", d);
375 ar & make_nvp("first_kday_of_month_month", m);
377 fkd = gregorian::first_kday_of_month(d,m);
379 //!override needed b/c no default constructor
380 template<class Archive>
381 inline void load_construct_data(Archive & ar,
382 gregorian::first_kday_of_month* fkd,
383 const unsigned int /*file_version*/)
385 // values used are not significant
386 ::new(fkd) gregorian::first_kday_of_month(gregorian::Monday,gregorian::Jan);
389 /**** last_kday_of_month ****/
391 //! Function to save last_day_of_the_week_in_month objects using serialization lib
392 /*! last_day_of_the_week_in_month objects are broken down into 2 parts for
393 * serialization: the day of the week, and the month
395 template<class Archive>
396 void save(Archive & ar, const gregorian::last_kday_of_month& lkd,
397 unsigned int /*version*/)
399 typename gregorian::last_kday_of_month::day_of_week_type d(lkd.day_of_week().as_number());
400 typename gregorian::last_kday_of_month::month_type m(lkd.month().as_number());
401 ar & make_nvp("last_kday_of_month_day_of_week", d);
402 ar & make_nvp("last_kday_of_month_month", m);
404 //! Function to load last_day_of_the_week_in_month objects using serialization lib
405 /*! last_day_of_the_week_in_month objects are broken down into 2 parts for
406 * serialization: the day of the week, and the month
408 template<class Archive>
409 void load(Archive & ar, gregorian::last_kday_of_month& lkd, unsigned int /*version*/)
411 typename gregorian::last_kday_of_month::day_of_week_type d(gregorian::Monday);
412 typename gregorian::last_kday_of_month::month_type m(gregorian::Jan);
413 ar & make_nvp("last_kday_of_month_day_of_week", d);
414 ar & make_nvp("last_kday_of_month_month", m);
416 lkd = gregorian::last_kday_of_month(d,m);
418 //!override needed b/c no default constructor
419 template<class Archive>
420 inline void load_construct_data(Archive & ar,
421 gregorian::last_kday_of_month* lkd,
422 const unsigned int /*file_version*/)
424 // values used are not significant
425 ::new(lkd) gregorian::last_kday_of_month(gregorian::Monday,gregorian::Jan);
428 /**** first_kday_before ****/
430 //! Function to save first_day_of_the_week_before objects using serialization lib
431 template<class Archive>
432 void save(Archive & ar, const gregorian::first_kday_before& fkdb,
433 unsigned int /*version*/)
435 typename gregorian::first_kday_before::day_of_week_type d(fkdb.day_of_week().as_number());
436 ar & make_nvp("first_kday_before_day_of_week", d);
438 //! Function to load first_day_of_the_week_before objects using serialization lib
439 template<class Archive>
440 void load(Archive & ar, gregorian::first_kday_before& fkdb, unsigned int /*version*/)
442 typename gregorian::first_kday_before::day_of_week_type d(gregorian::Monday);
443 ar & make_nvp("first_kday_before_day_of_week", d);
445 fkdb = gregorian::first_kday_before(d);
447 //!override needed b/c no default constructor
448 template<class Archive>
449 inline void load_construct_data(Archive & ar,
450 gregorian::first_kday_before* fkdb,
451 const unsigned int /*file_version*/)
453 // values used are not significant
454 ::new(fkdb) gregorian::first_kday_before(gregorian::Monday);
457 /**** first_kday_after ****/
459 //! Function to save first_day_of_the_week_after objects using serialization lib
460 template<class Archive>
461 void save(Archive & ar, const gregorian::first_kday_after& fkda,
462 unsigned int /*version*/)
464 typename gregorian::first_kday_after::day_of_week_type d(fkda.day_of_week().as_number());
465 ar & make_nvp("first_kday_after_day_of_week", d);
467 //! Function to load first_day_of_the_week_after objects using serialization lib
468 template<class Archive>
469 void load(Archive & ar, gregorian::first_kday_after& fkda, unsigned int /*version*/)
471 typename gregorian::first_kday_after::day_of_week_type d(gregorian::Monday);
472 ar & make_nvp("first_kday_after_day_of_week", d);
474 fkda = gregorian::first_kday_after(d);
476 //!override needed b/c no default constructor
477 template<class Archive>
478 inline void load_construct_data(Archive & ar,
479 gregorian::first_kday_after* fkda,
480 const unsigned int /*file_version*/)
482 // values used are not significant
483 ::new(fkda) gregorian::first_kday_after(gregorian::Monday);
486 } // namespace serialization
487 } // namespace boost
489 #endif