summaryrefslogtreecommitdiff
path: root/include/namespace.h
blob: 388f2b9d730f2ed01b1a86fdf50bf8e3a1adfebc (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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/**
 *  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 NativeFunction;

/**
 *  Class definition
 */
class PHPCPP_EXPORT Namespace
{
protected:
    /**
     *  Name of the namespace
     *  @var    string
     */
    std::string _name;

    /**
     *  Functions defined in the namespace
     *  @var    list
     */
    std::list<std::shared_ptr<NativeFunction>> _functions;

    /**
     *  Classes defined in the namespace
     *  @var    list
     */
    std::list<std::shared_ptr<ClassBase>> _classes;

    /**
     *  Constants defined in the namespace
     *  @var    list
     */
    std::list<std::shared_ptr<Constant>> _constants;

    /**
     *  Namespaces defined inside the namespace
     *  @var    list
     */
    std::list<std::shared_ptr<Namespace>> _namespaces;

    /**
     *  Is the object locked?
     *
     *  After the object is locked, no more elements can be added to it.
     *  This happens after the call to get_module - it no longer makes
     *  sense to add more objects. When 'apache reload' is executed, the
     *  get_module() function is called for a second (or third, or fourth)
     *  time, but the classes, functions and namespaces will then not be
     *  filled.
     *
     *  @return bool
     */
    virtual bool locked() const
    {
        // by default, namespaces are not locked (only derived extension
        // objects can end up in a locked state
        return false;
    }


public:
    /**
     *  Constructor
     *  @param  name        Name of the namespace
     */
    Namespace(const char *name) : _name(name) {}

    /**
     *  Destructor
     */
    virtual ~Namespace() {}

    /**
     *  Add a native function directly to the namespace
     *  @param  name        Name of the function
     *  @param  function    The function to add
     *  @param  arguments   Optional argument specification
     *  @return Namespace   Same object to allow chaining
     */
    Namespace &add(const char *name, const native_callback_0 &function, const Arguments &arguments = {});
    Namespace &add(const char *name, const native_callback_1 &function, const Arguments &arguments = {});
    Namespace &add(const char *name, const native_callback_2 &function, const Arguments &arguments = {});
    Namespace &add(const char *name, const native_callback_3 &function, const Arguments &arguments = {});

    /**
     *  Add a native class to the namespace by moving it
     *  @param  type        The class implementation
     *  @return Namespace   Same object to allow chaining
     */
    template<typename T>
    Namespace &add(Class<T> &&type)
    {
        // skip when locked
        if (locked()) return *this;

        // make a copy of the object, and add it to the list of classes
        _classes.push_back(std::unique_ptr<ClassBase>(new Class<T>(std::move(type))));

        // allow chaining
        return *this;
    }

    /**
     *  Add a native class to the namespace by copying it
     *  @param  type        The class implementation
     *  @return Namespace   Same object to allow chaining
     */
    template<typename T>
    Namespace &add(const Class<T> &type)
    {
        // skip when locked
        if (locked()) return *this;

        // and add it to the list of classes
        _classes.push_back(std::unique_ptr<ClassBase>(new Class<T>(type)));

        // allow chaining
        return *this;
    }

    /**
     *  Add an interface to the namespace by moving it
     *  @param  interface   The interface properties
     *  @return Namespace   Same object to allow chaining
     */
    Namespace &add(Interface &&interface)
    {
        // skip when locked
        if (locked()) return *this;

        // make a copy and add it to the list of classes
        _classes.push_back(std::unique_ptr<ClassBase>(new Interface(std::move(interface))));

        // allow chaining
        return *this;
    }

    /**
     *  Add an interface to the namespace by copying it
     *  @param  interface   The interface properties
     *  @return Namespace   Same object to allow chaining
     */
    Namespace &add(const Interface &interface)
    {
        // skip when locked
        if (locked()) return *this;

        // make a copy and add it to the list of classes
        _classes.push_back(std::unique_ptr<ClassBase>(new Interface(interface)));

        // allow chaining
        return *this;
    }

    /**
     *  Add a constant to the namespace
     *  @param  constant    The constant to add
     *  @return Namespace   Same object to allow chaining
     */
    Namespace &add(Constant &&constant)
    {
        // skip when locked
        if (locked()) return *this;

        // and add it to the list of constants
        _constants.push_back(std::unique_ptr<Constant>(new Constant(std::move(constant))));

        // allow chaining
        return *this;
    }

    /**
     *  Add a constant to the namespace
     *  @param  constant    The constant to add
     *  @return Namespace   Same object to allow chaining
     */
    Namespace &add(const Constant &constant)
    {
        // skip when locked
        if (locked()) return *this;

        // and add it to the list of constants
        _constants.push_back(std::unique_ptr<Constant>(new Constant(constant)));

        // allow chaining
        return *this;
    }

    /**
     *  Add a namespace to the namespace by moving it
     *  @param  ns          The namespace
     *  @return Namespace   Same object to allow chaining
     */
    Namespace &add(Namespace &&ns)
    {
        // skip when locked
        if (locked()) return *this;

        // add it to the list of namespaces
        _namespaces.push_back(std::unique_ptr<Namespace>(new Namespace(std::move(ns))));

        // allow chaining
        return *this;
    }

    /**
     *  Add a namespace to the namespace by copying it
     *  @param  ns          The namespace
     *  @return Namespace   Same object to allow chaining
     */
    Namespace &add(const Namespace &ns)
    {
        // skip when locked
        if (locked()) return *this;

        // make a copy and add it to the list of namespaces
        _namespaces.push_back(std::unique_ptr<Namespace>(new Namespace(ns)));

        // allow chaining
        return *this;
    }

    /**
     *  The total number of functions
     *  @return size_t
     */
    size_t functions() const
    {
        // 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;
    }

    /**
     *  Apply a callback to each registered function
     *
     *  The callback will be called with the name of the namespace, and
     *  a reference to the registered function.
     *
     *  @param  callback
     */
    void functions(const std::function<void(const std::string &ns, NativeFunction &func)> &callback);

    /**
     *  Apply a callback to each registered class
     *
     *  The callback will be called with the name of the namespace, and
     *  a reference to the registered class.
     *
     *  @param  callback
     */
    void classes(const std::function<void(const std::string &ns, ClassBase &clss)> &callback);

    /**
     *  Apply a callback to each registered constant
     *
     *  The callback will be called with the name of the namespace, and
     *  a reference to the registered constant
     *
     *  @param  callback
     */
    void constants(const std::function<void(const std::string &ns, Constant &constant)> &callback);

};

/**
 *  End namespace
 */
}