Serializing a struct whose definition is not known

120 views Asked by At

I am using geos library in my software as the geometry engine. I am currently using its capi(as that is the recommended api).

Now the problem is I would like to serialize and deserialize the struct GEOSGeometry. The library itself is in c++ and the capi is a wrapper around it. So the struct definition is not available per say. What are my options?

This is what the capi mentions

/* 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;

And this is how it is wrapped

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

Any help is appreciated.

1

There are 1 answers

1
Paolo M On

First of all, if you really can't access struct's definition in a source file, I'd try to inspect it with C++11 type_traits classes, e.g. is_pod, is_trivial, is_standard_layout, ...

This way, you can get an idea of what are you dealing with. If you see that the struct is quite simple, you can "hope" that it stores all data inside itself, i.e. not points to other memory areas. Sadly, as far as I know, there is no way to find out if a class has got a member pointer.

Eventually, all you can do is trying to serialize it brutally writing to your output sizeof(GEOSGeometry) bytes (chars). Then read it back and... good luck!