File: virtual_poly.i

package info (click to toggle)
swig1.3 1.3.24-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 19,336 kB
  • ctags: 10,604
  • sloc: cpp: 27,917; ansic: 24,160; yacc: 4,412; python: 4,255; java: 4,156; makefile: 3,735; sh: 3,552; cs: 2,250; ruby: 2,150; lisp: 1,605; tcl: 1,136; perl: 980; php: 879; ml: 825
file content (135 lines) | stat: -rw-r--r-- 2,807 bytes parent folder | download
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
%module(directors="1") virtual_poly

%warnfilter(822, 842) copy; /* Java, C# covariant return types */
%warnfilter(822, 842) ref_this; /* Java, C# covariant return types */

//
// 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);
  }
%}