]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/date_time/gregorian/greg_serialize.hpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / date_time / gregorian / greg_serialize.hpp
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.txt or http://www.boost.org/LICENSE_1_0.txt)
8 * Author: Jeff Garland, Bart Garst
9 * $Date$
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 #include "boost/serialization/nvp.hpp"
16
17
18 // macros to split serialize functions into save & load functions
19 // An expanded version is below for gregorian::date
20 // NOTE: these macros define template functions in the boost::serialization namespace.
21 // They must be expanded *outside* of any namespace
22 BOOST_SERIALIZATION_SPLIT_FREE(::boost::gregorian::date_duration)
23 BOOST_SERIALIZATION_SPLIT_FREE(::boost::gregorian::date_duration::duration_rep)
24 BOOST_SERIALIZATION_SPLIT_FREE(::boost::gregorian::date_period)
25 BOOST_SERIALIZATION_SPLIT_FREE(::boost::gregorian::greg_year)
26 BOOST_SERIALIZATION_SPLIT_FREE(::boost::gregorian::greg_month)
27 BOOST_SERIALIZATION_SPLIT_FREE(::boost::gregorian::greg_day)
28 BOOST_SERIALIZATION_SPLIT_FREE(::boost::gregorian::greg_weekday)
29 BOOST_SERIALIZATION_SPLIT_FREE(::boost::gregorian::partial_date)
30 BOOST_SERIALIZATION_SPLIT_FREE(::boost::gregorian::nth_kday_of_month)
31 BOOST_SERIALIZATION_SPLIT_FREE(::boost::gregorian::first_kday_of_month)
32 BOOST_SERIALIZATION_SPLIT_FREE(::boost::gregorian::last_kday_of_month)
33 BOOST_SERIALIZATION_SPLIT_FREE(::boost::gregorian::first_kday_before)
34 BOOST_SERIALIZATION_SPLIT_FREE(::boost::gregorian::first_kday_after)
35
36 namespace boost {
37
38 namespace gregorian {
39 std::string to_iso_string(const date&);
40 }
41
42 namespace serialization {
43
44 /*! Method that does serialization for gregorian::date -- splits to load/save
45 */
46 template<class Archive>
47 inline void serialize(Archive & ar,
48 ::boost::gregorian::date & d,
49 const unsigned int file_version)
50 {
51 split_free(ar, d, file_version);
52 }
53
54 //! Function to save gregorian::date objects using serialization lib
55 /*! Dates are serialized into a string for transport and storage.
56 * While it would be more efficient to store the internal
57 * integer used to manipulate the dates, it is an unstable solution.
58 */
59 template<class Archive>
60 void save(Archive & ar,
61 const ::boost::gregorian::date & d,
62 unsigned int /* version */)
63 {
64 std::string ds = to_iso_string(d);
65 ar & make_nvp("date", ds);
66 }
67
68 //! Function to load gregorian::date objects using serialization lib
69 /*! Dates are serialized into a string for transport and storage.
70 * While it would be more efficient to store the internal
71 * integer used to manipulate the dates, it is an unstable solution.
72 */
73 template<class Archive>
74 void load(Archive & ar,
75 ::boost::gregorian::date & d,
76 unsigned int /*version*/)
77 {
78 std::string ds;
79 ar & make_nvp("date", ds);
80 try{
81 d = ::boost::gregorian::from_undelimited_string(ds);
82 }catch(bad_lexical_cast&) {
83 gregorian::special_values sv = gregorian::special_value_from_string(ds);
84 if(sv == gregorian::not_special) {
85 throw; // no match found, rethrow original exception
86 }
87 else {
88 d = gregorian::date(sv);
89 }
90 }
91 }
92
93
94 //!override needed b/c no default constructor
95 template<class Archive>
96 inline void load_construct_data(Archive & /*ar*/,
97 ::boost::gregorian::date* dp,
98 const unsigned int /*file_version*/)
99 {
100 // retrieve data from archive required to construct new
101 // invoke inplace constructor to initialize instance of date
102 ::new(dp) ::boost::gregorian::date(::boost::gregorian::not_a_date_time);
103 }
104
105 /**** date_duration ****/
106
107 //! Function to save gregorian::date_duration objects using serialization lib
108 template<class Archive>
109 void save(Archive & ar, const gregorian::date_duration & dd,
110 unsigned int /*version*/)
111 {
112 typename gregorian::date_duration::duration_rep dr = dd.get_rep();
113 ar & make_nvp("date_duration", dr);
114 }
115 //! Function to load gregorian::date_duration objects using serialization lib
116 template<class Archive>
117 void load(Archive & ar, gregorian::date_duration & dd, unsigned int /*version*/)
118 {
119 typename gregorian::date_duration::duration_rep dr(0);
120 ar & make_nvp("date_duration", dr);
121 dd = gregorian::date_duration(dr);
122 }
123 //!override needed b/c no default constructor
124 template<class Archive>
125 inline void load_construct_data(Archive & /*ar*/, gregorian::date_duration* dd,
126 const unsigned int /*file_version*/)
127 {
128 ::new(dd) gregorian::date_duration(gregorian::not_a_date_time);
129 }
130
131 /**** date_duration::duration_rep (most likely int_adapter) ****/
132
133 //! helper unction to save date_duration objects using serialization lib
134 template<class Archive>
135 void save(Archive & ar, const gregorian::date_duration::duration_rep & dr,
136 unsigned int /*version*/)
137 {
138 typename gregorian::date_duration::duration_rep::int_type it = dr.as_number();
139 ar & make_nvp("date_duration_duration_rep", it);
140 }
141 //! helper function to load date_duration objects using serialization lib
142 template<class Archive>
143 void load(Archive & ar, gregorian::date_duration::duration_rep & dr, unsigned int /*version*/)
144 {
145 typename gregorian::date_duration::duration_rep::int_type it(0);
146 ar & make_nvp("date_duration_duration_rep", it);
147 dr = gregorian::date_duration::duration_rep::int_type(it);
148 }
149 //!override needed b/c no default constructor
150 template<class Archive>
151 inline void load_construct_data(Archive & /*ar*/, gregorian::date_duration::duration_rep* dr,
152 const unsigned int /*file_version*/)
153 {
154 ::new(dr) gregorian::date_duration::duration_rep(0);
155 }
156
157 /**** date_period ****/
158
159 //! Function to save gregorian::date_period objects using serialization lib
160 /*! date_period objects are broken down into 2 parts for serialization:
161 * the begining date object and the end date object
162 */
163 template<class Archive>
164 void save(Archive & ar, const gregorian::date_period& dp,
165 unsigned int /*version*/)
166 {
167 gregorian::date d1 = dp.begin();
168 gregorian::date d2 = dp.end();
169 ar & make_nvp("date_period_begin_date", d1);
170 ar & make_nvp("date_period_end_date", d2);
171 }
172 //! Function to load gregorian::date_period objects using serialization lib
173 /*! date_period objects are broken down into 2 parts for serialization:
174 * the begining date object and the end date object
175 */
176 template<class Archive>
177 void load(Archive & ar, gregorian::date_period& dp, unsigned int /*version*/)
178 {
179 gregorian::date d1(gregorian::not_a_date_time);
180 gregorian::date d2(gregorian::not_a_date_time);
181 ar & make_nvp("date_period_begin_date", d1);
182 ar & make_nvp("date_period_end_date", d2);
183 dp = gregorian::date_period(d1,d2);
184 }
185 //!override needed b/c no default constructor
186 template<class Archive>
187 inline void load_construct_data(Archive & /*ar*/, gregorian::date_period* dp,
188 const unsigned int /*file_version*/)
189 {
190 gregorian::date d(gregorian::not_a_date_time);
191 gregorian::date_duration dd(1);
192 ::new(dp) gregorian::date_period(d,dd);
193 }
194
195 /**** greg_year ****/
196
197 //! Function to save gregorian::greg_year objects using serialization lib
198 template<class Archive>
199 void save(Archive & ar, const gregorian::greg_year& gy,
200 unsigned int /*version*/)
201 {
202 unsigned short us = gy;
203 ar & make_nvp("greg_year", us);
204 }
205 //! Function to load gregorian::greg_year objects using serialization lib
206 template<class Archive>
207 void load(Archive & ar, gregorian::greg_year& gy, unsigned int /*version*/)
208 {
209 unsigned short us;
210 ar & make_nvp("greg_year", us);
211 gy = gregorian::greg_year(us);
212 }
213 //!override needed b/c no default constructor
214 template<class Archive>
215 inline void load_construct_data(Archive & /*ar*/, gregorian::greg_year* gy,
216 const unsigned int /*file_version*/)
217 {
218 ::new(gy) gregorian::greg_year(1900);
219 }
220
221 /**** greg_month ****/
222
223 //! Function to save gregorian::greg_month objects using serialization lib
224 template<class Archive>
225 void save(Archive & ar, const gregorian::greg_month& gm,
226 unsigned int /*version*/)
227 {
228 unsigned short us = gm.as_number();
229 ar & make_nvp("greg_month", us);
230 }
231 //! Function to load gregorian::greg_month objects using serialization lib
232 template<class Archive>
233 void load(Archive & ar, gregorian::greg_month& gm, unsigned int /*version*/)
234 {
235 unsigned short us;
236 ar & make_nvp("greg_month", us);
237 gm = gregorian::greg_month(us);
238 }
239 //!override needed b/c no default constructor
240 template<class Archive>
241 inline void load_construct_data(Archive & /*ar*/, gregorian::greg_month* gm,
242 const unsigned int /*file_version*/)
243 {
244 ::new(gm) gregorian::greg_month(1);
245 }
246
247 /**** greg_day ****/
248
249 //! Function to save gregorian::greg_day objects using serialization lib
250 template<class Archive>
251 void save(Archive & ar, const gregorian::greg_day& gd,
252 unsigned int /*version*/)
253 {
254 unsigned short us = gd.as_number();
255 ar & make_nvp("greg_day", us);
256 }
257 //! Function to load gregorian::greg_day objects using serialization lib
258 template<class Archive>
259 void load(Archive & ar, gregorian::greg_day& gd, unsigned int /*version*/)
260 {
261 unsigned short us;
262 ar & make_nvp("greg_day", us);
263 gd = gregorian::greg_day(us);
264 }
265 //!override needed b/c no default constructor
266 template<class Archive>
267 inline void load_construct_data(Archive & /*ar*/, gregorian::greg_day* gd,
268 const unsigned int /*file_version*/)
269 {
270 ::new(gd) gregorian::greg_day(1);
271 }
272
273 /**** greg_weekday ****/
274
275 //! Function to save gregorian::greg_weekday objects using serialization lib
276 template<class Archive>
277 void save(Archive & ar, const gregorian::greg_weekday& gd,
278 unsigned int /*version*/)
279 {
280 unsigned short us = gd.as_number();
281 ar & make_nvp("greg_weekday", us);
282 }
283 //! Function to load gregorian::greg_weekday objects using serialization lib
284 template<class Archive>
285 void load(Archive & ar, gregorian::greg_weekday& gd, unsigned int /*version*/)
286 {
287 unsigned short us;
288 ar & make_nvp("greg_weekday", us);
289 gd = gregorian::greg_weekday(us);
290 }
291 //!override needed b/c no default constructor
292 template<class Archive>
293 inline void load_construct_data(Archive & /*ar*/, gregorian::greg_weekday* gd,
294 const unsigned int /*file_version*/)
295 {
296 ::new(gd) gregorian::greg_weekday(1);
297 }
298
299 /**** date_generators ****/
300
301 /**** partial_date ****/
302
303 //! Function to save gregorian::partial_date objects using serialization lib
304 /*! partial_date objects are broken down into 2 parts for serialization:
305 * the day (typically greg_day) and month (typically greg_month) objects
306 */
307 template<class Archive>
308 void save(Archive & ar, const gregorian::partial_date& pd,
309 unsigned int /*version*/)
310 {
311 gregorian::greg_day gd(pd.day());
312 gregorian::greg_month gm(pd.month().as_number());
313 ar & make_nvp("partial_date_day", gd);
314 ar & make_nvp("partial_date_month", gm);
315 }
316 //! Function to load gregorian::partial_date objects using serialization lib
317 /*! partial_date objects are broken down into 2 parts for serialization:
318 * the day (greg_day) and month (greg_month) objects
319 */
320 template<class Archive>
321 void load(Archive & ar, gregorian::partial_date& pd, unsigned int /*version*/)
322 {
323 gregorian::greg_day gd(1);
324 gregorian::greg_month gm(1);
325 ar & make_nvp("partial_date_day", gd);
326 ar & make_nvp("partial_date_month", gm);
327 pd = gregorian::partial_date(gd,gm);
328 }
329 //!override needed b/c no default constructor
330 template<class Archive>
331 inline void load_construct_data(Archive & /*ar*/, gregorian::partial_date* pd,
332 const unsigned int /*file_version*/)
333 {
334 gregorian::greg_month gm(1);
335 gregorian::greg_day gd(1);
336 ::new(pd) gregorian::partial_date(gd,gm);
337 }
338
339 /**** nth_kday_of_month ****/
340
341 //! Function to save nth_day_of_the_week_in_month objects using serialization lib
342 /*! nth_day_of_the_week_in_month objects are broken down into 3 parts for
343 * serialization: the week number, the day of the week, and the month
344 */
345 template<class Archive>
346 void save(Archive & ar, const gregorian::nth_kday_of_month& nkd,
347 unsigned int /*version*/)
348 {
349 typename gregorian::nth_kday_of_month::week_num wn(nkd.nth_week());
350 typename gregorian::nth_kday_of_month::day_of_week_type d(nkd.day_of_week().as_number());
351 typename gregorian::nth_kday_of_month::month_type m(nkd.month().as_number());
352 ar & make_nvp("nth_kday_of_month_week_num", wn);
353 ar & make_nvp("nth_kday_of_month_day_of_week", d);
354 ar & make_nvp("nth_kday_of_month_month", m);
355 }
356 //! Function to load nth_day_of_the_week_in_month objects using serialization lib
357 /*! nth_day_of_the_week_in_month objects are broken down into 3 parts for
358 * serialization: the week number, the day of the week, and the month
359 */
360 template<class Archive>
361 void load(Archive & ar, gregorian::nth_kday_of_month& nkd, unsigned int /*version*/)
362 {
363 typename gregorian::nth_kday_of_month::week_num wn(gregorian::nth_kday_of_month::first);
364 typename gregorian::nth_kday_of_month::day_of_week_type d(gregorian::Monday);
365 typename gregorian::nth_kday_of_month::month_type m(gregorian::Jan);
366 ar & make_nvp("nth_kday_of_month_week_num", wn);
367 ar & make_nvp("nth_kday_of_month_day_of_week", d);
368 ar & make_nvp("nth_kday_of_month_month", m);
369
370 nkd = gregorian::nth_kday_of_month(wn,d,m);
371 }
372 //!override needed b/c no default constructor
373 template<class Archive>
374 inline void load_construct_data(Archive & /*ar*/,
375 gregorian::nth_kday_of_month* nkd,
376 const unsigned int /*file_version*/)
377 {
378 // values used are not significant
379 ::new(nkd) gregorian::nth_kday_of_month(gregorian::nth_kday_of_month::first,
380 gregorian::Monday,gregorian::Jan);
381 }
382
383 /**** first_kday_of_month ****/
384
385 //! Function to save first_day_of_the_week_in_month objects using serialization lib
386 /*! first_day_of_the_week_in_month objects are broken down into 2 parts for
387 * serialization: the day of the week, and the month
388 */
389 template<class Archive>
390 void save(Archive & ar, const gregorian::first_kday_of_month& fkd,
391 unsigned int /*version*/)
392 {
393 typename gregorian::first_kday_of_month::day_of_week_type d(fkd.day_of_week().as_number());
394 typename gregorian::first_kday_of_month::month_type m(fkd.month().as_number());
395 ar & make_nvp("first_kday_of_month_day_of_week", d);
396 ar & make_nvp("first_kday_of_month_month", m);
397 }
398 //! Function to load first_day_of_the_week_in_month objects using serialization lib
399 /*! first_day_of_the_week_in_month objects are broken down into 2 parts for
400 * serialization: the day of the week, and the month
401 */
402 template<class Archive>
403 void load(Archive & ar, gregorian::first_kday_of_month& fkd, unsigned int /*version*/)
404 {
405 typename gregorian::first_kday_of_month::day_of_week_type d(gregorian::Monday);
406 typename gregorian::first_kday_of_month::month_type m(gregorian::Jan);
407 ar & make_nvp("first_kday_of_month_day_of_week", d);
408 ar & make_nvp("first_kday_of_month_month", m);
409
410 fkd = gregorian::first_kday_of_month(d,m);
411 }
412 //!override needed b/c no default constructor
413 template<class Archive>
414 inline void load_construct_data(Archive & /*ar*/,
415 gregorian::first_kday_of_month* fkd,
416 const unsigned int /*file_version*/)
417 {
418 // values used are not significant
419 ::new(fkd) gregorian::first_kday_of_month(gregorian::Monday,gregorian::Jan);
420 }
421
422 /**** last_kday_of_month ****/
423
424 //! Function to save last_day_of_the_week_in_month objects using serialization lib
425 /*! last_day_of_the_week_in_month objects are broken down into 2 parts for
426 * serialization: the day of the week, and the month
427 */
428 template<class Archive>
429 void save(Archive & ar, const gregorian::last_kday_of_month& lkd,
430 unsigned int /*version*/)
431 {
432 typename gregorian::last_kday_of_month::day_of_week_type d(lkd.day_of_week().as_number());
433 typename gregorian::last_kday_of_month::month_type m(lkd.month().as_number());
434 ar & make_nvp("last_kday_of_month_day_of_week", d);
435 ar & make_nvp("last_kday_of_month_month", m);
436 }
437 //! Function to load last_day_of_the_week_in_month objects using serialization lib
438 /*! last_day_of_the_week_in_month objects are broken down into 2 parts for
439 * serialization: the day of the week, and the month
440 */
441 template<class Archive>
442 void load(Archive & ar, gregorian::last_kday_of_month& lkd, unsigned int /*version*/)
443 {
444 typename gregorian::last_kday_of_month::day_of_week_type d(gregorian::Monday);
445 typename gregorian::last_kday_of_month::month_type m(gregorian::Jan);
446 ar & make_nvp("last_kday_of_month_day_of_week", d);
447 ar & make_nvp("last_kday_of_month_month", m);
448
449 lkd = gregorian::last_kday_of_month(d,m);
450 }
451 //!override needed b/c no default constructor
452 template<class Archive>
453 inline void load_construct_data(Archive & /*ar*/,
454 gregorian::last_kday_of_month* lkd,
455 const unsigned int /*file_version*/)
456 {
457 // values used are not significant
458 ::new(lkd) gregorian::last_kday_of_month(gregorian::Monday,gregorian::Jan);
459 }
460
461 /**** first_kday_before ****/
462
463 //! Function to save first_day_of_the_week_before objects using serialization lib
464 template<class Archive>
465 void save(Archive & ar, const gregorian::first_kday_before& fkdb,
466 unsigned int /*version*/)
467 {
468 typename gregorian::first_kday_before::day_of_week_type d(fkdb.day_of_week().as_number());
469 ar & make_nvp("first_kday_before_day_of_week", d);
470 }
471 //! Function to load first_day_of_the_week_before objects using serialization lib
472 template<class Archive>
473 void load(Archive & ar, gregorian::first_kday_before& fkdb, unsigned int /*version*/)
474 {
475 typename gregorian::first_kday_before::day_of_week_type d(gregorian::Monday);
476 ar & make_nvp("first_kday_before_day_of_week", d);
477
478 fkdb = gregorian::first_kday_before(d);
479 }
480 //!override needed b/c no default constructor
481 template<class Archive>
482 inline void load_construct_data(Archive & /*ar*/,
483 gregorian::first_kday_before* fkdb,
484 const unsigned int /*file_version*/)
485 {
486 // values used are not significant
487 ::new(fkdb) gregorian::first_kday_before(gregorian::Monday);
488 }
489
490 /**** first_kday_after ****/
491
492 //! Function to save first_day_of_the_week_after objects using serialization lib
493 template<class Archive>
494 void save(Archive & ar, const gregorian::first_kday_after& fkda,
495 unsigned int /*version*/)
496 {
497 typename gregorian::first_kday_after::day_of_week_type d(fkda.day_of_week().as_number());
498 ar & make_nvp("first_kday_after_day_of_week", d);
499 }
500 //! Function to load first_day_of_the_week_after objects using serialization lib
501 template<class Archive>
502 void load(Archive & ar, gregorian::first_kday_after& fkda, unsigned int /*version*/)
503 {
504 typename gregorian::first_kday_after::day_of_week_type d(gregorian::Monday);
505 ar & make_nvp("first_kday_after_day_of_week", d);
506
507 fkda = gregorian::first_kday_after(d);
508 }
509 //!override needed b/c no default constructor
510 template<class Archive>
511 inline void load_construct_data(Archive & /*ar*/,
512 gregorian::first_kday_after* fkda,
513 const unsigned int /*file_version*/)
514 {
515 // values used are not significant
516 ::new(fkda) gregorian::first_kday_after(gregorian::Monday);
517 }
518
519 } // namespace serialization
520 } // namespace boost
521
522 #endif