如何在Boost.Serialization中更改默认的枚举序列化 [英] How to change the default enums serialization in Boost.Serialization

查看:71
本文介绍了如何在Boost.Serialization中更改默认的枚举序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

默认情况下,Boost.Serialization中的枚举类型被序列化为32位整数.但是我需要将一些枚举类型序列化为不同的宽度整数.我尝试专门化boost :: serialization :: serialize方法,但似乎不适用于枚举.

这是我的尝试:

  #include< iostream>#include< boost/archive/binary_oarchive.hpp>#include< boost/asio.hpp>枚举MyEnum_t{你好再见};命名空间提升{命名空间序列化{模板<类Archive>无效保存(Archive& ar,const MyEnum_t& t,unsigned int version){无符号字符c =(无符号字符)t;ar&C;}模板<类Archive>无效负载(存档&ar,MyEnum_t& t,未签名的int版本){未签名的字符c;ar&C;t =(MyEnum_t)c;}}//命名空间序列化}//命名空间提升BOOST_SERIALIZATION_SPLIT_FREE(MyEnum_t)int main(int argc,const char * argv []){boost :: asio :: streambuf buf;boost :: archive :: binary_oarchive pboa(buf);buf.consume(buf.size());//忽略标题MyEnum_t me = HELLO;pboa<<我;std :: cout<<buf.size()<<std :: endl;//buf.size()= 4,但我想要1返回0;} 

解决方案

这可能不起作用,因为枚举不是实数,我不认为您通常可以为特定枚举重载函数./p>

您可以通过对包含MyEnum_t的任何对象进行序列化以转换为char来完成所需的操作.您也可以按照Dan的建议进行操作,并将枚举封装为一流的类型,从而可以重载序列化.像这样:

  class MyEnum_clone {未签名的字符v_;MyEnum_clone(MyEnum_t v):v_(v){};运算符MyEnum_t()const {返回MyEnum_t(v_);};//序列化...}; 

尽管如此,它仍然可能不是完全透明的.

但是,我不明白为什么您关心类型的序列化方式.只要您可以正确还原对象,就不必关心序列化的内部表示,这不是序列化的意义.内部表示似乎是档案的属性.

By default in Boost.Serialization, enum types are serialized as a 32-bit integer. But I need to serialize some enum types as different width integer. I've tried to specialize the boost::serialization::serialize method, but it seems it doesn't work for enums.

Here is my attempt:

#include <iostream>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/asio.hpp>

enum MyEnum_t
{
    HELLO, BYE
};

namespace boost 
{ 
namespace serialization
{

template< class Archive >
void save(Archive & ar, const MyEnum_t & t, unsigned int version)
{
    unsigned char c = (unsigned char) t;
    ar & c;
}

template< class Archive >
void load(Archive & ar, MyEnum_t & t, unsigned int version)
{
    unsigned char c;
    ar & c;
    t = (MyEnum_t) c;
}

} // namespace serialization
} // namespace boost

BOOST_SERIALIZATION_SPLIT_FREE(MyEnum_t)

int main(int argc, const char *argv[])
{
    boost::asio::streambuf buf;
    boost::archive::binary_oarchive pboa(buf); 

    buf.consume(buf.size()); // Ignore headers

    MyEnum_t me = HELLO;
    pboa << me;

    std::cout << buf.size() << std::endl; // buf.size() = 4, but I want 1

    return 0;
} 

解决方案

This probably doesn't work because enums aren't a real type, I don't think you can in general overload a function for a specific enum.

You could accomplish what you want by doing the conversion to char in the serialization of whatever object contains your MyEnum_t. You could also do what Dan suggested and encapsulate the enum in a first-class type for which you can overload the serialization. Something like:

class MyEnum_clone {
  unsigned char v_;
  MyEnum_clone(MyEnum_t v) : v_(v) {};
  operator MyEnum_t() const {return MyEnum_t(v_); };

  // serialization...
};

That still likely won't be completetely transparent, though.

However, I don't see why you care about how the type is serialized. Isn't the point of serializing that you don't have to care about the internal representation of the serialization, as long as you can restore the object correctly. The internal representation seems like a property of the archive.

这篇关于如何在Boost.Serialization中更改默认的枚举序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