序列化包含std::string的自定义Struct
Serialize a Custom Struct Containing std::string
我希望序列化一个包含std::string到文件的类,并轻松地在Python中加载此数据:
class A {
public:
int a;
char b;
bool c;
std::string s1;
std::string s2;
}
我有一个非常繁忙的线程,它处理a的许多实例。它将有趣的实例添加到一个类中,供不那么繁忙的线程稍后编写。
class Blob {
public:
char data[1024]
size_t length;
}
void createBlob(void *data, int length) {
Blob saved_a;
saved_a.length = length;
memcpy(saved_a.data, a, length);
}
然后,低优先级线程异步写入blobs到file:文件。Write (reinterpret_cast (&saved_a.length), sizeof(saved_a.length));文件。写(saved_a saved_a.length);
然后Python读取这些文件,并使用struct库加载数据/句柄端序。
我没有一个很好的方法来存储std::string(部分原因是我不明白std::string的生命周期有什么保证)。日志线程是否能够强制转换saved_a。数据到类型a,然后读取字符串?或者memcpy是否只保存指向可能不再有效的字符串的指针?
复制A结构是不可能的,因为createBlob可以采用许多不同的数据结构(只需要一个void *和一个size)。我愿意牺牲平台独立性,并通过打包计算/测试来确保Python解析器正常工作,但确实需要最小化创建blob的函数的负载,并需要确保它可以创建许多不同数据类型的blob。
如果std::字符串在低优先级记录器到达它们时仍然有效,则可以重铸数据并执行完整复制。否则,在传递给createBlob函数之前,是否有一个轻量级的解决方案来序列化结构(在性能上与做内存相当)?
Memcpy对指针不起作用,因为它复制的是指针,而不是指针的值。所以它对结构体中存储的任何对象或数组都没有帮助。没有简单的方法可以自动做到这一点。但是对于字符串,您可以使用0作为字符串结束标志直接将其字节写入内存。像这样:
class A {
public:
int a;
char b;
bool c;
std::string s1;
std::string s2;
int length()
{
sizeof(a) + sizeof(b) + sizeof(c) + s1.length*sizeof(char) + 1 + s2.length*sizeof(char) + 1;
}
void* toByteArray()
{
char * res = new char[length()];
int pos =0 ;
pos+=writebytes(res, pos, tobytes(a));
pos+=writebytes(res, pos, tobytes(b));
pos+=writebytes(res, pos, tobytes(c));
pos+=writebytes(res, pos, tobytes(s1));//string version should append zero char after string
pos+=writebytes(res, pos, tobytes(s2));
}
}
也永远不要使用memcpy复制类,因为它也复制虚表指针,而不仅仅是类中的变量。
不,当然不是。不能使用memcpy()将字符串硬塞到blob中。这里最糟糕的是,由于某些实现中可用的小字符串优化,它实际上可能在某些数据上工作。然后它会神奇地在另一组数据上中断。如果你想让你的东西是二进制序列化的(我个人认为二进制序列化已经过时了),用某种CharArray实现替换类中的字符串,它使用数组作为存储。我个人更喜欢适当的序列化。
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- 如何将点击的信号和插槽添加到qt中的自定义按钮中
- C++自定义比较函数
- 如何比较自定义类的std::变体
- std::设置自定义比较器
- 如何正确实现和访问运算符的各种自定义枚举器
- flutter:即使shouldRepaint()返回true,自定义画家也不会重新绘制
- 自定义先决条件对移动分配运算符有效吗
- 使用VS Code和CMake Tools运行自定义命令
- 如何创建从Maya(或类似程序)到虚幻引擎的自定义数据导出插件
- std::ranges::elements_view,用于自定义类似元组的数据
- 跟随整数索引列表的自定义类迭代器
- 参数化自定义CMake工具链
- 使用自定义比较函数使用std::sort()对矢量字符串进行排序时出现问题
- 如何在自定义类中启用'auto loops'?
- 使用QJsEngine在Qt中注册自定义类型
- 为什么在对自定义对象的向量进行分类时,我们将在struct的定义中包括比较函数
- 矢量<struct>的自定义迭代器
- 从自定义集合(struct Array)中读取数据
- 序列化包含std::string的自定义Struct