c++ - 序列化一个定义未知的结构

标签 c++ serialization struct deserialization geos

我在我的软件中使用 geos 库作为几何引擎。我目前正在使用它的 capi(因为这是推荐的 api)。

现在的问题是我想序列化和反序列化结构 GEOSGeometry。库本身是用 c++ 编写的,capi 是它的包装器。因此,结构定义不可用。我有哪些选择?

这是capi提到的

/* When we're included by geos_c.cpp, those are #defined to the original
* JTS definitions via preprocessor. We don't touch them to allow the
* compiler to cross-check the declarations. However, for all "normal"
* C-API users, we need to define them as "opaque" struct pointers, as
* those clients don't have access to the original C++ headers, by design.
*/
#ifndef GEOSGeometry
typedef struct GEOSGeom_t GEOSGeometry;

这就是它的包装方式

// Some extra magic to make type declarations in geos_c.h work - 
// for cross-checking of types in header.
#define GEOSGeometry geos::geom::Geometry

感谢任何帮助。

最佳答案

首先,如果您真的无法在源文件中访问 struct 的定义,我会尝试使用 C++11 type_traits 检查它类,例如is_pod, is_trivial, is_standard_layout, ...

这样,您就可以了解自己在处理什么。如果您看到 struct 非常简单,您可以“希望”它将所有数据存储在自身内部,即不指向其他内存区域。遗憾的是,据我所知,没有办法查明类是否有成员指针。

最终,您所能做的就是尝试将其序列化,残酷地写入您的输出 sizeof(GEOSGeometry) 字节 (chars)。然后再读回去……祝你好运!

关于c++ - 序列化一个定义未知的结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30728718/

相关文章:

c++ - 类的 boost::shared_ptr 作为结构成员

c++ - 未生成隐式移动函数

c++循环编译器优化

c# - 发生类型为 'System.AccessViolationException' 的未处理异常

c# - 如何反序列化包含 JSON.NET 中的字符串和对象的数组

c - 在 C 中使用数组结构函数

c++ - 使用 FFmpeg 通过 RTMP 发送 H.264 编码流

C++,从内存加载数据结构到缓存的延迟

python - json.dump 无法序列化包含 python 列表的字典

c - 使用内联汇编将 int 保存在来自结构指针的函数变量中