[gdal-dev] OGRLayerH as a more specific type

Frank Warmerdam warmerdam at pobox.com
Sat May 10 15:25:27 EDT 2008


Folks,

In the past the C "handle" types for OGR classes were defined like this:

   typedef void *OGRLayerH;

The problem is that this type becomes interchangable with any other void *,
including stuff like OGRDataSourceH and OGRFeatureH.  So type checking in the
C API is somewhat degraded.

It was suggested to change this to:

   typedef struct OGRLayerHS *OGRLayerH;

This still provides an opaque handle type, but the compiler will now
differentiate it from other handle types.  We looked at doing this for
the GDAL C API but it was messy, in part because the GDAL API needs to be
able to treat various objects as GDALMajorObjectH and that relationship is
hard to express in C type definitions.

However, since most OGR classes have no common base class it is fairly
easy there.  So, I have modified ogr_api.h to use the following definitions:

#ifdef DEBUG
typedef struct OGRFieldDefnHS   *OGRFieldDefnH;
typedef struct OGRFeatureDefnHS *OGRFeatureDefnH;
typedef struct OGRFeatureHS     *OGRFeatureH;
#else
typedef void *OGRFieldDefnH;
typedef void *OGRFeatureDefnH;
typedef void *OGRFeatureH;
#endif

#ifdef DEBUG
typedef struct OGRLayerHS      *OGRLayerH;
typedef struct OGRDataSourceHS *OGRDataSourceH;
typedef struct OGRDriverHS     *OGRSFDriverH;
#else
typedef void *OGRLayerH;
typedef void *OGRDataSourceH;
typedef void *OGRSFDriverH;
#endif

I found I had to fix up a variety of casting issues in the OGR code itself
to get this working properly. So one lesson is that slightly sloppy code
will no longer compile with these changes.  In the interest of reducing the
impact of this change, I made it only apply in DEBUG mode when we presume
developers want the compiler helping them as much as possible to keep their
code clean.

Please let me know if folks find this change problematic.  We still have a
fair amount of time before 1.6 release in which we could back it out if
so desired. We might also consider extending it somewhat more widely though
I'm ambivalent in the C case because of the whole "GDAMajorObjectH" thing.

Best regards,
-- 
---------------------------------------+--------------------------------------
I set the clouds in motion - turn up   | Frank Warmerdam, warmerdam at pobox.com
light and sound - activate the windows | http://pobox.com/~warmerdam
and watch the world go round - Rush    | President OSGeo, http://osgeo.org



More information about the gdal-dev mailing list