New example
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@736 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
a59ad4b201
commit
d47e6a185c
7 changed files with 82 additions and 22 deletions
|
|
@ -16,6 +16,8 @@ The following examples illustrate the use of SWIG with Tcl.
|
||||||
be used to wrap a C function and a global variable.
|
be used to wrap a C function and a global variable.
|
||||||
<li><a href="constants/index.html">constants</a>. This shows how preprocessor macros and
|
<li><a href="constants/index.html">constants</a>. This shows how preprocessor macros and
|
||||||
certain C declarations are turned into constants.
|
certain C declarations are turned into constants.
|
||||||
|
<li><a href="variables/index.html">variables</a>. How SWIG can be used to wrap C global variables.
|
||||||
|
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
<h2>Compilation Issues</h2>
|
<h2>Compilation Issues</h2>
|
||||||
|
|
|
||||||
|
|
@ -3,3 +3,5 @@
|
||||||
|
|
||||||
extern int gcd(int x, int y);
|
extern int gcd(int x, int y);
|
||||||
extern double Foo;
|
extern double Foo;
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include "example.h"
|
||||||
|
|
||||||
int ivar = 0;
|
int ivar = 0;
|
||||||
short svar = 0;
|
short svar = 0;
|
||||||
|
|
@ -17,8 +18,15 @@ char cvar = 0;
|
||||||
float fvar = 0;
|
float fvar = 0;
|
||||||
double dvar = 0;
|
double dvar = 0;
|
||||||
char *strvar = 0;
|
char *strvar = 0;
|
||||||
|
const char *cstrvar = 0;
|
||||||
int *iptrvar = 0;
|
int *iptrvar = 0;
|
||||||
char name[256] = "Dave";
|
char name[256] = "Dave";
|
||||||
|
char path[256] = "/home/beazley";
|
||||||
|
|
||||||
|
|
||||||
|
/* Global variables involving a structure */
|
||||||
|
Point *ptptr = 0;
|
||||||
|
Point pt = { 10, 20 };
|
||||||
|
|
||||||
/* A variable that we will make read-only in the interface */
|
/* A variable that we will make read-only in the interface */
|
||||||
int status = 1;
|
int status = 1;
|
||||||
|
|
@ -38,8 +46,11 @@ void print_vars() {
|
||||||
printf("dvar = %g\n", dvar);
|
printf("dvar = %g\n", dvar);
|
||||||
printf("cvar = %c\n", cvar);
|
printf("cvar = %c\n", cvar);
|
||||||
printf("strvar = %s\n", strvar ? strvar : "(null)");
|
printf("strvar = %s\n", strvar ? strvar : "(null)");
|
||||||
|
printf("cstrvar = %s\n", cstrvar ? cstrvar : "(null)");
|
||||||
printf("iptrvar = %x\n", iptrvar);
|
printf("iptrvar = %x\n", iptrvar);
|
||||||
printf("name = %s\n", name);
|
printf("name = %s\n", name);
|
||||||
|
printf("ptptr = %x (%d, %d)\n", ptptr, ptptr ? ptptr->x : 0, ptptr ? ptptr->y : 0);
|
||||||
|
printf("pt = (%d, %d)\n", pt.x, pt.y);
|
||||||
printf("status = %d\n", status);
|
printf("status = %d\n", status);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -51,4 +62,25 @@ int *new_int(int value) {
|
||||||
return ip;
|
return ip;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* A function to create a point */
|
||||||
|
|
||||||
|
Point *new_Point(int x, int y) {
|
||||||
|
Point *p = (Point *) malloc(sizeof(Point));
|
||||||
|
p->x = x;
|
||||||
|
p->y = y;
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
|
||||||
|
char * Point_print(Point *p) {
|
||||||
|
static char buffer[256];
|
||||||
|
if (p) {
|
||||||
|
sprintf(buffer,"(%d,%d)", p->x,p->y);
|
||||||
|
} else {
|
||||||
|
sprintf(buffer,"null");
|
||||||
|
}
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
void pt_print() {
|
||||||
|
printf("(%d, %d)\n", pt.x, pt.y);
|
||||||
|
}
|
||||||
|
|
|
||||||
6
SWIG/Examples/tcl/variables/example.h
Normal file
6
SWIG/Examples/tcl/variables/example.h
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
/* File: example.h */
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
int x,y;
|
||||||
|
} Point;
|
||||||
|
|
||||||
|
|
@ -1,5 +1,8 @@
|
||||||
/* File : example.i */
|
/* File : example.i */
|
||||||
%module example
|
%module example
|
||||||
|
%{
|
||||||
|
#include "example.h"
|
||||||
|
%}
|
||||||
|
|
||||||
/* Some global variable declarations */
|
/* Some global variable declarations */
|
||||||
extern int ivar;
|
extern int ivar;
|
||||||
|
|
@ -14,18 +17,28 @@ extern char cvar;
|
||||||
extern float fvar;
|
extern float fvar;
|
||||||
extern double dvar;
|
extern double dvar;
|
||||||
extern char *strvar;
|
extern char *strvar;
|
||||||
|
extern const char *cstrvar;
|
||||||
extern int *iptrvar;
|
extern int *iptrvar;
|
||||||
extern char name[256];
|
extern char name[256];
|
||||||
|
|
||||||
|
extern Point *ptptr;
|
||||||
|
extern Point pt;
|
||||||
|
|
||||||
|
|
||||||
|
/* Some read-only variables */
|
||||||
|
|
||||||
|
%readonly
|
||||||
|
extern int status;
|
||||||
|
extern char path[256];
|
||||||
|
%readwrite
|
||||||
|
|
||||||
/* Some helper functions to make it easier to test */
|
/* Some helper functions to make it easier to test */
|
||||||
extern void print_vars();
|
extern void print_vars();
|
||||||
extern int *new_int(int value);
|
extern int *new_int(int value);
|
||||||
|
extern Point *new_Point(int x, int y);
|
||||||
/* A read-only variable */
|
extern char *Point_print(Point *p);
|
||||||
|
extern void pt_print();
|
||||||
%readonly
|
|
||||||
extern int status;
|
|
||||||
%readwrite
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,10 @@ set cvar "S"
|
||||||
set fvar 3.14159
|
set fvar 3.14159
|
||||||
set dvar 2.1828
|
set dvar 2.1828
|
||||||
set strvar "Hello World"
|
set strvar "Hello World"
|
||||||
|
set cstrvar "Goodbye"
|
||||||
set iptrvar [new_int 37]
|
set iptrvar [new_int 37]
|
||||||
|
set ptptr [new_Point 37 42]
|
||||||
|
set name "Bill"
|
||||||
|
|
||||||
# Now print out the values of the variables
|
# Now print out the values of the variables
|
||||||
|
|
||||||
|
|
@ -35,8 +38,11 @@ puts "fvar = $fvar"
|
||||||
puts "dvar = $dvar"
|
puts "dvar = $dvar"
|
||||||
puts "cvar = $cvar"
|
puts "cvar = $cvar"
|
||||||
puts "strvar = $strvar"
|
puts "strvar = $strvar"
|
||||||
|
puts "cstrvar = $cstrvar"
|
||||||
puts "iptrvar = $iptrvar"
|
puts "iptrvar = $iptrvar"
|
||||||
puts "name = $name"
|
puts "name = $name"
|
||||||
|
puts "ptptr = $ptptr [Point_print $ptptr]"
|
||||||
|
puts "pt = $pt [Point_print $pt]"
|
||||||
|
|
||||||
puts "\nVariables (values printed from C)"
|
puts "\nVariables (values printed from C)"
|
||||||
|
|
||||||
|
|
@ -44,9 +50,9 @@ print_vars
|
||||||
|
|
||||||
puts "\nNow I'm going to try and modify some read only variables";
|
puts "\nNow I'm going to try and modify some read only variables";
|
||||||
|
|
||||||
puts " Tring to set 'name'";
|
puts " Tring to set 'path'";
|
||||||
if { [catch {
|
if { [catch {
|
||||||
set name "Whoa!"
|
set path "Whoa!"
|
||||||
puts "Hey, what's going on?!?! This shouldn't work"
|
puts "Hey, what's going on?!?! This shouldn't work"
|
||||||
}]} {
|
}]} {
|
||||||
puts "Good."
|
puts "Good."
|
||||||
|
|
@ -60,4 +66,13 @@ if { [catch {
|
||||||
puts "Good."
|
puts "Good."
|
||||||
}
|
}
|
||||||
|
|
||||||
|
puts "\nI'm going to try and update a structure variable.\n"
|
||||||
|
|
||||||
|
set pt $ptptr
|
||||||
|
|
||||||
|
puts "The new value is"
|
||||||
|
pt_print
|
||||||
|
puts "You should see the value [Point_print $ptptr]"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -39,20 +39,10 @@ string. However, whenever the value of such a variable is set from Tcl, the ol
|
||||||
value is destroyed using <tt>free()</tt> or <tt>delete</tt> (the choice of which depends
|
value is destroyed using <tt>free()</tt> or <tt>delete</tt> (the choice of which depends
|
||||||
on whether or not SWIG was run with the -c++ option).
|
on whether or not SWIG was run with the -c++ option).
|
||||||
<li><tt>signed char</tt> and <tt>unsigned char</tt> are handled as small 8-bit integers.
|
<li><tt>signed char</tt> and <tt>unsigned char</tt> are handled as small 8-bit integers.
|
||||||
<li>Array variables such as '<tt>char name[256]</tt>' are read-only variables because
|
<li>String array variables such as '<tt>char name[256]</tt>' are managed as Tcl strings, but
|
||||||
SWIG doesn't really know how to change the "value" of an array. You can work
|
when setting the value, the result is truncated to the maximum length of the array. Furthermore, the string is assumed to be null-terminated.
|
||||||
around this by writing some kind of helper function in the SWIG interface.
|
<li>When structures and classes are used as global variables, they are mapped into pointers.
|
||||||
For example:
|
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.
|
||||||
|
|
||||||
<blockquote>
|
|
||||||
<pre>
|
|
||||||
%inline %{
|
|
||||||
void set_name(char *newname) {
|
|
||||||
strncpy(name,newname,256);
|
|
||||||
}
|
|
||||||
%}
|
|
||||||
</pre>
|
|
||||||
</blockquote>
|
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
<h2>Creating read-only variables</h2>
|
<h2>Creating read-only variables</h2>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue