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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
%module(directors="1") virtual_poly
%warnfilter(SWIGWARN_JAVA_COVARIANT_RET, SWIGWARN_CSHARP_COVARIANT_RET) copy; /* Java, C# covariant return types */
%warnfilter(SWIGWARN_JAVA_COVARIANT_RET, SWIGWARN_CSHARP_COVARIANT_RET) ref_this; /* Java, C# covariant return types */
%warnfilter(SWIGWARN_JAVA_COVARIANT_RET, SWIGWARN_CSHARP_COVARIANT_RET) covariant; /* Java, C# covariant return types */
%warnfilter(SWIGWARN_JAVA_COVARIANT_RET, SWIGWARN_CSHARP_COVARIANT_RET) covariant2; /* Java, C# covariant return types */
%warnfilter(SWIGWARN_JAVA_COVARIANT_RET, SWIGWARN_CSHARP_COVARIANT_RET) covariant3; /* Java, C# covariant return types */
%warnfilter(SWIGWARN_JAVA_COVARIANT_RET, SWIGWARN_CSHARP_COVARIANT_RET) covariant4; /* Java, C# covariant return types */
%{
#if defined(__SUNPRO_CC)
#pragma error_messages (off, wbadasg) /* Assigning extern "C" ... */
#endif
%}
//
// Check this example with directors wherever possible.
//
//%feature("director");
// This shouldn't get used.
// %newobject *::copy();
%newobject *::copy() const;
%inline %{
struct NNumber
{
virtual ~NNumber() {};
virtual NNumber* copy() const = 0;
virtual NNumber& ref_this()
{
return *this;
}
NNumber* nnumber()
{
return this;
}
};
/*
NInt and NDouble are both NNumber derived classes, but they
have more different than common attributes.
In particular the function 'get', that is type dependent, can't
be included in the NNumber abstract interface.
For this reason, the virtual 'copy' method has a polymorphic (covariant)
return type, since in most of the cases we don't want to lose the
original object type, which is very very important.
Using the polymorphic return type reduced greatly the need of
using 'dynamic_cast' at the C++ side, and at the target languages
that support it.
*/
struct NInt : NNumber
{
NInt(int v) : val(v)
{
}
int get() const
{
return val;
}
virtual NInt* copy() const
{
return new NInt(val);
}
virtual NInt& ref_this()
{
return *this;
}
/* See below */
static NInt* narrow(NNumber* nn);
private:
int val;
};
inline NInt& incr(NInt& i) {
i = i.get() + 1;
return i;
}
struct NDouble : NNumber
{
NDouble(double v) : val(v)
{
}
double get() const
{
return val;
}
virtual NDouble* copy() const
{
return new NDouble(val);
}
virtual NDouble& ref_this()
{
return *this;
}
/* See below */
static NDouble* narrow(NNumber* nn);
private:
double val;
};
/*
Java and C# do not support the polymorphic (covariant) return types used
in the copy method. So, they just emit 'plain' copy functions as if this is
being wrapped instead:
NNumber* NNumber::copy() const;
NNumber* NInt::copy() const;
NNumber* NDouble::copy() const;
However, since the objects provide their own downcasting
mechanism, the narrow methods similar to the CORBA mechanism,
could be used, otherwise use the Java/C# downcasts.
*/
inline NInt* NInt::narrow(NNumber* n) {
// this is just a plain C++ dynamic_cast, but in theory the user
// could use whatever he wants.
return dynamic_cast<NInt*>(n);
}
inline NDouble* NDouble::narrow(NNumber* n) {
return dynamic_cast<NDouble*>(n);
}
%}
%inline %{
// These three classes test covariant return types and whether swig accurately matches
// polymorphic methods (mainly for C# override keyword). Also tests methods which hide
// the base class' method (for C#, new keyword required on method declaration).
typedef int* IntegerPtr;
typedef double Double;
template<typename T> struct Base {
T t;
virtual IntegerPtr method() const = 0;
virtual IntegerPtr foxy() const = 0;
virtual IntegerPtr foxy(int a) const = 0;
virtual int * foxy(int*& a) { return 0; }
virtual double afunction() = 0;
virtual IntegerPtr defaultargs(double d, int * a = 0) = 0;
static void StaticHidden() {}
void AmIAmINotVirtual() {}
IntegerPtr NotVirtual(IntegerPtr i) { return 0; }
virtual Base * covariant(int a = 0, int * i = 0) { return 0; }
typedef Base * BasePtr;
virtual BasePtr covariant2() { return 0; }
virtual BasePtr covariant3() { return 0; }
virtual ~Base() {}
};
template<typename T> struct Derived : Base<T> {
int * method() const { return 0; }
IntegerPtr foxy() const { return 0; }
int * foxy(int a) const { return 0; }
virtual int * foxy(int*& a) { return 0; }
Double afunction() { return 0; }
int * defaultargs(Double d, IntegerPtr a = 0) { return 0; }
void AmIAmINotVirtual() {}
int * NotVirtual(int *i) { return 0; }
typedef Derived * DerivedPtr;
DerivedPtr covariant(int a = 0, IntegerPtr i = 0) { return 0; }
DerivedPtr covariant2() { return 0; }
Derived<T> * covariant3() { return 0; }
virtual Derived<T> * covariant4(double d) { return 0; }
virtual int IsVirtual() { return 0; }
};
template<typename T> struct Bottom : Derived<T> {
int * method() const { return 0; }
static void StaticHidden() {}
void AmIAmINotVirtual() {}
IntegerPtr NotVirtual(IntegerPtr i) { return 0; }
void (*funcptr)(int a, bool b);
Bottom<T> * covariant(int a = 0, IntegerPtr i = 0) { return 0; }
Derived<T> * covariant2() { return 0; }
Bottom<T> * covariant3() { return 0; }
Bottom<T> * covariant4(double d) { return 0; }
int IsVirtual() { return 0; }
};
%}
%template(BaseInt) Base<int>;
%template(DerivedInt) Derived<int>;
%template(BottomInt) Bottom<int>;
|