提升嵌套结构的序列化不起作用 [英] boost serialization of nested struct does not work
问题描述
我正在尝试序列化包含两个或更多其他类型的turct的结构.但是似乎序列化不起作用.下面是示例代码:
I'm trying to serialize a struct which contains two or more other types of sturct. But it seems that serialization does not work. Below is sampel code :
//SimpleData.hpp
//SimpleData.hpp
#include <boost/serialization/access.hpp>
#include <boost/serialization/base_object.hpp>
#include <boost/serialization/optional.hpp>
#include <boost/serialization/map.hpp>
#include <boost/serialization/shared_ptr.hpp>
namespace A
{
Struct Name
{
std::string firstname;
std::string lastname;
template<class Archive>
void serialize(Archive &ar, const unsigned int version)
{
ar & firstname;
ar & lastname;
}
};
Struct Address
{
std::string street;
std::string city;
template<class Archive>
void serialize(Archive &ar, const unsigned int version)
{
ar & street;
ar & city;
}
}
Struct FullData
{
public :
FullData();
FullData(const parameter_strings & parms);
virtual ~FullData();
boost::optional<Name> nameinfo; // instance of struct Name
boost::optional<Address> addressinfo; // instance of struct Address
std::string country;
std::int pincode;
private :
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive &ar, const unsigned int version)
{
ar & nameinfo; // are these enough to take of serialization of Name and
ar & addressinfo; // Address
ar & country;;
ar & pincode;
}
};
}
//SimpleData.cpp
//SimpleData.cpp
#include "SimpleData.hpp"
FullData::FullData()
{
}
FullData::~FullData()
{
}
FullData::~FullData(const parameter_strings & parms): Detail(parms)
{
// impelmentation
}
BOOST_CLASS_EXPORT_IMPLEMENT(FullData);
BOOST_CLASS_IMPLEMENTATION(FullData),boost::serialization::object_serializable);
BOOST_CLASS_TRACKING(FullData),boost::serialization::track_never);
我不确定上述代码是否可以正确序列化.上面的代码有什么问题. 我是否还需要添加以下代码来对SimpleData.cpp文件中的Name和Address结构进行序列化
I'm not sure if above code would serialize properly or not. Whats wrong with above code. Do i need to add below code also for serialization of Name and Address struct in SimpleData.cpp file
BOOST_CLASS_EXPORT_IMPLEMENT(Name);
BOOST_CLASS_IMPLEMENTATION(name,boost::serialization::object_serializable);
BOOST_CLASS_TRACKING(Name,boost::serialization::track_never);
BOOST_CLASS_EXPORT_IMPLEMENT(Address);
BOOST_CLASS_IMPLEMENTATION(Address,boost::serialization::object_serializable);
BOOST_CLASS_TRACKING(Address,boost::serialization::track_never);
推荐答案
我不知道问题是什么.我/do/知道代码到处都是错误.
I don't know what the question was. I /do/ know that the code was littered with errors.
我修复了这些问题,然后发现:工作效率:
I fixed those, and behold: workiness:
int main()
{
A::FullData data({ { "key1", "value1" }, { "key2" , "value2" } });
data.nameinfo = A::Name { "firstname", "lastname" };
data.addressinfo = A::Address { "street", "city" };
data.pincode = 99977;
data.country = "Gibraltar";
boost::archive::text_oarchive oa(std::cout);
oa << data;
}
打印
22 serialization::archive 10 0 0 1 0 0 0 9 firstname 8 lastname 0 0 1 0 0 0 6 street 4 city 9 Gibraltar 99977 0 0 2 0 0 0 4 key1 6 value1 4 key2 6 value2
查看 在Coliru上直播
See it Live On Coliru
#include <boost/serialization/access.hpp>
#include <boost/serialization/base_object.hpp>
#include <boost/serialization/optional.hpp>
#include <boost/serialization/map.hpp>
#include <boost/serialization/shared_ptr.hpp>
namespace A
{
typedef std::map<std::string, std::string> parameter_strings;
struct Name
{
std::string firstname;
std::string lastname;
template<class Archive>
void serialize(Archive &ar, const unsigned int version)
{
ar & firstname;
ar & lastname;
}
};
struct Address
{
std::string street;
std::string city;
template<class Archive>
void serialize(Archive &ar, const unsigned int version)
{
ar & street;
ar & city;
}
};
struct FullData
{
FullData();
FullData(const parameter_strings & parms);
virtual ~FullData();
boost::optional<Name> nameinfo; // instance of struct Name
boost::optional<Address> addressinfo; // instance of struct Address
parameter_strings detail;
std::string country;
int pincode;
private :
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive &ar, const unsigned int version)
{
ar & nameinfo; // are these enough to take of serialization of Name and
ar & addressinfo; // Address
ar & country;
ar & pincode;
ar & detail;
}
};
}
SimpleData.cpp
#include "SimpleData.hpp"
#include <boost/serialization/export.hpp>
namespace A
{
FullData::FullData()
{
}
FullData::~FullData()
{
}
FullData::FullData(const parameter_strings & parms): detail(parms)
{
// impelmentation
}
}
#include <boost/archive/text_oarchive.hpp>
BOOST_CLASS_EXPORT_IMPLEMENT(A::FullData)
BOOST_CLASS_IMPLEMENTATION(A::FullData , boost::serialization::object_serializable)
BOOST_CLASS_TRACKING(A::FullData , boost::serialization::track_never)
int main()
{
A::FullData data({ { "key1", "value1" }, { "key2" , "value2" } });
data.nameinfo = A::Name { "firstname", "lastname" };
data.addressinfo = A::Address { "street", "city" };
data.pincode = 99977;
data.country = "Gibraltar";
boost::archive::text_oarchive oa(std::cout);
oa << data;
}
这篇关于提升嵌套结构的序列化不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!