[cable] pointer & reference swapped in python wrapping

Kaben Nanlohy k_cable.public.kitware.com at random.poofygoof.com
Thu Feb 12 16:34:01 EST 2004


Hello;

I am working with code using pointers to functions returning references,
something like :

  typedef cFu &(*tPtrRefFun)();

and a class method taking such a pointer as an argument :

  void cHello::setPtrRefFun(tPtrRefFun fun);

CSwig generates a slightly mixed-up wrapping for this function :

  static PyObject *_wrap_cHello_setPtrRefFun(PyObject *self, PyObject *args) {
    (void)self; (void)args;
    PyObject *resultobj;
    cHello *arg1 = (cHello *) 0 ;
    cFu *(*arg2)() = (cFu *(*)()) 0 ;
    
    .
    .
    
    (arg1)->setPtrRefFun(*arg2);

... the last line tries to pass a referenced pointer (*arg2) instead of
a pointer to setPtrRefFun(); and instead of having type tPtrRefFun, arg2
is a pointer to a function returning a pointer. I've been tweaking the
wrapping by hand to make it compile. I'm not using the callbacks from
python just yet, and would be happy if I could prevent the wrapping.

Three other functions are wrapped correctly :

  void cHello::setPtrPtrFun(tPtrPtrFun fun);
  tPtrPtrFun cHello::getPtrPtrFun();
  tPtrRefFun cHello::getPtrRefFun();

All four functions are wrapped correctly using regular Swig.

Here's a little test case :

  // cHello.hh
  //
  #ifndef CHELLO_HH
  #define CHELLO_HH

  class cFu;
  typedef cFu *(*tPtrPtrFun)();
  typedef cFu &(*tPtrRefFun)();
  class cHello {
  private:
    tPtrPtrFun p_p_fun;
    tPtrRefFun p_r_fun;
  public:
    cHello();
    void setPtrPtrFun(tPtrPtrFun fun);
    void setPtrRefFun(tPtrRefFun fun);
    tPtrPtrFun getPtrPtrFun();
    tPtrRefFun getPtrRefFun();
    void callPtrPtrFun();
    void callPtrRefFun();
  };
  #endif        


  // cHello.cc
  //
  #include "cHello.hh"
  #include <iostream>

  class cFu { public: cFu(){} void fu(); };
  void cFu::fu(){ std::cout << "fu." << std::endl; }

  cFu ret;
  cFu *ptr(){ return &ret; }
  cFu &ref(){ return ret; }

  cHello::cHello(){ setPtrPtrFun(&ptr); setPtrRefFun(&ref); }
  void cHello::setPtrPtrFun(tPtrPtrFun fun){ p_p_fun=fun; }
  void cHello::setPtrRefFun(tPtrRefFun fun){ p_r_fun=fun; }
  tPtrPtrFun cHello::getPtrPtrFun(){ return p_p_fun; }
  tPtrRefFun cHello::getPtrRefFun(){ return p_r_fun; }
  void cHello::callPtrPtrFun()
  { if(getPtrPtrFun()) getPtrPtrFun()()->fu();
    else std::cout << "p_p_fun==0." << std::endl; }
  void cHello::callPtrRefFun()
  { if(getPtrRefFun()) getPtrRefFun()().fu();
    else std::cout << "p_r_fun==0." << std::endl; }


  // wrap_cHello.cc
  //
  #include "cHello.hh"

  #ifdef CABLE_CONFIGURATION
  namespace _cable_
  {
    const char* const package = "cHello";
    const char* const group = "cHello";
    namespace wrappers { typedef ::cHello cHello; }
  }
  #endif


  // cHello.i
  //
  %module cHello
  %{
  #include "cHello.hh"
  %}
  %include "cHello.hh"


P.S. : I really like using cswig.
Thanks -- Kaben




More information about the cable mailing list