source: NonGTP/Boost/boost/date_time/gregorian/greg_serialize.hpp @ 857

Revision 857, 18.6 KB checked in by igarcia, 19 years ago (diff)
Line 
1#ifndef GREGORIAN_SERIALIZE_HPP___
2#define GREGORIAN_SERIALIZE_HPP___
3
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 or http://www.boost.org/LICENSE-1.0)
8 * Author: Jeff Garland, Bart Garst
9 * $Date: 2005/06/21 03:33:14 $
10 */
11
12#include "boost/date_time/gregorian/gregorian_types.hpp"
13#include "boost/date_time/gregorian/parsers.hpp"
14#include "boost/serialization/split_free.hpp"
15
16 
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
21BOOST_SERIALIZATION_SPLIT_FREE(::boost::gregorian::date_duration)
22BOOST_SERIALIZATION_SPLIT_FREE(::boost::gregorian::date_duration::duration_rep)
23BOOST_SERIALIZATION_SPLIT_FREE(::boost::gregorian::date_period)
24BOOST_SERIALIZATION_SPLIT_FREE(::boost::gregorian::greg_month)
25BOOST_SERIALIZATION_SPLIT_FREE(::boost::gregorian::greg_day)
26BOOST_SERIALIZATION_SPLIT_FREE(::boost::gregorian::greg_weekday)
27BOOST_SERIALIZATION_SPLIT_FREE(::boost::gregorian::partial_date)
28BOOST_SERIALIZATION_SPLIT_FREE(::boost::gregorian::nth_kday_of_month)
29BOOST_SERIALIZATION_SPLIT_FREE(::boost::gregorian::first_kday_of_month)
30BOOST_SERIALIZATION_SPLIT_FREE(::boost::gregorian::last_kday_of_month)
31BOOST_SERIALIZATION_SPLIT_FREE(::boost::gregorian::first_kday_before)
32BOOST_SERIALIZATION_SPLIT_FREE(::boost::gregorian::first_kday_after)
33
34namespace boost {
35namespace serialization {
36
37/*! Method that does serialization for gregorian::date -- splits to load/save
38 */
39template<class Archive>                         
40inline void serialize(Archive & ar,                               
41                      ::boost::gregorian::date & d,
42                      const unsigned int file_version)
43{
44  split_free(ar, d, file_version);             
45}                                               
46
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. 
51 */
52template<class Archive>
53void save(Archive & ar,
54          const ::boost::gregorian::date & d,
55          unsigned int version)
56{
57  std::string ds = to_iso_string(d);
58  ar & make_nvp("date", ds);
59}
60
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. 
65 */
66template<class Archive>
67void load(Archive & ar,
68          ::boost::gregorian::date & d,
69          unsigned int version)
70{
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 be) {
76    gregorian::special_values sv = gregorian::special_value_from_string(ds);
77    if(sv == gregorian::not_special) {
78      throw(be); // no match found, rethrow original exception
79    }
80    else {
81      d = gregorian::date(sv);
82    }
83  }
84}
85
86
87//!override needed b/c no default constructor
88template<class Archive>
89inline void load_construct_data(Archive & ar,
90                                ::boost::gregorian::date* dp,
91                                const unsigned int file_version)
92{
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);
96}
97
98/**** date_duration ****/
99
100//! Function to save gregorian::date_duration objects using serialization lib
101template<class Archive>
102void save(Archive & ar, const gregorian::date_duration & dd,
103          unsigned int version)
104{
105  typename gregorian::date_duration::duration_rep dr = dd.get_rep();
106  ar & make_nvp("date_duration", dr);
107}
108//! Function to load gregorian::date_duration objects using serialization lib
109template<class Archive>
110void load(Archive & ar, gregorian::date_duration & dd, unsigned int version)
111{
112  typename gregorian::date_duration::duration_rep dr(0);
113  ar & make_nvp("date_duration", dr);
114  dd = gregorian::date_duration(dr);
115}
116//!override needed b/c no default constructor
117template<class Archive>
118inline void load_construct_data(Archive & ar, gregorian::date_duration* dd,
119                                const unsigned int file_version)
120{
121  ::new(dd) gregorian::date_duration(gregorian::not_a_date_time);
122}
123
124/**** date_duration::duration_rep (most likely int_adapter) ****/
125
126//! helper unction to save date_duration objects using serialization lib
127template<class Archive>
128void save(Archive & ar, const gregorian::date_duration::duration_rep & dr,
129          unsigned int version)
130{
131  typename gregorian::date_duration::duration_rep::int_type it = dr.as_number();
132  ar & make_nvp("date_duration_duration_rep", it);
133}
134//! helper function to load date_duration objects using serialization lib
135template<class Archive>
136void load(Archive & ar, gregorian::date_duration::duration_rep & dr, unsigned int version)
137{
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);
141}
142//!override needed b/c no default constructor
143template<class Archive>
144inline void load_construct_data(Archive & ar, gregorian::date_duration::duration_rep* dr,
145                                const unsigned int file_version)
146{
147  ::new(dr) gregorian::date_duration::duration_rep(0);
148}
149
150/**** date_period ****/
151
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
155 */
156template<class Archive>
157void save(Archive & ar, const gregorian::date_period& dp,
158          unsigned int version)
159{
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);
164}
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
168 */
169template<class Archive>
170void load(Archive & ar, gregorian::date_period& dp, unsigned int version)
171{
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);
177}
178//!override needed b/c no default constructor
179template<class Archive>
180inline void load_construct_data(Archive & ar, gregorian::date_period* dp,
181                                const unsigned int file_version)
182{
183  gregorian::date d(gregorian::not_a_date_time);
184  gregorian::date_duration dd(1);
185  ::new(dp) gregorian::date_period(d,dd);
186}
187
188/**** greg_month ****/
189
190//! Function to save gregorian::greg_month objects using serialization lib
191template<class Archive>
192void save(Archive & ar, const gregorian::greg_month& gm,
193          unsigned int version)
194{
195  unsigned short us = gm.as_number();
196  ar & make_nvp("greg_month", us);
197}
198//! Function to load gregorian::greg_month objects using serialization lib
199template<class Archive>
200void load(Archive & ar, gregorian::greg_month& gm, unsigned int version)
201{
202  unsigned short us;
203  ar & make_nvp("greg_month", us);
204  gm = gregorian::greg_month(us);
205}
206//!override needed b/c no default constructor
207template<class Archive>
208inline void load_construct_data(Archive & ar, gregorian::greg_month* gm,
209                                const unsigned int file_version)
210{
211  ::new(gm) gregorian::greg_month(1);
212}
213
214/**** greg_day ****/
215
216//! Function to save gregorian::greg_day objects using serialization lib
217template<class Archive>
218void save(Archive & ar, const gregorian::greg_day& gd,
219          unsigned int version)
220{
221  unsigned short us = gd.as_number();
222  ar & make_nvp("greg_day", us);
223}
224//! Function to load gregorian::greg_day objects using serialization lib
225template<class Archive>
226void load(Archive & ar, gregorian::greg_day& gd, unsigned int version)
227{
228  unsigned short us;
229  ar & make_nvp("greg_day", us);
230  gd = gregorian::greg_day(us);
231}
232//!override needed b/c no default constructor
233template<class Archive>
234inline void load_construct_data(Archive & ar, gregorian::greg_day* gd,
235                                const unsigned int file_version)
236{
237  ::new(gd) gregorian::greg_day(1);
238}
239
240/**** greg_weekday ****/
241
242//! Function to save gregorian::greg_weekday objects using serialization lib
243template<class Archive>
244void save(Archive & ar, const gregorian::greg_weekday& gd,
245          unsigned int version)
246{
247  unsigned short us = gd.as_number();
248  ar & make_nvp("greg_weekday", us);
249}
250//! Function to load gregorian::greg_weekday objects using serialization lib
251template<class Archive>
252void load(Archive & ar, gregorian::greg_weekday& gd, unsigned int version)
253{
254  unsigned short us;
255  ar & make_nvp("greg_weekday", us);
256  gd = gregorian::greg_weekday(us);
257}
258//!override needed b/c no default constructor
259template<class Archive>
260inline void load_construct_data(Archive & ar, gregorian::greg_weekday* gd,
261                                const unsigned int file_version)
262{
263  ::new(gd) gregorian::greg_weekday(1);
264}
265
266/**** date_generators ****/
267
268/**** partial_date ****/
269
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
273 */
274template<class Archive>
275void save(Archive & ar, const gregorian::partial_date& pd,
276          unsigned int version)
277{
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);
282}
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
286 */
287template<class Archive>
288void load(Archive & ar, gregorian::partial_date& pd, unsigned int version)
289{
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);
295}
296//!override needed b/c no default constructor
297template<class Archive>
298inline void load_construct_data(Archive & ar, gregorian::partial_date* pd,
299                                const unsigned int file_version)
300{
301  gregorian::greg_month gm(1);
302  gregorian::greg_day gd(1);
303  ::new(pd) gregorian::partial_date(gd,gm);
304}
305
306/**** nth_kday_of_month ****/
307
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
311 */
312template<class Archive>
313void save(Archive & ar, const gregorian::nth_kday_of_month& nkd,
314          unsigned int version)
315{
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);
322}
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
326 */
327template<class Archive>
328void load(Archive & ar, gregorian::nth_kday_of_month& nkd, unsigned int version)
329{
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);
336 
337  nkd = gregorian::nth_kday_of_month(wn,d,m);
338}
339//!override needed b/c no default constructor
340template<class Archive>
341inline void load_construct_data(Archive & ar,
342                                gregorian::nth_kday_of_month* nkd,
343                                const unsigned int file_version)
344{
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);
348}
349
350/**** first_kday_of_month ****/
351
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
355 */
356template<class Archive>
357void save(Archive & ar, const gregorian::first_kday_of_month& fkd,
358          unsigned int version)
359{
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);
364}
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
368 */
369template<class Archive>
370void load(Archive & ar, gregorian::first_kday_of_month& fkd, unsigned int version)
371{
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);
376 
377  fkd = gregorian::first_kday_of_month(d,m);
378}
379//!override needed b/c no default constructor
380template<class Archive>
381inline void load_construct_data(Archive & ar,
382                                gregorian::first_kday_of_month* fkd,
383                                const unsigned int file_version)
384{
385  // values used are not significant
386  ::new(fkd) gregorian::first_kday_of_month(gregorian::Monday,gregorian::Jan);
387}
388
389/**** last_kday_of_month ****/
390
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
394 */
395template<class Archive>
396void save(Archive & ar, const gregorian::last_kday_of_month& lkd,
397          unsigned int version)
398{
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);
403}
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
407 */
408template<class Archive>
409void load(Archive & ar, gregorian::last_kday_of_month& lkd, unsigned int version)
410{
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);
415 
416  lkd = gregorian::last_kday_of_month(d,m);
417}
418//!override needed b/c no default constructor
419template<class Archive>
420inline void load_construct_data(Archive & ar,
421                                gregorian::last_kday_of_month* lkd,
422                                const unsigned int file_version)
423{
424  // values used are not significant
425  ::new(lkd) gregorian::last_kday_of_month(gregorian::Monday,gregorian::Jan);
426}
427
428/**** first_kday_before ****/
429
430//! Function to save first_day_of_the_week_before objects using serialization lib
431template<class Archive>
432void save(Archive & ar, const gregorian::first_kday_before& fkdb,
433          unsigned int version)
434{
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);
437}
438//! Function to load first_day_of_the_week_before objects using serialization lib
439template<class Archive>
440void load(Archive & ar, gregorian::first_kday_before& fkdb, unsigned int version)
441{
442  typename gregorian::first_kday_before::day_of_week_type d(gregorian::Monday);
443  ar & make_nvp("first_kday_before_day_of_week", d);
444 
445  fkdb = gregorian::first_kday_before(d);
446}
447//!override needed b/c no default constructor
448template<class Archive>
449inline void load_construct_data(Archive & ar,
450                                gregorian::first_kday_before* fkdb,
451                                const unsigned int file_version)
452{
453  // values used are not significant
454  ::new(fkdb) gregorian::first_kday_before(gregorian::Monday);
455}
456
457/**** first_kday_after ****/
458
459//! Function to save first_day_of_the_week_after objects using serialization lib
460template<class Archive>
461void save(Archive & ar, const gregorian::first_kday_after& fkda,
462          unsigned int version)
463{
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);
466}
467//! Function to load first_day_of_the_week_after objects using serialization lib
468template<class Archive>
469void load(Archive & ar, gregorian::first_kday_after& fkda, unsigned int version)
470{
471  typename gregorian::first_kday_after::day_of_week_type d(gregorian::Monday);
472  ar & make_nvp("first_kday_after_day_of_week", d);
473 
474  fkda = gregorian::first_kday_after(d);
475}
476//!override needed b/c no default constructor
477template<class Archive>
478inline void load_construct_data(Archive & ar,
479                                gregorian::first_kday_after* fkda,
480                                const unsigned int file_version)
481{
482  // values used are not significant
483  ::new(fkda) gregorian::first_kday_after(gregorian::Monday);
484}
485
486} // namespace serialization
487} // namespace boost
488
489#endif
Note: See TracBrowser for help on using the repository browser.