Hi,
I want to wrap the function
void do(MyType & arg);
whereby arg is not modified, but really set.
When applying SWIG
#ifdef SWIG
void do (MyType & OUTPUT);
#endif
or if typing
%apply SWIGTYPE& OUTPUT{MyType & arg};
then I expect Lua to behave like this:
x = do()
In reality a wrapper is created which is controlled in Lua by
x = MyType()
do(MyType)
That's not really nice.
The same goes to modified variables (INOUT) which is not working as Lua-like as expected. I believe, the behaviour should be :
In case of OUTPUT the wrapper routine should create the output object and send the dereferenced pointer to the C++ routine. Then the object should be returned by Lua.
In case of INOUT the wrapper routine should make a copy of the input object via a copy constructor (assumes that it exists) or by operator= (whatever). This copy should be dereferenced and send to the C++ routine. The (now) modified object should be returned by Lua.
Thanks for listening
Reproducible with git master. Here's a self-contained testcase:
Looking at this again in more detail, I think SWIG's default wrapping is actually reasonable here.
My filled-in reproducer above could not be automatically wrapped as you suggest because there's no default constructor for
MyTypeso the output object creation would also need to be told how to construct a suitable dummy object. The typemap doesn't have access to information about what constructor overloads are available, but even if it did it can't know which (if any) are safe to use for dummy construction - as a rather extreme hypotherical example,MyType(12345)might be safe but any other value recursively deletes your home directory...This is a fairly easy way to get SWIG to wrap as you want:
Interface file:
where
test.hcontains:With this I can do:
It may be possible to provide a named typemap to automate this a bit, but I don't see a nice way due to the need to tell it how to construct a suitable dummy object. Given nobody else seems to have even looked at this ticket for over 15 years I'm going to close this now.