Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 09:35:19

0001 #ifndef GREGORIAN_SERIALIZE_HPP___
0002 #define GREGORIAN_SERIALIZE_HPP___
0003 
0004 /* Copyright (c) 2004-2005 CrystalClear Software, Inc.
0005  * Use, modification and distribution is subject to the 
0006  * Boost Software License, Version 1.0. (See accompanying
0007  * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
0008  * Author: Jeff Garland, Bart Garst
0009  * $Date$
0010  */
0011 
0012 #include "boost/date_time/gregorian/gregorian_types.hpp"
0013 #include "boost/date_time/gregorian/parsers.hpp"
0014 #include "boost/core/nvp.hpp"
0015 
0016 
0017 namespace boost {
0018 
0019   namespace gregorian {
0020     std::string to_iso_string(const date&);
0021   }
0022 
0023 namespace serialization {
0024 
0025 // A macro to split serialize functions into save & load functions.
0026 // It is here to avoid dependency on Boost.Serialization just for the
0027 // BOOST_SERIALIZATION_SPLIT_FREE macro
0028 #define BOOST_DATE_TIME_SPLIT_FREE(T)                                         \
0029 template<class Archive>                                                       \
0030 inline void serialize(Archive & ar,                                           \
0031                       T & t,                                                  \
0032                       const unsigned int file_version)                        \
0033 {                                                                             \
0034     split_free(ar, t, file_version);                                          \
0035 }
0036 
0037 /*! Method that does serialization for gregorian::date -- splits to load/save
0038  */
0039 BOOST_DATE_TIME_SPLIT_FREE(::boost::gregorian::date)
0040 BOOST_DATE_TIME_SPLIT_FREE(::boost::gregorian::date_duration)
0041 BOOST_DATE_TIME_SPLIT_FREE(::boost::gregorian::date_duration::duration_rep)
0042 BOOST_DATE_TIME_SPLIT_FREE(::boost::gregorian::date_period)
0043 BOOST_DATE_TIME_SPLIT_FREE(::boost::gregorian::greg_year)
0044 BOOST_DATE_TIME_SPLIT_FREE(::boost::gregorian::greg_month)
0045 BOOST_DATE_TIME_SPLIT_FREE(::boost::gregorian::greg_day)
0046 BOOST_DATE_TIME_SPLIT_FREE(::boost::gregorian::greg_weekday)
0047 BOOST_DATE_TIME_SPLIT_FREE(::boost::gregorian::partial_date)
0048 BOOST_DATE_TIME_SPLIT_FREE(::boost::gregorian::nth_kday_of_month)
0049 BOOST_DATE_TIME_SPLIT_FREE(::boost::gregorian::first_kday_of_month)
0050 BOOST_DATE_TIME_SPLIT_FREE(::boost::gregorian::last_kday_of_month)
0051 BOOST_DATE_TIME_SPLIT_FREE(::boost::gregorian::first_kday_before)
0052 BOOST_DATE_TIME_SPLIT_FREE(::boost::gregorian::first_kday_after)
0053 
0054 #undef BOOST_DATE_TIME_SPLIT_FREE
0055 
0056 //! Function to save gregorian::date objects using serialization lib
0057 /*! Dates are serialized into a string for transport and storage. 
0058  *  While it would be more efficient to store the internal
0059  *  integer used to manipulate the dates, it is an unstable solution.  
0060  */
0061 template<class Archive>
0062 void save(Archive & ar, 
0063           const ::boost::gregorian::date & d, 
0064           unsigned int /* version */)
0065 {
0066   std::string ds = to_iso_string(d);
0067   ar & make_nvp("date", ds);
0068 }
0069 
0070 //! Function to load gregorian::date objects using serialization lib
0071 /*! Dates are serialized into a string for transport and storage. 
0072  *  While it would be more efficient to store the internal
0073  *  integer used to manipulate the dates, it is an unstable solution.  
0074  */
0075 template<class Archive>
0076 void load(Archive & ar, 
0077           ::boost::gregorian::date & d, 
0078           unsigned int /*version*/)
0079 {
0080   std::string ds;
0081   ar & make_nvp("date", ds);
0082   try{
0083     d = ::boost::gregorian::from_undelimited_string(ds);
0084   }catch(bad_lexical_cast&) {
0085     gregorian::special_values sv = gregorian::special_value_from_string(ds);
0086     if(sv == gregorian::not_special) {
0087       throw; // no match found, rethrow original exception
0088     }
0089     else {
0090       d = gregorian::date(sv);
0091     }
0092   }
0093 }
0094 
0095 
0096 //!override needed b/c no default constructor
0097 template<class Archive>
0098 inline void load_construct_data(Archive & /*ar*/, 
0099                                 ::boost::gregorian::date* dp, 
0100                                 const unsigned int /*file_version*/)
0101 {
0102   // retrieve data from archive required to construct new 
0103   // invoke inplace constructor to initialize instance of date
0104   ::new(dp) ::boost::gregorian::date(::boost::gregorian::not_a_date_time);
0105 }
0106 
0107 /**** date_duration ****/
0108 
0109 //! Function to save gregorian::date_duration objects using serialization lib
0110 template<class Archive>
0111 void save(Archive & ar, const gregorian::date_duration & dd, 
0112           unsigned int /*version*/)
0113 {
0114   typename gregorian::date_duration::duration_rep dr = dd.get_rep();
0115   ar & make_nvp("date_duration", dr);
0116 }
0117 //! Function to load gregorian::date_duration objects using serialization lib
0118 template<class Archive>
0119 void load(Archive & ar, gregorian::date_duration & dd, unsigned int /*version*/)
0120 {
0121   typename gregorian::date_duration::duration_rep dr(0);
0122   ar & make_nvp("date_duration", dr);
0123   dd = gregorian::date_duration(dr);
0124 }
0125 //!override needed b/c no default constructor
0126 template<class Archive>
0127 inline void load_construct_data(Archive & /*ar*/, gregorian::date_duration* dd, 
0128                                 const unsigned int /*file_version*/)
0129 {
0130   ::new(dd) gregorian::date_duration(gregorian::not_a_date_time);
0131 }
0132 
0133 /**** date_duration::duration_rep (most likely int_adapter) ****/
0134 
0135 //! helper unction to save date_duration objects using serialization lib
0136 template<class Archive>
0137 void save(Archive & ar, const gregorian::date_duration::duration_rep & dr, 
0138           unsigned int /*version*/)
0139 {
0140   typename gregorian::date_duration::duration_rep::int_type it = dr.as_number();
0141   ar & make_nvp("date_duration_duration_rep", it);
0142 }
0143 //! helper function to load date_duration objects using serialization lib
0144 template<class Archive>
0145 void load(Archive & ar, gregorian::date_duration::duration_rep & dr, unsigned int /*version*/)
0146 {
0147   typename gregorian::date_duration::duration_rep::int_type it(0);
0148   ar & make_nvp("date_duration_duration_rep", it);
0149   dr = gregorian::date_duration::duration_rep::int_type(it);
0150 }
0151 //!override needed b/c no default constructor
0152 template<class Archive>
0153 inline void load_construct_data(Archive & /*ar*/, gregorian::date_duration::duration_rep* dr, 
0154                                 const unsigned int /*file_version*/)
0155 {
0156   ::new(dr) gregorian::date_duration::duration_rep(0);
0157 }
0158 
0159 /**** date_period ****/
0160 
0161 //! Function to save gregorian::date_period objects using serialization lib
0162 /*! date_period objects are broken down into 2 parts for serialization:
0163  * the begining date object and the end date object
0164  */
0165 template<class Archive>
0166 void save(Archive & ar, const gregorian::date_period& dp, 
0167           unsigned int /*version*/)
0168 {
0169   gregorian::date d1 = dp.begin();
0170   gregorian::date d2 = dp.end();
0171   ar & make_nvp("date_period_begin_date", d1);
0172   ar & make_nvp("date_period_end_date", d2);
0173 }
0174 //! Function to load gregorian::date_period objects using serialization lib
0175 /*! date_period objects are broken down into 2 parts for serialization:
0176  * the begining date object and the end date object
0177  */
0178 template<class Archive>
0179 void load(Archive & ar, gregorian::date_period& dp, unsigned int /*version*/)
0180 {
0181   gregorian::date d1(gregorian::not_a_date_time);
0182   gregorian::date d2(gregorian::not_a_date_time);
0183   ar & make_nvp("date_period_begin_date", d1);
0184   ar & make_nvp("date_period_end_date", d2);
0185   dp = gregorian::date_period(d1,d2);
0186 }
0187 //!override needed b/c no default constructor
0188 template<class Archive>
0189 inline void load_construct_data(Archive & /*ar*/, gregorian::date_period* dp, 
0190                                 const unsigned int /*file_version*/)
0191 {
0192   gregorian::date d(gregorian::not_a_date_time);
0193   gregorian::date_duration dd(1);
0194   ::new(dp) gregorian::date_period(d,dd);
0195 }
0196 
0197 /**** greg_year ****/
0198 
0199 //! Function to save gregorian::greg_year objects using serialization lib
0200 template<class Archive>
0201 void save(Archive & ar, const gregorian::greg_year& gy, 
0202           unsigned int /*version*/)
0203 {
0204   unsigned short us = gy;
0205   ar & make_nvp("greg_year", us);
0206 }
0207 //! Function to load gregorian::greg_year objects using serialization lib
0208 template<class Archive>
0209 void load(Archive & ar, gregorian::greg_year& gy, unsigned int /*version*/)
0210 {
0211   unsigned short us;
0212   ar & make_nvp("greg_year", us);
0213   gy = gregorian::greg_year(us);
0214 }
0215 //!override needed b/c no default constructor
0216 template<class Archive>
0217 inline void load_construct_data(Archive & /*ar*/, gregorian::greg_year* gy, 
0218                                 const unsigned int /*file_version*/)
0219 {
0220   ::new(gy) gregorian::greg_year(1900);
0221 }
0222 
0223 /**** greg_month ****/
0224 
0225 //! Function to save gregorian::greg_month objects using serialization lib
0226 template<class Archive>
0227 void save(Archive & ar, const gregorian::greg_month& gm, 
0228           unsigned int /*version*/)
0229 {
0230   unsigned short us = gm.as_number();
0231   ar & make_nvp("greg_month", us);
0232 }
0233 //! Function to load gregorian::greg_month objects using serialization lib
0234 template<class Archive>
0235 void load(Archive & ar, gregorian::greg_month& gm, unsigned int /*version*/)
0236 {
0237   unsigned short us;
0238   ar & make_nvp("greg_month", us);
0239   gm = gregorian::greg_month(us);
0240 }
0241 //!override needed b/c no default constructor
0242 template<class Archive>
0243 inline void load_construct_data(Archive & /*ar*/, gregorian::greg_month* gm, 
0244                                 const unsigned int /*file_version*/)
0245 {
0246   ::new(gm) gregorian::greg_month(1);
0247 }
0248 
0249 /**** greg_day ****/
0250 
0251 //! Function to save gregorian::greg_day objects using serialization lib
0252 template<class Archive>
0253 void save(Archive & ar, const gregorian::greg_day& gd, 
0254           unsigned int /*version*/)
0255 {
0256   unsigned short us = gd.as_number();
0257   ar & make_nvp("greg_day", us);
0258 }
0259 //! Function to load gregorian::greg_day objects using serialization lib
0260 template<class Archive>
0261 void load(Archive & ar, gregorian::greg_day& gd, unsigned int /*version*/)
0262 {
0263   unsigned short us;
0264   ar & make_nvp("greg_day", us);
0265   gd = gregorian::greg_day(us);
0266 }
0267 //!override needed b/c no default constructor
0268 template<class Archive>
0269 inline void load_construct_data(Archive & /*ar*/, gregorian::greg_day* gd, 
0270                                 const unsigned int /*file_version*/)
0271 {
0272   ::new(gd) gregorian::greg_day(1);
0273 }
0274 
0275 /**** greg_weekday ****/
0276 
0277 //! Function to save gregorian::greg_weekday objects using serialization lib
0278 template<class Archive>
0279 void save(Archive & ar, const gregorian::greg_weekday& gd, 
0280           unsigned int /*version*/)
0281 {
0282   unsigned short us = gd.as_number();
0283   ar & make_nvp("greg_weekday", us);
0284 }
0285 //! Function to load gregorian::greg_weekday objects using serialization lib
0286 template<class Archive>
0287 void load(Archive & ar, gregorian::greg_weekday& gd, unsigned int /*version*/)
0288 {
0289   unsigned short us;
0290   ar & make_nvp("greg_weekday", us);
0291   gd = gregorian::greg_weekday(us);
0292 }
0293 //!override needed b/c no default constructor
0294 template<class Archive>
0295 inline void load_construct_data(Archive & /*ar*/, gregorian::greg_weekday* gd, 
0296                                 const unsigned int /*file_version*/)
0297 {
0298   ::new(gd) gregorian::greg_weekday(1);
0299 }
0300 
0301 /**** date_generators ****/
0302 
0303 /**** partial_date ****/
0304 
0305 //! Function to save gregorian::partial_date objects using serialization lib
0306 /*! partial_date objects are broken down into 2 parts for serialization:
0307  * the day (typically greg_day) and month (typically greg_month) objects
0308  */
0309 template<class Archive>
0310 void save(Archive & ar, const gregorian::partial_date& pd, 
0311           unsigned int /*version*/)
0312 {
0313   gregorian::greg_day gd(pd.day());
0314   gregorian::greg_month gm(pd.month().as_number());
0315   ar & make_nvp("partial_date_day", gd);
0316   ar & make_nvp("partial_date_month", gm);
0317 }
0318 //! Function to load gregorian::partial_date objects using serialization lib
0319 /*! partial_date objects are broken down into 2 parts for serialization:
0320  * the day (greg_day) and month (greg_month) objects
0321  */
0322 template<class Archive>
0323 void load(Archive & ar, gregorian::partial_date& pd, unsigned int /*version*/)
0324 {
0325   gregorian::greg_day gd(1);
0326   gregorian::greg_month gm(1);
0327   ar & make_nvp("partial_date_day", gd);
0328   ar & make_nvp("partial_date_month", gm);
0329   pd = gregorian::partial_date(gd,gm);
0330 }
0331 //!override needed b/c no default constructor
0332 template<class Archive>
0333 inline void load_construct_data(Archive & /*ar*/, gregorian::partial_date* pd, 
0334                                 const unsigned int /*file_version*/)
0335 {
0336   gregorian::greg_month gm(1);
0337   gregorian::greg_day gd(1);
0338   ::new(pd) gregorian::partial_date(gd,gm);
0339 }
0340 
0341 /**** nth_kday_of_month ****/
0342 
0343 //! Function to save nth_day_of_the_week_in_month objects using serialization lib
0344 /*! nth_day_of_the_week_in_month  objects are broken down into 3 parts for 
0345  * serialization: the week number, the day of the week, and the month
0346  */
0347 template<class Archive>
0348 void save(Archive & ar, const gregorian::nth_kday_of_month& nkd, 
0349           unsigned int /*version*/)
0350 {
0351   typename gregorian::nth_kday_of_month::week_num wn(nkd.nth_week());
0352   typename gregorian::nth_kday_of_month::day_of_week_type d(nkd.day_of_week().as_number());
0353   typename gregorian::nth_kday_of_month::month_type m(nkd.month().as_number());
0354   ar & make_nvp("nth_kday_of_month_week_num", wn);
0355   ar & make_nvp("nth_kday_of_month_day_of_week", d);
0356   ar & make_nvp("nth_kday_of_month_month", m);
0357 }
0358 //! Function to load nth_day_of_the_week_in_month objects using serialization lib
0359 /*! nth_day_of_the_week_in_month  objects are broken down into 3 parts for 
0360  * serialization: the week number, the day of the week, and the month
0361  */
0362 template<class Archive>
0363 void load(Archive & ar, gregorian::nth_kday_of_month& nkd, unsigned int /*version*/)
0364 {
0365   typename gregorian::nth_kday_of_month::week_num wn(gregorian::nth_kday_of_month::first);
0366   typename gregorian::nth_kday_of_month::day_of_week_type d(gregorian::Monday);
0367   typename gregorian::nth_kday_of_month::month_type m(gregorian::Jan);
0368   ar & make_nvp("nth_kday_of_month_week_num", wn);
0369   ar & make_nvp("nth_kday_of_month_day_of_week", d);
0370   ar & make_nvp("nth_kday_of_month_month", m);
0371   
0372   nkd = gregorian::nth_kday_of_month(wn,d,m);
0373 }
0374 //!override needed b/c no default constructor
0375 template<class Archive>
0376 inline void load_construct_data(Archive & /*ar*/, 
0377                                 gregorian::nth_kday_of_month* nkd, 
0378                                 const unsigned int /*file_version*/)
0379 {
0380   // values used are not significant
0381   ::new(nkd) gregorian::nth_kday_of_month(gregorian::nth_kday_of_month::first,
0382                                          gregorian::Monday,gregorian::Jan);
0383 }
0384 
0385 /**** first_kday_of_month ****/
0386 
0387 //! Function to save first_day_of_the_week_in_month objects using serialization lib
0388 /*! first_day_of_the_week_in_month objects are broken down into 2 parts for 
0389  * serialization: the day of the week, and the month
0390  */
0391 template<class Archive>
0392 void save(Archive & ar, const gregorian::first_kday_of_month& fkd, 
0393           unsigned int /*version*/)
0394 {
0395   typename gregorian::first_kday_of_month::day_of_week_type d(fkd.day_of_week().as_number());
0396   typename gregorian::first_kday_of_month::month_type m(fkd.month().as_number());
0397   ar & make_nvp("first_kday_of_month_day_of_week", d);
0398   ar & make_nvp("first_kday_of_month_month", m);
0399 }
0400 //! Function to load first_day_of_the_week_in_month objects using serialization lib
0401 /*! first_day_of_the_week_in_month objects are broken down into 2 parts for 
0402  * serialization: the day of the week, and the month
0403  */
0404 template<class Archive>
0405 void load(Archive & ar, gregorian::first_kday_of_month& fkd, unsigned int /*version*/)
0406 {
0407   typename gregorian::first_kday_of_month::day_of_week_type d(gregorian::Monday);
0408   typename gregorian::first_kday_of_month::month_type m(gregorian::Jan);
0409   ar & make_nvp("first_kday_of_month_day_of_week", d);
0410   ar & make_nvp("first_kday_of_month_month", m);
0411   
0412   fkd = gregorian::first_kday_of_month(d,m);
0413 }
0414 //!override needed b/c no default constructor
0415 template<class Archive>
0416 inline void load_construct_data(Archive & /*ar*/, 
0417                                 gregorian::first_kday_of_month* fkd, 
0418                                 const unsigned int /*file_version*/)
0419 {
0420   // values used are not significant
0421   ::new(fkd) gregorian::first_kday_of_month(gregorian::Monday,gregorian::Jan);
0422 }
0423 
0424 /**** last_kday_of_month ****/
0425 
0426 //! Function to save last_day_of_the_week_in_month objects using serialization lib
0427 /*! last_day_of_the_week_in_month objects are broken down into 2 parts for 
0428  * serialization: the day of the week, and the month
0429  */
0430 template<class Archive>
0431 void save(Archive & ar, const gregorian::last_kday_of_month& lkd, 
0432           unsigned int /*version*/)
0433 {
0434   typename gregorian::last_kday_of_month::day_of_week_type d(lkd.day_of_week().as_number());
0435   typename gregorian::last_kday_of_month::month_type m(lkd.month().as_number());
0436   ar & make_nvp("last_kday_of_month_day_of_week", d);
0437   ar & make_nvp("last_kday_of_month_month", m);
0438 }
0439 //! Function to load last_day_of_the_week_in_month objects using serialization lib
0440 /*! last_day_of_the_week_in_month objects are broken down into 2 parts for 
0441  * serialization: the day of the week, and the month
0442  */
0443 template<class Archive>
0444 void load(Archive & ar, gregorian::last_kday_of_month& lkd, unsigned int /*version*/)
0445 {
0446   typename gregorian::last_kday_of_month::day_of_week_type d(gregorian::Monday);
0447   typename gregorian::last_kday_of_month::month_type m(gregorian::Jan);
0448   ar & make_nvp("last_kday_of_month_day_of_week", d);
0449   ar & make_nvp("last_kday_of_month_month", m);
0450   
0451   lkd = gregorian::last_kday_of_month(d,m);
0452 }
0453 //!override needed b/c no default constructor
0454 template<class Archive>
0455 inline void load_construct_data(Archive & /*ar*/, 
0456                                 gregorian::last_kday_of_month* lkd, 
0457                                 const unsigned int /*file_version*/)
0458 {
0459   // values used are not significant
0460   ::new(lkd) gregorian::last_kday_of_month(gregorian::Monday,gregorian::Jan);
0461 }
0462 
0463 /**** first_kday_before ****/
0464 
0465 //! Function to save first_day_of_the_week_before objects using serialization lib
0466 template<class Archive>
0467 void save(Archive & ar, const gregorian::first_kday_before& fkdb, 
0468           unsigned int /*version*/)
0469 {
0470   typename gregorian::first_kday_before::day_of_week_type d(fkdb.day_of_week().as_number());
0471   ar & make_nvp("first_kday_before_day_of_week", d);
0472 }
0473 //! Function to load first_day_of_the_week_before objects using serialization lib
0474 template<class Archive>
0475 void load(Archive & ar, gregorian::first_kday_before& fkdb, unsigned int /*version*/)
0476 {
0477   typename gregorian::first_kday_before::day_of_week_type d(gregorian::Monday);
0478   ar & make_nvp("first_kday_before_day_of_week", d);
0479   
0480   fkdb = gregorian::first_kday_before(d);
0481 }
0482 //!override needed b/c no default constructor
0483 template<class Archive>
0484 inline void load_construct_data(Archive & /*ar*/, 
0485                                 gregorian::first_kday_before* fkdb, 
0486                                 const unsigned int /*file_version*/)
0487 {
0488   // values used are not significant
0489   ::new(fkdb) gregorian::first_kday_before(gregorian::Monday);
0490 }
0491 
0492 /**** first_kday_after ****/
0493 
0494 //! Function to save first_day_of_the_week_after objects using serialization lib
0495 template<class Archive>
0496 void save(Archive & ar, const gregorian::first_kday_after& fkda, 
0497           unsigned int /*version*/)
0498 {
0499   typename gregorian::first_kday_after::day_of_week_type d(fkda.day_of_week().as_number());
0500   ar & make_nvp("first_kday_after_day_of_week", d);
0501 }
0502 //! Function to load first_day_of_the_week_after objects using serialization lib
0503 template<class Archive>
0504 void load(Archive & ar, gregorian::first_kday_after& fkda, unsigned int /*version*/)
0505 {
0506   typename gregorian::first_kday_after::day_of_week_type d(gregorian::Monday);
0507   ar & make_nvp("first_kday_after_day_of_week", d);
0508   
0509   fkda = gregorian::first_kday_after(d);
0510 }
0511 //!override needed b/c no default constructor
0512 template<class Archive>
0513 inline void load_construct_data(Archive & /*ar*/, 
0514                                 gregorian::first_kday_after* fkda, 
0515                                 const unsigned int /*file_version*/)
0516 {
0517   // values used are not significant
0518   ::new(fkda) gregorian::first_kday_after(gregorian::Monday);
0519 }
0520 
0521 } // namespace serialization
0522 } // namespace boost
0523 
0524 #endif