Merged with recent changes from trunk.
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/gsoc2008-maciekd@11187 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
da5ade3143
commit
8c74fa0f46
703 changed files with 21126 additions and 9266 deletions
17
Examples/php/check.list
Normal file
17
Examples/php/check.list
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
# see top-level Makefile.in
|
||||
# (see also top-level configure.in kludge)
|
||||
class
|
||||
constants
|
||||
cpointer
|
||||
disown
|
||||
enum
|
||||
funcptr
|
||||
overloading
|
||||
pointer
|
||||
pragmas
|
||||
reference
|
||||
proxy
|
||||
simple
|
||||
sync
|
||||
value
|
||||
variables
|
||||
24
Examples/php/class/Makefile
Normal file
24
Examples/php/class/Makefile
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../preinst-swig
|
||||
CXXSRCS = example.cxx
|
||||
TARGET = example
|
||||
INTERFACE = example.i
|
||||
LIBS =
|
||||
SWIGOPT = -noproxy
|
||||
|
||||
all::
|
||||
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' \
|
||||
php_cpp
|
||||
|
||||
static::
|
||||
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='myphp' INTERFACE='$(INTERFACE)' \
|
||||
php_cpp_static
|
||||
|
||||
clean::
|
||||
$(MAKE) -f $(TOP)/Makefile php_clean
|
||||
rm -f $(TARGET).php
|
||||
|
||||
check: all
|
||||
$(MAKE) -f $(TOP)/Makefile php_run
|
||||
39
Examples/php/class/example.cxx
Normal file
39
Examples/php/class/example.cxx
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
/* File : example.c */
|
||||
|
||||
#include "example.h"
|
||||
#include <math.h>
|
||||
#ifndef M_PI
|
||||
# define M_PI 3.14159265358979323846
|
||||
#endif
|
||||
|
||||
int Shape::get_nshapes() {
|
||||
return nshapes;
|
||||
}
|
||||
|
||||
/* Move the shape to a new location */
|
||||
void Shape::move(double dx, double dy) {
|
||||
x += dx;
|
||||
y += dy;
|
||||
}
|
||||
|
||||
int Shape::nshapes = 0;
|
||||
|
||||
void Circle::set_radius( double r ) {
|
||||
radius = r;
|
||||
}
|
||||
|
||||
double Circle::area(void) {
|
||||
return M_PI*radius*radius;
|
||||
}
|
||||
|
||||
double Circle::perimeter(void) {
|
||||
return 2*M_PI*radius;
|
||||
}
|
||||
|
||||
double Square::area(void) {
|
||||
return width*width;
|
||||
}
|
||||
|
||||
double Square::perimeter(void) {
|
||||
return 4*width;
|
||||
}
|
||||
38
Examples/php/class/example.h
Normal file
38
Examples/php/class/example.h
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
/* File : example.h */
|
||||
|
||||
class Shape {
|
||||
public:
|
||||
Shape() {
|
||||
nshapes++;
|
||||
}
|
||||
virtual ~Shape() {
|
||||
nshapes--;
|
||||
}
|
||||
double x, y;
|
||||
void move(double dx, double dy);
|
||||
virtual double area(void) = 0;
|
||||
virtual double perimeter(void) = 0;
|
||||
static int nshapes;
|
||||
static int get_nshapes();
|
||||
};
|
||||
|
||||
class Circle : public Shape {
|
||||
private:
|
||||
double radius;
|
||||
public:
|
||||
Circle(double r) : radius(r) { }
|
||||
~Circle() { }
|
||||
void set_radius( double r );
|
||||
virtual double area(void);
|
||||
virtual double perimeter(void);
|
||||
};
|
||||
|
||||
class Square : public Shape {
|
||||
private:
|
||||
double width;
|
||||
public:
|
||||
Square(double w) : width(w) { }
|
||||
~Square() { }
|
||||
virtual double area(void);
|
||||
virtual double perimeter(void);
|
||||
};
|
||||
10
Examples/php/class/example.i
Normal file
10
Examples/php/class/example.i
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
/* File : example.i */
|
||||
%module example
|
||||
|
||||
%{
|
||||
#include "example.h"
|
||||
%}
|
||||
|
||||
/* Let's just grab the original header file here */
|
||||
%include "example.h"
|
||||
|
||||
64
Examples/php/class/runme.php
Normal file
64
Examples/php/class/runme.php
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
<?php
|
||||
|
||||
# This file illustrates the low-level C++ interface
|
||||
# created by SWIG. In this case, all of our C++ classes
|
||||
# get converted into function calls.
|
||||
|
||||
require("example.php");
|
||||
|
||||
# ----- Object creation -----
|
||||
|
||||
print "Creating some objects:\n";
|
||||
$c = new_Circle(10);
|
||||
print " Created circle $c\n";
|
||||
$s = new_Square(10);
|
||||
print " Created square $s\n";
|
||||
|
||||
# ----- Access a static member -----
|
||||
|
||||
print "\nA total of " . nshapes() . " shapes were created\n";
|
||||
|
||||
# ----- Member data access -----
|
||||
|
||||
# Set the location of the object.
|
||||
# Note: methods in the base class Shape are used since
|
||||
# x and y are defined there.
|
||||
|
||||
Shape_x_set($c, 20);
|
||||
Shape_y_set($c, 30);
|
||||
Shape_x_set($s,-10);
|
||||
Shape_y_set($s,5);
|
||||
|
||||
print "\nHere is their current position:\n";
|
||||
print " Circle = (" . Shape_x_get($c) . "," . Shape_y_get($c) . ")\n";
|
||||
print " Square = (" . Shape_x_get($s) . "," . Shape_y_get($s) . ")\n";
|
||||
|
||||
# ----- Call some methods -----
|
||||
|
||||
# Notice how the Shape_area() and Shape_perimeter() functions really
|
||||
# invoke the appropriate virtual method on each object.
|
||||
print "\nHere are some properties of the shapes:\n";
|
||||
foreach (array($c,$s) as $o) {
|
||||
print " $o\n";
|
||||
print " area = " . Shape_area($o) . "\n";
|
||||
print " perimeter = " . Shape_perimeter($o) . "\n";
|
||||
}
|
||||
|
||||
# ----- Delete everything -----
|
||||
|
||||
print "\nGuess I'll clean up now\n";
|
||||
|
||||
# Note: this invokes the virtual destructor
|
||||
#delete_Shape($c);
|
||||
#delete_Shape($s);
|
||||
$c = NULL;
|
||||
$s = NULL;
|
||||
|
||||
# and don't forget the $o from the for loop above. It still refers to
|
||||
# the square.
|
||||
$o = NULL;
|
||||
|
||||
print nshapes() . " shapes remain\n";
|
||||
print "Goodbye\n";
|
||||
|
||||
?>
|
||||
24
Examples/php/constants/Makefile
Normal file
24
Examples/php/constants/Makefile
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../preinst-swig
|
||||
SRCS =
|
||||
TARGET = example
|
||||
INTERFACE = example.i
|
||||
LIBS =
|
||||
SWIGOPT =
|
||||
|
||||
all::
|
||||
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' \
|
||||
php
|
||||
|
||||
static::
|
||||
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='myphp' INTERFACE='$(INTERFACE)' \
|
||||
php_static
|
||||
|
||||
clean::
|
||||
$(MAKE) -f $(TOP)/Makefile php_clean
|
||||
rm -f $(TARGET).php
|
||||
|
||||
check: all
|
||||
$(MAKE) -f $(TOP)/Makefile php_run
|
||||
26
Examples/php/constants/example.i
Normal file
26
Examples/php/constants/example.i
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
/* File : example.i */
|
||||
%module example
|
||||
|
||||
/* A few preprocessor macros */
|
||||
|
||||
#define ICONST 42
|
||||
#define FCONST 2.1828
|
||||
#define CCONST 'x'
|
||||
#define CCONST2 '\n'
|
||||
#define SCONST "Hello World"
|
||||
#define SCONST2 "\"Hello World\""
|
||||
|
||||
/* This should work just fine */
|
||||
#define EXPR ICONST + 3*(FCONST)
|
||||
|
||||
/* This shouldn't do anything */
|
||||
#define EXTERN extern
|
||||
|
||||
/* Neither should this (BAR isn't defined) */
|
||||
#define FOO (ICONST + BAR)
|
||||
|
||||
/* The following statements also produce constants */
|
||||
%constant int iconst = 37;
|
||||
%constant double fconst = 3.14;
|
||||
|
||||
|
||||
28
Examples/php/constants/runme.php
Normal file
28
Examples/php/constants/runme.php
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
<?php
|
||||
|
||||
require "example.php";
|
||||
|
||||
print "ICONST = " . ICONST . " (should be 42)\n";
|
||||
print "FCONST = " . FCONST . " (should be 2.1828)\n";
|
||||
print "CCONST = " . CCONST . " (should be 'x')\n";
|
||||
print "CCONST2 = " . CCONST2 . " (this should be on a new line)\n";
|
||||
print "SCONST = " . SCONST . " (should be 'Hello World')\n";
|
||||
print "SCONST2 = " . SCONST2 . " (should be '\"Hello World\"')\n";
|
||||
print "EXPR = " . EXPR . " (should be 48.5484)\n";
|
||||
print "iconst = " . iconst . " (should be 37)\n";
|
||||
print "fconst = " . fconst . " (should be 3.14)\n";
|
||||
|
||||
if (EXTERN!="EXTERN") {
|
||||
print "EXTERN = " . EXTERN . " (Arg! This shouldn't print anything)\n";
|
||||
} else {
|
||||
print "EXTERN defaults to 'EXTERN', it probably isn't defined (good)\n";
|
||||
}
|
||||
|
||||
if (FOO!="FOO") {
|
||||
print "FOO = " . FOO . "(Arg! This shouldn't print anything)\n";
|
||||
} else {
|
||||
print "FOO defaults to 'FOO', it probably isn't defined (good)\n";
|
||||
}
|
||||
|
||||
|
||||
?>
|
||||
24
Examples/php/cpointer/Makefile
Normal file
24
Examples/php/cpointer/Makefile
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../preinst-swig
|
||||
SRCS = example.c
|
||||
TARGET = example
|
||||
INTERFACE = example.i
|
||||
LIBS =
|
||||
SWIGOPT =
|
||||
|
||||
all::
|
||||
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' \
|
||||
php
|
||||
|
||||
static::
|
||||
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='myphp' INTERFACE='$(INTERFACE)' \
|
||||
php_static
|
||||
|
||||
clean::
|
||||
$(MAKE) -f $(TOP)/Makefile php_clean
|
||||
rm -f $(TARGET).php
|
||||
|
||||
check: all
|
||||
$(MAKE) -f $(TOP)/Makefile php_run
|
||||
16
Examples/php/cpointer/example.c
Normal file
16
Examples/php/cpointer/example.c
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
/* File : example.c */
|
||||
|
||||
void add(double *x, double *y, double *result) {
|
||||
*result = *x + *y;
|
||||
}
|
||||
|
||||
void sub(int *x, int *y, int *result) {
|
||||
*result = *x - *y;
|
||||
}
|
||||
|
||||
int divide(int n, int d, int *r) {
|
||||
int q;
|
||||
q = n/d;
|
||||
*r = n - q*d;
|
||||
return q;
|
||||
}
|
||||
26
Examples/php/cpointer/example.i
Normal file
26
Examples/php/cpointer/example.i
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
/* File : example.i */
|
||||
%module example
|
||||
|
||||
/* This example illustrates a couple of different techniques
|
||||
for manipulating C pointers */
|
||||
|
||||
/* First we'll use the pointer library */
|
||||
extern void add(int *x, int *y, int *result);
|
||||
|
||||
%include cpointer.i
|
||||
%pointer_functions(int, intp);
|
||||
|
||||
/* Next we'll use some typemaps */
|
||||
|
||||
%include typemaps.i
|
||||
extern void sub(int *INPUT, int *INPUT, int *OUTPUT);
|
||||
|
||||
/* Next we'll use typemaps and the %apply directive */
|
||||
|
||||
//%apply int *OUTPUT { int *r };
|
||||
//extern int divide(int n, int d, int *r);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
45
Examples/php/cpointer/runme.php
Normal file
45
Examples/php/cpointer/runme.php
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
<?php
|
||||
|
||||
require "example.php";
|
||||
|
||||
# First create some objects using the pointer library.
|
||||
|
||||
print "Testing the pointer library\n";
|
||||
|
||||
$a = new_intp();
|
||||
$b = new_intp();
|
||||
$c = new_intp();
|
||||
intp_assign($a,37);
|
||||
intp_assign($b,42);
|
||||
|
||||
print " a = $a\n";
|
||||
print " b = $b\n";
|
||||
print " c = $c\n";
|
||||
|
||||
# Call the add() function wuth some pointers
|
||||
add($a,$b,$c);
|
||||
|
||||
# Now get the result
|
||||
$r = intp_value($c);
|
||||
|
||||
print " 38 + 42 = $r\n";
|
||||
|
||||
# Clean up the pointers
|
||||
delete_intp($a);
|
||||
delete_intp($b);
|
||||
delete_intp($c);
|
||||
|
||||
# Now try the typemap library
|
||||
# This should be much easier. Now how it is no longer
|
||||
# necessary to manufacture pointers.
|
||||
|
||||
print "Trying the typemap library\n";
|
||||
$r = sub(37,42);
|
||||
print " 37 - 42 = $r\n";
|
||||
|
||||
# Now try the version with multiple return values
|
||||
# print "Testing multiple return values\n";
|
||||
# ($q,$r) = divide(42,37);
|
||||
# print " 42/37 = $q remainder $r\n";
|
||||
|
||||
?>
|
||||
24
Examples/php/disown/Makefile
Normal file
24
Examples/php/disown/Makefile
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../preinst-swig
|
||||
CXXSRCS = example.cxx
|
||||
TARGET = example
|
||||
INTERFACE = example.i
|
||||
LIBS =
|
||||
SWIGOPT =
|
||||
|
||||
all::
|
||||
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' \
|
||||
php_cpp
|
||||
|
||||
static::
|
||||
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='myphp' INTERFACE='$(INTERFACE)' \
|
||||
php_cpp_static
|
||||
|
||||
clean::
|
||||
$(MAKE) -f $(TOP)/Makefile php_clean
|
||||
rm -f $(TARGET).php
|
||||
|
||||
check: all
|
||||
$(MAKE) -f $(TOP)/Makefile php_run
|
||||
51
Examples/php/disown/example.cxx
Normal file
51
Examples/php/disown/example.cxx
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
/* File : example.c */
|
||||
|
||||
#include "example.h"
|
||||
#include <math.h>
|
||||
#ifndef M_PI
|
||||
# define M_PI 3.14159265358979323846
|
||||
#endif
|
||||
|
||||
int Shape::get_nshapes() {
|
||||
return nshapes;
|
||||
}
|
||||
|
||||
/* Move the shape to a new location */
|
||||
void Shape::move(double dx, double dy) {
|
||||
x += dx;
|
||||
y += dy;
|
||||
}
|
||||
|
||||
int Shape::nshapes = 0;
|
||||
|
||||
void Circle::set_radius( double r ) {
|
||||
radius = r;
|
||||
}
|
||||
|
||||
double Circle::area(void) {
|
||||
return M_PI*radius*radius;
|
||||
}
|
||||
|
||||
double Circle::perimeter(void) {
|
||||
return 2*M_PI*radius;
|
||||
}
|
||||
|
||||
double Square::area(void) {
|
||||
return width*width;
|
||||
}
|
||||
|
||||
double Square::perimeter(void) {
|
||||
return 4*width;
|
||||
}
|
||||
|
||||
ShapeContainer::~ShapeContainer() {
|
||||
iterator i=shapes.begin();
|
||||
for( iterator i = shapes.begin(); i != shapes.end(); ++i ) {
|
||||
delete *i;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
ShapeContainer::addShape( Shape *s ) {
|
||||
shapes.push_back( s );
|
||||
}
|
||||
50
Examples/php/disown/example.h
Normal file
50
Examples/php/disown/example.h
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
/* File : example.h */
|
||||
|
||||
#include <vector>
|
||||
|
||||
class Shape {
|
||||
public:
|
||||
Shape() {
|
||||
nshapes++;
|
||||
}
|
||||
virtual ~Shape() {
|
||||
nshapes--;
|
||||
};
|
||||
double x, y;
|
||||
void move(double dx, double dy);
|
||||
virtual double area(void) = 0;
|
||||
virtual double perimeter(void) = 0;
|
||||
static int nshapes;
|
||||
static int get_nshapes();
|
||||
};
|
||||
|
||||
class Circle : public Shape {
|
||||
private:
|
||||
double radius;
|
||||
public:
|
||||
Circle(double r) : radius(r) { };
|
||||
~Circle() { };
|
||||
void set_radius( double r );
|
||||
virtual double area(void);
|
||||
virtual double perimeter(void);
|
||||
};
|
||||
|
||||
class Square : public Shape {
|
||||
private:
|
||||
double width;
|
||||
public:
|
||||
Square(double w) : width(w) { };
|
||||
~Square() { }
|
||||
virtual double area(void);
|
||||
virtual double perimeter(void);
|
||||
};
|
||||
|
||||
class ShapeContainer {
|
||||
private:
|
||||
typedef std::vector<Shape*>::iterator iterator;
|
||||
std::vector<Shape*> shapes;
|
||||
public:
|
||||
ShapeContainer() : shapes() {};
|
||||
~ShapeContainer();
|
||||
void addShape( Shape *s );
|
||||
};
|
||||
12
Examples/php/disown/example.i
Normal file
12
Examples/php/disown/example.i
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
/* File : example.i */
|
||||
%module example
|
||||
|
||||
%{
|
||||
#include "example.h"
|
||||
%}
|
||||
|
||||
%apply SWIGTYPE *DISOWN {(Shape *s)};
|
||||
|
||||
/* Let's just grab the original header file here */
|
||||
%include "example.h"
|
||||
|
||||
49
Examples/php/disown/runme.php
Normal file
49
Examples/php/disown/runme.php
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
<?php
|
||||
|
||||
# This file illustrates the low-level C++ interface
|
||||
# created by SWIG. In this case, all of our C++ classes
|
||||
# get converted into function calls.
|
||||
|
||||
require("example.php");
|
||||
|
||||
# ----- Object creation -----
|
||||
|
||||
print "Creating some objects:\n";
|
||||
$c = new Circle(10);
|
||||
print " Created circle $c\n";
|
||||
$s = new Square(10);
|
||||
print " Created square $s\n";
|
||||
|
||||
# ----- Create the ShapeContainer ----
|
||||
|
||||
$container = new ShapeContainer();
|
||||
|
||||
$container->addShape($c);
|
||||
$container->addShape($s);
|
||||
|
||||
# ----- Access a static member -----
|
||||
|
||||
print "\nA total of " . Shape::nshapes() . " shapes were created\n";
|
||||
|
||||
# ----- Delete by the old references -----
|
||||
# This should not truely delete the shapes because they are now owned
|
||||
# by the ShapeContainer.
|
||||
|
||||
print "Delete the old references.";
|
||||
|
||||
# Note: this invokes the virtual destructor
|
||||
$c = NULL;
|
||||
$s = NULL;
|
||||
|
||||
print "\nA total of " . Shape::nshapes() . " shapes remain\n";
|
||||
|
||||
# ----- Delete by the container -----
|
||||
# This should truely delete the shapes
|
||||
|
||||
print "Delete the container.";
|
||||
$container = NULL;
|
||||
print "\nA total of " . Shape::nshapes() . " shapes remain\n";
|
||||
|
||||
print "Goodbye\n";
|
||||
|
||||
?>
|
||||
24
Examples/php/enum/Makefile
Normal file
24
Examples/php/enum/Makefile
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../preinst-swig
|
||||
CXXSRCS = example.cxx
|
||||
TARGET = example
|
||||
INTERFACE = example.i
|
||||
LIBS =
|
||||
SWIGOPT = -noproxy
|
||||
|
||||
all::
|
||||
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' \
|
||||
php_cpp
|
||||
|
||||
static::
|
||||
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='myphp' INTERFACE='$(INTERFACE)' \
|
||||
php_cpp_static
|
||||
|
||||
clean::
|
||||
$(MAKE) -f $(TOP)/Makefile php_clean
|
||||
rm -f $(TARGET).php
|
||||
|
||||
check: all
|
||||
$(MAKE) -f $(TOP)/Makefile php_run
|
||||
37
Examples/php/enum/example.cxx
Normal file
37
Examples/php/enum/example.cxx
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
/* File : example.cxx */
|
||||
|
||||
#include "example.h"
|
||||
#include <stdio.h>
|
||||
|
||||
void Foo::enum_test(speed s) {
|
||||
if (s == IMPULSE) {
|
||||
printf("IMPULSE speed\n");
|
||||
} else if (s == WARP) {
|
||||
printf("WARP speed\n");
|
||||
} else if (s == LUDICROUS) {
|
||||
printf("LUDICROUS speed\n");
|
||||
} else {
|
||||
printf("Unknown speed\n");
|
||||
}
|
||||
}
|
||||
|
||||
void enum_test(color c, Foo::speed s) {
|
||||
if (c == RED) {
|
||||
printf("color = RED, ");
|
||||
} else if (c == BLUE) {
|
||||
printf("color = BLUE, ");
|
||||
} else if (c == GREEN) {
|
||||
printf("color = GREEN, ");
|
||||
} else {
|
||||
printf("color = Unknown color!, ");
|
||||
}
|
||||
if (s == Foo::IMPULSE) {
|
||||
printf("speed = IMPULSE speed\n");
|
||||
} else if (s == Foo::WARP) {
|
||||
printf("speed = WARP speed\n");
|
||||
} else if (s == Foo::LUDICROUS) {
|
||||
printf("speed = LUDICROUS speed\n");
|
||||
} else {
|
||||
printf("speed = Unknown speed!\n");
|
||||
}
|
||||
}
|
||||
13
Examples/php/enum/example.h
Normal file
13
Examples/php/enum/example.h
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
/* File : example.h */
|
||||
|
||||
enum color { RED, BLUE, GREEN };
|
||||
|
||||
class Foo {
|
||||
public:
|
||||
Foo() { }
|
||||
enum speed { IMPULSE, WARP, LUDICROUS };
|
||||
void enum_test(speed s);
|
||||
};
|
||||
|
||||
void enum_test(color c, Foo::speed s);
|
||||
|
||||
12
Examples/php/enum/example.i
Normal file
12
Examples/php/enum/example.i
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
/* File : example.i */
|
||||
%module example
|
||||
|
||||
%{
|
||||
#include "example.h"
|
||||
%}
|
||||
|
||||
|
||||
/* Let's just grab the original header file here */
|
||||
|
||||
%include "example.h"
|
||||
|
||||
32
Examples/php/enum/runme.php
Normal file
32
Examples/php/enum/runme.php
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
<?php
|
||||
|
||||
require "example.php";
|
||||
|
||||
# ----- Object creation -----
|
||||
|
||||
# Print out the value of some enums
|
||||
print "*** color ***";
|
||||
print " RED =" . RED;
|
||||
print " BLUE =" . BLUE;
|
||||
print " GREEN =" . GREEN;
|
||||
|
||||
print "\n*** Foo::speed ***";
|
||||
print " Foo_IMPULSE =" . Foo_IMPULSE;
|
||||
print " Foo_WARP =" . Foo_WARP;
|
||||
print " Foo_LUDICROUS =" . Foo_LUDICROUS;
|
||||
|
||||
print "\nTesting use of enums with functions\n";
|
||||
|
||||
enum_test(RED, Foo_IMPULSE);
|
||||
enum_test(BLUE, Foo_WARP);
|
||||
enum_test(GREEN, Foo_LUDICROUS);
|
||||
enum_test(1234,5678);
|
||||
|
||||
print "\nTesting use of enum with class method\n";
|
||||
$f = new_Foo();
|
||||
|
||||
Foo_enum_test($f,Foo_IMPULSE);
|
||||
Foo_enum_test($f,Foo_WARP);
|
||||
Foo_enum_test($f,Foo_LUDICROUS);
|
||||
|
||||
?>
|
||||
24
Examples/php/funcptr/Makefile
Normal file
24
Examples/php/funcptr/Makefile
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../preinst-swig
|
||||
SRCS = example.c
|
||||
TARGET = example
|
||||
INTERFACE = example.i
|
||||
LIBS =
|
||||
SWIGOPT =
|
||||
|
||||
all::
|
||||
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' \
|
||||
php
|
||||
|
||||
static::
|
||||
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='myphp' INTERFACE='$(INTERFACE)' \
|
||||
php_static
|
||||
|
||||
clean::
|
||||
$(MAKE) -f $(TOP)/Makefile php_clean
|
||||
rm -f $(TARGET).php
|
||||
|
||||
check: all
|
||||
$(MAKE) -f $(TOP)/Makefile php_run
|
||||
17
Examples/php/funcptr/example.c
Normal file
17
Examples/php/funcptr/example.c
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
/* File : example.c */
|
||||
|
||||
int do_op(int a, int b, int (*op)(int,int)) {
|
||||
return (*op)(a,b);
|
||||
}
|
||||
|
||||
int add(int a, int b) {
|
||||
return a+b;
|
||||
}
|
||||
|
||||
int sub(int a, int b) {
|
||||
return a-b;
|
||||
}
|
||||
|
||||
int mul(int a, int b) {
|
||||
return a*b;
|
||||
}
|
||||
7
Examples/php/funcptr/example.h
Normal file
7
Examples/php/funcptr/example.h
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
/* file: example.h */
|
||||
|
||||
extern int do_op(int,int, int (*op)(int,int));
|
||||
extern int add(int,int);
|
||||
extern int sub(int,int);
|
||||
extern int mul(int,int);
|
||||
|
||||
15
Examples/php/funcptr/example.i
Normal file
15
Examples/php/funcptr/example.i
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
/* File : example.i */
|
||||
%module example
|
||||
%{
|
||||
#include "example.h"
|
||||
%}
|
||||
|
||||
/* Wrap a function taking a pointer to a function */
|
||||
extern int do_op(int a, int b, int (*op)(int, int));
|
||||
|
||||
/* Now install a bunch of "ops" as constants */
|
||||
%constant int (*ADD)(int,int) = add;
|
||||
%constant int (*SUB)(int,int) = sub;
|
||||
%constant int (*MUL)(int,int) = mul;
|
||||
|
||||
|
||||
24
Examples/php/funcptr/runme.php
Normal file
24
Examples/php/funcptr/runme.php
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
<?php
|
||||
|
||||
require "example.php";
|
||||
|
||||
$a = 37;
|
||||
$b = 42;
|
||||
|
||||
# Now call our C function with a bunch of callbacks
|
||||
|
||||
print "Trying some C callback functions\n";
|
||||
print " a = $a\n";
|
||||
print " b = $b\n";
|
||||
print " ADD(a,b) = ". do_op($a,$b,ADD)."\n";
|
||||
print " SUB(a,b) = ". do_op($a,$b,SUB)."\n";
|
||||
print " MUL(a,b) = ". do_op($a,$b,MUL)."\n";
|
||||
|
||||
print "Here is what the C callback function objects look like in php\n";
|
||||
print "Using swig style string pointers as we need them registered as constants\n";
|
||||
print " ADD = " . ADD . "\n";
|
||||
print " SUB = " . SUB . "\n";
|
||||
print " MUL = " . MUL . "\n";
|
||||
|
||||
?>
|
||||
|
||||
24
Examples/php/overloading/Makefile
Normal file
24
Examples/php/overloading/Makefile
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../preinst-swig
|
||||
CXXSRCS = example.cxx
|
||||
TARGET = example
|
||||
INTERFACE = example.i
|
||||
LIBS =
|
||||
SWIGOPT =
|
||||
|
||||
all::
|
||||
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' \
|
||||
php_cpp
|
||||
|
||||
static::
|
||||
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='myphp' INTERFACE='$(INTERFACE)' \
|
||||
php_cpp_static
|
||||
|
||||
clean::
|
||||
$(MAKE) -f $(TOP)/Makefile php_clean
|
||||
rm -f $(TARGET).php
|
||||
|
||||
check: all
|
||||
$(MAKE) -f $(TOP)/Makefile php_run
|
||||
55
Examples/php/overloading/example.cxx
Normal file
55
Examples/php/overloading/example.cxx
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
/* File : example.c */
|
||||
|
||||
#include "example.h"
|
||||
#include <math.h>
|
||||
#ifndef M_PI
|
||||
# define M_PI 3.14159265358979323846
|
||||
#endif
|
||||
|
||||
int Shape::get_nshapes() {
|
||||
return nshapes;
|
||||
}
|
||||
|
||||
/* Move the shape to a new location */
|
||||
void Shape::move(double dx, double dy) {
|
||||
x += dx;
|
||||
y += dy;
|
||||
}
|
||||
|
||||
int Shape::nshapes = 0;
|
||||
|
||||
double Circle::area(void) {
|
||||
return M_PI*radius*radius;
|
||||
}
|
||||
|
||||
double Circle::perimeter(void) {
|
||||
return 2*M_PI*radius;
|
||||
}
|
||||
|
||||
double Square::area(void) {
|
||||
return width*width;
|
||||
}
|
||||
|
||||
double Square::perimeter(void) {
|
||||
return 4*width;
|
||||
}
|
||||
|
||||
const char *overloaded(int i) {
|
||||
return "Overloaded with int";
|
||||
}
|
||||
|
||||
const char *overloaded(double d) {
|
||||
return "Overloaded with double";
|
||||
}
|
||||
|
||||
const char *overloaded(const char * str) {
|
||||
return "Overloaded with char *";
|
||||
}
|
||||
|
||||
const char *overloaded( const Circle& ) {
|
||||
return "Overloaded with Circle";
|
||||
}
|
||||
|
||||
const char *overloaded( const Shape& ) {
|
||||
return "Overloaded with Shape";
|
||||
}
|
||||
46
Examples/php/overloading/example.h
Normal file
46
Examples/php/overloading/example.h
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
/* File : example.h */
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
class Shape {
|
||||
public:
|
||||
Shape() {
|
||||
nshapes++;
|
||||
}
|
||||
virtual ~Shape() {
|
||||
nshapes--;
|
||||
}
|
||||
double x, y;
|
||||
void move(double dx, double dy);
|
||||
virtual double area(void) = 0;
|
||||
virtual double perimeter(void) = 0;
|
||||
static int nshapes;
|
||||
static int get_nshapes();
|
||||
};
|
||||
|
||||
class Circle : public Shape {
|
||||
private:
|
||||
double radius;
|
||||
public:
|
||||
Circle(double r) : radius(r) { }
|
||||
~Circle() { }
|
||||
virtual double area(void);
|
||||
virtual double perimeter(void);
|
||||
};
|
||||
|
||||
class Square : public Shape {
|
||||
private:
|
||||
double width;
|
||||
public:
|
||||
Square(double w) : width(w) { }
|
||||
~Square() { }
|
||||
virtual double area(void);
|
||||
virtual double perimeter(void);
|
||||
};
|
||||
|
||||
const char *overloaded( int i );
|
||||
const char *overloaded( double d );
|
||||
const char *overloaded( const char * str );
|
||||
const char *overloaded( const Circle& );
|
||||
const char *overloaded( const Shape& );
|
||||
|
||||
8
Examples/php/overloading/example.i
Normal file
8
Examples/php/overloading/example.i
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
/* File : example.i */
|
||||
%module example
|
||||
|
||||
%{
|
||||
#include "example.h"
|
||||
%}
|
||||
|
||||
%include "example.h"
|
||||
59
Examples/php/overloading/runme.php
Normal file
59
Examples/php/overloading/runme.php
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
<?php
|
||||
|
||||
# This file illustrates the low-level C++ interface
|
||||
# created by SWIG. In this case, all of our C++ classes
|
||||
# get converted into function calls.
|
||||
|
||||
include("example.php");
|
||||
|
||||
# ----- Object creation -----
|
||||
|
||||
print "Creating some objects:\n";
|
||||
$c = new Circle(10);
|
||||
print " Created circle $c\n";
|
||||
$s = new Square(10);
|
||||
print " Created square $s\n";
|
||||
|
||||
# ----- Access a static member -----
|
||||
|
||||
print "\nA total of " . Shape::nshapes() . " shapes were created\n";
|
||||
|
||||
# ----- Member data access -----
|
||||
|
||||
# Set the location of the object.
|
||||
# Note: methods in the base class Shape are used since
|
||||
# x and y are defined there.
|
||||
|
||||
$c->x = 20;
|
||||
$c->y = 30;
|
||||
$s->x = -10;
|
||||
$s->y = 5;
|
||||
|
||||
print "\nHere is their current position:\n";
|
||||
print " Circle = (" . $c->x . "," . $c->y . ")\n";
|
||||
print " Square = (" . $s->x . "," . $s->y . ")\n";
|
||||
|
||||
# ----- Call some methods -----
|
||||
|
||||
print "\nHere are some properties of the shapes:\n";
|
||||
foreach (array(1, 2.1, "quick brown fox", $c, $s) as $o) {
|
||||
print " ".get_class($o)." $o\n";
|
||||
print " overloaded = " . overloaded($o) . "\n";
|
||||
}
|
||||
|
||||
# Need to unset($o) or else we hang on to a reference to the Square object.
|
||||
unset($o);
|
||||
|
||||
# ----- Delete everything -----
|
||||
|
||||
print "\nGuess I'll clean up now\n";
|
||||
|
||||
# Note: this invokes the virtual destructor
|
||||
unset($c);
|
||||
$s = 42;
|
||||
|
||||
print Shape::nshapes() . " shapes remain\n";
|
||||
|
||||
print "Goodbye\n";
|
||||
|
||||
?>
|
||||
24
Examples/php/pointer/Makefile
Normal file
24
Examples/php/pointer/Makefile
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../preinst-swig
|
||||
SRCS = example.c
|
||||
TARGET = example
|
||||
INTERFACE = example.i
|
||||
LIBS =
|
||||
SWIGOPT =
|
||||
|
||||
all::
|
||||
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' \
|
||||
php
|
||||
|
||||
static::
|
||||
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='myphp' INTERFACE='$(INTERFACE)' \
|
||||
php_static
|
||||
|
||||
clean::
|
||||
$(MAKE) -f $(TOP)/Makefile php_clean
|
||||
rm -f $(TARGET).php
|
||||
|
||||
check: all
|
||||
$(MAKE) -f $(TOP)/Makefile php_run
|
||||
16
Examples/php/pointer/example.c
Normal file
16
Examples/php/pointer/example.c
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
/* File : example.c */
|
||||
|
||||
void add(double *x, double *y, double *result) {
|
||||
*result = *x + *y;
|
||||
}
|
||||
|
||||
void sub(int *x, int *y, int *result) {
|
||||
*result = *x - *y;
|
||||
}
|
||||
|
||||
int divide(int n, int d, int *r) {
|
||||
int q;
|
||||
q = n/d;
|
||||
*r = n - q*d;
|
||||
return q;
|
||||
}
|
||||
24
Examples/php/pointer/example.i
Normal file
24
Examples/php/pointer/example.i
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
/* File : example.i */
|
||||
%module example
|
||||
|
||||
/* This example illustrates a couple of different techniques
|
||||
for manipulating C pointers */
|
||||
|
||||
%include phppointers.i
|
||||
/* First we'll use the pointer library */
|
||||
extern void add(double *REF, double *REF, double *REF);
|
||||
|
||||
/* Next we'll use some typemaps */
|
||||
|
||||
%include typemaps.i
|
||||
extern void sub(int *INPUT, int *INPUT, int *OUTPUT);
|
||||
|
||||
/* Next we'll use typemaps and the %apply directive */
|
||||
|
||||
//%apply int *OUTPUT { int *r };
|
||||
//extern int divide(int n, int d, int *r);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
35
Examples/php/pointer/runme.php
Normal file
35
Examples/php/pointer/runme.php
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
<?php
|
||||
|
||||
require "example.php";
|
||||
|
||||
# First create some objects using the pointer library.
|
||||
|
||||
print "Testing the pointer library\n";
|
||||
|
||||
$a = 37.145;
|
||||
$b = 42.555;
|
||||
$c = ""; // $c must be defined and not null.
|
||||
|
||||
print " a = $a\n";
|
||||
print " b = $b\n";
|
||||
print " c = $c\n";
|
||||
|
||||
# Call the add() function wuth some pointers
|
||||
add(&$a,&$b,&$c);
|
||||
|
||||
print " $a + $b = $c\n";
|
||||
|
||||
# Now try the typemap library
|
||||
# This should be much easier. Now how it is no longer
|
||||
# necessary to manufacture pointers.
|
||||
|
||||
print "Trying the typemap library\n";
|
||||
$r = sub(37,42);
|
||||
print " 37 - 42 = $r\n";
|
||||
|
||||
# Now try the version with multiple return values
|
||||
# print "Testing multiple return values\n";
|
||||
# ($q,$r) = divide(42,37);
|
||||
# print " 42/37 = $q remainder $r\n";
|
||||
|
||||
?>
|
||||
24
Examples/php/pragmas/Makefile
Normal file
24
Examples/php/pragmas/Makefile
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../preinst-swig
|
||||
SRCS =
|
||||
TARGET = example
|
||||
INTERFACE = example.i
|
||||
LIBS =
|
||||
SWIGOPT =
|
||||
|
||||
all::
|
||||
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' \
|
||||
php
|
||||
|
||||
static::
|
||||
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='myphp' INTERFACE='$(INTERFACE)' \
|
||||
php_static
|
||||
|
||||
clean::
|
||||
$(MAKE) -f $(TOP)/Makefile php_clean
|
||||
rm -f $(TARGET).php
|
||||
|
||||
check: all
|
||||
$(MAKE) -f $(TOP)/Makefile php_run
|
||||
31
Examples/php/pragmas/example.i
Normal file
31
Examples/php/pragmas/example.i
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
/* File : example.i */
|
||||
%module example
|
||||
|
||||
%init{
|
||||
zend_printf("This was %%init\n");
|
||||
}
|
||||
|
||||
%minit{
|
||||
zend_printf("This was %%minit\n");
|
||||
}
|
||||
|
||||
%mshutdown{
|
||||
zend_printf("This was %%shutdown\n");
|
||||
}
|
||||
|
||||
%rinit{
|
||||
zend_printf("This was %%rinit\n");
|
||||
}
|
||||
|
||||
%rshutdown{
|
||||
zend_printf("This was %%rshutdown\n");
|
||||
}
|
||||
|
||||
%pragma(php) include="include.php";
|
||||
|
||||
%pragma(php) code="
|
||||
# This code is inserted into example.php
|
||||
echo \"this was php code\\n\";
|
||||
"
|
||||
|
||||
%pragma(php) phpinfo="php_info_print_table_start();"
|
||||
7
Examples/php/pragmas/include.php
Normal file
7
Examples/php/pragmas/include.php
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
<?php
|
||||
|
||||
# This code is inserted into example.php
|
||||
echo "this is include.php\n";
|
||||
|
||||
|
||||
?>
|
||||
5
Examples/php/pragmas/runme.php
Executable file
5
Examples/php/pragmas/runme.php
Executable file
|
|
@ -0,0 +1,5 @@
|
|||
<?php
|
||||
|
||||
require "example.php";
|
||||
|
||||
?>
|
||||
24
Examples/php/proxy/Makefile
Normal file
24
Examples/php/proxy/Makefile
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../preinst-swig
|
||||
CXXSRCS = example.cxx
|
||||
TARGET = example
|
||||
INTERFACE = example.i
|
||||
LIBS =
|
||||
SWIGOPT =
|
||||
|
||||
all::
|
||||
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' \
|
||||
php_cpp
|
||||
|
||||
static::
|
||||
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='myphp' INTERFACE='$(INTERFACE)' \
|
||||
php_cpp_static
|
||||
|
||||
clean::
|
||||
$(MAKE) -f $(TOP)/Makefile php_clean
|
||||
rm -f $(TARGET).php
|
||||
|
||||
check: all
|
||||
$(MAKE) -f $(TOP)/Makefile php_run
|
||||
43
Examples/php/proxy/example.cxx
Normal file
43
Examples/php/proxy/example.cxx
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
/* File : example.c */
|
||||
|
||||
#include "example.h"
|
||||
#include <math.h>
|
||||
#ifndef M_PI
|
||||
# define M_PI 3.14159265358979323846
|
||||
#endif
|
||||
|
||||
int Shape::get_nshapes() {
|
||||
return nshapes;
|
||||
}
|
||||
|
||||
/* Move the shape to a new location */
|
||||
void Shape::move(double dx, double dy) {
|
||||
x += dx;
|
||||
y += dy;
|
||||
}
|
||||
|
||||
int Shape::nshapes = 0;
|
||||
|
||||
void Circle::set_radius( double r ) {
|
||||
radius = r;
|
||||
}
|
||||
|
||||
double Circle::area(void) {
|
||||
return M_PI*radius*radius;
|
||||
}
|
||||
|
||||
double Circle::perimeter(void) {
|
||||
return 2*M_PI*radius;
|
||||
}
|
||||
|
||||
double Square::area(void) {
|
||||
return width*width;
|
||||
}
|
||||
|
||||
double Square::perimeter(void) {
|
||||
return 4*width;
|
||||
}
|
||||
|
||||
Circle *CircleFactory( double r ) {
|
||||
return new Circle(r);
|
||||
}
|
||||
43
Examples/php/proxy/example.h
Normal file
43
Examples/php/proxy/example.h
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
/* File : example.h */
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
class Shape {
|
||||
public:
|
||||
Shape() {
|
||||
nshapes++;
|
||||
}
|
||||
virtual ~Shape() {
|
||||
nshapes--;
|
||||
};
|
||||
double x, y;
|
||||
void move(double dx, double dy);
|
||||
virtual double area(void) = 0;
|
||||
virtual double perimeter(void) = 0;
|
||||
static int nshapes;
|
||||
static int get_nshapes();
|
||||
};
|
||||
|
||||
class Circle : public Shape {
|
||||
private:
|
||||
double radius;
|
||||
public:
|
||||
Circle(double r) : radius(r) { };
|
||||
~Circle() { };
|
||||
void set_radius( double r );
|
||||
virtual double area(void);
|
||||
virtual double perimeter(void);
|
||||
};
|
||||
|
||||
class Square : public Shape {
|
||||
private:
|
||||
double width;
|
||||
public:
|
||||
Square(double w) : width(w) { };
|
||||
~Square() { }
|
||||
virtual double area(void);
|
||||
virtual double perimeter(void);
|
||||
};
|
||||
|
||||
Circle *CircleFactory( double r );
|
||||
|
||||
12
Examples/php/proxy/example.i
Normal file
12
Examples/php/proxy/example.i
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
/* File : example.i */
|
||||
%module example
|
||||
|
||||
%{
|
||||
#include "example.h"
|
||||
%}
|
||||
|
||||
/* Let's just grab the original header file here */
|
||||
|
||||
%newobject CircleFactory;
|
||||
%include "example.h"
|
||||
|
||||
68
Examples/php/proxy/runme.php
Normal file
68
Examples/php/proxy/runme.php
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
<?php
|
||||
|
||||
# This file illustrates the low-level C++ interface
|
||||
# created by SWIG. In this case, all of our C++ classes
|
||||
# get converted into function calls.
|
||||
|
||||
include("example.php");
|
||||
|
||||
# ----- Object creation -----
|
||||
|
||||
print "Creating some objects:\n";
|
||||
$c = CircleFactory(10);
|
||||
print " Created circle $c with area ". $c->area() ."\n";
|
||||
$s = new Square(10);
|
||||
print " Created square $s\n";
|
||||
|
||||
# ----- Access a static member -----
|
||||
|
||||
print "\nA total of " . Shape::nshapes() . " shapes were created\n";
|
||||
|
||||
# ----- Member data access -----
|
||||
|
||||
# Set the location of the object.
|
||||
# Note: methods in the base class Shape are used since
|
||||
# x and y are defined there.
|
||||
|
||||
$c->x = 20;
|
||||
$c->y = 30;
|
||||
$s->x = -10;
|
||||
$s->y = 5;
|
||||
|
||||
print "\nHere is their current position:\n";
|
||||
print " Circle = (" . $c->x . "," . $c->y . ")\n";
|
||||
print " Square = (" . $s->x . "," . $s->y . ")\n";
|
||||
|
||||
# ----- Call some methods -----
|
||||
|
||||
print "\nHere are some properties of the shapes:\n";
|
||||
foreach (array($c,$s) as $o) {
|
||||
print " ".get_class($o)." $o\n";
|
||||
print " x = " . $o->x . "\n";
|
||||
print " y = " . $o->y . "\n";
|
||||
print " area = " . $o->area() . "\n";
|
||||
print " perimeter = " . $o->perimeter() . "\n";
|
||||
}
|
||||
|
||||
# Need to unset($o) or else we hang on to a reference to the Square object.
|
||||
unset($o);
|
||||
|
||||
# ----- Delete everything -----
|
||||
|
||||
print "\nGuess I'll clean up now\n";
|
||||
|
||||
# Note: this invokes the virtual destructor
|
||||
unset($c);
|
||||
$s = 42;
|
||||
|
||||
print Shape::nshapes() . " shapes remain\n";
|
||||
|
||||
print "Manually setting nshapes\n";
|
||||
|
||||
Shape::nshapes(42);
|
||||
|
||||
print Shape::get_nshapes() ." == 42\n";
|
||||
|
||||
print "Goodbye\n";
|
||||
|
||||
?>
|
||||
24
Examples/php/reference/Makefile
Normal file
24
Examples/php/reference/Makefile
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../preinst-swig
|
||||
CXXSRCS = example.cxx
|
||||
TARGET = example
|
||||
INTERFACE = example.i
|
||||
LIBS =
|
||||
SWIGOPT = -noproxy
|
||||
|
||||
all::
|
||||
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' \
|
||||
php_cpp
|
||||
|
||||
static::
|
||||
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='myphp' INTERFACE='$(INTERFACE)' \
|
||||
php_cpp_static
|
||||
|
||||
clean::
|
||||
$(MAKE) -f $(TOP)/Makefile php_clean
|
||||
rm -f $(TARGET).php
|
||||
|
||||
check: all
|
||||
$(MAKE) -f $(TOP)/Makefile php_run
|
||||
50
Examples/php/reference/example.cxx
Normal file
50
Examples/php/reference/example.cxx
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
/* File : example.cxx */
|
||||
|
||||
/* Deal with Microsoft's attempt at deprecating C standard runtime functions */
|
||||
#if !defined(SWIG_NO_CRT_SECURE_NO_DEPRECATE) && defined(_MSC_VER)
|
||||
# define _CRT_SECURE_NO_DEPRECATE
|
||||
#endif
|
||||
|
||||
#include "example.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
Vector operator+(const Vector &a, const Vector &b) {
|
||||
Vector r;
|
||||
r.x = a.x + b.x;
|
||||
r.y = a.y + b.y;
|
||||
r.z = a.z + b.z;
|
||||
return r;
|
||||
}
|
||||
|
||||
char *Vector::print() {
|
||||
static char temp[512];
|
||||
sprintf(temp,"Vector %p (%g,%g,%g)", this, x,y,z);
|
||||
return temp;
|
||||
}
|
||||
|
||||
VectorArray::VectorArray(int size) {
|
||||
items = new Vector[size];
|
||||
maxsize = size;
|
||||
printf("VectorArray new: self=%p\n",this);
|
||||
}
|
||||
|
||||
VectorArray::~VectorArray() {
|
||||
printf("VectorArray delete: self=%p\n",this);
|
||||
delete [] items;
|
||||
}
|
||||
|
||||
Vector &VectorArray::operator[](int index) {
|
||||
printf("VectorArray: read[%d] self=%p\n",index,this);
|
||||
if ((index < 0) || (index >= maxsize)) {
|
||||
printf("Panic! Array index out of bounds.\n");
|
||||
exit(1);
|
||||
}
|
||||
return items[index];
|
||||
}
|
||||
|
||||
int VectorArray::size() {
|
||||
printf("VectorArray: size %d self=%p\n",maxsize,this);
|
||||
return maxsize;
|
||||
}
|
||||
|
||||
26
Examples/php/reference/example.h
Normal file
26
Examples/php/reference/example.h
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
/* File : example.h */
|
||||
|
||||
class Vector {
|
||||
private:
|
||||
double x,y,z;
|
||||
public:
|
||||
Vector() : x(0), y(0), z(0) { };
|
||||
Vector(double x, double y, double z) : x(x), y(y), z(z) { };
|
||||
friend Vector operator+(const Vector &a, const Vector &b);
|
||||
char *print();
|
||||
};
|
||||
|
||||
class VectorArray {
|
||||
private:
|
||||
Vector *items;
|
||||
int maxsize;
|
||||
public:
|
||||
VectorArray(int maxsize);
|
||||
~VectorArray();
|
||||
Vector &operator[](int);
|
||||
int size();
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
44
Examples/php/reference/example.i
Normal file
44
Examples/php/reference/example.i
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
/* File : example.i */
|
||||
|
||||
/* This file has a few "typical" uses of C++ references. */
|
||||
|
||||
%module example
|
||||
|
||||
%{
|
||||
#include "example.h"
|
||||
%}
|
||||
|
||||
class Vector {
|
||||
public:
|
||||
Vector(double x, double y, double z);
|
||||
~Vector();
|
||||
char *print();
|
||||
};
|
||||
|
||||
/* This helper function calls an overloaded operator */
|
||||
%inline %{
|
||||
Vector addv(Vector &a, Vector &b) {
|
||||
return a+b;
|
||||
}
|
||||
%}
|
||||
|
||||
/* Wrapper around an array of vectors class */
|
||||
|
||||
class VectorArray {
|
||||
public:
|
||||
VectorArray(int maxsize);
|
||||
~VectorArray();
|
||||
int size();
|
||||
|
||||
/* This wrapper provides an alternative to the [] operator */
|
||||
%extend {
|
||||
Vector &get(int index) {
|
||||
printf("VectorArray extended get: %p %d\n",$self,index);
|
||||
return (*$self)[index];
|
||||
}
|
||||
void set(int index, Vector &a) {
|
||||
(*$self)[index] = a;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
79
Examples/php/reference/runme-proxy.php4
Normal file
79
Examples/php/reference/runme-proxy.php4
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
<?php
|
||||
|
||||
# This file illustrates the manipulation of C++ references in Php.
|
||||
# This uses the low-level interface. Proxy classes work differently.
|
||||
|
||||
require "example.php";
|
||||
|
||||
# ----- Object creation -----
|
||||
|
||||
print "Creating some objects:\n";
|
||||
$a = new Vector(3,4,5);
|
||||
$b = new Vector(10,11,12);
|
||||
|
||||
print " Created a: $a " . $a->print() . "\n";
|
||||
print " Created b: $b " . $b->print() . "\n";
|
||||
|
||||
# ----- Call an overloaded operator -----
|
||||
|
||||
# This calls the wrapper we placed around
|
||||
#
|
||||
# operator+(const Vector &a, const Vector &)
|
||||
#
|
||||
# It returns a new allocated object.
|
||||
|
||||
print "Adding a+b\n";
|
||||
$c = addv($a,$b);
|
||||
print " a+b =". $c->print()."\n";
|
||||
|
||||
# Note: Unless we free the result, a memory leak will occur
|
||||
$c = 0;
|
||||
|
||||
# ----- Create a vector array -----
|
||||
|
||||
# Note: Using the high-level interface here
|
||||
print "Creating an array of vectors\n";
|
||||
$va = new VectorArray(10);
|
||||
|
||||
print " va: $va size=".$va->size()."\n";
|
||||
|
||||
# ----- Set some values in the array -----
|
||||
|
||||
# These operators copy the value of $a and $b to the vector array
|
||||
$va->set(0,$a);
|
||||
$va->set(1,$b);
|
||||
|
||||
$va->get(0);
|
||||
# This will work, but it will cause a memory leak!
|
||||
|
||||
$va->set(2,addv($a,$b));
|
||||
|
||||
# The non-leaky way to do it
|
||||
|
||||
$c = addv($a,$b);
|
||||
$va->set(3,$c);
|
||||
$c = NULL;
|
||||
|
||||
# Get some values from the array
|
||||
|
||||
print "Getting some array values\n";
|
||||
for ($i = 0; $i < 5; $i++) {
|
||||
print "do $i\n";
|
||||
$v = $va->get($i);
|
||||
print " va($i) = ". $v->print(). "\n";
|
||||
}
|
||||
|
||||
# Watch under resource meter to check on this
|
||||
#print "Making sure we don't leak memory.\n";
|
||||
#for ($i = 0; $i < 1000000; $i++) {
|
||||
# $c = VectorArray_get($va,$i % 10);
|
||||
#}
|
||||
|
||||
# ----- Clean up -----
|
||||
print "Cleaning up\n";
|
||||
# wants fixing FIXME
|
||||
#delete_VectorArray($va);
|
||||
#delete_Vector($a);
|
||||
#delete_Vector($b);
|
||||
|
||||
?>
|
||||
78
Examples/php/reference/runme.php
Normal file
78
Examples/php/reference/runme.php
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
<?php
|
||||
|
||||
# This file illustrates the manipulation of C++ references in Php.
|
||||
# This uses the low-level interface. Proxy classes work differently.
|
||||
|
||||
require "example.php";
|
||||
|
||||
# ----- Object creation -----
|
||||
|
||||
print "Creating some objects:\n";
|
||||
$a = new_Vector(3,4,5);
|
||||
$b = new_Vector(10,11,12);
|
||||
|
||||
print " Created a: $a " . Vector_print($a) . "\n";
|
||||
print " Created b: $b " . Vector_print($b) . "\n";
|
||||
|
||||
# ----- Call an overloaded operator -----
|
||||
|
||||
# This calls the wrapper we placed around
|
||||
#
|
||||
# operator+(const Vector &a, const Vector &)
|
||||
#
|
||||
# It returns a new allocated object.
|
||||
|
||||
print "Adding a+b\n";
|
||||
$c = addv($a,$b);
|
||||
print " a+b =". Vector_print($c)."\n";
|
||||
|
||||
# Note: Unless we free the result, a memory leak will occur
|
||||
$c = None;
|
||||
|
||||
# ----- Create a vector array -----
|
||||
|
||||
# Note: Using the high-level interface here
|
||||
print "Creating an array of vectors\n";
|
||||
$va = new_VectorArray(10);
|
||||
|
||||
print " va: $va size=".VectorArray_size($va)."\n";
|
||||
|
||||
# ----- Set some values in the array -----
|
||||
|
||||
# These operators copy the value of $a and $b to the vector array
|
||||
VectorArray_set($va,0,$a);
|
||||
VectorArray_set($va,1,$b);
|
||||
|
||||
VectorArray_get($va,0);
|
||||
# This will work, but it will cause a memory leak!
|
||||
|
||||
VectorArray_set($va,2,addv($a,$b));
|
||||
|
||||
# The non-leaky way to do it
|
||||
|
||||
$c = addv($a,$b);
|
||||
VectorArray_set($va,3,$c);
|
||||
$c = None;
|
||||
|
||||
# Get some values from the array
|
||||
|
||||
print "Getting some array values\n";
|
||||
for ($i = 0; $i < 5; $i++) {
|
||||
print "do $i\n";
|
||||
print " va($i) = ". Vector_print(VectorArray_get($va,$i)). "\n";
|
||||
}
|
||||
|
||||
# Watch under resource meter to check on this
|
||||
#print "Making sure we don't leak memory.\n";
|
||||
#for ($i = 0; $i < 1000000; $i++) {
|
||||
# $c = VectorArray_get($va,$i % 10);
|
||||
#}
|
||||
|
||||
# ----- Clean up -----
|
||||
print "Cleaning up\n";
|
||||
# wants fixing FIXME
|
||||
$va = None;
|
||||
$a = None;
|
||||
$b = None;
|
||||
|
||||
?>
|
||||
24
Examples/php/simple/Makefile
Normal file
24
Examples/php/simple/Makefile
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../preinst-swig
|
||||
SRCS = example.c
|
||||
TARGET = example
|
||||
INTERFACE = example.i
|
||||
LIBS =
|
||||
SWIGOPT =
|
||||
|
||||
all::
|
||||
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' \
|
||||
php
|
||||
|
||||
static::
|
||||
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='myphp' INTERFACE='$(INTERFACE)' \
|
||||
php_static
|
||||
|
||||
clean::
|
||||
$(MAKE) -f $(TOP)/Makefile php_clean
|
||||
rm -f $(TARGET).php
|
||||
|
||||
check: all
|
||||
$(MAKE) -f $(TOP)/Makefile php_run
|
||||
23
Examples/php/simple/example.c
Normal file
23
Examples/php/simple/example.c
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
/* File : example.c */
|
||||
#include <stdio.h>
|
||||
|
||||
/* A global variable */
|
||||
double Foo = 3.0;
|
||||
|
||||
void print_Foo() {
|
||||
printf("In C, Foo = %f\n",Foo);
|
||||
}
|
||||
|
||||
/* Compute the greatest common divisor of positive integers */
|
||||
int gcd(int x, int y) {
|
||||
int g;
|
||||
g = y;
|
||||
while (x > 0) {
|
||||
g = x;
|
||||
x = y % x;
|
||||
y = g;
|
||||
}
|
||||
return g;
|
||||
}
|
||||
|
||||
|
||||
10
Examples/php/simple/example.i
Normal file
10
Examples/php/simple/example.i
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
/* File : example.i */
|
||||
%module example
|
||||
|
||||
%{
|
||||
extern double Foo;
|
||||
%}
|
||||
|
||||
extern double Foo;
|
||||
void print_Foo();
|
||||
int gcd(int x, int y);
|
||||
25
Examples/php/simple/runme.php
Executable file
25
Examples/php/simple/runme.php
Executable file
|
|
@ -0,0 +1,25 @@
|
|||
<?php
|
||||
|
||||
require "example.php";
|
||||
|
||||
# Call our gcd() function
|
||||
|
||||
$x = "42 aaa";
|
||||
$y = 105;
|
||||
$g = gcd($x,$y);
|
||||
print "The gcd of $x and $y is $g\n";
|
||||
|
||||
# Manipulate the Foo global variable
|
||||
|
||||
# Output its current value
|
||||
print "Foo = " . Foo_get() . "\n";
|
||||
|
||||
# Change its value
|
||||
Foo_set(3.1415926);
|
||||
|
||||
# See if the change took effect ( this isn't a good example for php, see
|
||||
# manual for why. )
|
||||
print "Foo = " . Foo_get() . "\n";
|
||||
print_Foo();
|
||||
|
||||
?>
|
||||
24
Examples/php/sync/Makefile
Normal file
24
Examples/php/sync/Makefile
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../preinst-swig
|
||||
CXXSRCS = example.cxx
|
||||
TARGET = example
|
||||
INTERFACE = example.i
|
||||
LIBS =
|
||||
SWIGOPT =
|
||||
|
||||
all::
|
||||
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' \
|
||||
php_cpp
|
||||
|
||||
static::
|
||||
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='myphp' INTERFACE='$(INTERFACE)' \
|
||||
php_cpp_static
|
||||
|
||||
clean::
|
||||
$(MAKE) -f $(TOP)/Makefile php_clean
|
||||
rm -f $(TARGET).php
|
||||
|
||||
check: all
|
||||
$(MAKE) -f $(TOP)/Makefile php_run
|
||||
13
Examples/php/sync/example.cxx
Normal file
13
Examples/php/sync/example.cxx
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
#include "example.h"
|
||||
#include <stdio.h>
|
||||
|
||||
int x = 42;
|
||||
char *s = (char *)"Test";
|
||||
|
||||
void Sync::printer(void) {
|
||||
|
||||
printf("The value of global s is %s\n", s);
|
||||
printf("The value of global x is %d\n", x);
|
||||
printf("The value of class s is %s\n", s);
|
||||
printf("The value of class x is %d\n", x);
|
||||
};
|
||||
9
Examples/php/sync/example.h
Normal file
9
Examples/php/sync/example.h
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
extern char *s;
|
||||
extern int x;
|
||||
|
||||
class Sync {
|
||||
public:
|
||||
int x;
|
||||
char *s;
|
||||
void printer(void);
|
||||
};
|
||||
7
Examples/php/sync/example.i
Normal file
7
Examples/php/sync/example.i
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
%module example
|
||||
|
||||
%{
|
||||
#include "example.h"
|
||||
%}
|
||||
|
||||
%include "example.h"
|
||||
15
Examples/php/sync/runme.php
Normal file
15
Examples/php/sync/runme.php
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
<?
|
||||
|
||||
// Load module and PHP classes.
|
||||
include("example.php");
|
||||
|
||||
echo "Got new object\n";
|
||||
echo "Got string $s and value $x \n";
|
||||
|
||||
$s = new Sync();
|
||||
echo "Got new object\n";
|
||||
|
||||
$s->printer();
|
||||
|
||||
?>
|
||||
|
||||
24
Examples/php/value/Makefile
Normal file
24
Examples/php/value/Makefile
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../preinst-swig
|
||||
SRCS = example.c
|
||||
TARGET = example
|
||||
INTERFACE = example.i
|
||||
LIBS =
|
||||
SWIGOPT = -noproxy
|
||||
|
||||
all::
|
||||
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' \
|
||||
php
|
||||
|
||||
static::
|
||||
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='myphp' INTERFACE='$(INTERFACE)' \
|
||||
php_static
|
||||
|
||||
clean::
|
||||
$(MAKE) -f $(TOP)/Makefile php_clean
|
||||
rm -f $(TARGET).php
|
||||
|
||||
check: all
|
||||
$(MAKE) -f $(TOP)/Makefile php_run
|
||||
13
Examples/php/value/example.c
Normal file
13
Examples/php/value/example.c
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
/* File : example.c */
|
||||
|
||||
#include "example.h"
|
||||
|
||||
double dot_product(Vector a, Vector b) {
|
||||
return (a.x*b.x + a.y*b.y + a.z*b.z);
|
||||
}
|
||||
|
||||
void vector_add(Vector a, Vector b, Vector* result) {
|
||||
result->x = a.x + b.x;
|
||||
result->y = a.y + b.y;
|
||||
result->z = a.z + b.z;
|
||||
}
|
||||
8
Examples/php/value/example.h
Normal file
8
Examples/php/value/example.h
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
/* File : example.h */
|
||||
|
||||
typedef struct {
|
||||
double x, y, z;
|
||||
} Vector;
|
||||
|
||||
double dot_product(Vector a, Vector b);
|
||||
void vector_add(Vector a, Vector b, Vector* result);
|
||||
17
Examples/php/value/example.i
Normal file
17
Examples/php/value/example.i
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
// Tests SWIG's handling of pass-by-value for complex datatypes
|
||||
%module example
|
||||
|
||||
%{
|
||||
#include "example.h"
|
||||
%}
|
||||
|
||||
%include "example.h"
|
||||
|
||||
/* Some helper functions for our interface */
|
||||
%inline %{
|
||||
|
||||
void vector_print(Vector *v) {
|
||||
printf("Vector %p = (%g, %g, %g)\n", v, v->x, v->y, v->z);
|
||||
}
|
||||
%}
|
||||
|
||||
43
Examples/php/value/runme.php
Normal file
43
Examples/php/value/runme.php
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
<?php
|
||||
|
||||
require "example.php";
|
||||
|
||||
|
||||
$v = new_vector();
|
||||
vector_x_set($v,1.0);
|
||||
vector_y_set($v,2.0);
|
||||
vector_z_set($v,3.0);
|
||||
|
||||
$w = new_vector();
|
||||
vector_x_set($w,10.0);
|
||||
vector_y_set($w,11.0);
|
||||
vector_z_set($w,12.0);
|
||||
|
||||
echo "I just created the following vector\n";
|
||||
vector_print($v);
|
||||
vector_print($w);
|
||||
|
||||
echo "\nNow I'm going to compute the dot product\n";
|
||||
|
||||
$d = dot_product($v, $w);
|
||||
|
||||
echo "dot product = $d (should be 68)\n";
|
||||
|
||||
echo "\nNow I'm going to add the vectors together\n";
|
||||
|
||||
$r = new_vector();
|
||||
vector_add($v, $w, $r);
|
||||
|
||||
vector_print($r);
|
||||
|
||||
echo "The value should be (11,13,15)\n";
|
||||
|
||||
echo "\nNow I'm going to clean up the return result\n";
|
||||
|
||||
# free($r);
|
||||
|
||||
echo "Good\n";
|
||||
|
||||
?>
|
||||
|
||||
|
||||
24
Examples/php/variables/Makefile
Normal file
24
Examples/php/variables/Makefile
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../preinst-swig
|
||||
SRCS = example.c
|
||||
TARGET = example
|
||||
INTERFACE = example.i
|
||||
LIBS =
|
||||
SWIGOPT =
|
||||
|
||||
all::
|
||||
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' \
|
||||
php
|
||||
|
||||
static::
|
||||
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
|
||||
SWIGOPT='$(SWIGOPT)' TARGET='myphp' INTERFACE='$(INTERFACE)' \
|
||||
php_static
|
||||
|
||||
clean::
|
||||
$(MAKE) -f $(TOP)/Makefile php_clean
|
||||
rm -f $(TARGET).php
|
||||
|
||||
check: all
|
||||
$(MAKE) -f $(TOP)/Makefile php_run
|
||||
95
Examples/php/variables/example.c
Normal file
95
Examples/php/variables/example.c
Normal file
|
|
@ -0,0 +1,95 @@
|
|||
/* File : example.c */
|
||||
|
||||
/* I'm a file containing some C global variables */
|
||||
|
||||
/* Deal with Microsoft's attempt at deprecating C standard runtime functions */
|
||||
#if !defined(SWIG_NO_CRT_SECURE_NO_DEPRECATE) && defined(_MSC_VER)
|
||||
# define _CRT_SECURE_NO_DEPRECATE
|
||||
#endif
|
||||
|
||||
#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[] = "Goodbye";
|
||||
int *iptrvar = 0;
|
||||
char name[5] = "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 = %p\n", iptrvar);
|
||||
printf("name = %c%c%c%c%c\n", name[0],name[1],name[2],name[3],name[4]);
|
||||
printf("ptptr = %p %s\n", ptptr, Point_print( ptptr ) );
|
||||
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;
|
||||
}
|
||||
|
||||
int value_int(int *value) {
|
||||
return *value;
|
||||
}
|
||||
|
||||
/* 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);
|
||||
}
|
||||
34
Examples/php/variables/example.h
Normal file
34
Examples/php/variables/example.h
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
/* File: example.h */
|
||||
|
||||
typedef struct {
|
||||
int x,y;
|
||||
} Point;
|
||||
|
||||
/* 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[5];
|
||||
|
||||
extern Point *ptptr;
|
||||
extern Point pt;
|
||||
|
||||
extern int status;
|
||||
extern char path[256];
|
||||
|
||||
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();
|
||||
44
Examples/php/variables/example.i
Normal file
44
Examples/php/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[5];
|
||||
|
||||
extern Point *ptptr;
|
||||
extern Point pt;
|
||||
|
||||
/* Some read-only variables */
|
||||
|
||||
%immutable;
|
||||
extern int status;
|
||||
extern char path[256];
|
||||
%mutable;
|
||||
|
||||
/* 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();
|
||||
|
||||
|
||||
|
||||
|
||||
96
Examples/php/variables/runme.php
Normal file
96
Examples/php/variables/runme.php
Normal file
|
|
@ -0,0 +1,96 @@
|
|||
<?php
|
||||
|
||||
require "example.php";
|
||||
echo "\nVariables (values printed from C)\n";
|
||||
|
||||
print_vars();
|
||||
|
||||
echo "Variables (values printed from PHP)\n";
|
||||
|
||||
echo "ivar = ".ivar_get()."\n";
|
||||
echo "svar = ".svar_get()."\n";
|
||||
echo "lvar = ".lvar_get()."\n";
|
||||
echo "uivar = ".uivar_get()."\n";
|
||||
echo "usvar = ".usvar_get()."\n";
|
||||
echo "ulvar = ".ulvar_get()."\n";
|
||||
echo "scvar = ".scvar_get()."\n";
|
||||
echo "ucvar = ".ucvar_get()."\n";
|
||||
echo "cvar = ".cvar_get()."\n";
|
||||
echo "fvar = ".fvar_get()."\n";
|
||||
echo "dvar = ".dvar_get()."\n";
|
||||
echo "strvar = ".strvar_get()."\n";
|
||||
echo "cstrvar = ".cstrvar_get()."\n";
|
||||
echo "iptrvar = ".iptrvar_get()."\n";
|
||||
echo "name = \"".name_get()."\"\n";
|
||||
echo "ptptr = ".ptptr_get() , point_print(ptptr_get()) , "\n";
|
||||
echo "pt = ".pt_get(), point_print(pt_get()) , "\n";
|
||||
|
||||
/* Try to set the values of some global variables */
|
||||
$a = "42.14";
|
||||
|
||||
ivar_set($a);
|
||||
echo "a = $a\n";
|
||||
svar_set(-31000);
|
||||
lvar_set(65537);
|
||||
uivar_set(123456);
|
||||
usvar_set(61000);
|
||||
ulvar_set(654321);
|
||||
scvar_set(-13);
|
||||
ucvar_set(251);
|
||||
cvar_set("S");
|
||||
fvar_set(3.14159);
|
||||
dvar_set(2.1828);
|
||||
strvar_set("Hello World");
|
||||
iptrvar_set(new_int(37));
|
||||
ptptr_set(new_point(37,42));
|
||||
name_set("B");
|
||||
|
||||
echo "Variables (values printed from PHP)\n";
|
||||
|
||||
echo "ivar = ".ivar_get()."\n";
|
||||
echo "svar = ".svar_get()."\n";
|
||||
echo "lvar = ".lvar_get()."\n";
|
||||
echo "uivar = ".uivar_get()."\n";
|
||||
echo "usvar = ".usvar_get()."\n";
|
||||
echo "ulvar = ".ulvar_get()."\n";
|
||||
echo "scvar = ".scvar_get()."\n";
|
||||
echo "ucvar = ".ucvar_get()."\n";
|
||||
echo "cvar = ".cvar_get()."\n";
|
||||
echo "fvar = ".fvar_get()."\n";
|
||||
echo "dvar = ".dvar_get()."\n";
|
||||
echo "strvar = ".strvar_get()."\n";
|
||||
echo "cstrvar = ".cstrvar_get()."\n";
|
||||
echo "iptrvar = ".iptrvar_get()."\n";
|
||||
echo "name = ".name_get()."\n";
|
||||
echo "ptptr = ".ptptr_get() , point_print(ptptr_get()) , "\n";
|
||||
echo "pt = ".pt_get(), point_print(pt_get()) , "\n";
|
||||
|
||||
echo "\nVariables (values printed from C)\n";
|
||||
|
||||
print_vars();
|
||||
|
||||
echo "\nI'm going to try and update a structure variable.\n";
|
||||
|
||||
pt_set(ptptr_get());
|
||||
|
||||
echo "The new value is \n";
|
||||
|
||||
pt_print();
|
||||
|
||||
echo "You should see the value", point_print(ptptr_get()), "\n";
|
||||
|
||||
echo "\nNow I'm going to try and modify some read only variables\n";
|
||||
|
||||
echo "Trying to set 'path'\n";
|
||||
|
||||
//path_set("Whoa!");
|
||||
echo "Path = ".path_get()."\n";
|
||||
|
||||
echo "Trying to set 'status'\n";
|
||||
|
||||
/* And this */
|
||||
//status_set(0);
|
||||
echo "Status = ".status_get()."\n";
|
||||
|
||||
?>
|
||||
|
||||
80
Examples/php/variables/runme.php4.old
Normal file
80
Examples/php/variables/runme.php4.old
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
<?php
|
||||
### THIS VERSION was written for when php global vars fakingly mirrored
|
||||
### the wrapped global vars, but it was very inefficient.
|
||||
### For now we don't do this (pending some changes to php itself) so
|
||||
### we use accessor functions instead; WE KEEP THIS version around ready
|
||||
### for when those php changes are made and we can switch back.
|
||||
### Specifically we want $_GLOBALS variable overloading like object
|
||||
### property overloading
|
||||
|
||||
require "example.php";
|
||||
|
||||
/* Try to set the values of some global variables */
|
||||
|
||||
$ivar = 42;
|
||||
$svar = -31000;
|
||||
$lvar = 65537;
|
||||
$uivar = 123456;
|
||||
$usvar = 61000;
|
||||
$ulvar = 654321;
|
||||
$scvar = -13;
|
||||
$ucvar = 251;
|
||||
$cvar = "S";
|
||||
$fvar = 3.14159;
|
||||
$dvar = 2.1828;
|
||||
$strvar = "Hello World";
|
||||
$cstrvar = "Goodbye";
|
||||
$iptrvar = new_int(37);
|
||||
$ptptr = new_point(37,42);
|
||||
$name = "Bill";
|
||||
|
||||
echo "Variables (values printed from PHP)\n";
|
||||
|
||||
echo "ivar = $ivar\n";
|
||||
echo "svar = $svar\n";
|
||||
echo "lvar = $lvar\n";
|
||||
echo "uivar = $uivar\n";
|
||||
echo "usvar = $usvar\n";
|
||||
echo "ulvar = $ulvar\n";
|
||||
echo "scvar = $scvar\n";
|
||||
echo "ucvar = $ucvar\n";
|
||||
echo "cvar = $cvar\n";
|
||||
echo "fvar = $fvar\n";
|
||||
echo "dvar = $dvar\n";
|
||||
echo "strvar = $strvar\n";
|
||||
echo "cstrvar = $cstrvar\n";
|
||||
echo "iptrvar = $iptrvar\n";
|
||||
echo "name = $name\n";
|
||||
echo "ptptr = $ptptr" , point_print($ptptr) , "\n";
|
||||
echo "pt = $pt" , point_print($pt) , "\n";
|
||||
|
||||
echo "\nVariables (values printed from C)\n";
|
||||
|
||||
print_vars();
|
||||
|
||||
echo "\nI'm going to try and update a structure variable.\n";
|
||||
|
||||
$pt = $ptptr;
|
||||
|
||||
echo "The new value is \n";
|
||||
|
||||
pt_print();
|
||||
|
||||
echo "You should see the value", point_print($ptptr), "\n";
|
||||
|
||||
echo "\nNow I'm going to try and modify some read only variables\n";
|
||||
|
||||
echo "Trying to set 'path'\n";
|
||||
|
||||
/* Sadly this works */
|
||||
$path = "Whoa!";
|
||||
echo "Path = $path\n";
|
||||
|
||||
echo "Trying to set 'status'\n";
|
||||
|
||||
/* And this */
|
||||
$status = 0;
|
||||
echo "Status = $status\n";
|
||||
|
||||
?>
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue