如何使用Boost库序列化结构中的联合?

时间:2014-07-11 06:37:35

标签: c++ serialization boost struct

我想序列化并存储在struct中声明的union并将其存储在boost::message_queue中,因为我们无法在Message队列中存储和检索struct对象。

我已经序列化并存储了这样的结构,它工作正常。

typedef struct
{
  unsigned short m_short1;
  unsigned short m_short2;
  unsigned char m_character;

  template <typename Archive>
  void serialize(Archive& ar, const unsigned int version)
  {
    ar & m_short1;
    ar & m_short2;
    ar & m_character;
  }
} MyDataType;

但我想存储一个具有union和sub结构的结构对象,如下所示

typedef struct 
{
  int  msg_type;
  char msg_name[100];

  union
  {
    struct 
    {
        int     ID;
        char    ReportedTime[100];
        char    ReceivedAt[200];
        int     Number;

    } mess1;

    struct
    {
        char    host_ip[20];
        char    mac_addr[30];
        char    time_stamp[100];

    }mess2;

  }struct_type;

}msg_struct;

为了序列化这个,我确实喜欢这个。

typedef struct 
{
int  msg_type;
char msg_name[100];

union
{
    struct 
    {
        int     ID;
        char    ReportedTime[100];
        char    ReceivedAt[200];
        int     Number;

        template <typename Archive>
        void serialize(Archive& ar, const unsigned int version)
        {
            ar & ID;
            ar & DeviceReportedTime;
            ar & ReceivedAt;
            ar & Number;

        }
    } mess1;

    struct
    {
        char    host_ip[20];
        char    mac_addr[30];
        char    time_stamp[100];
        template <typename Archive>
        void serialize(Archive& ar, const unsigned int version)
        {
            ar & host_ip;

            ar & mac_addr;
            ar & time_stamp;
        }

    }mess2;
    template <typename Archive>
    void serialize(Archive& ar, const unsigned int version)
    {
        ar & mess1;
        ar & mess2;

    }

}struct_type;
template <typename Archive>
void serialize(Archive& ar, const unsigned int version)
{
        ar & msg_type;
        ar & msg_name;
        ar & struct_type;

}

}msg_struct;

这是正确的方法, 但是它给出了以下错误

 from ControlQueue.cpp:1:
/usr/local/include/boost/archive/detail/check.hpp: In instantiation of ‘void          boost::archive::detail::check_object_level() [with T = msg_struct::<anonymous union>]’:
/usr/local/include/boost/archive/detail/iserializer.hpp:438:33:   required from ‘static void boost::archive::detail::load_non_pointer_type<Archive>::invoke(Archive&, T&) [with T = msg_struct::<anonymous union>; Archive = boost::archive::text_iarchive]’
/usr/local/include/boost/archive/detail/iserializer.hpp:592:24:   required from ‘void boost::archive::load(Archive&, T&) [with Archive = boost::archive::text_iarchive; T = msg_struct::<anonymous union>]’
/usr/local/include/boost/archive/detail/common_iarchive.hpp:66:40:   required from ‘void boost::archive::detail::common_iarchive<Archive>::load_override(T&, int) [with T = msg_struct::<anonymous union>; Archive = boost::archive::text_iarchive]’
/usr/local/include/boost/archive/basic_text_iarchive.hpp:65:9:   required from ‘void boost::archive::basic_text_iarchive<Archive>::load_override(T&, int) [with T = msg_struct::<anonymous union>; Archive = boost::archive::text_iarchive]’
/usr/local/include/boost/archive/text_iarchive.hpp:82:57:   required from ‘void boost::archive::text_iarchive_impl<Archive>::load_override(T&, int) [with T = msg_struct::<anonymous union>; Archive = boost::archive::text_iarchive]’
/usr/local/include/boost/archive/detail/interface_iarchive.hpp:60:9:   [ skipping 15 instantiation contexts, use -ftemplate-backtrace-limit=0 to disable ]
  /usr/local/include/boost/archive/detail/iserializer.hpp:592:24:   required from ‘void boost::archive::load(Archive&, T&) [with Archive = boost::archive::text_iarchive; T = msg_struct*]’
    /usr/local/include/boost/archive/detail/common_iarchive.hpp:66:40:   required from ‘void boost::archive::detail::common_iarchive<Archive>::load_override(T&, int) [with T = msg_struct*; Archive = boost::archive::text_iarchive]’
  /usr/local/include/boost/archive/basic_text_iarchive.hpp:65:9:   required from ‘void boost::archive::basic_text_iarchive<Archive>::load_override(T&, int) [with T = msg_struct*; Archive = boost::archive::text_iarchive]’
    /usr/local/include/boost/archive/text_iarchive.hpp:82:57:   required from ‘void boost::archive::text_iarchive_impl<Archive>::load_override(T&, int) [with T = msg_struct*; Archive = boost::archive::text_iarchive]’
 /usr/local/include/boost/archive/detail/interface_iarchive.hpp:60:9:   required from ‘Archive& boost::archive::detail::interface_iarchive<Archive>::operator>>(T&) [with T = msg_struct*; Archive = boost::archive::text_iarchive]’
 ControlQueue.cpp:177:15:   required from here
  /usr/local/include/boost/archive/detail/check.hpp:60:5: error: static assertion failed: typex::value
 BOOST_STATIC_ASSERT(typex::value);
 ^
 make: *** [ControlQueue.o] Error 1

