diff --git a/Examples/test-suite/csharp/Makefile.in b/Examples/test-suite/csharp/Makefile.in index 2ff489db5..5f55f1e64 100644 --- a/Examples/test-suite/csharp/Makefile.in +++ b/Examples/test-suite/csharp/Makefile.in @@ -16,6 +16,7 @@ CPP_TEST_CASES = \ csharp_attributes \ csharp_exceptions \ csharp_features \ + csharp_prepost \ csharp_typemaps \ enum_thorough_simple \ enum_thorough_typesafe diff --git a/Examples/test-suite/csharp/csharp_prepost_runme.cs b/Examples/test-suite/csharp/csharp_prepost_runme.cs new file mode 100644 index 000000000..d0bacdaba --- /dev/null +++ b/Examples/test-suite/csharp/csharp_prepost_runme.cs @@ -0,0 +1,70 @@ + +using System; +using System.Reflection; +using csharp_prepostNamespace; + +public class csharp_prepost_runme { + + public static void Main() { + { + double[] v; + csharp_prepost.globalfunction(out v); + Assert(v.Length, 3); + Assert(v[0], 0.0); + Assert(v[1], 1.1); + Assert(v[2], 2.2); + } + { + double[] v; + new PrePostTest(out v); + Assert(v.Length, 2); + Assert(v[0], 3.3); + Assert(v[1], 4.4); + } + { + double[] v; + PrePostTest p = new PrePostTest(); + p.method(out v); + Assert(v.Length, 2); + Assert(v[0], 5.5); + Assert(v[1], 6.6); + } + { + double[] v; + PrePostTest.staticmethod(out v); + Assert(v.Length, 2); + Assert(v[0], 7.7); + Assert(v[1], 8.8); + } + + // Check attributes are generated for the constructor helper function + { + CsinAttributes c = new CsinAttributes(5); + Assert(c.getVal(), 500); + + Type type = typeof(CsinAttributes); + { + MethodInfo member = (MethodInfo)type.GetMember("SwigConstructCsinAttributes", BindingFlags.NonPublic | BindingFlags.Static)[0]; + if (Attribute.GetCustomAttribute(member, typeof(CustomIntPtrAttribute)) == null) + throw new Exception("No CustomIntPtr attribute for " + member.Name); + ParameterInfo parameter = member.GetParameters()[0]; // expecting one parameter + if (parameter.Name != "val") + throw new Exception("Incorrect parameter name"); + Attribute attribute = Attribute.GetCustomAttributes(parameter)[0]; + if (attribute.GetType() != typeof(CustomIntAttribute)) + throw new Exception("Expecting CustomInt attribute"); + } + } + } + private static void Assert(double d1, double d2) { + if (d1 != d2) + throw new Exception("assertion failure. " + d1 + " != " + d2); + } +} + +// Custom attribute classes +[AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)] +public class CustomIntAttribute : Attribute {} + +[AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)] +public class CustomIntPtrAttribute : Attribute {} diff --git a/Examples/test-suite/csharp_prepost.i b/Examples/test-suite/csharp_prepost.i new file mode 100644 index 000000000..9c2cedc83 --- /dev/null +++ b/Examples/test-suite/csharp_prepost.i @@ -0,0 +1,90 @@ +%module csharp_prepost + +// Test the pre, post and cshin attributes for csin typemaps + +%include "std_vector.i" + +%define VECTOR_DOUBLE_CSIN_POST +" int count$csinput = d$csinput.Count; + $csinput = new double[count$csinput]; + for (int i=0; i &v "out double[]" +%typemap(csin, pre=" DoubleVector d$csinput = new DoubleVector();", post=VECTOR_DOUBLE_CSIN_POST, cshin="out $csinput") std::vector &v + "$csclassname.getCPtr(d$csinput)" + +%apply std::vector & v { std::vector & v2 } + +// pre only in csin typemap +%typemap(cstype) std::vector &vpre "ref double[]" +%typemap(csin, pre=" DoubleVector d$csinput = new DoubleVector();\n foreach (double d in $csinput) {\n d$csinput.Add(d);\n }", cshin="ref $csinput") std::vector &vpre + "$csclassname.getCPtr(d$csinput)" + +// post only in csin typemap +%typemap(csin, post=" int size = $csinput.Count;\n for (int i=0; i &vpost + "$csclassname.getCPtr($csinput)" + +%inline %{ +bool globalfunction(std::vector & v) { + v.push_back(0.0); + v.push_back(1.1); + v.push_back(2.2); + return true; +} +struct PrePostTest { + PrePostTest() { + } + PrePostTest(std::vector & v) { + v.push_back(3.3); + v.push_back(4.4); + } + bool method(std::vector & v) { + v.push_back(5.5); + v.push_back(6.6); + return true; + } + static bool staticmethod(std::vector & v) { + v.push_back(7.7); + v.push_back(8.8); + return true; + } +}; + +// Check pre and post only typemaps and that they coexist okay and that the generated code line spacing looks okay +bool globalfunction2(std::vector & v, std::vector &v2, std::vector & vpre, std::vector & vpost) { + return true; +} +struct PrePost2 { + PrePost2() { + } + PrePost2(std::vector & v, std::vector &v2, std::vector & vpre, std::vector & vpost) { + } + bool method(std::vector & v, std::vector &v2, std::vector & vpre, std::vector & vpost) { + return true; + } + static bool staticmethod(std::vector & v, std::vector &v2, std::vector & vpre, std::vector & vpost) { + return true; + } +}; +%} + +%template(DoubleVector) std::vector; + +// Check attributes in the typemaps +%typemap(cstype, inattributes="[CustomInt]") int val "int" +%typemap(csin, pre=" int tmp_$csinput = $csinput * 100;") int "tmp_$csinput" +%typemap(imtype, out="IntPtr/*overridden*/", outattributes="[CustomIntPtr]") CsinAttributes * "HandleRef/*overridden*/" + +%inline %{ +class CsinAttributes { + int m_val; +public: + CsinAttributes(int val) : m_val(val) {} + int getVal() { return m_val; } +}; +%} + diff --git a/Examples/test-suite/java/Makefile.in b/Examples/test-suite/java/Makefile.in index 7c0a375a7..642367959 100644 --- a/Examples/test-suite/java/Makefile.in +++ b/Examples/test-suite/java/Makefile.in @@ -25,6 +25,7 @@ CPP_TEST_CASES = \ java_lib_various \ java_jnitypes \ java_pragmas \ + java_prepost \ java_throws \ java_typemaps_proxy \ java_typemaps_typewrapper diff --git a/Examples/test-suite/java/java_prepost_runme.java b/Examples/test-suite/java/java_prepost_runme.java new file mode 100644 index 000000000..6eeb60393 --- /dev/null +++ b/Examples/test-suite/java/java_prepost_runme.java @@ -0,0 +1,28 @@ + +import java_prepost.*; + +public class java_prepost_runme { + + static { + try { + System.loadLibrary("java_prepost"); + } catch (UnsatisfiedLinkError e) { + System.err.println("Native code library failed to load. See the chapter on Dynamic Linking Problems in the SWIG Java documentation for help.\n" + e); + System.exit(1); + } + } + + public static void main(String argv[]) + { + // ensure checked exception is generated + try { + PrePostThrows ppt = new PrePostThrows(null); + } catch (InstantiationException e) { + } + } + + private static void Assert(double d1, double d2) { + if (d1 != d2) + throw new RuntimeException("assertion failure. " + d1 + " != " + d2); + } +} diff --git a/Examples/test-suite/java_prepost.i b/Examples/test-suite/java_prepost.i new file mode 100644 index 000000000..3e3839248 --- /dev/null +++ b/Examples/test-suite/java_prepost.i @@ -0,0 +1,91 @@ +%module java_prepost + +// Test the pre, post attributes for javain typemaps + +%include "std_vector.i" + +%define VECTOR_DOUBLE_JAVAIN_POST +" int count$javainput = (int)d$javainput.size(); + $javainput = new double[count$javainput]; + for (int i=0; i &v "long" // could suppress pgcpp instead of using pgcppname, but not recommended +%typemap(jstype) std::vector &v "double[]" +%typemap(javain, pre=" DoubleVector d$javainput = new DoubleVector();", post=VECTOR_DOUBLE_JAVAIN_POST, pgcppname="d$javainput") std::vector &v + "$javaclassname.getCPtr(d$javainput)" + +%apply std::vector & v { std::vector & v2 } + +// pre only in javain typemap +//%typemap(jtype, nopgcpp=1) std::vector &vpre "long" // could suppress pgcpp instead of using pgcppname, but not recommended +%typemap(jstype) std::vector &vpre "double[]" +%typemap(javain, pre=" DoubleVector d$javainput = new DoubleVector();\n for (int i=0; i<$javainput.length; ++i) {\n double d = $javainput[i];\n d$javainput.add(d);\n }", pgcppname="d$javainput") std::vector &vpre + "$javaclassname.getCPtr(d$javainput)" + +// post only in javain typemap +%typemap(javain, post=" int size = (int)$javainput.size();\n for (int i=0; i &vpost + "$javaclassname.getCPtr($javainput)" + +%inline %{ +bool globalfunction(std::vector & v) { + v.push_back(0.0); + v.push_back(1.1); + v.push_back(2.2); + return true; +} +struct PrePostTest { + PrePostTest() { + } + PrePostTest(std::vector & v) { + v.push_back(3.3); + v.push_back(4.4); + } + bool method(std::vector & v) { + v.push_back(5.5); + v.push_back(6.6); + return true; + } + static bool staticmethod(std::vector & v) { + v.push_back(7.7); + v.push_back(8.8); + return true; + } +}; + +// Check pre and post only typemaps and that they coexist okay and that the generated code line spacing looks okay +bool globalfunction2(std::vector & v, std::vector &v2, std::vector & vpre, std::vector & vpost) { + return true; +} +struct PrePost2 { + PrePost2() { + } + PrePost2(std::vector & v, std::vector &v2, std::vector & vpre, std::vector & vpost) { + } + bool method(std::vector & v, std::vector &v2, std::vector & vpre, std::vector & vpost) { + return true; + } + static bool staticmethod(std::vector & v, std::vector &v2, std::vector & vpre, std::vector & vpost) { + return true; + } +}; +%} + +%template(DoubleVector) std::vector; + + +// Check pre post constructor helper deals with checked exceptions, InstantiationException is just a random checked exception +%typemap(javain, pre=" if ($javainput == null)\n throw new InstantiationException(\"empty value!!\");", throws="InstantiationException") PrePostTest * + "$javaclassname.getCPtr($javainput)" + +%inline %{ +struct PrePostThrows { + PrePostThrows(PrePostTest *ppt) { + } +}; +%} + +