[tng-devel] Output variables in SWIG vs. C++ references?

[ Thread Index | Date Index | More lists.tuxfamily.org/tng-devel Archives ]


Dear List,

I would like to let you know a way to handle multiple output variables and output variables using C++ references in SWIG.

For example, you have want to have the code in Lua:

a1 = myfunc(); or even
a1,a2 = myfunc()

Since using pointers in C++ is very bad habit, but SWIG only supports pointers well, we have some trouble.

A C++ function

void myfunc(int & x);

will always be translated as nil=myfunc(x); and if 'x' is marked as INOUT variable, then SWIG will produce y=myfunc(x) which is mostly not what is intended.

strategy:

#ifdef SWIG
void myfunc(const int & INPUT, int ** OUTPUT, int ** OUTPUT)
#else
void myfunc(const int & a, int ** x, int ** y)
{
   *x = new int;
   *y = new int;
   myfunc(a,**x,**y);
}
virtual void myfunc(const int & a, int & x, int & y);
#endif

By doing so we achieve:
(1) Happy C++ with references.
(2) Subclassing is possible without redefining the SWIG interface. The derived virtual functions should be part of a #ifndef SWIG .. #endif block, though.
(3) Lua syntax: x,y = myfunc(a)

Best regards
SW





Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/