SWIG C++ bindings with callback
Asked Answered
C

2

8

I am writing some SWIG/Python bindings for some C++ code. This is for what is called the Kinect Accidental API, I have the motor and led functions working. The callbacks to the Listener class which parse and populate the RGB and Depth buffers do not seem to get called from SWIG. The data capture threads evidently start up and start hogging the CPU, but no debug lines from the callback come through. What would be better way to populate data buffers and easily access them from python ?

class KinectListener
{
     public:
        virtual ~KinectListener(){};
        virtual void KinectDisconnected(Kinect *K) {};
        virtual void DepthReceived(Kinect *K) {};
        virtual void ColorReceived(Kinect *K) {};
        virtual void AudioReceived(Kinect *K) {};
};

Here is the listener class with the virtual methods, can the Python wrapped version of this class be used to inherit listeners for the c++ class ? I added a minimal listener in C++ and now the remaining work is to access the arrays efficiently with typemaps. Currently I am using this naive typemap

%typemap(out) unsigned short [ANY] {
  int i;
  $result = PyList_New($1_dim0);
  for (i = 0; i < $1_dim0; i++) {
    PyObject *o = PyInt_FromLong((long)$1[i]);
    PyList_SetItem($result,i,o);
  }
}

Better options ?

Cassiecassil answered 23/11, 2010 at 12:3 Comment(0)
O
3

There is a way using the directors feature. Enable it for your KinectListener proxy, one line of code :

%feature("director") KinectListener

Then you can inherit from KinectListener class in python code and define your functions.

Opiumism answered 29/12, 2010 at 17:10 Comment(1)
I had the same question - N0vember's response answers it. Instead of receiving a pointer to a function to call, receive a pointer to a polymorphic class where you know the name of the function you plan to call.Stiffler
V
3

By coincidence, I happen to be looking into callbacks with SWIG at the moment.

The SWIG 2.0 documentation says this:

SWIG provides full support for function pointers provided that the callback functions are defined in C and not in the target language. ... However, existing C functions can be used as arguments provided you install them as constants. One way to do this is to use the %constant directive like this ...

I'm planning to write a C callback with hand-written JNI to call into Java. If there's another way, I would also love to hear it.

Vaca answered 29/11, 2010 at 21:33 Comment(0)
O
3

There is a way using the directors feature. Enable it for your KinectListener proxy, one line of code :

%feature("director") KinectListener

Then you can inherit from KinectListener class in python code and define your functions.

Opiumism answered 29/12, 2010 at 17:10 Comment(1)
I had the same question - N0vember's response answers it. Instead of receiving a pointer to a function to call, receive a pointer to a polymorphic class where you know the name of the function you plan to call.Stiffler

© 2022 - 2024 — McMap. All rights reserved.