On Sun, Oct 31, 2010 at 8:44 PM, Brett Calcott <[email protected]> wrote:
> Hi,
> I'm wrapping a C++ library (http://www.box2d.org/). A python version done
> with swig already exists, but I'm trying to make it more pythonic/cythonic,
> and preparing it for a future project. It's all going well, and I'm having a
> fun time -- cython rocks.

Thanks.

> I have a few questions and thoughts about wrapping. Consider wrapping
> something simple like this, where things are public:
> class Vector2d {
> public:
>   float x, y
>   Vector2d() : x(0.0), y(0.0) {}
>   // other useful stuff here
> };
> I begin by doing this:
> extern from "vector2d.h":
>   cdef cppclass Vector2d:
>      float x, y
>      Vector2d()
> Here are the questions:
> 1. Why must I use a *pointer* in my python class?
> cdef class PythonVector2d:
>   cdef Vector2d *obj
> There is a default constructor, and I can happily declare it on the stack in
> a function. I'm sure I'm missing something here; is there some danger with
> allowing this?

It's conceivable that we could allow "stack allocated" members in an
object in the presence default constructors, but that would require
modifying the constructor and destructor to manually initialize and
deconstruct the object when the PyObject* is allocated and reclaimed.
Note that stack-allocated local variables are all initialized at the
top of a function and live until the function returns, unlike C++
where you can constrain their scope. Eventually, I'd like to do
something like references where it's all pointers under the hood, but
that's not obvious to the user, and Cython has the ability to perform
stack-allocations if possible.

> 2. How hard would it be to automatically forward member access to a
> contained cppclass? Consider the following nasty hack:
> cdef class PythonVector2d:
>   cdef public float x, y
>   cdef Vector2d *obj
>   def __cinit__(self, x, y):
>     self.x = x
>     self.y = y
>     self.obj = <Vector 2d*>x
> ok. That is a bit nasty (I'm sure someone can tell me lots of reasons why I
> shouldn't do this).

In reading that code, I'm not sure what your intent is.

> But it really makes things easy. I don't have to declare
> all the properties independently. And when I need the cpp version, I just
> pass around pointer.
> Something like this would be nice:
> cdef class PythonVector2d:
>   cdef Vector obj
>   properties:
>      public obj.x as x
>      public obj.y as y
> Does this look crazy?

A better way to specify "simple" properties would be nice. Another
option would be

    property obj.x as x

or

    property x:
        obj.x

which would create a getter and setter automatically (if the body is
an expression). It would be nice for exposing this to Python. I'm not
a huge fan of doing the automatic-rewriting for C-level access, though
perhaps some eventual macro system could. Cython is not a
wrapper-generator (though it would make sense for a wrapper-generator
to emit Cython code).

- Robert
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to