summaryrefslogtreecommitdiff
path: root/Examples/php/reference
diff options
context:
space:
mode:
Diffstat (limited to 'Examples/php/reference')
-rw-r--r--Examples/php/reference/Makefile24
-rw-r--r--Examples/php/reference/example.cxx49
-rw-r--r--Examples/php/reference/example.h26
-rw-r--r--Examples/php/reference/example.i47
-rw-r--r--Examples/php/reference/runme-proxy.php479
-rw-r--r--Examples/php/reference/runme.php49
6 files changed, 274 insertions, 0 deletions
diff --git a/Examples/php/reference/Makefile b/Examples/php/reference/Makefile
new file mode 100644
index 0000000..1bc0bea
--- /dev/null
+++ b/Examples/php/reference/Makefile
@@ -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
diff --git a/Examples/php/reference/example.cxx b/Examples/php/reference/example.cxx
new file mode 100644
index 0000000..13e47ea
--- /dev/null
+++ b/Examples/php/reference/example.cxx
@@ -0,0 +1,49 @@
+/* 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::as_string() {
+ 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;
+}
diff --git a/Examples/php/reference/example.h b/Examples/php/reference/example.h
new file mode 100644
index 0000000..1b88cbf
--- /dev/null
+++ b/Examples/php/reference/example.h
@@ -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 *as_string();
+};
+
+class VectorArray {
+private:
+ Vector *items;
+ int maxsize;
+public:
+ VectorArray(int maxsize);
+ ~VectorArray();
+ Vector &operator[](int);
+ int size();
+};
+
+
+
+
diff --git a/Examples/php/reference/example.i b/Examples/php/reference/example.i
new file mode 100644
index 0000000..d612286
--- /dev/null
+++ b/Examples/php/reference/example.i
@@ -0,0 +1,47 @@
+/* File : example.i */
+
+/* This example has nothing to do with references but the name is used by all
+ * the other languages so it's hard to rename to something more meaningful.
+ *
+ * Mostly it shows how to use %extend.
+ */
+
+%module example
+
+%{
+#include "example.h"
+%}
+
+class Vector {
+public:
+ Vector(double x, double y, double z);
+ ~Vector();
+ char *as_string();
+};
+
+/* 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;
+ }
+ }
+};
diff --git a/Examples/php/reference/runme-proxy.php4 b/Examples/php/reference/runme-proxy.php4
new file mode 100644
index 0000000..9d216f7
--- /dev/null
+++ b/Examples/php/reference/runme-proxy.php4
@@ -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);
+
+?>
diff --git a/Examples/php/reference/runme.php b/Examples/php/reference/runme.php
new file mode 100644
index 0000000..5d264ee
--- /dev/null
+++ b/Examples/php/reference/runme.php
@@ -0,0 +1,49 @@
+<?php
+
+# This file illustrates the manipulation of C++ references in PHP.
+
+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->as_string()}\n";
+print " Created b: {$b->as_string()}\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 = example::addv($a, $b);
+print " a+b ={$c->as_string()}\n";
+
+# ----- Create a vector array -----
+
+print "Creating an array of vectors\n";
+$va = new VectorArray(10);
+
+print " 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->set(2, addv($a, $b));
+
+# Get some values from the array
+
+print "Getting some array values\n";
+for ($i = 0; $i < 5; $i++) {
+ print " va[$i] = {$va->get($i)->as_string()}\n";
+}
+
+?>