File: friends_operator_overloading.i

package info (click to toggle)
swig 4.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 45,980 kB
  • sloc: cpp: 54,284; ansic: 29,022; java: 17,595; python: 12,734; cs: 10,421; ruby: 7,263; yacc: 6,501; makefile: 5,860; javascript: 5,538; sh: 5,422; perl: 4,246; php: 3,733; ml: 2,198; tcl: 2,015; lisp: 1,448; xml: 115
file content (114 lines) | stat: -rw-r--r-- 3,357 bytes parent folder | download | duplicates (2)
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
%module friends_operator_overloading

// Tests friend operators within a namespace
// Demonstrates how to turn friend operators into member functions (required for some languages - tests includes a Python runtime test)
// Note that by default the friend functions result in global function wrappers (overloaded as there are friends from two different classes)
// Testcase highlighted a compilation problem with Python builtin wrappers
// Tests only the languages that don't ignore operator<<

%warnfilter(SWIGWARN_LANG_IDENTIFIER,                    // Warning 503: Can't wrap 'operator <<' unless renamed to a valid identifier.
            SWIGWARN_IGNORE_OPERATOR_LSHIFT) operator<<; // Warning 373: operator<< ignored

%inline %{
// Remove this define to test the equivalent implementation using member methods instead of friends
#define FRIENDS

// Debugging/tracing using printf
#include <cstdio>
//#define myprintf(a, b) printf(a, b)
#define myprintf(a, b)

namespace shifting {

class ShiftA {
  int val;
public:
  ShiftA(int val = 0) : val(val) {}
#if !defined(FRIENDS)
  ShiftA operator<<(const ShiftA& gd) {
    ShiftA ret(val - gd.getVal());
    myprintf("member operator << (GeoData) %d\n", ret.getVal());
    return ret;
  }
  ShiftA operator<<(int amount) {
    ShiftA ret(val - amount);
    myprintf("member operator << (int) %d\n", ret.getVal());
    return ret;
  }
#else
  friend ShiftA operator<<(const ShiftA& this_, const ShiftA& gd) {
    ShiftA ret(this_.val - gd.getVal());
    myprintf("friend operator << (GeoData) %d\n", ret.getVal());
    return ret;
  }
  friend ShiftA operator<<(const ShiftA& this_, int amount) {
    ShiftA ret(this_.val - amount);
    myprintf("friend operator << (int) %d\n", ret.getVal());
    return ret;
  }
#if defined(SWIG)
%extend {
  ShiftA operator<<(const ShiftA& gd) { return *$self << gd; }
  ShiftA operator<<(int amount) { return *$self << amount; }
}
#endif
#endif
  int getVal() const { return val; }
};

class ShiftB {
  int val;
public:
  ShiftB(int val = 0) : val(val) {}
#if !defined(FRIENDS)
  ShiftB operator<<(const ShiftB& gd) {
    ShiftB ret(val - gd.getVal());
    myprintf("member operator << (GeoData) %d\n", ret.getVal());
    return ret;
  }
  ShiftB operator<<(int amount) {
    ShiftB ret(val - amount);
    myprintf("member operator << (int) %d\n", ret.getVal());
    return ret;
  }
#else
  friend ShiftB operator<<(const ShiftB& this_, const ShiftB& gd) {
    ShiftB ret(this_.val - gd.getVal());
    myprintf("friend operator << (GeoData) %d\n", ret.getVal());
    return ret;
  }
  friend ShiftB operator<<(const ShiftB& this_, int amount) {
    ShiftB ret(this_.val - amount);
    myprintf("friend operator << (int) %d\n", ret.getVal());
    return ret;
  }
#if defined(SWIG)
%extend {
  ShiftB operator<<(const ShiftB& gd) { return *$self << gd; }
  ShiftB operator<<(int amount) { return *$self << amount; }
}
#endif
#endif
  int getVal() const { return val; }
};

void sanity_checker_ShiftA() {
  ShiftA gd1(20);
  ShiftA gd2(100);
  ShiftA gd3(gd2 << gd1);
  myprintf("gd3 %d\n", gd3.getVal());
  ShiftA gd4(gd2 << 30);
  myprintf("gd4 %d\n", gd4.getVal());

}
void sanity_checker_ShiftB() {
  ShiftB gd1(20);
  ShiftB gd2(100);
  ShiftB gd3(gd2 << gd1);
  myprintf("gd3 %d\n", gd3.getVal());
  ShiftB gd4(gd2 << 30);
  myprintf("gd4 %d\n", gd4.getVal());
}

}
%}