注意:我正在使用make文件并包含所有链接库。

如何解决这个问题

1 个答案:

答案 0 :(得分:1)

我注意到你花了很多精力来保持结构POD。您可以使用

简单地利用它
BOOST_IS_BITWISE_SERIALIZABLE(msg_struct)

完成序列化。奇怪的是,这似乎是 limited to arrays of POD ,但C ++标准允许我们将单个对象视为单元素数组,因此我们可以:

struct msg_struct {
    int  msg_type;
    char msg_name[100];

    union X {
        struct {
            int     ID;
            char    ReportedTime[100];
            char    ReceivedAt[200];
            int     Number;
        } mess1;

        struct XB {
            char    host_ip[20];
            char    mac_addr[30];
            char    time_stamp[100];
        } mess2;
    } struct_type;
};

#include <boost/serialization/is_bitwise_serializable.hpp>
BOOST_IS_BITWISE_SERIALIZABLE(msg_struct)

可以进行序列化/反序列化:查看 Live On Coliru

int main()
{
    std::stringstream ss;
    boost::archive::binary_oarchive oa(ss);

    msg_struct msg;

    strncpy(msg.msg_name,                       "msg_name",     sizeof(msg.msg_name));
    strncpy(msg.struct_type.mess1.ReportedTime, "ReportedTime", sizeof(msg.struct_type.mess1.ReportedTime));
    strncpy(msg.struct_type.mess1.ReceivedAt,   "ReceivedAt",   sizeof(msg.struct_type.mess1.ReceivedAt));
    strncpy(msg.struct_type.mess2.host_ip,      "host_ip",      sizeof(msg.struct_type.mess2.host_ip));
    strncpy(msg.struct_type.mess2.mac_addr,     "mac_addr",     sizeof(msg.struct_type.mess2.mac_addr));
    strncpy(msg.struct_type.mess2.time_stamp,   "time_stamp",   sizeof(msg.struct_type.mess2.time_stamp));

    oa << boost::serialization::make_array(&msg, 1);

    for(auto c : ss.str())
        std::cout << std::hex << std::setfill('0') << std::setw(2) << ((int) c);

    boost::archive::binary_iarchive ia(ss);
    ia >> boost::serialization::make_array(&msg, 1);
}

完整代码

struct msg_struct {
    int  msg_type;
    char msg_name[100];

    union X {
        struct {
            int     ID;
            char    ReportedTime[100];
            char    ReceivedAt[200];
            int     Number;
        } mess1;

        struct XB {
            char    host_ip[20];
            char    mac_addr[30];
            char    time_stamp[100];
        } mess2;
    } struct_type;
};

#include <boost/serialization/is_bitwise_serializable.hpp>
BOOST_IS_BITWISE_SERIALIZABLE(msg_struct)

#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>

#include <sstream>
#include <iostream>
#include <cstring>
#include <iomanip>

int main()
{
    std::stringstream ss;
    boost::archive::binary_oarchive oa(ss);

    msg_struct msg;

    strncpy(msg.msg_name,                       "msg_name",     sizeof(msg.msg_name));
    strncpy(msg.struct_type.mess1.ReportedTime, "ReportedTime", sizeof(msg.struct_type.mess1.ReportedTime));
    strncpy(msg.struct_type.mess1.ReceivedAt,   "ReceivedAt",   sizeof(msg.struct_type.mess1.ReceivedAt));
    strncpy(msg.struct_type.mess2.host_ip,      "host_ip",      sizeof(msg.struct_type.mess2.host_ip));
    strncpy(msg.struct_type.mess2.mac_addr,     "mac_addr",     sizeof(msg.struct_type.mess2.mac_addr));
    strncpy(msg.struct_type.mess2.time_stamp,   "time_stamp",   sizeof(msg.struct_type.mess2.time_stamp));

    oa << boost::serialization::make_array(&msg, 1);

    for(auto c : ss.str())
        std::cout << std::hex << std::setfill('0') << std::setw(2) << ((int) c);

    boost::archive::binary_iarchive ia(ss);
    ia >> boost::serialization::make_array(&msg, 1);
}