我有一个源自Field
的模板类IField
。这样就可以将Field
和Field<int>
等特定Field<double>
添加到std::vector<IField>
。在Model-class中,我正在序列化这个向量。我可以编译所有内容,但我没有得到所需的输出。谢谢你的帮助!
我的抽象类:
class IField {
public:
IField(){}
virtual ~IField() {}
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive&, const unsigned int) {}
};
BOOST_SERIALIZATION_ASSUME_ABSTRACT(IField)
我的模板类:
template<typename T>
class Field: public IField {
public:
Field(std::string name, T value) :
mName(name), mValue(value) {
}
const std::string& getName() const {
return mName;
}
void setName(const std::string& name) {
mName = name;
}
T getValue() const {
return mValue;
}
void setValue(T value) {
mValue = value;
}
private:
friend class boost::serialization::access;
template<typename Archive>
void serialize(Archive& archive, const unsigned int) {
archive & boost::serialization::make_nvp("IField", boost::serialization::base_object<IField>(*this));
archive & boost::serialization::make_nvp("Name", mName);
archive & boost::serialization::make_nvp("Value", mValue);
}
std::string mName;
T mValue;
};
我自定义的矢量级std::vector<IField>
:
class FieldSet: public std::vector<IField> {
public:
FieldSet() :
mSize(0) {
}
virtual ~FieldSet() {
}
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive &ar, const unsigned int version) {
if (Archive::is_loading::value) {
this->clear();
ar & boost::serialization::make_nvp("Size", mSize);
for (int i = 0; i < mSize; i++) {
IField field;
ar & boost::serialization::make_nvp("Field", field);
this->push_back(field);
}
} else {
mSize = this->size();
ar & boost::serialization::make_nvp("Size", mSize);
for (int i = 0; i < mSize; i++) {
ar & boost::serialization::make_nvp("Field", this->at(i));
}
}
}
int mSize; // You have to add vector size
};
我的Model.h:
class Model {
public:
Model();
virtual ~Model(){}
private:
FieldSet mFieldSet;
// Fields
Field<int> mTime;
Field<double> mSpeed;
};
我的Model.cpp:
Model::Model (): mTime("Time", 0), mSpeed("Speed", 0.0) {
mFieldSet.push_back(mTime);
mFieldSet.push_back(mSpeed);
}
void Model::store(std::string filename) {
std::ofstream ofs(filename);
boost::archive::xml_oarchive oa(ofs);
oa << boost::serialization::make_nvp("FieldSet", mFieldSet);
}
我正在获取正在进行的XML输出:
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE boost_serialization>
<boost_serialization signature="serialization::archive" version="10">
<FieldSet class_id="0" tracking_level="0" version="0">
<Size>5</Size>
<Field class_id="1" tracking_level="0" version="0"></Field>
<Field></Field>
<Field></Field>
</FieldSet>
</boost_serialization>
但是想要输出如下:
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE boost_serialization>
<boost_serialization signature="serialization::archive" version="10">
<FieldSet class_id="0" tracking_level="0" version="0">
<Size>5</Size>
<Field class_id="1" tracking_level="0" version="0"></Field>
<Field>
<Name>Time</Name>
<Value>0</Value>
</Field>
<Field>
<Name>Speed</Name>
<Value>0.0</Value>
</Field>
</FieldSet>
</boost_serialization>