C# directors from C++ with Xamarin iOS (repost)

C# directors from C++ with Xamarin iOS (repost)

Hi, (this is a repost after subscribing to the list properly! sorry)

We have a body of code already using SWIG for Java (for Android mainly) from a common C++ code base. This uses a IXXXXListener 'interface' (pure abstract class in C++) to call back up to Java from the C++. This all works well. However, I am now trying to make this work with C# in order to work with Xamarin for iOS (and Android) and am having a problem with the delegates that the SWIG director creates for C#. When I run I get an error:

The Xamarin documentation talks about applying the MonoPInvokeCallback attribute wherever a delegate is provided to the native code (which I understand; there can be no JIT on iOS). It also says that these need to be static. I have done this as necessary and documented elsewhere for the exceptions (http://blog.reblochon.org/2013/01/c-bindings-for-monotouch-using-swig.html), but it's not obvious how to do this with the director code that SWIG generates.

Does anyone have any ideas? Can I make SWIG produce more Java-like wrappers for C# directors? Or how would I go about turning the generated code into statics that could be decorated with the appropriate attribute?

> Hi, (this is a repost after subscribing to the list properly! sorry)
>
> We have a body of code already using SWIG for Java (for Android mainly) from
> a common C++ code base. This uses a IXXXXListener 'interface' (pure
> abstract class in C++) to call back up to Java from the C++. This all works
> well. However, I am now trying to make this work with C# in order to work
> with Xamarin for iOS (and Android) and am having a problem with the
> delegates that the SWIG director creates for C#. When I run I get an error:
>
> Attempting to JIT compile method '(wrapper native-to-managed)
> XXXXXX.IXXXXListener:SwigDirectoronXXXXX ()' while running with --aot-only.
> See http://docs.xamarin.com/ios/about/limitations for more information.
>
> The Xamarin documentation talks about applying the MonoPInvokeCallback
> attribute wherever a delegate is provided to the native code (which I
> understand; there can be no JIT on iOS). It also says that these need to be
> static. I have done this as necessary and documented elsewhere for the
> exceptions
> (http://blog.reblochon.org/2013/01/c-bindings-for-monotouch-using-swig.html),
> but it's not obvious how to do this with the director code that SWIG
> generates.
>
> Does anyone have any ideas? Can I make SWIG produce more Java-like wrappers
> for C# directors? Or how would I go about turning the generated code into
> statics that could be decorated with the appropriate attribute?
>
> Thanks,
>
> Steve
>
>
>
> --
> View this message in context: http://swig.10945.n7.nabble.com/C-directors-from-C-with-Xamarin-iOS-repost-tp14534.html> Sent from the swig-user mailing list archive at Nabble.com.
>
> ------------------------------------------------------------------------------
> _______________________________________________
> Swig-user mailing list
> [hidden email]> https://lists.sourceforge.net/lists/listinfo/swig-user

Re: C# directors from C++ with Xamarin iOS (repost)

Thanks William,

I will try it out and let you know

Stephen

> On 6 Oct 2015, at 22:17, William S Fulton <[hidden email]> wrote:
>
> Hi Stephen
>
> Please take a look at the monotouch branch:
> https://github.com/swig/swig/commits/monotouch. I put in these changes
> after talking to Stephane Delcroix and Miguel de Icaza in Feb 2013,
> but never received the final thumbs up for merging it into master. In
> particular, this commit for director support needs testing:
> https://github.com/swig/swig/commit/8b3a13129a3976c586f99c93dd8766e93c1211cf.
> I suggest you try it out. I would like to know if the
> Examples/csharp/extend and Examples/csharp/callback work. The testing
> on Travis seem to fail at the time and that'll need investigation -
> https://travis-ci.org/swig/swig/swig/jobs/11613887.
>
> William
>
> On 1 October 2015 at 16:36, StephenJStarkie
> <[hidden email]> wrote:
>> Hi, (this is a repost after subscribing to the list properly! sorry)
>>
>> We have a body of code already using SWIG for Java (for Android mainly) from
>> a common C++ code base. This uses a IXXXXListener 'interface' (pure
>> abstract class in C++) to call back up to Java from the C++. This all works
>> well. However, I am now trying to make this work with C# in order to work
>> with Xamarin for iOS (and Android) and am having a problem with the
>> delegates that the SWIG director creates for C#. When I run I get an error:
>>
>> Attempting to JIT compile method '(wrapper native-to-managed)
>> XXXXXX.IXXXXListener:SwigDirectoronXXXXX ()' while running with --aot-only.
>> See http://docs.xamarin.com/ios/about/limitations for more information.
>>
>> The Xamarin documentation talks about applying the MonoPInvokeCallback
>> attribute wherever a delegate is provided to the native code (which I
>> understand; there can be no JIT on iOS). It also says that these need to be
>> static. I have done this as necessary and documented elsewhere for the
>> exceptions
>> (http://blog.reblochon.org/2013/01/c-bindings-for-monotouch-using-swig.html),
>> but it's not obvious how to do this with the director code that SWIG
>> generates.
>>
>> Does anyone have any ideas? Can I make SWIG produce more Java-like wrappers
>> for C# directors? Or how would I go about turning the generated code into
>> statics that could be decorated with the appropriate attribute?
>>
>> Thanks,
>>
>> Steve
>>
>>
>>
>> --
>> View this message in context: http://swig.10945.n7.nabble.com/C-directors-from-C-with-Xamarin-iOS-repost-tp14534.html>> Sent from the swig-user mailing list archive at Nabble.com.
>>
>> ------------------------------------------------------------------------------
>> _______________________________________________
>> Swig-user mailing list
>> [hidden email]>> https://lists.sourceforge.net/lists/listinfo/swig-user

Re: C# directors from C++ with Xamarin iOS (repost)

I am using PJSIP (PJSUA2) in Xamarin.Android and Xamarin.iOS projects using C#. PJSIP (PJSUA2) utilizes SWIG to generate the Java and C# bindings for the C++ library.

I am new to SWIG and am having difficulties understanding how to best use SWIG on C++ Callbacks to generate appropriate SWIG C# for iOS which is AOT only. ( SWIG C# for java/Android works great since JIT is allowed )

I have resolved the pjsua2PINVOKE.SWIGExceptionHelper class:
1. Added the [ObjCRuntime.MonoNativeFunctionWrapper] descriptor to the delegates
2. Added the [ObjCRuntime.MonoPInvokeCallback(typeof(ExceptionDelegate))] descriptor to the static functions.

Now I have encountered issues with callbacks using directors, how do I resolve?

1. Added [ObjCRuntime.MonoNativeFunctionWrapper] descriptor to the delgates
2. Attempted to add [ObjCRuntime.MonoPInvokeCallback(typeof(SwigDelegateCall_0))] descriptor, but it won't work on non-static functions.

What is the best practice for having SWIG generate the appropriate Callbacks?

On 1 October 2015 at 16:36, StephenJStarkie
<[hidden email]> wrote:
> Hi, (this is a repost after subscribing to the list properly! sorry)
>
> We have a body of code already using SWIG for Java (for Android mainly) from
> a common C++ code base. This uses a IXXXXListener 'interface' (pure
> abstract class in C++) to call back up to Java from the C++. This all works
> well. However, I am now trying to make this work with C# in order to work
> with Xamarin for iOS (and Android) and am having a problem with the
> delegates that the SWIG director creates for C#. When I run I get an error:
>
> Attempting to JIT compile method '(wrapper native-to-managed)
> XXXXXX.IXXXXListener:SwigDirectoronXXXXX ()' while running with --aot-only.
> See http://docs.xamarin.com/ios/about/limitations for more information.
>
> The Xamarin documentation talks about applying the MonoPInvokeCallback
> attribute wherever a delegate is provided to the native code (which I
> understand; there can be no JIT on iOS). It also says that these need to be
> static. I have done this as necessary and documented elsewhere for the
> exceptions
> (http://blog.reblochon.org/2013/01/c-bindings-for-monotouch-using-swig.html),
> but it's not obvious how to do this with the director code that SWIG
> generates.
>
> Does anyone have any ideas? Can I make SWIG produce more Java-like wrappers
> for C# directors? Or how would I go about turning the generated code into
> statics that could be decorated with the appropriate attribute?
>
> Thanks,
>
> Steve
>
>
>
> --
> View this message in context: http://swig.10945.n7.nabble.com/C-directors-from-C-with-Xamarin-iOS-repost-tp14534.html> Sent from the swig-user mailing list archive at Nabble.com.
>
> ------------------------------------------------------------------------------
> _______________________________________________
> Swig-user mailing list
> [hidden email]> https://lists.sourceforge.net/lists/listinfo/swig-user