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.

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?

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). 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?

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

Reply via email to