PageRenderTime 27ms CodeModel.GetById 21ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/Examples/ruby/variables/index.html

#
HTML | 94 lines | 77 code | 17 blank | 0 comment | 0 complexity | d7dce4ef4f27f1b3fbc4d35177c28fad MD5 | raw file
 1<html>
 2<head>
 3<title>SWIG:Examples:ruby:variables</title>
 4</head>
 5
 6<body bgcolor="#ffffff">
 7
 8<tt>SWIG/Examples/ruby/variables/</tt>
 9<hr>
10
11<H2>Wrapping C Global Variables</H2>
12
13<p>
14When a C global variable appears in an interface file, SWIG tries to
15wrap it using a technique known as "variable linking."  The idea is
16pretty simple---we try to create a Ruby variable (actually module method) that
17magically retrieves or updates the value of the underlying C variable when it is
18accessed.  Click <a href="example.i">here</a> to see a SWIG interface with some variable
19declarations in it.
20
21<h2>Manipulating Variables from Ruby</h2>
22
23Before going any further, it is important to understand some important
24differences between C and Ruby variables.  In C, a variable is
25simply a name that refers to a specific location in memory.  For
26example, when you declare a global variable '<tt>double a</tt>' you
27know that somewhere in memory, 8 bytes have been set aside to hold a
28<tt>double</tt> and that <tt>a</tt> is bound to this location for the
29life of the program.  In Ruby, variable creation is nothing more
30than a naming operation.  For example, when you say '<tt>a = 3</tt>',
31'a' becomes a name that refers to some object '3'.  Later on, if you say
32'<tt>a = 7.5</tt>, the name 'a' is bound to an entirely different object
33containing the value '7.5' (the contents of the original object are not
34changed).  The end result of this is that a variable in Ruby can refer
35to a virtually unlimited number of different objects (memory locations)
36over the lifetime of a program.
37
38<p>
39Because of Ruby's somewhat unusual variable assignment semantics, it is not
40possible to directly link a C global variable into an equivalent Ruby variable.
41Instead, all C global variables are accessed as attributes of the module.
42For example, if you had a global variable
43
44<blockquote>
45<pre>
46double foo;
47</pre>
48</blockquote>
49
50it will be accessed in the Ruby module as <tt>Example.foo</tt>. Click
51<a href="runme.rb">here</a> to see a script that updates and prints
52out the values of the variables using this technique.
53
54<h2>Key points</h2>
55
56<ul>
57<li>When a global variable has the type "<tt>char *</tt>", SWIG manages it as a character
58string.   However, whenever the value of such a variable is set from Ruby, the old
59value is destroyed using <tt>free()</tt>.
60<li><tt>signed char</tt> and <tt>unsigned char</tt> are handled as small 8-bit integers.
61<li>String array variables such as '<tt>char name[256]</tt>' are managed as Ruby strings, but
62when setting the value, the result is truncated to the maximum length of the array.  Furthermore, the string is assumed to be null-terminated.
63<li>When structures and classes are used as global variables, they are mapped into pointers.
64Getting the "value" returns a pointer to the global variable.  Setting the value of a structure results in a memory copy from a pointer to the global.
65</ul>
66
67<h2>Creating read-only variables</h2>
68
69The <tt>%immutable</tt> and <tt>%mutable</tt> directives can be used to
70specify a collection of read-only variables.  For example:
71
72<blockquote>
73<pre>
74%immutable;
75int    status;
76double blah;
77...
78%mutable;
79</pre>
80</blockquote>
81
82The <tt>%immutable</tt> directive remains in effect until it is explicitly disabled
83using the <tt>%mutable</tt> directive.
84
85<h2>Comments</h2>
86<ul>
87<li>Management of global variables is one of the most problematic aspects 
88of C/C++ wrapping because the scripting interface and resulting memory management
89is much trickier than simply creating a wrapper function.
90</ul>
91
92</body>
93</html>
94<hr>