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
|
/**
* 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
{
protected:
/**
* Name of the namespace
* @var string
*/
std::string _name;
/**
* Functions defined in the namespace
* @var list
*/
std::list<std::shared_ptr<Function>> _functions;
/**
* Classes defined in the namespace
* @var list
*/
std::list<std::shared_ptr<ClassBase>> _classes;
/**
* Namespaces defined inside the namespace
* @var list
*/
std::list<std::shared_ptr<Namespace>> _namespaces;
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)
{
// 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)
{
// 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)
{
// 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)
{
// 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 namespace to the namespace by moving it
* @param ns The namespace
* @return Namespace Same object to allow chaining
*/
Namespace &add(Namespace &&ns)
{
// 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)
{
// 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()
{
// 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 apply(const std::function<void(const std::string &ns, Function &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 apply(const std::function<void(const std::string &ns, ClassBase &clss)> &callback);
};
/**
* End namespace
*/
}
|