From d87b3ca8f1dbcb395f2dfd6540483da7a0e5e15c Mon Sep 17 00:00:00 2001 From: Emiel Bruijntjes Date: Thu, 15 Jan 2015 16:34:14 +0100 Subject: 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 --- include/function.h | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 include/function.h (limited to 'include/function.h') 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 + * @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 &function); + + /** + * Constructor to wrap a function that does not accept parameters + * @param function The C++ function to be wrapped + */ + Function(const std::function &function) : Function([function](Parameters ¶ms) -> 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 + */ +} + -- cgit v1.2.3