<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta content="text/html;charset=UTF-8" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
<br>
<blockquote cite="mid4340823A.6080109@ku.edu" type="cite">
  <pre wrap="">I gotta say, it's very wierd of ruby to treat 0 as true.  In fact, one
might really question it's sanity. </pre>
</blockquote>
<br>
Yah, many people have questioned this :)  It certainly comes up on the
Ruby lists from time to time.<br>
<br>
Anyway, the good news is that SWIG seems to define the appropriate bool
typemaps already for each language, so I don't think we need to
introduce a fake BOOLEAN type.  Instead, I think all we have to do is
change the return type from int to bool on the appropriate methods.<br>
<br>
Thus in ogr.i:<br>
<br>
int TestCapability (const char *cap) {<br>
    return OGR_Dr_TestCapability(self, cap);<br>
}<br>
<br>
Becomes:<br>
<br>
bool TestCapability (const char *cap) {<br>
}<br>
<br>
In the wrapper method, SWIG then typecasts the results from
OGR_Dr_TestCapability to a bool, and applies the languages bool typemap
(which they seem to all have).  So:<br>
<br>
static PyObject *_wrap_Driver_TestCapability(PyObject *, PyObject
*args) {<br>
    PyObject *resultobj = NULL;<br>
    ...<br>
    result = (bool)OGRDriverShadow_TestCapability(arg1,(char const
*)arg2);<br>
    ...<br>
   /* language specific typemap here, this is python */<br>
      resultobj = SWIG_From_bool(static_cast&lt;bool &gt;(result)); <br>
}<br>
<br>
I suppose we could do this:<br>
<br>
int TestCapability (const char *cap) {<br>
    if (result = OGR_Dr_TestCapability(self, cap))<br>
       return true;<br>
   else<br>
      return false;<br>
}<br>
<br>
But I don't think we have to.<br>
<br>
The methods in ogr.i I changed are:<br>
<br>
Equal<br>
testcapability<br>
IsFieldSet<br>
Intersect<br>
Equal<br>
Disjoint<br>
Touches<br>
Crosses<br>
Within<br>
Contains<br>
Overlaps<br>
<br>
I'd be glad to check it in or send it to you if you want to take a look
and test with Python.<br>
<br>
<br>
Charlie<br>
<br>
<br>
<br>
<blockquote cite="mid4340823A.6080109@ku.edu" type="cite">
  <pre wrap="">}

The "type" BOOLEAN never really exists, it's just an indicator to apply
the proper typemap.

We've used this trick before with a typemap called IF_FALSE_RETURN_NONE.

Kevin

Charlie Savage wrote:

  </pre>
  <blockquote type="cite">
    <pre wrap="">The various TestCapability methods in OGR have a return type of
integer.  However, if you read the OGR C API documentation its says:

"Returns: TRUE if the layer has the requested capability, or FALSE
otherwise. OGRLayers will return FALSE for any unrecognized
capabilities."

So it seems like the intent it to return a boolean.  In C-like
languages returning an int is ok, since 0 evaluates to false.

This however, is not the case in all languages.  In Ruby, for example,
false is either nil (same as NULL or PyNone) or false.  Thus the value
0 does *not* evaluate to false.  That means you can't do something
what is natural:

if lyr.test_capability( 'FastGetExtent')

It will always evaluate to true in Ruby!

I would like to change the SWIG wrapper code so that all the
testcapability methods return booleans.  I think this is ok, since the
bindings are built using SWIG's C++ mode, so boolean is a supported
data type.  That way I don't have to put in a separate mapping for
these methods just in the Ruby bindings.

Thanks,

Charlie

_______________________________________________
Gdal-dev mailing list
<a class="moz-txt-link-abbreviated" href="mailto:Gdal-dev@lists.maptools.org">Gdal-dev@lists.maptools.org</a>
<a class="moz-txt-link-freetext" href="http://lists.maptools.org/mailman/listinfo/gdal-dev">http://lists.maptools.org/mailman/listinfo/gdal-dev</a>
    </pre>
  </blockquote>
  <pre wrap=""><!---->



  </pre>
</blockquote>
</body>
</html>