SWIG C++与回调的绑定

发布于 2024-10-03 18:59:16 字数 930 浏览 5 评论 0原文

我正在为一些 C++ 代码编写一些 SWIG/Python 绑定。这是针对所谓的 Kinect Accidental API,我有电机和 LED 功能工作。解析和填充 RGB 和深度缓冲区的 Listener 类的回调似乎没有从 SWIG 调用。数据捕获线程显然启动并开始占用 CPU,但回调中没有调试线通过。填充数据缓冲区并从 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) {};
};

这是带有虚方法的侦听器类,该类的 Python 包装版本可以用于继承 C++ 类的侦听器吗?我在 C++ 中添加了一个最小的侦听器,现在剩下的工作是使用类型映射有效地访问数组。目前我正在使用这个幼稚的类型映射

%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);
  }
}

更好的选择?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故事还在继续 2024-10-10 18:59:17

巧合的是,我目前正在研究 SWIG 的回调。

SWIG 2.0 文档是这样描述的

SWIG 提供对函数指针的完全支持,前提是回调函数是用 C 而不是目标语言定义的。 ...但是,只要将现有的 C 函数安装为常量,它们就可以用作参数。一种方法是使用 %constant 指令,如下所示...

我计划用手写的 JNI 编写一个 C 回调来调用 Java。如果还有其他方法,我也很想听听。

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.

是你 2024-10-10 18:59:16

有一种方法可以使用导演功能。
为您的 KinectListener 代理启用它,一行代码:

%feature("director") KinectListener

然后您可以在 python 代码中继承 KinectListener 类并定义您的函数。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文