/Src/Dependencies/Boost/boost/date_time/gregorian/greg_serialize.hpp

http://hadesmem.googlecode.com/ · C++ Header · 490 lines · 338 code · 42 blank · 110 comment · 3 complexity · 572397fbecc9e2345ed87a8c228faa4a MD5 · raw file

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