/trunk/Examples/tcl/variables/index.html
HTML | 76 lines | 62 code | 14 blank | 0 comment | 0 complexity | 2920f0dc1a52da19faf337ac38957d3c MD5 | raw file
1<html> 2<head> 3<title>SWIG:Examples:tcl:variables</title> 4</head> 5 6<body bgcolor="#ffffff"> 7 8<tt>SWIG/Examples/tcl/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 wrap it using a technique 15known as "variable linking." The idea is pretty simple---we try to create a Tcl 16variable that works exactly like you would expect in a Tcl script, but which magically 17retrieves or updates the value of the underlying C variable. 18Click <a href="example.i">here</a> to see a SWIG interface with some variable declarations in it. 19 20<h2>Manipulating Variables from Tcl</h2> 21 22Click <a href="runme.tcl">here</a> to see a script that updates and prints out the values of 23the variables defined in the above file. Notice how the C global variables work just 24like normal Tcl variables. 25 26<h2>Key points</h2> 27 28<ul> 29<li>The <tt>set</tt> statement changes the value of the corresponding C global variable. 30<li>Whenever you access the value of a variable such as <tt>$ivar</tt>, the value 31of the C global variable is read. 32<li>If a C program changes a global variable independently of Tcl, this change is 33automatically reflected in the Tcl variable (i.e., reads will always return the 34most up to date value of the variable). 35<li>When a global variable has the type "<tt>char *</tt>", SWIG manages it as a character 36string. However, whenever the value of such a variable is set from Tcl, the old 37value is destroyed using <tt>free()</tt> or <tt>delete</tt> (the choice of which depends 38on whether or not SWIG was run with the -c++ option). 39<li><tt>signed char</tt> and <tt>unsigned char</tt> are handled as small 8-bit integers. 40<li>String array variables such as '<tt>char name[256]</tt>' are managed as Tcl strings, but 41when setting the value, the result is truncated to the maximum length of the array. Furthermore, the string is assumed to be null-terminated. 42<li>When structures and classes are used as global variables, they are mapped into pointers. 43Getting 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. 44</ul> 45 46<h2>Creating read-only variables</h2> 47 48The <tt>%immutable</tt> and <tt>%mutable</tt> directives can be used to 49specify a collection of read-only variables. For example: 50 51<blockquote> 52<pre> 53%immutable; 54int status; 55double blah; 56... 57%mutable; 58</pre> 59</blockquote> 60 61The <tt>%immutable</tt> directive remains in effect until it is explicitly disabled 62using the <tt>%mutable</tt> directive. 63 64<h2>Comments</h2> 65<ul> 66<li>Management of global variables is one of the most problematic aspects 67of C/C++ wrapping because the scripting interface and resulting memory management 68is much trickier than simply creating a wrapper function. 69<p> 70<li>You may be better off hiding global variables behind a function based 71interface. 72</ul> 73 74</body> 75</html> 76<hr>