18 September 2017

swig

  1. wxWidows(python) uses swig to communite with wxwidget(Clang)
  2. PyQT(python) use its sip to commuite with QT (Clang)
  3. python use builtin ctypes to do communicate with DLL files

swig vs sip

swig has tutorial sip only has a few articles in his document website Both generates PyObject to do the bridge between Clang and python

wxwidget sample

https://wiki.wxpython.org/C%2B%2BExtensions

swig sample at windows

http://falldog7.blogspot.tw/2013/07/python-swig-c-function.html

Bluetooth Stack for swig/sip

Thinking to use swig/sip with one of bluetooth stack on windows

  1. BOSS - BLE only open source, embedded-type
  2. BlueCove - java, profile-level, no-core
  3. 32feet - csharp, profile-level, no-core
  4. btstack - dual, clang, use POSIX linux-path, embedded-type

swig for callback

both sip and swig doesn’t support callback direclty

%module test

%{
#include "test.hh"

class PyCallback
{
    PyObject *func;
    PyCallback& operator=(const PyCallback&); // Not allowed
public:
    PyCallback(const PyCallback& o) : func(o.func) {
      Py_XINCREF(func);
    }
    PyCallback(PyObject *func) : func(func) {
      Py_XINCREF(this->func);
      assert(PyCallable_Check(this->func));
    }
    ~PyCallback() {
      Py_XDECREF(func);
    }
    void operator()(const std::string& s) {
      if (!func || Py_None == func || !PyCallable_Check(func))
        return;
      PyObject *args = Py_BuildValue("(s)", s.c_str());
      PyObject *result = PyObject_Call(func,args,0);
      Py_DECREF(args);
      Py_XDECREF(result);
    }
};
%}

%include "test.hh"

%inline%{
  void register_handler(const QQEvent& e, PyObject *callback) {
    register_handler(e, PyCallback(callback));
  }
%}




blog comments powered by Disqus