1 %module java_prepost
2
3 // Test the pre, post attributes for javain typemaps
4
5 %include "std_vector.i"
6
7 %define VECTOR_DOUBLE_JAVAIN_POST
8 " int count$javainput = (int)d$javainput.size();
9 $javainput = new double[count$javainput];
10 for (int i=0; i<count$javainput; ++i) {
11 $javainput[i] = d$javainput.get(i);
12 }"
13 %enddef
14
15 // pre and post in javain typemaps
16 //%typemap(jtype, nopgcpp=1) std::vector<double> &v "long" // could suppress pgcpp instead of using pgcppname, but not recommended
17 %typemap(jstype) std::vector<double> &v "double[]"
18 %typemap(javain, pre=" DoubleVector d$javainput = new DoubleVector();", post=VECTOR_DOUBLE_JAVAIN_POST, pgcppname="d$javainput") std::vector<double> &v
19 "$javaclassname.getCPtr(d$javainput)"
20
21 %apply std::vector<double> & v { std::vector<double> & v2 }
22
23 // pre only in javain typemap
24 //%typemap(jtype, nopgcpp=1) std::vector<double> &vpre "long" // could suppress pgcpp instead of using pgcppname, but not recommended
25 %typemap(jstype) std::vector<double> &vpre "double[]"
26 %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<double> &vpre
27 "$javaclassname.getCPtr(d$javainput)"
28
29 // post only in javain typemap
30 %typemap(javain, post=" int size = (int)$javainput.size();\n for (int i=0; i<size; ++i) {\n $javainput.set(i, $javainput.get(i)/100);\n }") std::vector<double> &vpost
31 "$javaclassname.getCPtr($javainput)"
32
33 %inline %{
globalfunction(std::vector<double> & v)34 bool globalfunction(std::vector<double> & v) {
35 v.push_back(0.0);
36 v.push_back(1.1);
37 v.push_back(2.2);
38 return true;
39 }
40 struct PrePostTest {
PrePostTestPrePostTest41 PrePostTest() {
42 }
PrePostTestPrePostTest43 PrePostTest(std::vector<double> & v) {
44 v.push_back(3.3);
45 v.push_back(4.4);
46 }
methodPrePostTest47 bool method(std::vector<double> & v) {
48 v.push_back(5.5);
49 v.push_back(6.6);
50 return true;
51 }
staticmethodPrePostTest52 static bool staticmethod(std::vector<double> & v) {
53 v.push_back(7.7);
54 v.push_back(8.8);
55 return true;
56 }
57 };
58
59 // Check pre and post only typemaps and that they coexist okay and that the generated code line spacing looks okay
globalfunction2(std::vector<double> & v,std::vector<double> & v2,std::vector<double> & vpre,std::vector<double> & vpost)60 bool globalfunction2(std::vector<double> & v, std::vector<double> &v2, std::vector<double> & vpre, std::vector<double> & vpost) {
61 return true;
62 }
63 struct PrePost2 {
PrePost2PrePost264 PrePost2() {
65 }
PrePost2PrePost266 PrePost2(std::vector<double> & v, std::vector<double> &v2, std::vector<double> & vpre, std::vector<double> & vpost) {
67 }
methodPrePost268 bool method(std::vector<double> & v, std::vector<double> &v2, std::vector<double> & vpre, std::vector<double> & vpost) {
69 return true;
70 }
staticmethodPrePost271 static bool staticmethod(std::vector<double> & v, std::vector<double> &v2, std::vector<double> & vpre, std::vector<double> & vpost) {
72 return true;
73 }
74 };
75 %}
76
77 %template(DoubleVector) std::vector<double>;
78
79
80 // Check pre post constructor helper deals with checked exceptions, InstantiationException is just a random checked exception
81 %typemap(javain, pre=" if ($javainput == null)\n throw new InstantiationException(\"empty value!!\");", throws="InstantiationException") PrePostTest *
82 "$javaclassname.getCPtr($javainput)"
83
84 %inline %{
85 struct PrePostThrows {
PrePostThrowsPrePostThrows86 PrePostThrows(PrePostTest *ppt, bool) {
87 }
88 };
89 %}
90
91
92