Re: How to inject code at the top and bottom of each method generated in cpp wrap file by SWIG?
prajjwal prem <[email protected]> Fri, 28 Apr 2023 12:01:33 -0600
| Newsgroups | gmane.comp.programming.swig |
|---|---|
| Message-ID | <CADqCKMwT+qgSYp2_43q0+AH_3ZW51Se3T7iXUh6Nq_nMaVRVOQ@mail.gmail.com> |
Hi William, Thank you for the suggestions. Yes, I was actually using the %exception directive before. The reason I am not able to use it is that it only encloses the function call that is being wrapped and leaves other parts (such as creation of new object on heap before it is returned) outside. This leads to race conditions in C# as calls to constructors are not thread safe. I will look into the "valuewrapper" feature. Best regards, Prajjwal On Fri, Apr 28, 2023 at 10:57 AM William S Fulton <[email protected]> wrote: > 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