summaryrefslogtreecommitdiff
path: root/Examples/simple/simple.cpp
diff options
context:
space:
mode:
authorJasperVanEck <jaspergkurtz@gmail.com>2013-11-28 10:02:43 +0100
committerJasperVanEck <jaspergkurtz@gmail.com>2013-11-28 10:02:43 +0100
commit815ad7ce6e7b129c52891446d1ecf10390b34dac (patch)
tree6f983a8ade641ae09f69682b45d6a5596d23a0d3 /Examples/simple/simple.cpp
parent4d85e028c2f05ea1dfc8c5797db4703deb6c698e (diff)
Moved tests to Examples
Diffstat (limited to 'Examples/simple/simple.cpp')
-rw-r--r--Examples/simple/simple.cpp184
1 files changed, 184 insertions, 0 deletions
diff --git a/Examples/simple/simple.cpp b/Examples/simple/simple.cpp
new file mode 100644
index 0000000..7942a3c
--- /dev/null
+++ b/Examples/simple/simple.cpp
@@ -0,0 +1,184 @@
+/**
+ * Simple.h
+ *
+ * A very simple extension that does almost nothing
+ *
+ * @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com>
+ * @copyright 2013 Copernica BV
+ */
+#include <string>
+#include <iostream>
+#include <phpcpp.h>
+
+/**
+ * Namespace to use
+ */
+using namespace std;
+
+
+Php::Value bubblesort(Php::Parameters &params)
+{
+ cout << "start bubblesort" << endl;
+
+ // the array that was passed
+ Php::Value array(params[0]);
+
+ cout << "go return" << endl;
+
+ return array;
+
+ // size of the array
+ int size = array.size();
+
+ cout << "convert to native" << endl;
+
+ int *x = new int[size];
+ for (int i=0; i<size; i++) x[i] = array[i];
+
+ cout << "converted" << endl;
+
+
+ // outer loop
+ for (int i=0; i<size; i++)
+ {
+ // left value
+ int left = x[i];
+// cout << "left: " << left << endl;
+
+ // inner loop
+ for (int j=i+1; j<size; j++)
+ {
+ // right value
+ int right = x[j];
+
+ if (left < right) continue;
+
+ // swap values
+ x[i] = right;
+ x[j] = left;
+ left = right;
+ }
+ }
+
+ cout << "algorithm end" << endl;
+
+ Php::Value r;
+
+ for (int i=0; i<size; i++) r[i] = x[i];
+
+
+ delete[] x;
+
+ // done
+ return r;
+}
+
+/**
+ * Our own "my_plus" function that will be available in PHP
+ * @param environment
+ * @param params
+ * @return Value
+ */
+static Php::Value my_plus(Php::Environment &env, Php::Parameters &params)
+{
+ Php::Value r(0);
+
+ for (unsigned int i=0; i<params.size(); i++) r += params[i];
+
+ return r;
+
+
+// int p1 = params[0];
+// int p2 = params[1];
+// return p1 + p2;
+//
+// cout << "global g1: " << env["g1"].stringValue() << endl;
+// cout << "global g2: " << env["g2"].stringValue() << endl;
+//
+// Php::Global g(env["g3"]);
+//
+// g = "zo kan het ook";
+//
+// string output = env.call("strtoupper","test in lowercase");
+//
+// cout << "output: " << output << endl;
+//
+// return p1 + p2;
+}
+
+/**
+ * Custom class that will be available in PHP
+ */
+class MyCustomClass : public Php::Base
+{
+private:
+ int _x;
+
+public:
+ MyCustomClass()
+ {
+ _x = 3;
+ cout << "MyCustomClass::MyCustomClass" << endl;
+ cout << this << endl;
+ cout << _x << endl;
+ }
+
+ virtual ~MyCustomClass()
+ {
+ cout << "MyCustomClass::~MyCustomClass" << endl;
+ }
+
+ virtual void __construct()
+ {
+ cout << "MyCustomClass::__construct" << endl;
+ }
+
+ virtual void __destruct()
+ {
+ cout << "MyCustomClass::__destruct" << endl;
+ }
+
+ void myMethod(Php::Parameters &params)
+ {
+ cout << "myMethod GETS CALLED!!!!" << endl;
+ cout << this << endl;
+ cout << _x << endl;
+// cout << "A: " << _properties["a"] << endl;
+// cout << "Z: " << _properties["z"] << endl;
+ }
+};
+
+// symbols are exported according to the "C" language
+extern "C"
+{
+ // export the "get_module" function that will be called by the Zend engine
+ PHPCPP_EXPORT void *get_module()
+ {
+ // create extension
+ static Php::Extension extension("simple","1.0");
+
+ // define the functions
+ extension.add("my_plus", my_plus, {
+// Php::ByVal("a", Php::numericType),
+// Php::ByVal("b", Php::numericType)
+// Php::ByVal("c", "MyClass"),
+// Php::ByRef("d", Php::stringType)
+ });
+
+ extension.add("bubblesort", bubblesort);
+
+ Php::Method<MyCustomClass> x(&MyCustomClass::myMethod);
+
+ // define classes
+ extension.add("my_class", Php::Class<MyCustomClass>({
+ Php::Public("a", 123),
+ Php::Protected("b", "abc"),
+ Php::Protected("myMethod", Php::Method<MyCustomClass>(&MyCustomClass::myMethod)),
+ Php::Public("__construct", Php::Method<MyCustomClass>(&MyCustomClass::__construct))
+ }));
+
+ // return the module entry
+ return extension.module();
+ }
+}
+