aboutsummaryrefslogtreecommitdiff
path: root/Examples/tcl/variables/index.html
blob: d41ace2a9ab0d5301fb99418dad61141538336a7 (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
<html>
<head>
<title>SWIG:Examples:tcl:variables</title>
</head>

<body bgcolor="#ffffff">

<tt>SWIG/Examples/tcl/variables/</tt>
<hr>

<H2>Wrapping C Global Variables</H2>

<tt>$Header$</tt><br>

<p>
When a C global variable appears in an interface file, SWIG tries to wrap it using a technique
known as "variable linking."  The idea is pretty simple---we try to create a Tcl
variable that works exactly like you would expect in a Tcl script, but which magically 
retrieves or updates the value of the underlying C variable.  
Click <a href="example.i">here</a> to see a SWIG interface with some variable declarations in it.

<h2>Manipulating Variables from Tcl</h2>

Click <a href="runme.tcl">here</a> to see a script that updates and prints out the values of
the variables defined in the above file.  Notice how the C global variables work just
like normal Tcl variables.

<h2>Key points</h2>

<ul>
<li>The <tt>set</tt> statement changes the value of the corresponding C global variable.
<li>Whenever you access the value of a variable such as <tt>$ivar</tt>, the value
of the C global variable is read. 
<li>If a C program changes a global variable independently of Tcl, this change is
automatically reflected in the Tcl variable (i.e., reads will always return the
most up to date value of the variable).
<li>When a global variable has the type "<tt>char *</tt>", SWIG manages it as a character
string.   However, whenever the value of such a variable is set from Tcl, the old
value is destroyed using <tt>free()</tt> or <tt>delete</tt> (the choice of which depends
on whether or not SWIG was run with the -c++ option).
<li><tt>signed char</tt> and <tt>unsigned char</tt> are handled as small 8-bit integers.
<li>String array variables such as '<tt>char name[256]</tt>' are managed as Tcl strings, but
when setting the value, the result is truncated to the maximum length of the array.  Furthermore, the string is assumed to be null-terminated.
<li>When structures and classes are used as global variables, they are mapped into pointers.
Getting 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.
</ul>

<h2>Creating read-only variables</h2>

The <tt>%immutable</tt> and <tt>%mutable</tt> directives can be used to
specify a collection of read-only variables.  For example:

<blockquote>
<pre>
%immutable;
int    status;
double blah;
...
%mutable;
</pre>
</blockquote>

The <tt>%immutable</tt> directive remains in effect until it is explicitly disabled
using the <tt>%mutable</tt> directive.

<h2>Comments</h2>
<ul>
<li>Management of global variables is one of the most problematic aspects 
of C/C++ wrapping because the scripting interface and resulting memory management
is much trickier than simply creating a wrapper function.
<p>
<li>You may be better off hiding global variables behind a function based
interface.
</ul>

</body>
</html>
<hr>