Delegates to SWIG - C #

(full duplicate http://old.nabble.com/C%2B%2B-pointer-to-method-as-parameter-to-C--td17645155.html , but cannot do the proposed macro work)

I have the following C ++ function (simplified):

InputPort addInputPort(void(*callback)(InputPort)); 

Problem: C # function generated signature:

 public InputPort addInputPort(SWIGTYPE_p_f__InputPort____void callback) 

SWIGTYPE_p_f__InputPort____void not a delegate (and has no common constructor), so I can not use addInputPort.

How do I tell SWIG to use a delegate instead? If the solution includes% typemap, please be patient with me ...

+6
source share
1 answer

The solution includes a% typemap map. The effect you see is a useless default mapping for unknown types. In your case, the unknown type is the function type void *(InputPort) .

In fact, the entire SWIG translation is based on the% typemaps that have already been written and live in the standard SWIG libraries. You can explore the initial% typemaps for C # in this Subversion Location .

In particular, find SWIGStringHelper in csharphead.swg . This code maps the delegate to the C callback. The trick is to add the helper callback implementation to the SWIG module.

+1
source

Source: https://habr.com/ru/post/892152/


All Articles