boost :: serialize:包含位字段成员变量的struct

时间:2016-08-18 00:44:18

标签: c++ winapi boost struct

我似乎无法弄清楚如何使这个结构序列化。看起来你不能通过引用传递任何有填充的成员,从我通过研究可以看出的只是设置填充的现实。我尝试了pragma pack语句,但它并没有什么区别。

这个结构: https://msdn.microsoft.com/en-us/library/windows/desktop/aa363214(v=vs.85).aspx

class SZ_DCB : DCB
{
private:
friend class boost::serialization::access;

template<class Archive>
void serialize(Archive &ar, const unsigned int version){
    ar & BOOST_SERIALIZATION_NVP(DCBlength);
      ar & BOOST_SERIALIZATION_NVP(BaudRate);
      ar & BOOST_SERIALIZATION_NVP(fBinary);
      ar & BOOST_SERIALIZATION_NVP(fParity);
      ar & BOOST_SERIALIZATION_NVP(fOutxCtsFlow);
      ar & BOOST_SERIALIZATION_NVP(fOutxDsrFlow);
      ar & BOOST_SERIALIZATION_NVP(fDtrControl);
      ar & BOOST_SERIALIZATION_NVP(fDsrSensitivity);
      ar & BOOST_SERIALIZATION_NVP(fTXContinueOnXoff);
      ar & BOOST_SERIALIZATION_NVP(fOutX);
      ar & BOOST_SERIALIZATION_NVP(fInX);
      ar & BOOST_SERIALIZATION_NVP(fErrorChar);
      ar & BOOST_SERIALIZATION_NVP(fNull);
      ar & BOOST_SERIALIZATION_NVP(fRtsControl);
      ar & BOOST_SERIALIZATION_NVP(fAbortOnError);
      ar & BOOST_SERIALIZATION_NVP(fDummy2);
      ar &  BOOST_SERIALIZATION_NVP(wReserved);
      ar &  BOOST_SERIALIZATION_NVP(XonLim);
      ar &  BOOST_SERIALIZATION_NVP(XoffLim);
      ar &  BOOST_SERIALIZATION_NVP(ByteSize);
      ar &  BOOST_SERIALIZATION_NVP(Parity);
      ar &  BOOST_SERIALIZATION_NVP(StopBits);
      ar &  BOOST_SERIALIZATION_NVP(XonChar);
      ar &  BOOST_SERIALIZATION_NVP(XoffChar);
      ar &  BOOST_SERIALIZATION_NVP(ErrorChar);
      ar & BOOST_SERIALIZATION_NVP(EofChar);
      ar &  BOOST_SERIALIZATION_NVP(EvtChar);
      ar & BOOST_SERIALIZATION_NVP(wReserved1);
}
};

1 个答案:

答案 0 :(得分:2)

&#34;标志&#34;位域(fBinaryfParity等)不能以这种方式序列化,因为您无法获取位字段的地址或引用位字段。

为了序列化它们,您需要将值分配给局部变量,序列化它们,然后将(可能已修改的)本地值保存回位域。

类似的东西:

BYTE binary = fBinary;
ar & BOOST_SERIALIZATION_NVP(binary);
fBinary = binary;

(使用BYTE,因为fBinary是一位。)