New example
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@743 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
3621fe789f
commit
b8c6298c42
7 changed files with 285 additions and 0 deletions
|
|
@ -16,6 +16,7 @@ The following examples illustrate the use of SWIG with Perl.
|
|||
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
|
||||
certain C declarations are turned into constants.
|
||||
<li><a href="variables/index.html">variables</a>. This example shows how to access C global variables from Perl.
|
||||
</ul>
|
||||
|
||||
<h2>Compilation Issues</h2>
|
||||
|
|
|
|||
18
Examples/perl5/variables/Makefile
Normal file
18
Examples/perl5/variables/Makefile
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../swig
|
||||
SRCS = example.c
|
||||
TARGET = example
|
||||
INTERFACE = example.i
|
||||
SWIGOPT =
|
||||
all::
|
||||
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' perl5
|
||||
|
||||
static::
|
||||
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='myperl' INTERFACE='$(INTERFACE)' perl5_static
|
||||
|
||||
clean::
|
||||
rm -f *_wrap* *.o core *~ *.so *.pm myperl
|
||||
|
||||
check: all
|
||||
86
Examples/perl5/variables/example.c
Normal file
86
Examples/perl5/variables/example.c
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
/* File : example.c */
|
||||
|
||||
/* I'm a file containing some C global variables */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "example.h"
|
||||
|
||||
int ivar = 0;
|
||||
short svar = 0;
|
||||
long lvar = 0;
|
||||
unsigned int uivar = 0;
|
||||
unsigned short usvar = 0;
|
||||
unsigned long ulvar = 0;
|
||||
signed char scvar = 0;
|
||||
unsigned char ucvar = 0;
|
||||
char cvar = 0;
|
||||
float fvar = 0;
|
||||
double dvar = 0;
|
||||
char *strvar = 0;
|
||||
const char *cstrvar = 0;
|
||||
int *iptrvar = 0;
|
||||
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 */
|
||||
int status = 1;
|
||||
|
||||
/* A debugging function to print out their values */
|
||||
|
||||
void print_vars() {
|
||||
printf("ivar = %d\n", ivar);
|
||||
printf("svar = %d\n", svar);
|
||||
printf("lvar = %ld\n", lvar);
|
||||
printf("uivar = %u\n", uivar);
|
||||
printf("usvar = %u\n", usvar);
|
||||
printf("ulvar = %lu\n", ulvar);
|
||||
printf("scvar = %d\n", scvar);
|
||||
printf("ucvar = %u\n", ucvar);
|
||||
printf("fvar = %g\n", fvar);
|
||||
printf("dvar = %g\n", dvar);
|
||||
printf("cvar = %c\n", cvar);
|
||||
printf("strvar = %s\n", strvar ? strvar : "(null)");
|
||||
printf("cstrvar = %s\n", cstrvar ? cstrvar : "(null)");
|
||||
printf("iptrvar = %x\n", iptrvar);
|
||||
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);
|
||||
}
|
||||
|
||||
/* A function to create an integer (to test iptrvar) */
|
||||
|
||||
int *new_int(int value) {
|
||||
int *ip = (int *) malloc(sizeof(int));
|
||||
*ip = value;
|
||||
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
Examples/perl5/variables/example.h
Normal file
6
Examples/perl5/variables/example.h
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
/* File: example.h */
|
||||
|
||||
typedef struct {
|
||||
int x,y;
|
||||
} Point;
|
||||
|
||||
44
Examples/perl5/variables/example.i
Normal file
44
Examples/perl5/variables/example.i
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
/* File : example.i */
|
||||
%module example
|
||||
%{
|
||||
#include "example.h"
|
||||
%}
|
||||
|
||||
/* Some global variable declarations */
|
||||
extern int ivar;
|
||||
extern short svar;
|
||||
extern long lvar;
|
||||
extern unsigned int uivar;
|
||||
extern unsigned short usvar;
|
||||
extern unsigned long ulvar;
|
||||
extern signed char scvar;
|
||||
extern unsigned char ucvar;
|
||||
extern char cvar;
|
||||
extern float fvar;
|
||||
extern double dvar;
|
||||
extern char *strvar;
|
||||
extern const char *cstrvar;
|
||||
extern int *iptrvar;
|
||||
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 */
|
||||
extern void print_vars();
|
||||
extern int *new_int(int value);
|
||||
extern Point *new_Point(int x, int y);
|
||||
extern char *Point_print(Point *p);
|
||||
extern void pt_print();
|
||||
|
||||
|
||||
|
||||
|
||||
65
Examples/perl5/variables/example.pl
Normal file
65
Examples/perl5/variables/example.pl
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
# file: example.pl
|
||||
|
||||
use example;
|
||||
|
||||
# Try to set the values of some global variables
|
||||
|
||||
$example::ivar = 42;
|
||||
$example::svar = -31000;
|
||||
$example::lvar = 65537;
|
||||
$example::uivar = 123456;
|
||||
$example::usvar = 61000;
|
||||
$example::ulvar = 654321;
|
||||
$example::scvar = -13;
|
||||
$example::ucvar = 251;
|
||||
$example::cvar = "S";
|
||||
$example::fvar = 3.14159;
|
||||
$example::dvar = 2.1828;
|
||||
$example::strvar = "Hello World";
|
||||
$example::cstrvar = "Goodbye";
|
||||
$example::iptrvar= example::new_int(37);
|
||||
$example::ptptr = example::new_Point(37,42);
|
||||
$example::name = "Bill";
|
||||
|
||||
# Now print out the values of the variables
|
||||
|
||||
print "Variables (values printed from Python)";
|
||||
|
||||
print "ivar = $example::ivar\n";
|
||||
print "svar = $example::svar\n";
|
||||
print "lvar = $example::lvar\n";
|
||||
print "uivar = $example::uivar\n";
|
||||
print "usvar = $example::usvar\n";
|
||||
print "ulvar = $example::ulvar\n";
|
||||
print "scvar = $example::scvar\n";
|
||||
print "ucvar = $example::ucvar\n";
|
||||
print "fvar = $example::fvar\n";
|
||||
print "dvar = $example::dvar\n";
|
||||
print "cvar = $example::cvar\n";
|
||||
print "strvar = $example::strvar\n";
|
||||
print "cstrvar = $example::cstrvar\n";
|
||||
print "iptrvar = $example::iptrvar\n";
|
||||
print "name = $example::name\n";
|
||||
print "ptptr = $example::ptptr", example::Point_print($example::ptptr), "\n";
|
||||
print "pt = $example::pt", example::Point_print($example::pt), "\n";
|
||||
|
||||
print "\nVariables (values printed from C)\n";
|
||||
|
||||
example::print_vars();
|
||||
|
||||
print "\nI'm going to try and update a structure variable.\n";
|
||||
|
||||
$example::pt = $example::ptptr;
|
||||
|
||||
print "The new value is ";
|
||||
example::pt_print();
|
||||
print "You should see the value", example::Point_print($example::ptptr), "\n";
|
||||
|
||||
|
||||
print "\nNow I'm going to try and modify some read only variables\n";
|
||||
|
||||
print " Tring to set 'path'\n";
|
||||
$example::path = "Whoa!";
|
||||
|
||||
print " Trying to set 'status'\n";
|
||||
$example::status = 0;
|
||||
65
Examples/perl5/variables/index.html
Normal file
65
Examples/perl5/variables/index.html
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
<html>
|
||||
<head>
|
||||
<title>SWIG:Examples:perl5:variables</title>
|
||||
</head>
|
||||
|
||||
<body bgcolor="#ffffff">
|
||||
|
||||
<tt>SWIG/Examples/perl5/variables/</tt>
|
||||
<hr>
|
||||
|
||||
<H2>Wrapping C Global Variables</H2>
|
||||
|
||||
<tt>$Header$</tt><br>
|
||||
|
||||
<p>
|
||||
When a C global variable appears in an interface file, SWIG tries to
|
||||
wrap it using a technique known as "variable linking." The idea is
|
||||
pretty simple---we try to create a Perl variable that magically
|
||||
retrieves or updates the value of the underlying C variable when it is
|
||||
accessed. Click <a href="example.i">here</a> to see a SWIG interface with some variable
|
||||
declarations in it.
|
||||
|
||||
<h2>Manipulating Variables from Perl</h2>
|
||||
|
||||
Accessing a C global variable from Perl is easy---just reference it like a normal Perl variable.
|
||||
Click <a href="example.pl">here</a> to see a script that updates and prints some global variables.
|
||||
|
||||
<h2>Creating read-only variables</h2>
|
||||
|
||||
The <tt>%readonly</tt> and <tt>%readwrite</tt> directives can be used to
|
||||
specify a collection of read-only variables. For example:
|
||||
|
||||
<blockquote>
|
||||
<pre>
|
||||
%readonly
|
||||
int status;
|
||||
double blah;
|
||||
...
|
||||
%readwrite
|
||||
</pre>
|
||||
</blockquote>
|
||||
|
||||
The <tt>%readonly</tt> directive remains in effect until it is explicitly disabled
|
||||
using the <tt>%readwrite</tt> directive.
|
||||
|
||||
|
||||
<h2>Notes:</h2>
|
||||
|
||||
<ul>
|
||||
<li>When a global variable has the type "<tt>char *</tt>", SWIG manages it as a character
|
||||
string. However, whenever the value of such a variable is set from Perl, the old
|
||||
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).
|
||||
<li><tt>signed char</tt> and <tt>unsigned char</tt> are handled as small 8-bit integers.
|
||||
<li>String array variables such as '<tt>char name[256]</tt>' are managed as Perl strings, but
|
||||
when setting the value, the result is truncated to the maximum length of the array. Furthermore, the string is assumed to be null-terminated.
|
||||
<li>When structures and classes are used as global variables, they are mapped into pointers.
|
||||
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.
|
||||
<li>Variables are linked using Perl's magic mechanism. Take a look at the Advanced Perl Programming book to
|
||||
find out more about this feature.
|
||||
</ul>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
<hr>
|
||||
Loading…
Add table
Add a link
Reference in a new issue