]> granicus.if.org Git - icinga2/blob - lib/base/functionwrapper.hpp
Update copyright headers for 2016
[icinga2] / lib / base / functionwrapper.hpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2016 Icinga Development Team (https://www.icinga.org/)  *
4  *                                                                            *
5  * This program is free software; you can redistribute it and/or              *
6  * modify it under the terms of the GNU General Public License                *
7  * as published by the Free Software Foundation; either version 2             *
8  * of the License, or (at your option) any later version.                     *
9  *                                                                            *
10  * This program is distributed in the hope that it will be useful,            *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
13  * GNU General Public License for more details.                               *
14  *                                                                            *
15  * You should have received a copy of the GNU General Public License          *
16  * along with this program; if not, write to the Free Software Foundation     *
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
18  ******************************************************************************/
19
20 #ifndef SCRIPTFUNCTIONWRAPPER_H
21 #define SCRIPTFUNCTIONWRAPPER_H
22
23 #include "base/i2-base.hpp"
24 #include "base/value.hpp"
25 #include <vector>
26 #include <boost/function.hpp>
27 #include <boost/bind.hpp>
28
29 namespace icinga
30 {
31
32 Value FunctionWrapperVV(void (*function)(void), const std::vector<Value>& arguments);
33 Value FunctionWrapperVA(void (*function)(const std::vector<Value>&), const std::vector<Value>& arguments);
34
35 boost::function<Value (const std::vector<Value>& arguments)> I2_BASE_API WrapFunction(void (*function)(void));
36
37 template<typename TR>
38 Value FunctionWrapperR(TR (*function)(void), const std::vector<Value>&)
39 {
40         return function();
41 }
42
43 template<typename TR>
44 boost::function<Value (const std::vector<Value>& arguments)> WrapFunction(TR (*function)(void))
45 {
46         return boost::bind(&FunctionWrapperR<TR>, function, _1);
47 }
48
49 template<typename T0>
50 Value FunctionWrapperV(void (*function)(T0), const std::vector<Value>& arguments)
51 {
52         if (arguments.size() < 1)
53                 BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function."));
54         else if (arguments.size() > 1)
55                 BOOST_THROW_EXCEPTION(std::invalid_argument("Too many arguments for function."));
56
57         function(static_cast<T0>(arguments[0]));
58
59         return Empty;
60 }
61
62 template<typename T0>
63 boost::function<Value (const std::vector<Value>& arguments)> WrapFunction(void (*function)(T0))
64 {
65         return boost::bind(&FunctionWrapperV<T0>, function, _1);
66 }
67
68 template<typename TR, typename T0>
69 Value FunctionWrapperR(TR (*function)(T0), const std::vector<Value>& arguments)
70 {
71         if (arguments.size() < 1)
72                 BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function."));
73         else if (arguments.size() > 1)
74                 BOOST_THROW_EXCEPTION(std::invalid_argument("Too many arguments for function."));
75
76         return function(static_cast<T0>(arguments[0]));
77 }
78
79 template<typename TR, typename T0>
80 boost::function<Value (const std::vector<Value>& arguments)> WrapFunction(TR (*function)(T0))
81 {
82         return boost::bind(&FunctionWrapperR<TR, T0>, function, _1);
83 }
84
85 template<typename T0, typename T1>
86 Value FunctionWrapperV(void (*function)(T0, T1), const std::vector<Value>& arguments)
87 {
88         if (arguments.size() < 2)
89                 BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function."));
90         else if (arguments.size() > 2)
91                 BOOST_THROW_EXCEPTION(std::invalid_argument("Too many arguments for function."));
92
93         function(static_cast<T0>(arguments[0]),
94             static_cast<T1>(arguments[1]));
95
96         return Empty;
97 }
98
99 template<typename T0, typename T1>
100 boost::function<Value (const std::vector<Value>& arguments)> WrapFunction(void (*function)(T0, T1))
101 {
102         return boost::bind(&FunctionWrapperV<T0, T1>, function, _1);
103 }
104
105 template<typename TR, typename T0, typename T1>
106 Value FunctionWrapperR(TR (*function)(T0, T1), const std::vector<Value>& arguments)
107 {
108         if (arguments.size() < 2)
109                 BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function."));
110         else if (arguments.size() > 2)
111                 BOOST_THROW_EXCEPTION(std::invalid_argument("Too many arguments for function."));
112
113         return function(static_cast<T0>(arguments[0]),
114             static_cast<T1>(arguments[1]));
115 }
116
117 template<typename TR, typename T0, typename T1>
118 boost::function<Value (const std::vector<Value>& arguments)> WrapFunction(TR (*function)(T0, T1))
119 {
120         return boost::bind(&FunctionWrapperR<TR, T0, T1>, function, _1);
121 }
122
123 template<typename T0, typename T1, typename T2>
124 Value FunctionWrapperV(void (*function)(T0, T1, T2), const std::vector<Value>& arguments)
125 {
126         if (arguments.size() < 3)
127                 BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function."));
128         else if (arguments.size() > 3)
129                 BOOST_THROW_EXCEPTION(std::invalid_argument("Too many arguments for function."));
130
131         function(static_cast<T0>(arguments[0]),
132             static_cast<T1>(arguments[1]),
133             static_cast<T2>(arguments[2]));
134
135         return Empty;
136 }
137
138 template<typename T0, typename T1, typename T2>
139 boost::function<Value (const std::vector<Value>& arguments)> WrapFunction(void (*function)(T0, T1, T2))
140 {
141         return boost::bind(&FunctionWrapperV<T0, T1, T2>, function, _1);
142 }
143
144 template<typename TR, typename T0, typename T1, typename T2>
145 Value FunctionWrapperR(TR (*function)(T0, T1, T2), const std::vector<Value>& arguments)
146 {
147         if (arguments.size() < 3)
148                 BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function."));
149         else if (arguments.size() > 3)
150                 BOOST_THROW_EXCEPTION(std::invalid_argument("Too many arguments for function."));
151
152         return function(static_cast<T0>(arguments[0]),
153             static_cast<T1>(arguments[1]),
154             static_cast<T2>(arguments[2]));
155 }
156
157 template<typename TR, typename T0, typename T1, typename T2>
158 boost::function<Value (const std::vector<Value>& arguments)> WrapFunction(TR (*function)(T0, T1, T2))
159 {
160         return boost::bind(&FunctionWrapperR<TR, T0, T1, T2>, function, _1);
161 }
162
163 template<typename T0, typename T1, typename T2, typename T3>
164 Value FunctionWrapperV(void (*function)(T0, T1, T2, T3), const std::vector<Value>& arguments)
165 {
166         if (arguments.size() < 4)
167                 BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function."));
168         else if (arguments.size() > 4)
169                 BOOST_THROW_EXCEPTION(std::invalid_argument("Too many arguments for function."));
170
171         function(static_cast<T0>(arguments[0]),
172             static_cast<T1>(arguments[1]),
173             static_cast<T2>(arguments[2]),
174             static_cast<T3>(arguments[3]));
175
176         return Empty;
177 }
178
179 template<typename T0, typename T1, typename T2, typename T3>
180 boost::function<Value (const std::vector<Value>& arguments)> WrapFunction(void (*function)(T0, T1, T2, T3))
181 {
182         return boost::bind(&FunctionWrapperV<T0, T1, T2, T3>, function, _1);
183 }
184
185 template<typename TR, typename T0, typename T1, typename T2, typename T3>
186 Value FunctionWrapperR(TR (*function)(T0, T1, T2, T3), const std::vector<Value>& arguments)
187 {
188         if (arguments.size() < 4)
189                 BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function."));
190         else if (arguments.size() > 4)
191                 BOOST_THROW_EXCEPTION(std::invalid_argument("Too many arguments for function."));
192
193         return function(static_cast<T0>(arguments[0]),
194             static_cast<T1>(arguments[1]),
195             static_cast<T2>(arguments[2]),
196             static_cast<T3>(arguments[3]));
197 }
198
199 template<typename TR, typename T0, typename T1, typename T2, typename T3>
200 boost::function<Value (const std::vector<Value>& arguments)> WrapFunction(TR (*function)(T0, T1, T2, T3))
201 {
202         return boost::bind(&FunctionWrapperR<TR, T0, T1, T2, T3>, function, _1);
203 }
204
205 template<typename T0, typename T1, typename T2, typename T3, typename T4>
206 Value FunctionWrapperV(void (*function)(T0, T1, T2, T3, T4), const std::vector<Value>& arguments)
207 {
208         if (arguments.size() < 5)
209                 BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function."));
210         else if (arguments.size() > 5)
211                 BOOST_THROW_EXCEPTION(std::invalid_argument("Too many arguments for function."));
212
213         function(static_cast<T0>(arguments[0]),
214             static_cast<T1>(arguments[1]),
215             static_cast<T2>(arguments[2]),
216             static_cast<T3>(arguments[3]),
217             static_cast<T4>(arguments[4]));
218
219         return Empty;
220 }
221
222 template<typename T0, typename T1, typename T2, typename T3, typename T4>
223 boost::function<Value (const std::vector<Value>& arguments)> WrapFunction(void (*function)(T0, T1, T2, T3, T4))
224 {
225         return boost::bind(&FunctionWrapperV<T0, T1, T2, T3, T4>, function, _1);
226 }
227
228 template<typename TR, typename T0, typename T1, typename T2, typename T3, typename T4>
229 Value FunctionWrapperR(TR (*function)(T0, T1, T2, T3, T4), const std::vector<Value>& arguments)
230 {
231         if (arguments.size() < 5)
232                 BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function."));
233         else if (arguments.size() > 5)
234                 BOOST_THROW_EXCEPTION(std::invalid_argument("Too many arguments for function."));
235
236         return function(static_cast<T0>(arguments[0]),
237             static_cast<T1>(arguments[1]),
238             static_cast<T2>(arguments[2]),
239             static_cast<T3>(arguments[3]),
240             static_cast<T4>(arguments[4]));
241 }
242
243 template<typename TR, typename T0, typename T1, typename T2, typename T3, typename T4>
244 boost::function<Value (const std::vector<Value>& arguments)> WrapFunction(TR (*function)(T0, T1, T2, T3, T4))
245 {
246         return boost::bind(&FunctionWrapperR<TR, T0, T1, T2, T3, T4>, function, _1);
247 }
248
249 template<typename T0, typename T1, typename T2, typename T3, typename T4, typename T5>
250 Value FunctionWrapperV(void (*function)(T0, T1, T2, T3, T4, T5), const std::vector<Value>& arguments)
251 {
252         if (arguments.size() < 6)
253                 BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function."));
254         else if (arguments.size() > 6)
255                 BOOST_THROW_EXCEPTION(std::invalid_argument("Too many arguments for function."));
256
257         function(static_cast<T0>(arguments[0]),
258             static_cast<T1>(arguments[1]),
259             static_cast<T2>(arguments[2]),
260             static_cast<T3>(arguments[3]),
261             static_cast<T4>(arguments[4]),
262             static_cast<T5>(arguments[5]));
263
264         return Empty;
265 }
266
267 template<typename T0, typename T1, typename T2, typename T3, typename T4, typename T5>
268 boost::function<Value (const std::vector<Value>& arguments)> WrapFunction(void (*function)(T0, T1, T2, T3, T4, T5))
269 {
270         return boost::bind(&FunctionWrapperV<T0, T1, T2, T3, T4, T5>, function, _1);
271 }
272
273 template<typename TR, typename T0, typename T1, typename T2, typename T3, typename T4, typename T5>
274 Value FunctionWrapperR(TR (*function)(T0, T1, T2, T3, T4, T5), const std::vector<Value>& arguments)
275 {
276         if (arguments.size() < 6)
277                 BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function."));
278         else if (arguments.size() > 6)
279                 BOOST_THROW_EXCEPTION(std::invalid_argument("Too many arguments for function."));
280
281         return function(static_cast<T0>(arguments[0]),
282             static_cast<T1>(arguments[1]),
283             static_cast<T2>(arguments[2]),
284             static_cast<T3>(arguments[3]),
285             static_cast<T4>(arguments[4]),
286             static_cast<T5>(arguments[5]));
287 }
288
289 template<typename TR, typename T0, typename T1, typename T2, typename T3, typename T4, typename T5>
290 boost::function<Value (const std::vector<Value>& arguments)> WrapFunction(TR (*function)(T0, T1, T2, T3, T4, T5))
291 {
292         return boost::bind(&FunctionWrapperR<TR, T0, T1, T2, T3, T4, T5>, function, _1);
293 }
294
295 template<typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
296 Value FunctionWrapperV(void (*function)(T0, T1, T2, T3, T4, T5, T6), const std::vector<Value>& arguments)
297 {
298         if (arguments.size() < 7)
299                 BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function."));
300         else if (arguments.size() > 7)
301                 BOOST_THROW_EXCEPTION(std::invalid_argument("Too many arguments for function."));
302
303         function(static_cast<T0>(arguments[0]),
304             static_cast<T1>(arguments[1]),
305             static_cast<T2>(arguments[2]),
306             static_cast<T3>(arguments[3]),
307             static_cast<T4>(arguments[4]),
308             static_cast<T5>(arguments[5]),
309             static_cast<T6>(arguments[6]));
310
311         return Empty;
312 }
313
314 template<typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
315 boost::function<Value (const std::vector<Value>& arguments)> WrapFunction(void (*function)(T0, T1, T2, T3, T4, T5, T6))
316 {
317         return boost::bind(&FunctionWrapperV<T0, T1, T2, T3, T4, T5, T6>, function, _1);
318 }
319
320 template<typename TR, typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
321 Value FunctionWrapperR(TR (*function)(T0, T1, T2, T3, T4, T5, T6), const std::vector<Value>& arguments)
322 {
323         if (arguments.size() < 7)
324                 BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function."));
325         else if (arguments.size() > 7)
326                 BOOST_THROW_EXCEPTION(std::invalid_argument("Too many arguments for function."));
327
328         return function(static_cast<T0>(arguments[0]),
329             static_cast<T1>(arguments[1]),
330             static_cast<T2>(arguments[2]),
331             static_cast<T3>(arguments[3]),
332             static_cast<T4>(arguments[4]),
333             static_cast<T5>(arguments[5]),
334             static_cast<T6>(arguments[6]));
335 }
336
337 template<typename TR, typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
338 boost::function<Value (const std::vector<Value>& arguments)> WrapFunction(TR (*function)(T0, T1, T2, T3, T4, T5, T6))
339 {
340         return boost::bind(&FunctionWrapperR<TR, T0, T1, T2, T3, T4, T5, T6>, function, _1);
341 }
342
343 template<typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7>
344 Value FunctionWrapperV(void (*function)(T0, T1, T2, T3, T4, T5, T6, T7), const std::vector<Value>& arguments)
345 {
346         if (arguments.size() < 8)
347                 BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function."));
348         else if (arguments.size() > 8)
349                 BOOST_THROW_EXCEPTION(std::invalid_argument("Too many arguments for function."));
350
351         function(static_cast<T0>(arguments[0]),
352             static_cast<T1>(arguments[1]),
353             static_cast<T2>(arguments[2]),
354             static_cast<T3>(arguments[3]),
355             static_cast<T4>(arguments[4]),
356             static_cast<T5>(arguments[5]),
357             static_cast<T6>(arguments[6]),
358             static_cast<T7>(arguments[7]));
359
360         return Empty;
361 }
362
363 template<typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7>
364 boost::function<Value (const std::vector<Value>& arguments)> WrapFunction(void (*function)(T0, T1, T2, T3, T4, T5, T6, T7))
365 {
366         return boost::bind(&FunctionWrapperV<T0, T1, T2, T3, T4, T5, T6, T7>, function, _1);
367 }
368
369 template<typename TR, typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7>
370 Value FunctionWrapperR(TR (*function)(T0, T1, T2, T3, T4, T5, T6, T7), const std::vector<Value>& arguments)
371 {
372         if (arguments.size() < 8)
373                 BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function."));
374         else if (arguments.size() > 8)
375                 BOOST_THROW_EXCEPTION(std::invalid_argument("Too many arguments for function."));
376
377         return function(static_cast<T0>(arguments[0]),
378             static_cast<T1>(arguments[1]),
379             static_cast<T2>(arguments[2]),
380             static_cast<T3>(arguments[3]),
381             static_cast<T4>(arguments[4]),
382             static_cast<T5>(arguments[5]),
383             static_cast<T6>(arguments[6]),
384             static_cast<T7>(arguments[7]));
385 }
386
387 template<typename TR, typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7>
388 boost::function<Value (const std::vector<Value>& arguments)> WrapFunction(TR (*function)(T0, T1, T2, T3, T4, T5, T6, T7))
389 {
390         return boost::bind(&FunctionWrapperR<TR, T0, T1, T2, T3, T4, T5, T6, T7>, function, _1);
391 }
392
393 template<typename TR>
394 boost::function<TR (const std::vector<Value>& arguments)> WrapFunction(TR (*function)(const std::vector<Value>&))
395 {
396         return boost::bind<TR>(function, _1);
397 }
398
399 inline boost::function<Value (const std::vector<Value>& arguments)> WrapFunction(void (*function)(const std::vector<Value>&))
400 {
401         return boost::bind(&FunctionWrapperVA, function, _1);
402 }
403
404 }
405
406 #endif /* SCRIPTFUNCTION_H */