我有一个采用实体组件系统框架的程序。从本质上讲,这意味着我有一组实体,这些实体上附加了各种组件。实体实际上只是整数 ID 号,组件通过将组件映射到实体的指定 ID 号来附加到它们。
现在,我需要将实体集合和相关组件存储到一个文件中,以便稍后修改,所以基本上我需要一个保存和加载功能。然而,作为 C++ 的新手,我很难弄清楚如何做到这一点。
来自 Java 和 C#,我的第一选择是将对象序列化为 JSON,然后在加载 JSON 时反序列化它们。但是,C++ 没有任何反射特性。所以,问题是:如何保存和加载 C++ 对象?我指的不是实际的文件操作,而是应该处理对象和结构以便在程序启动之间保留它们的方式。
最佳答案
一种方法是创建 Persistent Objects在 C++ 中,并存储您的数据。
查看以下链接:
类似于永恒的C++对象持久化库
http://sourceforge.net/projects/litesql/
http://en.wikipedia.org/wiki/ODB_(C%2B%2B)
http://drdobbs.com/cpp/184408893
http://tools.devshed.com/c/a/Web-Development/C-Programming-Persistence/
C++ 不直接支持持久化(有建议在未来为 C++ 添加持久化和反射)。持久性支持并不像乍看起来那么微不足道。同一对象的大小和内存布局可能因平台而异。不同的字节顺序或字节顺序使事情变得更加复杂。要使对象持久化,我们必须将其状态保留在非 volatile 存储设备中。即:编写一个持久对象以在创建它的程序范围之外保留其状态。
另一种方法是将对象存储到一个数组中,然后将数组缓冲区推送到一个文件中。 优点是磁盘盘片不会浪费时间,而且写入可以连续执行。
您可以通过使用线程来提高性能。将对象转储到缓冲区,完成后触发一个线程来处理输出。
示例: 以下代码未经编译,仅供说明之用。
#include <fstream>
#include <algorithm>
using std::ofstream;
using std::fill;
#define MAX_DATA_LEN 1024 // Assuming max size of data be 1024
class stream_interface
{
virtual void load_from_buffer(const unsigned char *& buf_ptr) = 0;
virtual size_t size_on_stream(void) const = 0;
virtual void store_to_buffer(unsigned char *& buf_ptr) const = 0;
};
struct Component
: public stream_interface,
data_length(MAX_DATA_LEN)
{
unsigned int entity;
std::string data;
const unsigned int data_length;
void load_from_buffer(const unsigned char *& buf_ptr)
{
entity = *((unsigned int *) buf_ptr);
buf_ptr += sizeof(unsigned int);
data = std::string((char *) buf_ptr);
buf_ptr += data_length;
return;
}
size_t size_on_stream(void) const
{
return sizeof(unsigned int) + data_length;
}
void store_to_buffer(unsigned char *& buf_ptr) const
{
*((unsigned int *) buf_ptr) = entity;
buf_ptr += sizeof(unsigned int);
std::fill(buf_ptr, 0, data_length);
strncpy((char *) buf_ptr, data.c_str(), data_length);
buf_ptr += data_length;
return;
}
};
int main(void)
{
Component c1;
c1.data = "Some Data";
c1.entity = 5;
ofstream data_file("ComponentList.bin", std::ios::binary);
// Determine size of buffer
size_t buffer_size = c1.size_on_stream();
// Allocate the buffer
unsigned char * buffer = new unsigned char [buffer_size];
unsigned char * buf_ptr = buffer;
// Write / store the object into the buffer.
c1.store_to_buffer(buf_ptr);
// Write the buffer to the file / stream.
data_file.write((char *) buffer, buffer_size);
data_file.close();
delete [] buffer;
return 0;
}
关于C++、ECS 和保存/加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28046324/