我正在使用 Boost::serialization 读取/写入包含 3d 模型的文件。
在文件达到一定大小(大约 8-12kb+)之后,每次我尝试读取文件时,序列化都会引发“输入流错误”异常。直到那时它每次都可以正常工作 - 例如我可以成功读/写一个 1kb 模型 12 次,然后在第 13 次及以后它会抛出异常!
这是序列化/反序列化的代码:
JonsPackagePtr ReadJonsPkg(const std::string& jonsPkgName)
{
std::ifstream jonsPkgStream(jonsPkgName.c_str(), std::fstream::binary || std::fstream::in);
JonsPackagePtr pkg(new JonsPackage()); // "JonsPackagePtr" is a boost_shared_ptr typedef
if (jonsPkgStream && jonsPkgStream.good() && jonsPkgStream.is_open())
{
boost::archive::binary_iarchive iar(jonsPkgStream);
iar >> (*pkg.get());
}
return pkg;
}
bool WriteJonsPkg(const std::string& jonsPkgName, const JonsPackagePtr pkg)
{
std::ofstream outStream(jonsPkgName.c_str(), std::fstream::out | std::fstream::binary);
bool ret = false;
if (outStream.is_open())
{
boost::archive::binary_oarchive oar(outStream);
oar << (*pkg.get());
ret = true;
}
return ret;
}
这是我存档的内容:
/* PackageHeader definition */
struct PackageHeader
{
std::string mSignature;
uint8_t mMajorVersion;
uint8_t mMinorVersion;
PackageHeader();
};
/* PackageMesh definition */
struct PackageMesh
{
std::vector<float> mVertexData;
std::vector<uint32_t> mIndiceData;
PackageMesh();
};
/* PackageModel definition */
struct PackageModel
{
std::string mName;
std::vector<PackageModel> mChildren;
std::vector<PackageMesh> mMeshes;
Mat4 mTransform;
PackageModel();
};
/* JonsPackage definition */
struct JonsPackage
{
PackageHeader mHeader;
std::vector<PackageModel> mModels;
JonsPackage();
};
typedef boost::shared_ptr<JonsPackage> JonsPackagePtr;
JonsPackagePtr ReadJonsPkg(const std::string& jonsPkgName);
bool WriteJonsPkg(const std::string& jonsPkgName, const JonsPackagePtr pkg);
/* PackageHeader inlines */
inline PackageHeader::PackageHeader() : mSignature("jons"), mMajorVersion(LatestMajorVersion), mMinorVersion(LatestMinorVersion)
{
}
/* PackageModel inlines */
inline PackageModel::PackageModel() : mName(""), mTransform(1.0f)
{
}
/* PackageMesh inlines */
inline PackageMesh::PackageMesh()
{
}
/* JonsPackage inlines */
inline JonsPackage::JonsPackage()
{
}
最后,这是我的非侵入式序列化定义:
namespace boost
{
namespace serialization
{
template<class Archive>
void serialize(Archive & ar, JonsEngine::PackageHeader& header, const unsigned int version)
{
ar & header.mMajorVersion;
ar & header.mMinorVersion;
ar & header.mSignature;
}
template<class Archive>
void serialize(Archive & ar, JonsEngine::PackageModel& model, const unsigned int version)
{
ar & model.mName;
ar & model.mChildren;
ar & model.mMeshes;
ar & model.mTransform;
}
template<class Archive>
void serialize(Archive & ar, JonsEngine::PackageMesh& mesh, const unsigned int version)
{
ar & mesh.mVertexData;
ar & mesh.mIndiceData;
}
template<class Archive>
void serialize(Archive & ar, JonsEngine::JonsPackage& pkg, const unsigned int version)
{
ar & pkg.mHeader;
ar & pkg.mModels;
}
template<class Archive>
void serialize(Archive & ar, glm::detail::tmat4x4<glm::mediump_float> transform, const unsigned int version)
{
ar & transform[0];
ar & transform[1];
ar & transform[2];
ar & transform[3];
}
template<class Archive>
void serialize(Archive & ar, glm::detail::tvec4<glm::mediump_float> vec, const unsigned int version)
{
ar & vec.x;
ar & vec.y;
ar & vec.z;
ar & vec.w;
}
} // namespace serialization
} // namespace boost
正如我上面提到的,只有在一定的文件大小(8-12kb+)之后,当我尝试读取它时它才会开始抛出异常。为什么会这样,什么可能导致它?到现在为止一切顺利...
谢谢