Re: How to inject code at the top and bottom of each method generated in cpp wrap file by SWIG?

William S Fulton <[email protected]> Fri, 28 Apr 2023 17:56:56 +0100
Newsgroups gmane.comp.programming.swig
Message-ID <CANGqftDytNph07yF7DhduE+GScpYdFD7KWvtOELLGpE_9f8FTg@mail.gmail.com>
Hi Prajjwal

Have you looked at %exception as that allows you to put code just before and
after the call to the wrapped function. The result variable is defined
before
the wrapped function is called.

I'm not entirely sure if this will help, but if you use SwigValueWrapper
via the "valuewrapper" feature it is a useful trick to remove the default
instantiation of the result variable.

William

On Tue, 25 Apr 2023 at 17:15, prajjwal prem <[email protected]> wrote:

> Hi Jim,
>
> Thanks for the solution. The typemap(ret) works perfectly for injecting
> the code at the end. But using typemap(check) does not include the
> declaration of the result variable. The c++ default constructor for result
> needs to be inside the injected mutex code. I tried using the optimal flag
> in typemap(out) but exception handling code overrides that behavior.
>
> Is there a way to tell swig that declaration of the result variable should
> be after the typemap(check)?
>
> Once again thanks for the solution above.
>
> On Mon, Apr 24, 2023 at 8:30 PM Jim <[email protected]> wrote:
>
>>
>> Hi Prajjwal,
>>  You can try this :
>>
>> %typemap(ret) SWIGTYPE {
>>  //inject-1
>> }
>>
>> %typemap(ret) SWIGTYPE & {
>>  //inject-2
>> }
>>
>> %typemap(ret) SWIGTYPE * {
>>  //inject-3
>> }
>>
>> %typemap(check) SWIGTYPE * {
>>   //inject-4
>> }
>>
>> %typemap(check) SWIGTYPE  {
>>   //inject-5
>> }
>>
>> Note: keyword *SWIGTYPE* math any type(eg int\long\bool\T)。*SWIGTYPE &*
>> match any reference type (eg. int & \ long & \ bool& \ T&)。 You can add
>> different typemap according to your function type as needed。
>>
>>
>>
>>
>> At 2023-04-25 05:27:05, "prajjwal prem" <[email protected]> wrote:
>>
>> Hi All,
>>
>> How to inject code at the top and bottom of each method generated in cpp
>> wrap file by SWIG?
>>
>> For Example:
>>
>> SWIGEXPORT int SWIGSTDCALL CSharp_swig_generated_method___(void *jarg1)
>>
>>   {
>>
>>     int jresult;    //SWIG generated
>>
>>
>>
>>     *// inject code here like mutex begin*
>>
>>
>>
>>     /*
>>
>>         SWIG generated code
>>
>>     */
>>
>>
>>
>>     jresult = result;
>>
>>
>>
>>     *// inject code here like mutex end*
>>
>>
>>
>>     return jresult;
>>
>>   }
>>
>>
>> Best regards,
>>
>> Prajjwal
>>
>> _______________________________________________
> 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