gpt4 book ai didi

c++ - 如何使用 Boost 库序列化结构中的 union ?

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

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

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

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 和子结构,如下所示

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 文件并包含所有链接库。

如何解决这个问题

最佳答案

我注意到您花了很多精力来保持结构 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);
}

关于c++ - 如何使用 Boost 库序列化结构中的 union ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24691479/

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