PageRenderTime 34ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/Examples/tcl/variables/index.html

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