PageRenderTime 29ms CodeModel.GetById 19ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 1ms

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

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