summaryrefslogtreecommitdiff
path: root/zend/functor.h
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 /zend/functor.h
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 'zend/functor.h')
-rw-r--r--zend/functor.h61
1 files changed, 61 insertions, 0 deletions
diff --git a/zend/functor.h b/zend/functor.h
new file mode 100644
index 0000000..7d5bef5
--- /dev/null
+++ b/zend/functor.h
@@ -0,0 +1,61 @@
+/**
+ * Functor.h
+ *
+ * We want to be able to wrap a std::function in an object and pass
+ * that to PHP. The normal "Closure" class from the Zend engine
+ * would be very suitable for that. However, the Zend engine does
+ * not really allow us to add a secret pointer to such closure object.
+ *
+ * Therefore, we create our own Closure class, this time using PHP-CPP
+ * code, to wrap a std::function.
+ *
+ * @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com>
+ * @copyright 2015 Copernica BV
+ */
+
+/**
+ * Set up namespace
+ */
+namespace Php {
+
+/**
+ * Class definition
+ */
+class Functor : public Base
+{
+public:
+ /**
+ * Constructor
+ * @param function The function to wrap
+ */
+ Functor(const std::function<Value(Parameters &params)> &function) : _function(function) {}
+
+ /**
+ * Destructor
+ */
+ virtual ~Functor() {}
+
+ /**
+ * Invoke the functor
+ * @param params
+ * @return Value
+ */
+ Value __invoke(Parameters &params) const
+ {
+ // pass on to the function
+ return _function(params);
+ }
+
+private:
+ /**
+ * The std::function that is wrapped in PHP code
+ * @var std::function
+ */
+ const std::function<Value(Parameters &params)> _function;
+};
+
+/**
+ * End of namespace
+ */
+}
+