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

/trunk/Examples/java/variables/index.html

#
HTML | 85 lines | 67 code | 18 blank | 0 comment | 0 complexity | da4bbdde321af939b6225fe399400938 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:java:variables</title>
  4. </head>
  5. <body bgcolor="#ffffff">
  6. <tt>SWIG/Examples/java/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 Java variable that magically
  13. 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. <p>Click <a href="../../../Doc/Manual/Java.html#global_variables">here</a> for the section on global variables in the SWIG and Java documentation.</p>
  17. <h2>Manipulating Variables from Java</h2>
  18. C variables are accessed through getters and setters from Java. Unfortunately this is the only way to get current values from variables because it is not possible to overload the dot operator in Java. All global variables are accessible from the module class. For example if the module class is called 'example', the global variable
  19. <blockquote>
  20. <pre>
  21. double foo;
  22. </pre>
  23. </blockquote>
  24. will be accessed in the Java module as
  25. <blockquote>
  26. <pre>
  27. example.get_foo();
  28. example.set_foo(12.3);
  29. </pre>
  30. </blockquote>
  31. Click <a href="runme.java">here</a> to see the example program that updates and prints
  32. out the values of the variables using this technique.
  33. <h2>Key points</h2>
  34. <ul>
  35. <li>When a global variable has the type "<tt>char *</tt>", SWIG manages it as a character
  36. string. However, whenever the value of such a variable is set from Java, the old
  37. value is destroyed using <tt>free()</tt> or <tt>delete</tt> (the choice of which depends
  38. on 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 Java strings, but
  41. when 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.
  43. 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.
  44. </ul>
  45. <h2>Creating read-only variables</h2>
  46. The <tt>%immutable</tt> and <tt>%mutable</tt> directives can be used to
  47. specify a collection of read-only variables. For example:
  48. <blockquote>
  49. <pre>
  50. %immutable;
  51. int status;
  52. double blah;
  53. ...
  54. %mutable;
  55. </pre>
  56. </blockquote>
  57. The <tt>%immutable</tt> directive remains in effect until it is explicitly disabled
  58. using the <tt>%mutable</tt> directive.
  59. <h2>Comments</h2>
  60. <ul>
  61. <li>Management of global variables is one of the most problematic aspects
  62. of C/C++ wrapping because the Java interface and resulting memory management
  63. is much trickier than simply creating a wrapper function.
  64. </ul>
  65. </body>
  66. </html>
  67. <hr>