2

我在我的软件中使用 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

任何帮助表示赞赏。

4

1 回答 1

0

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

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

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

于 2015-06-09T10:50:52.883 回答