blob: 927c00190769b7ecafa78c38e86aeb29e3004a34 (
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
|
<html>
<head>
<title>SWIG:Examples:ruby:class</title>
</head>
<body bgcolor="#ffffff">
<tt>SWIG/Examples/ruby/class/</tt>
<hr>
<H2>Wrapping a simple C++ class</H2>
<p>
This example illustrates wrapping a simple C++ class to give a Ruby class.
<h2>The C++ Code</h2>
Suppose you have some C++ classes described by the following (and admittedly lame)
header file:
<blockquote>
<pre>
/* File : example.h */
class Shape {
public:
Shape() {
nshapes++;
}
virtual ~Shape() {
nshapes--;
}
double x, y;
void move(double dx, double dy);
virtual double area() = 0;
virtual double perimeter() = 0;
static int nshapes;
};
class Circle : public Shape {
private:
double radius;
public:
Circle(double r) : radius(r) { }
virtual double area();
virtual double perimeter();
};
class Square : public Shape {
private:
double width;
public:
Square(double w) : width(w) { }
virtual double area();
virtual double perimeter();
};
</pre>
</blockquote>
<h2>The SWIG interface</h2>
A simple SWIG interface for this can be built by simply grabbing the header file
like this:
<blockquote>
<pre>
/* File : example.i */
%module example
%{
#include "example.h"
%}
/* Let's just grab the original header file here */
%include "example.h"
</pre>
</blockquote>
Note: when creating a C++ extension, you must run SWIG with the <tt>-c++</tt> option like this:
<blockquote>
<pre>
% swig -c++ -ruby example.i
</pre>
</blockquote>
<h2>A sample Ruby script</h2>
Click <a href="runme.rb">here</a> to see a script that calls the C++ functions from Ruby.
<h2>Key points</h2>
<ul>
<li>To create a new object, you call a constructor like this:
<blockquote>
<pre>
c = Example::Circle.new(10)
</pre>
</blockquote>
<p>
<li>To access member data, a pair of accessor methods are used.
For example:
<blockquote>
<pre>
c.x = 15 # Set member data
x = c.x # Get member data
</pre>
</blockquote>
<p>
<li>To invoke a member function, you simply do this
<blockquote>
<pre>
print "The area is ", c.area, "\n"
</pre>
</blockquote>
<p>
<li>When a instance of Ruby level wrapper class is garbage collected by the
Ruby interpreter, the corresponding C++ destructor is automatically invoked.
<p>
<li>Static member variables are wrapped as Ruby class accessor methods.
For example:
<blockquote>
<pre>
n = Shape.nshapes # Get a static data member
Shapes.nshapes = 13 # Set a static data member
</pre>
</blockquote>
</ul>
<h2>General Comments</h2>
<ul>
<li>Ruby module of SWIG differs from other language modules in wrapping C++
interfaces. They provide lower-level interfaces and optional higher-level
interfaces know as proxy classes. Ruby module needs no such redundancy
due to Ruby's sophisticated extension API.
<li>SWIG <b>does</b> know how to properly perform upcasting of objects in
an inheritance hierarchy except for multiple inheritance.
<li>C++ Namespaces - %nspace isn't yet supported for Ruby.
</ul>
<hr>
</body>
</html>
|