summaryrefslogtreecommitdiff
path: root/Examples/test-suite/friends.i
blob: 1ba1e5e17e5eea09f28d09bfc27b6bb1bf255029 (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
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
%module friends
%{
#include <iostream>
%}

%warnfilter(SWIGWARN_LANG_IDENTIFIER);

#if defined(SWIGOCTAVE)
%warnfilter(SWIGWARN_IGNORE_OPERATOR_LSHIFT_MSG) operator<<;
%warnfilter(SWIGWARN_IGNORE_OPERATOR_RSHIFT_MSG) operator>>;
#endif


%inline 
{

  void globalscope(); // forward declaration needed for some compilers

  struct A;
  struct B
  {
    B(int i) : v(i) 
    {
    }
    
    friend void ::globalscope();
    friend int mix(A* a, B *b);
    virtual ~B()
    {
    }
    
  private:
    int v;
    
  };
  
  void globalscope() { B b(0); b.v=10; }
  
  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;
    };

  namespace ns1 {

    void bas() {}

    void baz() {}
  }
}

// Use this version with extra qualifiers to test SWIG as some compilers accept this
  namespace ns1 {
    namespace ns2 {
      class Foo {
      public:
	Foo::Foo() {};
	friend void bar();
	friend void ns1::baz();	
	void Foo::member() { }
	
      };
      void bar() {}    
    }
  }

// Remove extra qualifiers for the compiler as some compilers won't compile the extra qaulification (eg gcc-4.1 onwards) 
%{
  namespace ns1 {
    namespace ns2 {
      class Foo {
      public:
	Foo() {};
	friend void bar();
	friend void ns1::baz();	
	void member() { }
	
      };
      void bar() {}    
    }
  }
%}
    

%template(D_i) D<int>;
%template(D_d) D<double>;