import source from 1.3.40
[external/swig.git] / Examples / test-suite / friends.i
1 %module friends
2 %{
3 #include <iostream>
4 %}
5
6 %warnfilter(SWIGWARN_LANG_IDENTIFIER);
7
8   
9 %inline 
10 {
11
12   void globalscope(); // forward declaration needed for some compilers
13
14   struct A;
15   struct B
16   {
17     B(int i) : v(i) 
18     {
19     }
20     
21     friend void ::globalscope();
22     friend int mix(A* a, B *b);
23     virtual ~B()
24     {
25     }
26     
27   private:
28     int v;
29     
30   };
31   
32   void globalscope() { B b(0); b.v=10; }
33   
34   struct A
35   {
36     A(int v) : val(v)
37     {
38     }
39
40     friend int get_val1(const A& a)
41     {
42       return a.val;
43     }
44
45     /* simple overloading */
46     friend int get_val1(const A& a, int o)
47     {
48       return a.val + o;
49     }
50
51     /*
52       note that operators << and >> are ignored, as they
53       should, since no rename is performed.
54     */
55     friend std::istream& operator>>(std::istream& in, A& a);
56
57     /* already declare at B */
58     friend int mix(A* a, B *b);
59
60   protected:
61     friend int get_val2(const A& a)
62     {
63       return a.val*2;
64     }
65     
66   private:
67     friend int get_val3(const A& a);
68
69     /* this should be ignored */
70     friend std::ostream& operator<<(std::ostream& out, const A& a)
71     {
72       out << a.val;
73       return out;
74     }
75
76     int val;    
77   };
78
79   /* 
80      'mix' is an interesting case, this is the third declaration
81      swig is getting (two friends + one inline).
82    */
83   inline int mix(A* a, B *b) {
84     return a->val + b->v;
85   }
86
87   /* this should be ignored */
88   inline std::istream& operator>>(std::istream& in, A& a) {
89     int v;
90     in >> v;
91     a = A(v);
92     return in;
93   }
94
95   inline int get_val3(const A& a) {
96     return a.val*3;
97   }
98   
99   /* another overloading */
100   inline int get_val1(int i, int a, int b) {
101     return i;
102   }
103
104
105   /*
106     sit and watch how well this case works, is just incredible!!,
107
108     also note that there is no special code added to manage friends
109     and templates (or overloading), this is just old swig magic
110     working at its best.
111   */
112
113   template <class C>
114     struct D
115     {
116       D(C v) : val(v) {}
117
118       /* note that here we are overloading the already super
119          overloaded 'get_val1' */
120       friend C get_val1(D& b)
121       {
122         return b.val;
123       }
124
125       /* here set will be 'auto' overloaded, depending of the
126          %template instantiations. */
127       friend void set(D& b, C v)
128       {
129         b.val = v;
130       }
131
132     private:
133       C val;
134     };
135
136   namespace ns1 {
137
138     void bas() {}
139
140     void baz() {}
141   }
142 }
143
144 // Use this version with extra qualifiers to test SWIG as some compilers accept this
145   namespace ns1 {
146     namespace ns2 {
147       class Foo {
148       public:
149         Foo::Foo() {};
150         friend void bar();
151         friend void ns1::baz(); 
152         void Foo::member() { }
153         
154       };
155       void bar() {}    
156     }
157   }
158
159 // Remove extra qualifiers for the compiler as some compilers won't compile the extra qaulification (eg gcc-4.1 onwards) 
160 %{
161   namespace ns1 {
162     namespace ns2 {
163       class Foo {
164       public:
165         Foo() {};
166         friend void bar();
167         friend void ns1::baz(); 
168         void member() { }
169         
170       };
171       void bar() {}    
172     }
173   }
174 %}
175     
176
177 %template(D_i) D<int>;
178 %template(D_d) D<double>;