Revert rev 11187 "Merged with recent changes from trunk."

This reverts commit c595e4d90ebfd63eb55430c735bb121cf690bd59.

Conflicts:

	Source/Modules/c.cxx

From: William S Fulton <wsf@fultondesigns.co.uk>

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/gsoc2008-maciekd@13033 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
William S Fulton 2012-05-06 01:13:16 +00:00
commit d6b81eb831
703 changed files with 9266 additions and 21128 deletions

View file

@ -0,0 +1,5 @@
#! /bin/sh -e
${SWIG:=swig} -php4 -make -c++ -withcxx example.cxx example.i
make
php -d extension_dir=. runme-proxy.php4

View 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)' \
php4_cpp
static::
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
SWIGOPT='$(SWIGOPT)' TARGET='myphp4' INTERFACE='$(INTERFACE)' \
php4_cpp_static
clean::
$(MAKE) -f $(TOP)/Makefile php4_clean
rm -f $(TARGET).php
check: all
$(MAKE) -f $(TOP)/Makefile php4_run

View 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;
}

View 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();
};

View 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;
}
}
};

View 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);
?>

View 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;
?>