How to convert to out-param to return value?

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

How to convert to out-param to return value?

Jeremy Cowles
I've have been struggling with this, how do I convert this:

class Foo {
...
bool GetMetadata(const TfToken& key, VtValue* value) const;
...
}

Into this (in C#):

class Foo {
...
VtValue GetMetadata(const TfToken& key) const;
...
}

The Swig docs suggest you do it this way, but it did not work:

%include "typemaps.i"
%apply VtValue* OUTPUT { VtValue* value };
class Foo {
...
bool GetMetadata(const TfToken& key, VtValue* value) const;
...
}

What am I doing wrong?

--
Jeremy

------------------------------------------------------------------------------

_______________________________________________
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: How to convert to out-param to return value?

Jeremy Cowles
I ended up doing it like this:

%extend Foo {
  VtValue GetMetadata(const TfToken& key) const {
   ...
  }
}
%ignore Foo::GetMetadata(const TfToken& key, VtValue* value) const;

class Foo {
...
bool GetMetadata(const TfToken& key, VtValue* value) const;
...
}

Is that the proper way to do this?
If so, is there a way to automate this pattern? It's pretty ubiquitous in the libraries I'm trying to wrap.


On Sat, Sep 24, 2016 at 11:11 AM Jeremy Cowles <[hidden email]> wrote:
I've have been struggling with this, how do I convert this:

class Foo {
...
bool GetMetadata(const TfToken& key, VtValue* value) const;
...
}

Into this (in C#):

class Foo {
...
VtValue GetMetadata(const TfToken& key) const;
...
}

The Swig docs suggest you do it this way, but it did not work:

%include "typemaps.i"
%apply VtValue* OUTPUT { VtValue* value };
class Foo {
...
bool GetMetadata(const TfToken& key, VtValue* value) const;
...
}

What am I doing wrong?

--
Jeremy

------------------------------------------------------------------------------

_______________________________________________
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: How to convert to out-param to return value?

William S Fulton
This sort of non-trivial modification of an API across multiple
types/parameters in the API is not part of what SWIG can do in a
simple way. Maybe some enhancements to SWIG in the future would enable
this kind of feature. The best thing I can think of for you is to use
a SWIG macro to semi-automate it.

You might get away with using multi-argument typemaps to collapse the
two C++ parameters into one C# parameter. However, if you write a
typemap for changing bool into VtValue, then it would apply to all
methods returning bool unless you used a named typemap. Once you start
using a named typemap though, you will need to name all the functions
it applies to, so probably no better than using a macro as you would
need to name the numerous functions that this would apply to. The next
best thing you could do is use swig -xml and write a tool to read the
generated xml, identify the APIs you want to modify and then to
generate the appropriate SWIG directives (%extend and %ignore as
mentioned).

William

On 24 September 2016 at 19:31, Jeremy Cowles <[hidden email]> wrote:

> I ended up doing it like this:
>
> %extend Foo {
>   VtValue GetMetadata(const TfToken& key) const {
>    ...
>   }
> }
> %ignore Foo::GetMetadata(const TfToken& key, VtValue* value) const;
>
> class Foo {
> ...
> bool GetMetadata(const TfToken& key, VtValue* value) const;
> ...
> }
>
> Is that the proper way to do this?
> If so, is there a way to automate this pattern? It's pretty ubiquitous in
> the libraries I'm trying to wrap.
>
>
> On Sat, Sep 24, 2016 at 11:11 AM Jeremy Cowles <[hidden email]>
> wrote:
>>
>> I've have been struggling with this, how do I convert this:
>>
>> class Foo {
>> ...
>> bool GetMetadata(const TfToken& key, VtValue* value) const;
>> ...
>> }
>>
>> Into this (in C#):
>>
>> class Foo {
>> ...
>> VtValue GetMetadata(const TfToken& key) const;
>> ...
>> }
>>
>> The Swig docs suggest you do it this way, but it did not work:
>>
>> %include "typemaps.i"
>> %apply VtValue* OUTPUT { VtValue* value };
>> class Foo {
>> ...
>> bool GetMetadata(const TfToken& key, VtValue* value) const;
>> ...
>> }
>>
>> What am I doing wrong?
>>
>> --
>> Jeremy
>
>
> ------------------------------------------------------------------------------
>
> _______________________________________________
> Swig-user mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/swig-user
>

------------------------------------------------------------------------------
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...