Treat Returned Reference as Value

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Treat Returned Reference as Value

Jake
I feel like I must just be missing this in the documentation somewhere, but when a function returns a reference SWIG treats that like a pointer by default.  How do we tell it to treat it as a value (i.e. make a copy and wrap that)?

For example:

struct Example {
  int& get(int x, int y);
};

I want the int copied and not as a SWIG_int_p.

-Jake Cobb

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Treat Returned Reference as Value

schullq
Do you include "typemaps.i" ?

If not, include it, and take a look a it ([swig folder]/Lib/[your target language]/typemaps.i]), you should get your answer here.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Treat Returned Reference as Value

William S Fulton

On 18 May 2017 at 15:05, Jake <[hidden email]> wrote:
I feel like I must just be missing this in the documentation somewhere, but when a function returns a reference SWIG treats that like a pointer by default.  How do we tell it to treat it as a value (i.e. make a copy and wrap that)?

For example:

struct Example {
  int& get(int x, int y);
};

I want the int copied and not as a SWIG_int_p.
 

const int& is marshalled by value in Java/C#. You can thus simply use these typemaps. There are various ways, but I suggest using %apply:

 %apply const int& { int& } // Everywhere (including input parameters and on function return)

 %apply const int& { int& Example::get} // Just for this function's return

On 18 May 2017 at 15:14, schullq <[hidden email]> wrote:
Do you include "typemaps.i" ?

If not, include it, and take a look a it ([swig folder]/Lib/[your target
language]/typemaps.i]), you should get your answer here.

typemaps.i is for changing the way a function's parameters are marshalled, so not of any use for a function's return. You'll notice there are no 'out' typemaps in this file which is what generates the C code for returning types from a function.

William


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Treat Returned Reference as Value

Jake
On Fri, May 19, 2017 at 2:58 AM, William S Fulton <[hidden email]> wrote:

 %apply const int& { int& Example::get} // Just for this function's return


I think this is what I'm looking for since std::vector wrapping provided by std_vector.i 
has a non-const return on operator[].  Thanks.

-Jake Cobb

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Loading...