【问题标题】:Serializing a struct whose definition is not known序列化定义未知的结构
【发布时间】:2015-06-09 09:52:33
【问题描述】:

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

现在的问题是我想序列化和反序列化 struct 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

感谢任何帮助。

【问题讨论】:

  • 在命名空间 geom 中查找命名空间 geos。或者,通过任何接口进行功能序列化。 cmets说它暴露给cpp用户,找到它。
  • 它是一个类。看来我毕竟必须使用 c++ api。

标签: c++ serialization struct deserialization geos


【解决方案1】:

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

通过这种方式,您可以了解自己在处理什么。如果您看到struct 非常简单,您可以“希望”它将所有数据存储在自身内部,即不指向其他内存区域。可悲的是,据我所知,没有办法知道一个类是否有一个成员指针。

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

【讨论】:

  • 谢谢。该结构根本不是结构。原来是一堂课。 sizeof(GEOSGeometry) 无法由编译器确定,因为它的定义不完整。我想我将不得不使用 c++ api 并序列化该类。不幸的是!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-09-04
  • 2017-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多