summaryrefslogtreecommitdiff
path: root/trunk/Examples/php/reference
diff options
context:
space:
mode:
Diffstat (limited to 'trunk/Examples/php/reference')
-rw-r--r--trunk/Examples/php/reference/Makefile24
-rw-r--r--trunk/Examples/php/reference/example.cxx49
-rw-r--r--trunk/Examples/php/reference/example.h26
-rw-r--r--trunk/Examples/php/reference/example.i47
-rw-r--r--trunk/Examples/php/reference/runme.php49
5 files changed, 195 insertions, 0 deletions
diff --git a/trunk/Examples/php/reference/Makefile b/trunk/Examples/php/reference/Makefile
new file mode 100644
index 000000000..1bc0beaab
--- /dev/null
+++ b/trunk/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/trunk/Examples/php/reference/example.cxx b/trunk/Examples/php/reference/example.cxx
new file mode 100644
index 000000000..13e47eade
--- /dev/null
+++ b/trunk/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/trunk/Examples/php/reference/example.h b/trunk/Examples/php/reference/example.h
new file mode 100644
index 000000000..1b88cbf5c
--- /dev/null
+++ b/trunk/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/trunk/Examples/php/reference/example.i b/trunk/Examples/php/reference/example.i
new file mode 100644
index 000000000..d6122866b
--- /dev/null
+++ b/trunk/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/trunk/Examples/php/reference/runme.php b/trunk/Examples/php/reference/runme.php
new file mode 100644
index 000000000..5d264ee43
--- /dev/null
+++ b/trunk/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";
+}
+
+?>