summaryrefslogtreecommitdiff
path: root/src/functions.h
blob: bc08b8ab43905ab5867fa6795ba53b5a263a0f03 (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
/**
 *  Functions.h
 *
 *  Internal helper class that parses the functions initializer list, and
 *  that converts it into a zend_function_entry array.
 *
 *  @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com>
 *  @copyright 2013 Copernica BV
 */

/**
 *  Set up namespace
 */
namespace PhpCpp {

/**
 *  Class definition
 */
class Functions
{
public:
    /**
     *  Constructor
     *  @param  functions   The functions to parse
     */
    Functions(const std::initializer_list<Function> &functions) : _functions(functions)
    {
        // allocate the function entries
        _entries = new zend_function_entry[functions.size() + 1];
        
        // keep iterator counter
        int i = 0;
        
        // loop through the functions
        for (auto it = begin(functions); it != functions.end(); it++)
        {
            // let the callable fill the array
            it->internal()->fill(&_entries[i++]);
        }
        
        // last entry should be set to all zeros
        zend_function_entry *last = &_entries[i];
        
        // all should be set to zero
        memset(last, 0, sizeof(zend_function_entry));
    }

    /**
     *  Destructor
     */
    virtual ~Functions()
    {
        delete[] _entries;
    }
    
    /**
     *  Retrieve the internal data
     *  @return zend_function_entry*
     */
    zend_function_entry *internal()
    {
        return _entries;
    }


private:
    /**
     *  The internal entries
     *  @var zend_function_entry*
     */
    zend_function_entry *_entries;
    
    /**
     *  Vector of functions (we need this because the function objects must
     *  remain in memory)
     *  @var vector
     */
    std::vector<Function> _functions;
};

/**
 *  End of namespace
 */
}