greg_serialize.hpp
来自「support vector clustering for vc++」· HPP 代码 · 共 490 行 · 第 1/2 页
HPP
490 行
#ifndef GREGORIAN_SERIALIZE_HPP___
#define GREGORIAN_SERIALIZE_HPP___
/* Copyright (c) 2004-2005 CrystalClear Software, Inc.
* Use, modification and distribution is subject to the
* Boost Software License, Version 1.0. (See accompanying
* file LICENSE-1.0 or http://www.boost.org/LICENSE-1.0)
* Author: Jeff Garland, Bart Garst
* $Date: 2007/03/02 02:45:57 $
*/
#include "boost/date_time/gregorian/gregorian_types.hpp"
#include "boost/date_time/gregorian/parsers.hpp"
#include "boost/serialization/split_free.hpp"
// macros to split serialize functions into save & load functions
// An expanded version is below for gregorian::date
// NOTE: these macros define template functions in the boost::serialization namespace.
// They must be expanded *outside* of any namespace
BOOST_SERIALIZATION_SPLIT_FREE(::boost::gregorian::date_duration)
BOOST_SERIALIZATION_SPLIT_FREE(::boost::gregorian::date_duration::duration_rep)
BOOST_SERIALIZATION_SPLIT_FREE(::boost::gregorian::date_period)
BOOST_SERIALIZATION_SPLIT_FREE(::boost::gregorian::greg_month)
BOOST_SERIALIZATION_SPLIT_FREE(::boost::gregorian::greg_day)
BOOST_SERIALIZATION_SPLIT_FREE(::boost::gregorian::greg_weekday)
BOOST_SERIALIZATION_SPLIT_FREE(::boost::gregorian::partial_date)
BOOST_SERIALIZATION_SPLIT_FREE(::boost::gregorian::nth_kday_of_month)
BOOST_SERIALIZATION_SPLIT_FREE(::boost::gregorian::first_kday_of_month)
BOOST_SERIALIZATION_SPLIT_FREE(::boost::gregorian::last_kday_of_month)
BOOST_SERIALIZATION_SPLIT_FREE(::boost::gregorian::first_kday_before)
BOOST_SERIALIZATION_SPLIT_FREE(::boost::gregorian::first_kday_after)
namespace boost {
namespace serialization {
/*! Method that does serialization for gregorian::date -- splits to load/save
*/
template<class Archive>
inline void serialize(Archive & ar,
::boost::gregorian::date & d,
const unsigned int file_version)
{
split_free(ar, d, file_version);
}
//! Function to save gregorian::date objects using serialization lib
/*! Dates are serialized into a string for transport and storage.
* While it would be more efficient to store the internal
* integer used to manipulate the dates, it is an unstable solution.
*/
template<class Archive>
void save(Archive & ar,
const ::boost::gregorian::date & d,
unsigned int /* version */)
{
std::string ds = to_iso_string(d);
ar & make_nvp("date", ds);
}
//! Function to load gregorian::date objects using serialization lib
/*! Dates are serialized into a string for transport and storage.
* While it would be more efficient to store the internal
* integer used to manipulate the dates, it is an unstable solution.
*/
template<class Archive>
void load(Archive & ar,
::boost::gregorian::date & d,
unsigned int /*version*/)
{
std::string ds;
ar & make_nvp("date", ds);
try{
d = ::boost::gregorian::from_undelimited_string(ds);
}catch(bad_lexical_cast be) {
gregorian::special_values sv = gregorian::special_value_from_string(ds);
if(sv == gregorian::not_special) {
throw(be); // no match found, rethrow original exception
}
else {
d = gregorian::date(sv);
}
}
}
//!override needed b/c no default constructor
template<class Archive>
inline void load_construct_data(Archive & ar,
::boost::gregorian::date* dp,
const unsigned int /*file_version*/)
{
// retrieve data from archive required to construct new
// invoke inplace constructor to initialize instance of date
::new(dp) ::boost::gregorian::date(::boost::gregorian::not_a_date_time);
}
/**** date_duration ****/
//! Function to save gregorian::date_duration objects using serialization lib
template<class Archive>
void save(Archive & ar, const gregorian::date_duration & dd,
unsigned int /*version*/)
{
typename gregorian::date_duration::duration_rep dr = dd.get_rep();
ar & make_nvp("date_duration", dr);
}
//! Function to load gregorian::date_duration objects using serialization lib
template<class Archive>
void load(Archive & ar, gregorian::date_duration & dd, unsigned int /*version*/)
{
typename gregorian::date_duration::duration_rep dr(0);
ar & make_nvp("date_duration", dr);
dd = gregorian::date_duration(dr);
}
//!override needed b/c no default constructor
template<class Archive>
inline void load_construct_data(Archive & ar, gregorian::date_duration* dd,
const unsigned int /*file_version*/)
{
::new(dd) gregorian::date_duration(gregorian::not_a_date_time);
}
/**** date_duration::duration_rep (most likely int_adapter) ****/
//! helper unction to save date_duration objects using serialization lib
template<class Archive>
void save(Archive & ar, const gregorian::date_duration::duration_rep & dr,
unsigned int /*version*/)
{
typename gregorian::date_duration::duration_rep::int_type it = dr.as_number();
ar & make_nvp("date_duration_duration_rep", it);
}
//! helper function to load date_duration objects using serialization lib
template<class Archive>
void load(Archive & ar, gregorian::date_duration::duration_rep & dr, unsigned int /*version*/)
{
typename gregorian::date_duration::duration_rep::int_type it(0);
ar & make_nvp("date_duration_duration_rep", it);
dr = gregorian::date_duration::duration_rep::int_type(it);
}
//!override needed b/c no default constructor
template<class Archive>
inline void load_construct_data(Archive & ar, gregorian::date_duration::duration_rep* dr,
const unsigned int /*file_version*/)
{
::new(dr) gregorian::date_duration::duration_rep(0);
}
/**** date_period ****/
//! Function to save gregorian::date_period objects using serialization lib
/*! date_period objects are broken down into 2 parts for serialization:
* the begining date object and the end date object
*/
template<class Archive>
void save(Archive & ar, const gregorian::date_period& dp,
unsigned int /*version*/)
{
gregorian::date d1 = dp.begin();
gregorian::date d2 = dp.end();
ar & make_nvp("date_period_begin_date", d1);
ar & make_nvp("date_period_end_date", d2);
}
//! Function to load gregorian::date_period objects using serialization lib
/*! date_period objects are broken down into 2 parts for serialization:
* the begining date object and the end date object
*/
template<class Archive>
void load(Archive & ar, gregorian::date_period& dp, unsigned int /*version*/)
{
gregorian::date d1(gregorian::not_a_date_time);
gregorian::date d2(gregorian::not_a_date_time);
ar & make_nvp("date_period_begin_date", d1);
ar & make_nvp("date_period_end_date", d2);
dp = gregorian::date_period(d1,d2);
}
//!override needed b/c no default constructor
template<class Archive>
inline void load_construct_data(Archive & ar, gregorian::date_period* dp,
const unsigned int /*file_version*/)
{
gregorian::date d(gregorian::not_a_date_time);
gregorian::date_duration dd(1);
::new(dp) gregorian::date_period(d,dd);
}
/**** greg_month ****/
//! Function to save gregorian::greg_month objects using serialization lib
template<class Archive>
void save(Archive & ar, const gregorian::greg_month& gm,
unsigned int /*version*/)
{
unsigned short us = gm.as_number();
ar & make_nvp("greg_month", us);
}
//! Function to load gregorian::greg_month objects using serialization lib
template<class Archive>
void load(Archive & ar, gregorian::greg_month& gm, unsigned int /*version*/)
{
unsigned short us;
ar & make_nvp("greg_month", us);
gm = gregorian::greg_month(us);
}
//!override needed b/c no default constructor
template<class Archive>
inline void load_construct_data(Archive & ar, gregorian::greg_month* gm,
const unsigned int /*file_version*/)
{
::new(gm) gregorian::greg_month(1);
}
/**** greg_day ****/
//! Function to save gregorian::greg_day objects using serialization lib
template<class Archive>
void save(Archive & ar, const gregorian::greg_day& gd,
unsigned int /*version*/)
{
unsigned short us = gd.as_number();
ar & make_nvp("greg_day", us);
}
//! Function to load gregorian::greg_day objects using serialization lib
template<class Archive>
void load(Archive & ar, gregorian::greg_day& gd, unsigned int /*version*/)
{
unsigned short us;
ar & make_nvp("greg_day", us);
gd = gregorian::greg_day(us);
}
//!override needed b/c no default constructor
template<class Archive>
inline void load_construct_data(Archive & ar, gregorian::greg_day* gd,
const unsigned int /*file_version*/)
{
::new(gd) gregorian::greg_day(1);
}
/**** greg_weekday ****/
//! Function to save gregorian::greg_weekday objects using serialization lib
template<class Archive>
void save(Archive & ar, const gregorian::greg_weekday& gd,
unsigned int /*version*/)
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?