summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorEmiel Bruijntjes <emiel.bruijntjes@copernica.com>2015-01-15 16:34:14 +0100
committerEmiel Bruijntjes <emiel.bruijntjes@copernica.com>2015-01-15 16:34:14 +0100
commitd87b3ca8f1dbcb395f2dfd6540483da7a0e5e15c (patch)
tree6dd3179162d5cecc474fc9028039bd757f6df04b /include
parentd8fe9239959dfeae11daa5de70126e30119d3b75 (diff)
Added the Php::Function class. This is an extension to the Php::Value class that can be used if you want to assign a std::function object to a Value. This is useful if you want to pass a C++ lambda to a PHP userspace function
Diffstat (limited to 'include')
-rw-r--r--include/class.h6
-rw-r--r--include/function.h73
-rw-r--r--include/namespace.h6
-rw-r--r--include/object.h16
4 files changed, 95 insertions, 6 deletions
diff --git a/include/class.h b/include/class.h
index 9257213..e11074c 100644
--- a/include/class.h
+++ b/include/class.h
@@ -555,10 +555,12 @@ private:
}
/**
- * Namespaces have access to the private base class, so that the classes
- * can be registered.
+ * Namespaces and the function have access to the private base class,
+ * so that the classes can be registered (the function object needs
+ * this to register the Functor class).
*/
friend class Namespace;
+ friend class Function;
/**
* All Php::Class<AnyThing> also need access to the base class to
diff --git a/include/function.h b/include/function.h
new file mode 100644
index 0000000..7073f84
--- /dev/null
+++ b/include/function.h
@@ -0,0 +1,73 @@
+/**
+ * Function.h
+ *
+ * Small extension to the Value class that allows a value to be
+ * constructed with a std::function.
+ *
+ * If you want to assign a std::function to a value, the following
+ * piece of code won't work:
+ *
+ * Php::Value value([]() { .... });
+ *
+ * Because the passed in function would match with many of the possible
+ * Value constructors. For that reason we have created a small and
+ * simple Function class that can be used instead:
+ *
+ * Php::Function valu([]() { .... });
+ *
+ * A Php::Function is an extended Php::Value object, so can be used
+ * in place of Php::Values all the time. The only difference is that
+ * it has a different constructor
+ *
+ * @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com>
+ * @copyright 2015 Copernica BV
+ */
+
+/**
+ * Set up namespace
+ */
+namespace Php {
+
+/**
+ * Class definition
+ */
+class Function : public Value
+{
+public:
+ /**
+ * Constructor to wrap a function that takes parameters
+ * @param function The C++ function to be wrapped
+ */
+ Function(const std::function<Value(Parameters&)> &function);
+
+ /**
+ * Constructor to wrap a function that does not accept parameters
+ * @param function The C++ function to be wrapped
+ */
+ Function(const std::function<Value()> &function) : Function([function](Parameters &params) -> Value {
+
+ // call original function, forget about the parameters
+ return function();
+
+ }) {}
+
+ /**
+ * Destructor
+ */
+ virtual ~Function() {}
+
+private:
+ /**
+ * Retrieve the class entry of the _functor class
+ * @return _zend_class_entry
+ */
+ static struct _zend_class_entry *entry();
+
+
+};
+
+/**
+ * End namespace
+ */
+}
+
diff --git a/include/namespace.h b/include/namespace.h
index e713850..84f0740 100644
--- a/include/namespace.h
+++ b/include/namespace.h
@@ -16,7 +16,7 @@ namespace Php {
/**
* Forward declaration
*/
-class Function;
+class NativeFunction;
/**
* Class definition
@@ -34,7 +34,7 @@ protected:
* Functions defined in the namespace
* @var list
*/
- std::list<std::shared_ptr<Function>> _functions;
+ std::list<std::shared_ptr<NativeFunction>> _functions;
/**
* Classes defined in the namespace
@@ -220,7 +220,7 @@ public:
*
* @param callback
*/
- void functions(const std::function<void(const std::string &ns, Function &func)> &callback);
+ void functions(const std::function<void(const std::string &ns, NativeFunction &func)> &callback);
/**
* Apply a callback to each registered class
diff --git a/include/object.h b/include/object.h
index 0cdeb5c..301fa57 100644
--- a/include/object.h
+++ b/include/object.h
@@ -54,9 +54,23 @@ public:
* }
*
* @param name Name of the class to instantiate
- * @param base Implementation of the class
+ * @param base C++ object to wrap
*/
Object(const char *name, Base *base);
+
+ /**
+ * If you already have the zend_class_entry, you can also pass the
+ * class_entry structure instead of the class name. This constructor
+ * works exactly like the Object(name, base) constructor mentioned
+ * above.
+ *
+ * Note that if you normally use PHP-CPP, you do not have the class_entry,
+ * so you probably need to pass the name anyway
+ *
+ * @param entry The PHP class entry
+ * @param base C++ object to wrap
+ */
+ Object(struct _zend_class_entry *entry, Base *base);
/**
* Wrap around an object implemented by us