summaryrefslogtreecommitdiff
path: root/zend/functor.h
blob: 5458027185360da053a048ff7c3644cd2b60fefc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/**
 *  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);
    }

    /**
     *  Get the functor class entry
     *  @return zend_class_entry
     */
    static zend_class_entry *entry()
    {
        // get the "member"
        return _entry;
    }

    /**
     *  Initialize the class
     *  @param  tsrmls
     */
    static void initialize(TSRMLS_D);

    /**
     *  Shutdown the class
     *  @param  tsrmls
     */
    static void shutdown(TSRMLS_D);

private:
    /**
     *  The std::function that is wrapped in PHP code
     *  @var std::function
     */
    const std::function<Value(Parameters &params)> _function;

    /**
     *  The classentry
     *  @var zend_class_entry
     */
    static zend_class_entry *_entry;

};

/**
 *  End of namespace
 */
}