boost::Serialize VS std::fstream
嘿,所以我想我对 boost::serialize 的目的有点困惑:
必须为您计划保存的每个类添加 boost::serialize 函数似乎有点违背了库的目的,而不是 < em>增加/减少开发时间似乎会增加大量时间,因为您必须进入并编辑您使用的每个库中每个类的源代码,以便它具有序列化功能。
我正在计划关于将它用于 SFML/Box2D 游戏,但现在一旦我想到这一点,我就会重新考虑...... 我使用库错误了吗?
这似乎是 std ::fstream 将是一个更好的主意,因为它不需要对您想要保存的任何类进行任何功能或更改,并且我可以设计一个“保存类” ”。
Hey so i guess i'm a little confused on the purpose of boost::serialize:
Having to add a boost::serialize function to every class you plan to save seems to kind of defeat the purpose of the library, as instead of boosting/ decreasing development time it seems like it would add a HUGE amount of time since you have to go in and edit the source of every class in every library your using so it has a Serialization function.
I was planning on using it for a SFML/Box2D game, but now i'm having second thoughts once i think this through.... I'm i using the library wrong?
It seems like std::fstream would be a much better idea, as that doesn't require any functions or changes to be made to whatever class you want to save, and i could design a "Save class."
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
呃,什么? Boost.Serialization 并不能替代 I/O 流。它是一个序列化框架(即从外部存储保存和恢复对象的状态),档案仍然包装某种流来实际读取和写入数据。当然,您需要编写序列化函数,库无法知道数据在哪里,或者应该如何在存档中布局——如果您使用
fstream
,无论这意味着什么在这种情况下,你仍然需要做同样的事情。您也不一定需要将保存/加载函数实现为类成员 - 文档说明了如何将它们作为自由函数。Er, what? Boost.Serialization is not a replacement for I/O streams. It's a framework for serialisation (i.e. saving and restoring the state of an object from an external storage), the archives still wrap some kind of stream to actually read and write data. Of course you need to write serialising functions, the library has no way of knowing where the data, or how should it be layed out in the archive otherwise — if you'd use
fstream
, whatever that means in this context, you'd still have to do the same. You don't necessarily need to implement the save/load functions as class members, either — the documentation says how to make them as free functions.直接写入标准流仍然需要编写序列化/反序列化函数对。不仅标准库的 iostream 组件不支持自定义类的 I/O,而且仅写入和读回 sizeof(yourObject) 字节也是行不通的。想想如果你的类包含指针成员会发生什么。
此外,序列化库提供了对不同格式和版本控制的支持等功能,这可能很有用。
Writing directly to a standard stream would still require you to write a serialization/deserialization function pair. Not only the iostream component of the standard library does not support I/O of custom classes, but just writing and reading back sizeof(yourObject) bytes wouldn't work. Just think about what would happen if your class contained pointer members.
Moreover the serialization library provides feature like support for different formats and versioning, which may be useful.
更好的主意该怎么做?
序列化通常用于保存和恢复对象的状态,而不是任意数据。它的目的是能够获取一包对象并生成一个文件,以便以后可以从该文件自动重建这些对象。
如果您可以将要保存的所有信息整理到“保存类”中,那么您就不需要序列化。
此外,您不能只将类写入流;还要将类写入流中。您必须实现运算符<<重载或其他一些函数来保存其数据并将其加载回来。是的,您可以使用 memcpy 扔掉对象的位,但这并不完全安全。我所说的“不完全安全”是指“除非您真的、真的知道自己在做什么,否则永远不应该这样做。”
A better idea to do what?
Serialization is typically used for saving and restoring the state of objects, not arbitrary data. It's purpose is to be able to take a bag of objects and produce a file, such that these objects can be automatically reconstructed from that file at a later date.
If you can collate all of the information you want to save into a "save class", then you don't need serialization.
Also, you cannot just write a class to a stream; you would have to implement a
operator<<
overload or some other function to save its data and load it back. Yes, you could just throw the bits of the object out withmemcpy
, but that's not exactly safe. And by "not exactly safe", I mean "you should never do this unless you really, really know what you're doing."