2008年11月26日 星期三

擴充boost serialization 支援 CString, CTime

boost的serialization是一個很強大的工具
原本的程式架構只支援std::string等STL元件
如果你的程式有需要用到MFC的話
MFC的CString, CTime或其他class可以很簡單的擴充到boost serialization


namespace boost
{
namespace serialization
{
template
inline void save(Archive & ar, const CString& str, const unsigned int file_version)
{
std::wstring wstr(str);
ar & BOOST_SERIALIZATION_NVP(wstr);
}

template
inline void load(Archive &ar, CString& str, const unsigned int file_version)
{
std::wstring wstr;
ar & BOOST_SERIALIZATION_NVP(wstr);
str = wstr.data();
}

template
inline void serialize(Archive &ar, CString &str, const unsigned int file_version)
{
boost::serialization::split_free(ar, str, file_version);
}

template
inline void save(Archive & ar, const CTime& time, const unsigned int file_version)
{
__time64_t t = time.GetTime();
ar & t;
}

template
inline void load(Archive &ar, CTime& time, const unsigned int file_version)
{
__time64_t t;
ar & t;
time = t;
}

template
inline void serialize(Archive &ar, CTime &time, const unsigned int file_version)
{
boost::serialization::split_free(ar, time, file_version);
}
}
}


ps. 要注意一點,擴充的class要放在boost::serialization namespace裡,要不然可能會找不到對映的function

Reference
http://www.boost.org/doc/libs/1_37_0/libs/serialization/doc/index.html

沒有留言:

LinkWithin

Related Posts with Thumbnails