gpt4 book ai didi

c++ - icu::UnicodeString with boost 序列化

转载 作者:太空宇宙 更新时间:2023-11-04 13:10:59 25 4
gpt4 key购买 nike

我正在尝试使用 boost 序列化库序列化 icu::UnicodeString 但遇到了问题。

icu::UnicodeString 没有序列化所需的序列化函数。所以我尝试创建它,但我不确定如何制作这些。示例代码:

#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;
}

用类似g++ -o new new.cpp -std=c++11 -lboost_serialization -licuuc 的东西编译

目前没有实现“保存”和“加载”功能。我尝试只执行 boost 手册中使用的 ar & str; 语句,但我遇到了段错误,我也无法修复。

最佳答案

我从未直接使用过 LibICU,所以可能有人可以查看此代码。

但是,根据我使用 Boost Serialization 的经验,我认为这应该会有所帮助:

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);
}

它适用于提供的简单测试用例:

#include <fstream>
#include <map>
#include <sstream>
#include <iostream>
#include <unicode/unistr.h>
#include <unicode/ustream.h>

#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/serialization/map.hpp>

namespace boost { namespace serialization {

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);
}


// 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> const 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;
}
}

打印

1

关于c++ - icu::UnicodeString with boost 序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39953919/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com