summaryrefslogtreecommitdiff
path: root/include/namespace.h
blob: 9d6e6f2c31c7a6dd1a8c1d7b30aaa91a3822872e (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/**
 *  Namespace.h
 * 
 *  Class that can be used to group  various functions and classes into one
 *  namespace.
 * 
 *  @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com>
 *  @copyright 2014 Copernica BV
 */

/**
 *  Set up namespace
 */
namespace Php {

/**
 *  Forward declaration
 */
class Function;

/**
 *  Class definition
 */
class Namespace
{
public:
    /**
     *  Constructor
     *  @param  name        Name of the namespace
     */
    Namespace(const char *name) : _name(name) {}
    
    /**
     *  Copy constructor
     *  @param  ns          Namespace to copy
     */
    Namespace(const Namespace &ns) : 
        _name(ns._name), 
        _functions(ns._functions),
        _classes(ns._classes),
        _namespaces(ns._namespaces) {}
        
    /**
     *  Move constructor
     *  @param  ns
     */
    Namespace(Namespace &&ns) :
        _name(std::move(ns._name)), 
        _functions(std::move(ns._functions)),
        _classes(std::move(ns._classes)),
        _namespaces(std::move(ns._namespaces)) {}
    
    /**
     *  Destructor
     */
    virtual ~Namespace() {}
    
    /**
     *  Add a native function directly to the extension
     *  @param  name        Name of the function
     *  @param  function    The function to add
     *  @param  arguments   Optional argument specification
     */
    void add(const char *name, const native_callback_0 &function, const Arguments &arguments = {});
    void add(const char *name, const native_callback_1 &function, const Arguments &arguments = {});
    void add(const char *name, const native_callback_2 &function, const Arguments &arguments = {});
    void add(const char *name, const native_callback_3 &function, const Arguments &arguments = {});
    
    /**
     *  Add a native class to the extension by moving it
     *  @param  type        The class implementation
     */
    template<typename T>
    void add(Class<T> &&type)
    {
        // make a copy of the object
        auto *copy = new Class<T>(std::move(type));
        
        // and add it to the list of classes
        _classes.push_back(std::unique_ptr<ClassBase>(copy));
    }

    /**
     *  Add a native class to the extension by copying it
     *  @param  type        The class implementation
     */
    template<typename T>
    void add(const Class<T> &type)
    {
        // make a copy of the object
        auto *copy = new Class<T>(std::move(type));
        
        // and add it to the list of classes
        _classes.push_back(std::unique_ptr<ClassBase>(copy));
    }

    /**
     *  Add an interface to the extension by moving it
     *  @param  interface   The interface properties
     */
    void add(Interface &&interface)
    {
        // make a copy of the object
        auto *copy = new Interface(std::move(interface));
        
        // and add it to the list of classes
        _classes.push_back(std::unique_ptr<ClassBase>(copy));
    }

    /**
     *  Add an interface to the extension by copying it
     *  @param  interface   The interface properties
     */
    void add(const Interface &interface)
    {
        // make a copy of the object
        auto *copy = new Interface(interface);
        
        // and add it to the list of classes
        _classes.push_back(std::unique_ptr<ClassBase>(copy));
    }
    
    /**
     *  Add a namespace to the extension by moving it
     *  @param  ns          The namespace
     */
    void add(Namespace &&ns)
    {
        // make a copy of the object
        auto *copy = new Namespace(std::move(ns));
        
        // add it to the list of namespaces
        _namespaces.push_back(std::unique_ptr<Namespace>(copy));
    }

    /**
     *  Add a namespace to the extension by copying it
     *  @param  ns          The namespace
     */
    void add(const Namespace &ns)
    {
        // make a copy of the object
        auto *copy = new Namespace(std::move(ns));
        
        // add it to the list of namespaces
        _namespaces.push_back(std::unique_ptr<Namespace>(copy));
    }


protected:
    /**
     *  Name of the namespace
     *  @var    string
     */
    std::string _name;

    /**
     *  Functions defined by the extension
     *  @var    list
     */
    std::list<std::shared_ptr<Function>> _functions;

    /**
     *  Classes defined by the extension
     *  @var    list
     */
    std::list<std::shared_ptr<ClassBase>> _classes;
    
    /**
     *  Namespaces defined by the extension
     *  @var    list
     */
    std::list<std::shared_ptr<Namespace>> _namespaces;
    
    /**
     *  The total number of functions
     *  @return size_t
     */
    size_t functions()
    {
        // number of functions in this namespace
        int result = _functions.size();
        
        // number of functions in sub-namespace
        for (auto &ns : _namespaces) result += ns->functions();
        
        // done
        return result;
    }

    /**
     *  Initialize all functions in this namespace
     *  @param  ns          Namespace prefix
     *  @param  entries     The array to be filled
     *  @return int         Number of functions that were initialized
     */
    size_t initialize(const std::string &ns, struct _zend_function_entry entries[]);

    /**
     *  Initialize the namespace after it was registered
     *  @param  parent      Parent namespace
     */
    void initialize(const std::string &parent)
    {
        // the namespace to use
        std::string prefix = parent.size() ? parent + "\\" + _name : _name;
        
        // loop through the classes in this namespace
        for (auto &c : _classes) c->initialize(prefix);
        
        // and loop through the other namespaces
        for (auto &n : _namespaces) n->initialize(prefix);
    }
};
    
/**
 *  End namespace
 */
}