diff options
author | Emiel Bruijntjes <emiel.bruijntjes@copernica.com> | 2015-01-15 16:34:14 +0100 |
---|---|---|
committer | Emiel Bruijntjes <emiel.bruijntjes@copernica.com> | 2015-01-15 16:34:14 +0100 |
commit | d87b3ca8f1dbcb395f2dfd6540483da7a0e5e15c (patch) | |
tree | 6dd3179162d5cecc474fc9028039bd757f6df04b /zend/function.cpp | |
parent | d8fe9239959dfeae11daa5de70126e30119d3b75 (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/function.cpp')
-rw-r--r-- | zend/function.cpp | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/zend/function.cpp b/zend/function.cpp new file mode 100644 index 0000000..789175e --- /dev/null +++ b/zend/function.cpp @@ -0,0 +1,49 @@ +/** + * Function.cpp + * + * Implementation file for the Function class + * + * @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com> + * @copyright 2015 Copernica BV + */ + +/** + * Dependencies + */ +#include "includes.h" + +/** + * Set up namespace + */ +namespace Php { + +/** + * Function returns a pointer to the class-entry of the Functor class + * @return zend_class_entry + */ +zend_class_entry *Function::entry() +{ + // and the actual class entry + static zend_class_entry *entry = nullptr; + + // is the class entry already valid? + if (entry) return entry; + + // construct functor object + static std::unique_ptr<ClassBase> functor(new Class<Functor>("Functor")); + + // initialize the functor class + return entry = functor->implementation()->initialize(functor.get(), "" TSRMLS_CC); +} + +/** + * Constructor + * @param function The function to be wrapped + */ +Function::Function(const std::function<Php::Value(Php::Parameters&)> &function) : Value(Object(entry(), new Functor(function))) {} + +/** + * End of namespace + */ +} + |