Re: Howto override the argument names of certain class member functions

William S Fulton <[email protected]>
Newsgroups gmane.comp.programming.swig
Message-ID <CANGqftC5NN8mwrpFO+irq6OoRNz5BQ1paATx4kGKeNyRvwmZLw@mail.gmail.com>
Hi Terry


On Sat, 27 Jun 2020 at 10:42, Terry Barnaby <[email protected]> wrote:

> Hi William,
>
> I have a large class library. There are multiple functions that have a,
> say, "(Channel& chan)" argument. In most cases these are used to return
> values, but in some cases the functions modify the contents of the object
> who's reference is passed. So I cannot generically use the %template
>

I think you meant %apply.

system as that only applies to the type and names of arguments passes,
> there is not ability to say a particular function is just using that
> argument as an output return. A lot of this libraries code is auto
> generated from a IDL and I generate a set of modified *.h files with the
> argument names changed to OUTPUT at the appropriate places but quite a few
> class member functions are not like that.
>
So I think you are saying you want to %apply these typemaps for parameters
with the same name and type, but only on a select few functions and you
don't want to modify the header files. The best solution SWIG has is to use
%apply and %clear as Rui alluded to. This will work to some degree as the
%apply will apply for all the functions in a header file you have include
with %include. You can then use %clear after the %include. If you want to
selectively apply the typemaps to parameters with the same type and name in
one header file, then the next best solution is to use %ignore and then add
the functions back in with %extend, renaming the parameters to something
else. You imply that you only have a few of these that you don't want the
typemaps to apply to, so this might be workable for you.

Looking at the original post, you have this in a header file:

  class DataFile {
   public:
       Error setInfo(const Info& info);    // Input only reference
       Error getInfo(Info& info);        // Output only reference
   };

the types are NOT the same as one is const and the other is not const. If
you look at the typemap rules, you'll see that if you have a typemap for
const Info&, it will not apply to non-const Info&. However a non-const
Info& typemap it wil apply to const Info&. So if you want to consistently
apply typemaps to const and non-const, then solution is easy, something
like:

%typemap(argout) const Info& OUTPUT "" // does nothing (effectively clears
the typemap)
%typemap(argout) Info& OUTPUT "/* some code for argout*/"
%apply Info& OUTPUT { Info& info };
%apply const Info& OUTPUT { const Info& info };

The main reason SWIG does not have function and parameter name combinations
for typemaps is there is no C++ scoping syntax to get at the scope of a
parameter name from outside of the function. I can elaborate more if anyone
is really interested.

William



> Terry
> On 27/06/2020 10:05, William S Fulton wrote:
>
> Hi Terry
>
> The %apply that Rui mentioned just applies the typemap you want for these
> named arguments and types. You can use these %apply directives before SWIG
> parses your unmodified C++ header files containing the original argument
> names. I don't see a reason to modify your header files, can you elaborate
> why you think you need to do that?
>
> William
>
> On Tue, 23 Jun 2020 at 06:19, Terry Barnaby <[email protected]> wrote:
>
>> Many thanks for your reply, yes I could do something like that but this
>> is a complex library with lots of classes and functions defined in
>> header files and I wanted to leave those header files alone and get SWIG
>> to use them directly. I don't want to rewrite the header files into SWIG
>> definition files as that would be a lot of work and unmaintainable. So I
>> was hoping that SWIG could, somehow, apply an argument rename on a
>> function by function basis.
>>
>> I have done it at the moment by pre-processing all of the header files
>> through sed before SWIG sees them, but that's a bit messy.
>>
>> Terry
>> On 22/06/2020 10:54, Rui Oliveira wrote:
>> > Hey,
>> >
>> > You're probably looking to do something like:
>> >
>> > %apply (float complex *OUTPUT) {(liquid_float_complex *_y),
>> > (liquid_float_complex *_x_hat)};
>> > void modem_modulate(modem _q, unsigned int _s, liquid_float_complex
>> *_y);
>> > void modem_get_demodulator_sample(modem _q, liquid_float_complex
>> > *_x_hat);
>> > %clear (liquid_float_complex *_y), (liquid_float_complex *_x_hat);
>> >
>> > This is an example from code I've written. This is documented in:
>> > http://www.swig.org/Doc4.0/SWIGDocumentation.html#Arguments_nn7
>> >
>> > Basically you write typemaps with generic names and then apply them to
>> > your particular function arguments.
>> > Me personally, I like to clear %apply directives when I no longer need
>> > them to avoid bugs to do "stray" typemaps.
>> >
>> > Hope it helped.
>> >
>> > Às 10:46 de 22/06/2020, Terry Barnaby escreveu:
>> >> I am wrapping a relatively complex C++ class library to produce a
>> >> Python interface. One thing I haven't worked out how to do is to tell
>> >> SWIG to return certain function reference arguments on the lhs of the
>> >> function call only, ie. assume them to be OUTPUT only references. For
>> >> example (very simplified):
>> >>
>> >> class DataFile {
>> >>
>> >> public:
>> >>
>> >>     Error setInfo(const Info& info);    // Input only reference
>> >>
>> >>     Error getInfo(Info& info);        // Output only reference
>> >>
>> >> };
>> >>
>> >> Now I can rename the getInfo function in the C++ class library to
>> >> "Error getInfo(Info& OUTPUT);" say and add the appropriate typemaps
>> >> to match this and that works, but I really don't want to rename all
>> >> of the C++'s API function arguments for all of the classes in this
>> >> library (They are used for doxygen etc).
>> >>
>> >> This there a simple way in SWIG to override/rename the arguments of a
>> >> particular function and/or particular classes member function ? So
>> >> something like:
>> >>
>> >> %rename("getInfo(Info& OUTPUT)") "getInfo(Info& info)"
>> >>
>> >> %rename("DataFile::getInfo(Info& OUTPUT)") "DataFile::getInfo(Info&
>> >> info)"
>> >>
>> >>
>> >>
>> >> _______________________________________________
>> >> Swig-user mailing list
>> >> [email protected]
>> >> https://lists.sourceforge.net/lists/listinfo/swig-user
>> >
>> >
>> > _______________________________________________
>> > Swig-user mailing list
>> > [email protected]
>> > https://lists.sourceforge.net/lists/listinfo/swig-user
>>
>>
>>
>>
>> _______________________________________________
>> Swig-user mailing list
>> [email protected]
>> https://lists.sourceforge.net/lists/listinfo/swig-user
>>
>
>

_______________________________________________
Swig-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/swig-user
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.