PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

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