These is the current tests for the friend function support.
They will give you and idea of what is currently working. If you have another case you think we need to test, please send me an email to add it and trying it here. git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@5581 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
364223c11e
commit
708021a809
3 changed files with 171 additions and 0 deletions
135
Examples/test-suite/friends.i
Normal file
135
Examples/test-suite/friends.i
Normal file
|
|
@ -0,0 +1,135 @@
|
|||
%module friends
|
||||
%{
|
||||
#include <istream>
|
||||
%}
|
||||
|
||||
#pragma SWIG nowarn=-503
|
||||
|
||||
|
||||
%inline
|
||||
%{
|
||||
|
||||
|
||||
class A;
|
||||
struct B
|
||||
{
|
||||
B(int i) : v(i)
|
||||
{
|
||||
}
|
||||
|
||||
friend int mix(A* a, B *b);
|
||||
virtual ~B()
|
||||
{
|
||||
}
|
||||
|
||||
private:
|
||||
int v;
|
||||
|
||||
};
|
||||
|
||||
|
||||
struct A
|
||||
{
|
||||
A(int v) : val(v)
|
||||
{
|
||||
}
|
||||
|
||||
friend int get_val1(const A& a)
|
||||
{
|
||||
return a.val;
|
||||
}
|
||||
|
||||
/* simple overloading */
|
||||
friend int get_val1(const A& a, int o)
|
||||
{
|
||||
return a.val + o;
|
||||
}
|
||||
|
||||
/*
|
||||
note that operators << and >> are ignored, as they
|
||||
should, since no rename is performed.
|
||||
*/
|
||||
friend std::istream& operator>>(std::istream& in, A& a);
|
||||
|
||||
/* already declare at B */
|
||||
friend int mix(A* a, B *b);
|
||||
|
||||
protected:
|
||||
friend int get_val2(const A& a)
|
||||
{
|
||||
return a.val*2;
|
||||
}
|
||||
|
||||
private:
|
||||
friend int get_val3(const A& a);
|
||||
|
||||
/* this should be ignored */
|
||||
friend std::ostream& operator<<(std::ostream& out, const A& a)
|
||||
{
|
||||
out << a.val;
|
||||
return out;
|
||||
}
|
||||
|
||||
int val;
|
||||
};
|
||||
|
||||
/*
|
||||
'mix' is an interesting case, this is the third declaration
|
||||
swig is getting (two friends + one inline).
|
||||
*/
|
||||
inline int mix(A* a, B *b) {
|
||||
return a->val + b->v;
|
||||
}
|
||||
|
||||
/* this should be ignored */
|
||||
inline std::istream& operator>>(std::istream& in, A& a) {
|
||||
int v;
|
||||
in >> v;
|
||||
a = A(v);
|
||||
return in;
|
||||
}
|
||||
|
||||
inline int get_val3(const A& a) {
|
||||
return a.val*3;
|
||||
}
|
||||
|
||||
/* another overloading */
|
||||
inline int get_val1(int i, int a, int b) {
|
||||
return i;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
sit and watch how well this case works, is just incredible!!,
|
||||
|
||||
also note that there is no special code added to manage friends
|
||||
and templates (or overloading), this is just old swig magic
|
||||
working at its best.
|
||||
*/
|
||||
|
||||
template <class C>
|
||||
struct D
|
||||
{
|
||||
D(C v) : val(v) {}
|
||||
|
||||
/* note that here we are overloading the already super
|
||||
overloaded 'get_val1' */
|
||||
friend C get_val1(D& b)
|
||||
{
|
||||
return b.val;
|
||||
}
|
||||
|
||||
/* here set will be 'auto' overloaded, depending of the
|
||||
%template instantiations. */
|
||||
friend void set(D& b, C v)
|
||||
{
|
||||
b.val = v;
|
||||
}
|
||||
|
||||
private:
|
||||
C val;
|
||||
};
|
||||
%}
|
||||
|
||||
%template(D_i) D<int>;
|
||||
%template(D_d) D<double>;
|
||||
28
Examples/test-suite/python/friends_runme.py
Normal file
28
Examples/test-suite/python/friends_runme.py
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
import friends
|
||||
|
||||
a = friends.A(2)
|
||||
|
||||
if friends.get_val1(a) != 2: raise RuntimeError
|
||||
if friends.get_val2(a) != 4: raise RuntimeError
|
||||
if friends.get_val3(a) != 6: raise RuntimeError
|
||||
|
||||
# nice overload working fine
|
||||
if friends.get_val1(1,2,3) != 1: raise RuntimeError
|
||||
|
||||
b = friends.B(3)
|
||||
|
||||
# David's case
|
||||
if friends.mix(a,b) != 5: raise RuntimeError
|
||||
|
||||
di = friends.D_d(2)
|
||||
dd = friends.D_d(3.3)
|
||||
|
||||
# incredible template overloading working just fine
|
||||
if friends.get_val1(di) != 2: raise RuntimeError
|
||||
if friends.get_val1(dd) != 3.3: raise RuntimeError
|
||||
|
||||
friends.set(di, 4)
|
||||
friends.set(dd, 1.3)
|
||||
|
||||
if friends.get_val1(di) != 4: raise RuntimeError
|
||||
if friends.get_val1(dd) != 1.3: raise RuntimeError
|
||||
8
Examples/test-suite/ruby/friends_runme.rb
Normal file
8
Examples/test-suite/ruby/friends_runme.rb
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
require 'friends'
|
||||
|
||||
|
||||
a = Friends::A.new(2)
|
||||
|
||||
raise RuntimeError if Friends::get_val1(a) != 2
|
||||
raise RuntimeError if Friends::get_val2(a) != 4
|
||||
raise RuntimeError if Friends::get_val3(a) != 6
|
||||
Loading…
Add table
Add a link
Reference in a new issue