我试图使用boost将我的struct存储到txt文件中,但是无法做到。我在用boost库。示例结构
struct Frame
{
uint32_t address{ 0 };
uint16_t marks{ 0 };
uint16_t age{ 0 };
char gender{ 'M' };
std::string userName;
};对于二进制,有简单的代码
boost::archive::binary_oarchive ar(ofs, boost::archive::no_header);
ar << boost::serialization::make_binary_object(&f, sizeof(Frame));假设文件是用fstream对象打开的,而“f”是“框架”的对象。
我想知道是否有类似的方式将结构写入txt文件,并且我不想一个接一个地编写数据类型。假设我们不知道结构中数据类型的类型/数量。
发布于 2021-08-23 13:50:12
正如其他人所评论的,您将不得不提供序列化帮助程序,告诉Boost如何按成员顺序序列化。
如果只有这样的聚合,则可以使用Boost PFR在一定程度上自动生成此函数。
pfr::for_each_field(s, [&](auto&& f) { ar & f; }); 下面是一个例子:
namespace MyLib {
struct Frame {
uint32_t address{0};
uint16_t marks{0};
uint16_t age{0};
char gender{'M'};
std::string userName;
};
struct Other {
std::string userName;
std::map<uint32_t, std::string> properties;
};
} // namespace MyLib注意,为了获得良好的样式,我将它们放置在名称空间中,这样我们就可以突出显示ADL用于查找serialize重载。现在,让我们定义重载:
namespace MyLib {
#define SERIALIZER(Aggregate) \
template <typename Archive> \
void serialize(Archive& ar, Aggregate& s, unsigned version) \
{ \
pfr::for_each_field(s, [&](auto&& f) { ar & f; }); \
}
SERIALIZER(Frame)
SERIALIZER(Other)
} // namespace MyLib使用宏我们可以避免重复的代码。当然,您也可以在没有宏的情况下做到这一点。
现在您可以将两者序列化。假设我们有:
MyLib::Frame const diablo{178, 42, 37, 'F', "Diablo"};
MyLib::Other const other{"diablo", {{1, "one"}, {2, "two"}, {3, "three"},}};然后序列化到文本流:
boost::archive::text_oarchive oa(ss);
oa & diablo;
oa & other;已经导致了流,包括。
22 serialization::archive 19 0 0 178 42 37 70 6 Diablo 0 0 6 diablo 0 0 3 0 0
0 1 3 one 2 3 two 3 5 three全演示
此演示程序检查反序列化的结果是否实际上与原始结构相同:
#include <string>
#include <map>
namespace MyLib {
struct Frame {
uint32_t address{0};
uint16_t marks{0};
uint16_t age{0};
char gender{'M'};
std::string userName;
};
struct Other {
std::string userName;
std::map<uint32_t, std::string> properties;
};
} // namespace MyLib
#include <boost/pfr.hpp>
namespace pfr = boost::pfr;
namespace MyLib {
#define SERIALIZER(Aggregate) \
template <typename Archive> \
void serialize(Archive& ar, Aggregate& s, unsigned version) \
{ \
pfr::for_each_field(s, [&](auto&& f) { ar & f; }); \
}
SERIALIZER(Frame)
SERIALIZER(Other)
} // namespace MyLib
#include <iostream>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/map.hpp>
int main() {
MyLib::Frame const diablo{178, 42, 37, 'F', "Diablo"};
MyLib::Other const other{"diablo", {{1, "one"}, {2, "two"}, {3, "three"},}};
std::stringstream ss;
{
boost::archive::text_oarchive oa(ss);
oa << diablo;
oa << other;
}
std::cout << ss.str() << "\n";
{
boost::archive::text_iarchive ia(ss);
MyLib::Frame f;
MyLib::Other o;
ia >> f >> o;
std::cout << std::boolalpha;
std::cout << "Frame identical: " << pfr::eq(diablo, f) << "\n";
std::cout << "Other identical: " << pfr::eq(other, o) << "\n";
}
}打印
g++ -std=c++2a -O2 -Wall -pedantic -pthread main.cpp -lboost_serialization && ./a.out
22 serialization::archive 19 0 0 178 42 37 70 6 Diablo 0 0 6 diablo 0 0 3 0 0 0 1 3 one 2 3 two 3 5 three
Frame identical: true
Other identical: truehttps://stackoverflow.com/questions/68889432
复制相似问题