summaryrefslogtreecommitdiff
path: root/src/callable.h
blob: 5b32558c053909ad2a9f2ee09e5d09a6c66362cb (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/**
 *  Callable.h
 *
 *  Object represents a callable function or method that is defined with the CPP 
 *  API.
 *
 *  @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com>
 *  @copyright 2013 Copernica BV
 */

/**
 *  Forward definitions
 */
struct _zend_function_entry;
struct _zend_internal_function_info;
 
/**
 *  Set up namespace
 */
namespace Php {

/** 
 *  Class definition
 */
class Callable
{
public:
    /**
     *  Constructor
     *  @param  name        Function or method name
     *  @param  arguments   Information about the arguments
     */
    Callable(const char *name, const Arguments &arguments = {}) : _ptr(this, name)
    {
        // construct vector for arguments
        _argc = arguments.size();
        _argv = new zend_arg_info[_argc+1];
        
        // counter
        int i=1;
        
        // loop through the arguments
        for (auto it = arguments.begin(); it != arguments.end(); it++)
        {
            // fill the arg info
            it->fill(&_argv[i++]);
        }
        
        // @todo find out number of required arguments
        _required = _argc;
    }

    /**
     *  Destructor
     */
    virtual ~Callable()
    {
        delete[] _argv;
    }
    
    /**
     *  Method that gets called every time the function is executed
     *  @param  params      The parameters that were passed
     *  @return Variable    Return value
     */
    virtual Value invoke(Parameters &params) = 0;
    
    /**
     *  Fill a function entry
     *  @param  entry       Entry to be filled
     *  @param  ns          Active namespace
     *  @param  classname   Optional class name
     *  @param  flags       Access flags
     */
    void initialize(struct _zend_function_entry *entry, const char *classname = nullptr, int flags = 0) const;

    /**
     *  Fill function info
     *  @param  info        Info object to be filled
     *  @param  ns          Active namespace
     *  @param  classname   Optional class name
     */
    void initialize(struct _zend_internal_function_info *info, const char *classname = nullptr) const;


protected:
    /**
     *  Hidden pointer to the name and the function
     *  @var    HiddenPointer
     */
    HiddenPointer<Callable> _ptr;

    /**
     *  Suggestion for the return type
     *  @var    Type
     */
    Type _return = Type::Null;

    /**
     *  Required number of arguments
     *  @var    integer
     */
    int _required = 0;

    /**
     *  Total number of arguments
     *  @var    integer
     */
    int _argc = 0;

    /**
     *  The arguments
     *  @var zend_arg_info[]
     */
    struct _zend_arg_info *_argv = nullptr;

};

/**
 *  End of namespace
 */
}