/tags/rel-1-3-24/SWIG/Examples/tcl/variables/index.html

# · HTML · 78 lines · 63 code · 15 blank · 0 comment · 0 complexity · 018de4f4552a9976faace3a80d6a6036 MD5 · raw file

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