Added some missing multi-argument typemaps: (char *STRING, size_t LENGTH) and (char *STRING, int LENGTH) - Java patch is from Volker Grabsch. Elements of the primitive_types.i testcase for this moved into char_binary.i. Documentation for this enhanced.

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@12393 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
William S Fulton 2011-01-14 19:06:43 +00:00
commit ef865e06d5
15 changed files with 194 additions and 37 deletions

View file

@ -4401,6 +4401,49 @@ well suited for applications in which you need to create buffers,
package binary data, etc.
</p>
<H3><a name="Java_binary_char"></a>Binary data vs Strings</H3>
<p>
By default SWIG handles <tt>char *</tt> as a string but there is a handy multi-argument typemap available as mentioned in <a href="Library.html#Library_nn10">Passing binary data</a>.
The following simple example demonstrates using a byte array instead of passing the default string type and length to the wrapped function.
</p>
<div class="code">
<pre>
%apply (char *STRING, size_t LENGTH) { (const char data[], size_t len) }
%inline %{
void binaryChar1(const char data[], size_t len) {
printf("len: %d data: ", len);
for (size_t i=0; i&lt;len; ++i)
printf("%x ", data[i]);
printf("\n");
}
%}
</pre>
</div>
<p>
Calling from Java requires just the byte array to be passed in as the multi-argument typemap being applied reduces the number of arguments in the target language to one, from the original two:
</p>
<div class="code">
<pre>
byte[] data = "hi\0jk".getBytes();
example.binaryChar1(data);
</pre>
</div>
<p>
resulting in the output
</p>
<div class="code"><pre>
$ java runme
len: 5 data: 68 69 0 6a 6b
</pre></div>
<H3><a name="Java_heap_allocations"></a>23.8.5 Overriding new and delete to allocate from Java heap</H3>

View file

@ -824,20 +824,20 @@ If you have a function that expects binary data,
<div class="code">
<pre>
int parity(char *str, int len, int initial);
size_t parity(char *str, size_t len, size_t initial);
</pre>
</div>
<p>
you can wrap the parameters <tt>(char *str, int len)</tt> as a single
you can wrap the parameters <tt>(char *str, size_t len)</tt> as a single
argument using a typemap. Just do this:
</p>
<div class="code">
<pre>
%apply (char *STRING, int LENGTH) { (char *str, int len) };
%apply (char *STRING, size_t LENGTH) { (char *str, size_t len) };
...
int parity(char *str, int len, int initial);
size_t parity(char *str, size_t len, size_t initial);
</pre>
</div>
@ -854,6 +854,7 @@ Now, in the target language, you can use binary string data like this:
<p>
In the wrapper function, the passed string will be expanded to a pointer and length parameter.
The <tt>(char *STRING, int LENGTH)</tt> multi-argument typemap is also available in addition to <tt>(char *STRING, size_t LENGTH)</tt>.
</p>
<H3><a name="Library_nn11"></a>8.3.3 Using %newobject to release memory</H3>