summaryrefslogtreecommitdiff
path: root/Examples/test-suite/java_prepost.i
blob: e8e4a9d430df2409db8a420eb662b7d4ac8f9e54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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<count$javainput; ++i) {
        $javainput[i] = d$javainput.get(i);
      }"
%enddef

// pre and post in javain typemaps
//%typemap(jtype, nopgcpp=1) std::vector<double> &v "long" // could suppress pgcpp instead of using pgcppname, but not recommended
%typemap(jstype) std::vector<double> &v "double[]"
%typemap(javain, pre="    DoubleVector d$javainput = new DoubleVector();", post=VECTOR_DOUBLE_JAVAIN_POST, pgcppname="d$javainput") std::vector<double> &v
  "$javaclassname.getCPtr(d$javainput)"

%apply std::vector<double> & v { std::vector<double> & v2 }

// pre only in javain typemap
//%typemap(jtype, nopgcpp=1) std::vector<double> &vpre "long" // could suppress pgcpp instead of using pgcppname, but not recommended
%typemap(jstype) std::vector<double> &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<double> &vpre
  "$javaclassname.getCPtr(d$javainput)"

// post only in javain typemap
%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
  "$javaclassname.getCPtr($javainput)"

%inline %{
bool globalfunction(std::vector<double> & v) {
  v.push_back(0.0);
  v.push_back(1.1);
  v.push_back(2.2);
  return true;
}
struct PrePostTest {
  PrePostTest() {
  }
  PrePostTest(std::vector<double> & v) {
    v.push_back(3.3);
    v.push_back(4.4);
  }
  bool method(std::vector<double> & v) {
    v.push_back(5.5);
    v.push_back(6.6);
    return true;
  }
  static bool staticmethod(std::vector<double> & 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<double> & v, std::vector<double> &v2, std::vector<double> & vpre, std::vector<double> & vpost) {
  return true;
}
struct PrePost2 {
  PrePost2() {
  }
  PrePost2(std::vector<double> & v, std::vector<double> &v2, std::vector<double> & vpre, std::vector<double> & vpost) {
  }
  bool method(std::vector<double> & v, std::vector<double> &v2, std::vector<double> & vpre, std::vector<double> & vpost) {
    return true;
  }
  static bool staticmethod(std::vector<double> & v, std::vector<double> &v2, std::vector<double> & vpre, std::vector<double> & vpost) {
    return true;
  }
};
%}

%template(DoubleVector) std::vector<double>;


// 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, bool) {
  }
};
%}