C++ icu::使用boost序列化进行销毁
我试图用boost序列化库序列化icu::UnicodeString,但遇到了问题 icu::UnicodeString没有序列化它所需的序列化函数。所以我试着创造它,但我不知道如何制作这些。示例代码:C++ icu::使用boost序列化进行销毁,c++,serialization,boost,icu,C++,Serialization,Boost,Icu,我试图用boost序列化库序列化icu::UnicodeString,但遇到了问题 icu::UnicodeString没有序列化它所需的序列化函数。所以我试着创造它,但我不知道如何制作这些。示例代码: #include <map> #include <sstream> #include <fstream> #include <unicode/unistr.h> #include <unicode/ustream.h> #includ
#include <map>
#include <sstream>
#include <fstream>
#include <unicode/unistr.h>
#include <unicode/ustream.h>
#include <boost/serialization/map.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
namespace boost {
namespace serialization {
template<class Archive>
inline void save(
Archive & ar,
const icu_55::UnicodeString& str,
const unsigned int /* file_version */
){
}
template<class Archive>
inline void load(
Archive & ar,
icu_55::UnicodeString& str,
const unsigned int /* file_version */
){
}
// split non-intrusive serialization function member into separate
// non intrusive save/load member functions
template<class Archive>
inline void serialize(
Archive & ar,
icu_55::UnicodeString& str,
const unsigned int file_version
){
boost::serialization::split_free(ar, str, file_version);
}
} // serialization
} // namespace boost
int main()
{
std::map<icu::UnicodeString, int> map = {{"asssdasd",2}, {"qwe",1}, {"Zxc",55}};
std::stringstream ss;
boost::archive::text_oarchive oarch(ss);
oarch << map;
std::map<icu::UnicodeString, int> new_map;
boost::archive::text_iarchive iarch(ss);
iarch >> new_map;
std::cout << (map == new_map) << std::endl;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
名称空间提升{
命名空间序列化{
模板
内联作废保存(
存档和ar,
const icu_55::Unicode销毁和str,
常量unsigned int/*文件\u版本*/
){
}
模板
线内空隙荷载(
存档和ar,
icu_55::Unicode销毁和str,
常量unsigned int/*文件\u版本*/
){
}
//将非侵入式序列化函数成员拆分为单独的
//非侵入式保存/加载成员函数
模板
内联空序列化(
存档和ar,
icu_55::Unicode销毁和str,
常量无符号整数文件\u版本
){
boost::serialization::split_free(ar、str、file_版本);
}
}//序列化
}//名称空间提升
int main()
{
地图地图={{“asssdasd”,2},{“qwe”,1},{“Zxc”,55};
std::stringstream-ss;
boost::archive::text_oarchive oarch(ss);
oarch>新地图;
std::cout我从未直接与LibICU合作过,所以可能有人可以查看此代码
但是,根据我使用Boost序列化的经验,我认为这应该是有帮助的:
template <class Archive>
inline void save(Archive &ar, icu_55::UnicodeString const &str, const unsigned int) {
auto sz = str.getCapacity();
auto len = str.length();
auto buf = str.getBuffer();
if (!buf) throw std::invalid_argument("str");
ar & sz & len & boost::serialization::make_array(buf, sz);
}
template <class Archive>
inline void load(Archive &ar, icu_55::UnicodeString &str, const unsigned int)
{
size_t sz, len;
ar & sz & len;
auto buf = str.getBuffer(sz);
if (!buf) throw std::invalid_argument("str");
try {
ar & boost::serialization::make_array(buf, sz);
}
catch(...) {
str.releaseBuffer(len);
throw;
}
str.releaseBuffer(len);
}
我从未直接与LibICU合作过,因此可能有人可以查看此代码
但是,根据我使用Boost序列化的经验,我认为这应该是有帮助的:
template <class Archive>
inline void save(Archive &ar, icu_55::UnicodeString const &str, const unsigned int) {
auto sz = str.getCapacity();
auto len = str.length();
auto buf = str.getBuffer();
if (!buf) throw std::invalid_argument("str");
ar & sz & len & boost::serialization::make_array(buf, sz);
}
template <class Archive>
inline void load(Archive &ar, icu_55::UnicodeString &str, const unsigned int)
{
size_t sz, len;
ar & sz & len;
auto buf = str.getBuffer(sz);
if (!buf) throw std::invalid_argument("str");
try {
ar & boost::serialization::make_array(buf, sz);
}
catch(...) {
str.releaseBuffer(len);
throw;
}
str.releaseBuffer(len);
}
很好。很好!谢谢!你真的不应该在没有充分审查的情况下从非专家在线获取随机代码片段。我刚刚添加了一些错误处理/异常安全。也许你可以将代码提交给任何ICU专家审查?很好。很好!谢谢!你真的不应该在没有充分审查的情况下从非专家在线获取随机代码片段。我刚刚添加了更多的错误处理/异常安全。也许你可以把代码放在任何ICU专家的审查?
1