Re: Typedef duplicates across modules?

William S Fulton <[email protected]> Fri, 1 Sep 2023 22:37:14 +0100
Newsgroups gmane.comp.programming.swig
Message-ID <CANGqftCKr=f9s2ChEXG+a5ZpboZPdST5RRevqewvDGG9E3ArjQ@mail.gmail.com>
The SWIGTYPE_p_f_r_MyClass__void.cs type wrapper class is generated each
time you invoke swig for the two modules. However, the generated file is
identical and so it doesn't really matter.

Here's a patch that I tested which works and runs okay:

diff --git a/Examples/test-suite/csharp/import_callback_runme.cs
b/Examples/test-suite/csharp/import_callback_runme.cs
new file mode 100644
index 000000000..e4ab43a38
--- /dev/null
+++ b/Examples/test-suite/csharp/import_callback_runme.cs
@@ -0,0 +1,11 @@
+using System;
+using import_callbackNamespace;
+
+public class runme
+{
+  static void Main() {
+    MyClass mc = new MyClass(import_callback_2.DoSomething_cb);
+//    import_callback_1.DoSomething(mc);
+    mc.call();
+  }
+}
diff --git a/Examples/test-suite/import_callback.list
b/Examples/test-suite/import_callback.list
new file mode 100644
index 000000000..8edab5b4f
--- /dev/null
+++ b/Examples/test-suite/import_callback.list
@@ -0,0 +1,2 @@
+import_callback_1
+import_callback_2
diff --git a/Examples/test-suite/import_callback_1.h
b/Examples/test-suite/import_callback_1.h
new file mode 100644
index 000000000..b48f6bd8c
--- /dev/null
+++ b/Examples/test-suite/import_callback_1.h
@@ -0,0 +1,11 @@
+class MyClass;
+typedef void(*MyClassCallback)(MyClass&);
+class MyClass
+{
+  MyClassCallback callback;
+public:
+  MyClass(MyClassCallback initializer) : callback(initializer) {}
+  void call() {
+    callback(*this);
+  }
+};
diff --git a/Examples/test-suite/import_callback_1.i
b/Examples/test-suite/import_callback_1.i
new file mode 100644
index 000000000..e0a188c7a
--- /dev/null
+++ b/Examples/test-suite/import_callback_1.i
@@ -0,0 +1,9 @@
+%module import_callback_1
+
+%{
+#include <iostream>
+%}
+%{
+#include "import_callback_1.h"
+%}
+%include "import_callback_1.h"
diff --git a/Examples/test-suite/import_callback_2.i
b/Examples/test-suite/import_callback_2.i
new file mode 100644
index 000000000..b3e2bb509
--- /dev/null
+++ b/Examples/test-suite/import_callback_2.i
@@ -0,0 +1,11 @@
+%module import_callback_2
+
+%import "import_callback_1.h"
+%callback("%s_cb");
+%inline %{
+#include <iostream>
+#include "import_callback_1.h"
+void DoSomething(MyClass&) { std::cout << "DoSomething (module 2)" <<
std::endl; }
+//void FuncPtrUse(MyClassCallback m) {}
+%}
+%nocallback;

It is a potential addition to the swig test-suite, which you may not be
familiar with, but it is in essence what you provided.

William


On Fri, 16 Jun 2023 at 09:24, Marc Dannemann <[email protected]> wrote:

> I have two modules which share a common class definition and therefore a
> required function pointer typedef:
>
> %module Module1
>
> typedef void(*MyClassCallback)(MyClass&);
> class MyClass
> {
> public:
>      MyClass(MyClassCallback initializer);
> };
>
> %module Module2
>
> %import "Module1.i"
> %callback("%s_cb");
> void DoSomething(MyClass&);
> %nocallback;
>
> When I generate C# wrappers for both modules, each module contains the
> exact same wrapper class for the function pointer typedef:
>
> public class SWIGTYPE_p_f_r_MyClass__void {
>    private global::System.Runtime.InteropServices.HandleRef swigCPtr;
>
>    internal SWIGTYPE_p_f_r_MyClass__void(global::System.IntPtr cPtr,
> bool futureUse) {
>      swigCPtr = new
> global::System.Runtime.InteropServices.HandleRef(this, cPtr);
>    }
>
>    protected SWIGTYPE_p_f_r_MyClass__void() {
>      swigCPtr = new
> global::System.Runtime.InteropServices.HandleRef(null,
> global::System.IntPtr.Zero);
>    }
>
>    internal static global::System.Runtime.InteropServices.HandleRef
> getCPtr(SWIGTYPE_p_f_r_MyClass__void obj) {
>      return (obj == null) ? new
> global::System.Runtime.InteropServices.HandleRef(null,
> global::System.IntPtr.Zero) : obj.swigCPtr;
>    }
>
>    internal static global::System.Runtime.InteropServices.HandleRef
> swigRelease(SWIGTYPE_p_f_r_MyClass__void obj) {
>      return (obj == null) ? new
> global::System.Runtime.InteropServices.HandleRef(null,
> global::System.IntPtr.Zero) : obj.swigCPtr;
>    }
> }
>
> Is this intended behaviour and, if yes, how can I produce valid code
> with no conflicting class definitions like this?
>
> Thanks in advance!
>
> Marc
>
>
>
> _______________________________________________
> 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