icu :: UnicodeString with boost serialize

时间:2016-10-10 08:12:15

标签: c++ serialization boost icu

我正在尝试使用boost序列化序列化序列化icu :: UnicodeString但是遇到了麻烦。

icu :: UnicodeString没有序列化所需的序列化函数。所以我试图创造它,但我不知道如何制作它们。示例代码:

function carousel() {
    var imgs = $(".slideshow > img");
    imgs.stop().hide(0); // hide all the images whenever the carousel() is called
    imgs.each(function(index, element) {   
        $(element).delay(index * 2000).fadeIn(1000, function() {
            if (index + 1 >= imgs.length) {
                carousel(); // call the function only after all images are fadeIn completed
            }
        });
    });
}

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

之类的内容进行编译

目前&#34;保存&#34;和#34;加载&#34;功能未实现。我尝试只使用增强手册中使用的g++ -o new new.cpp -std=c++11 -lboost_serialization -licuuc语句,但我遇到的分段错误,我也无法修复。

1 个答案:

答案 0 :(得分:0)

我从未直接与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);
}

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

#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