summaryrefslogtreecommitdiff
path: root/Examples/test-suite/java_director_ptrclass.i
blob: 6b4fc1f0831ff5a5b06d4d94bfc54b07dc3eaeb3 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
%module(directors="1") java_director_ptrclass

// Tests that custom director typemaps can be used with C++ types that
// represent a pointer, in such a way that Java perceives this class as
// equivalent to the underlying type. In particular, this verifies that
// a typemap lookup within a typemap kwarg, in this case
// directorin:descriptor, works as expected.

%{
namespace bar {
class Baz {
public:
	Baz() : touched(false) {}
	void SetTouched() { touched = true; }
	bool GetTouched() { return touched; }
private:
	bool touched;
};

template <typename T>
class Ptr {
public:
	Ptr(T* b) : b_(b) {}
        T* Get() const { return b_; }
private:
	T* b_;
};

class Foo {
public:
        // Calling FinalMaybeTouch from Java unambiguously goes through C++ to
        // reach MaybeTouch.
        Ptr< bar::Baz > FinalMaybeTouch(Baz* b) {
	  return MaybeTouch(Ptr< bar::Baz >(b));
	}
	virtual Ptr< bar::Baz > MaybeTouch(Ptr< bar::Baz > f) {
	  return f; /* Don't touch */
	}
	virtual ~Foo() {}
};
}
%}

%feature("director") Foo;

%typemap(jni) bar::Ptr< bar::Baz > "jlong"
%typemap(jtype) bar::Ptr< bar::Baz > "long"
%typemap(jstype) bar::Ptr< bar::Baz > "Baz"
%typemap(in) bar::Ptr< bar::Baz > {
  $1 = bar::Ptr< bar::Baz >(*( bar::Baz**)&$input);
}
%typemap(out) bar::Ptr< bar::Baz > {
  const bar::Ptr< bar::Baz >& ptr = $1;
  if (ptr.Get()) {
    $result = ($typemap(jni, bar::Baz))ptr.Get();
  } else {
    $result = 0;
  }
}
%typemap(javain) bar::Ptr< bar::Baz > "$typemap(jstype, bar::Baz).getCPtr($javainput)"
%typemap(javaout) bar::Ptr< bar::Baz > {
  long cPtr = $jnicall;
  return (cPtr == 0) ? null : new $typemap(jstype, bar::Baz)(cPtr, false);
}
%typemap(directorin, descriptor="L$packagepath/$typemap(jstype, bar::Baz);") bar::Ptr< bar::Baz >
%{ *((bar::Baz**)&$input) = ((bar::Ptr< bar::Baz >&)$1).Get(); %}
%typemap(directorout) bar::Ptr< bar::Baz > {
  $result = bar::Ptr< bar::Baz >(*( bar::Baz**)&$input);
}
%typemap(javadirectorin) bar::Ptr< bar::Baz > %{
  ((long)$jniinput == 0) ? null : new $typemap(jstype, bar::Baz)($jniinput, false)
%}
%typemap(javadirectorout) bar::Ptr< bar::Baz > "$typemap(jstype, bar::Baz).getCPtr($javacall)"

namespace bar {
class Baz {
public:
        Baz() : touched(false) {}
        void SetTouched() { touched = true; }
        bool GetTouched() { return touched; }
private:
        bool touched;
};

template <typename T>
class Ptr {
public:
        Ptr(T* b) : b_(b) {}
        T* Get() { return b_; }
private:
        T* b_;
};

class Foo {
public:
        // Calling FinalMaybeTouch from Java unambiguously goes through C++ to
        // reach MaybeTouch.
	Ptr< bar::Baz > FinalMaybeTouch(Baz* b) {
          return MaybeTouch(Ptr< bar::Baz >(b));
        }
        virtual Ptr< bar::Baz > MaybeTouch(Ptr< bar::Baz > f) {
          return f; /* Don't touch */
        }
        virtual ~Foo() {}
};
}