1 /******************************************************************************
3 * Copyright (C) 2012-2016 Icinga Development Team (https://www.icinga.org/) *
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. *
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. *
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 ******************************************************************************/
20 #ifndef SCRIPTFUNCTIONWRAPPER_H
21 #define SCRIPTFUNCTIONWRAPPER_H
23 #include "base/i2-base.hpp"
24 #include "base/value.hpp"
26 #include <boost/function.hpp>
27 #include <boost/bind.hpp>
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);
35 boost::function<Value (const std::vector<Value>& arguments)> I2_BASE_API WrapFunction(void (*function)(void));
38 Value FunctionWrapperR(TR (*function)(void), const std::vector<Value>&)
44 boost::function<Value (const std::vector<Value>& arguments)> WrapFunction(TR (*function)(void))
46 return boost::bind(&FunctionWrapperR<TR>, function, _1);
50 Value FunctionWrapperV(void (*function)(T0), const std::vector<Value>& arguments)
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."));
57 function(static_cast<T0>(arguments[0]));
63 boost::function<Value (const std::vector<Value>& arguments)> WrapFunction(void (*function)(T0))
65 return boost::bind(&FunctionWrapperV<T0>, function, _1);
68 template<typename TR, typename T0>
69 Value FunctionWrapperR(TR (*function)(T0), const std::vector<Value>& arguments)
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."));
76 return function(static_cast<T0>(arguments[0]));
79 template<typename TR, typename T0>
80 boost::function<Value (const std::vector<Value>& arguments)> WrapFunction(TR (*function)(T0))
82 return boost::bind(&FunctionWrapperR<TR, T0>, function, _1);
85 template<typename T0, typename T1>
86 Value FunctionWrapperV(void (*function)(T0, T1), const std::vector<Value>& arguments)
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."));
93 function(static_cast<T0>(arguments[0]),
94 static_cast<T1>(arguments[1]));
99 template<typename T0, typename T1>
100 boost::function<Value (const std::vector<Value>& arguments)> WrapFunction(void (*function)(T0, T1))
102 return boost::bind(&FunctionWrapperV<T0, T1>, function, _1);
105 template<typename TR, typename T0, typename T1>
106 Value FunctionWrapperR(TR (*function)(T0, T1), const std::vector<Value>& arguments)
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."));
113 return function(static_cast<T0>(arguments[0]),
114 static_cast<T1>(arguments[1]));
117 template<typename TR, typename T0, typename T1>
118 boost::function<Value (const std::vector<Value>& arguments)> WrapFunction(TR (*function)(T0, T1))
120 return boost::bind(&FunctionWrapperR<TR, T0, T1>, function, _1);
123 template<typename T0, typename T1, typename T2>
124 Value FunctionWrapperV(void (*function)(T0, T1, T2), const std::vector<Value>& arguments)
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."));
131 function(static_cast<T0>(arguments[0]),
132 static_cast<T1>(arguments[1]),
133 static_cast<T2>(arguments[2]));
138 template<typename T0, typename T1, typename T2>
139 boost::function<Value (const std::vector<Value>& arguments)> WrapFunction(void (*function)(T0, T1, T2))
141 return boost::bind(&FunctionWrapperV<T0, T1, T2>, function, _1);
144 template<typename TR, typename T0, typename T1, typename T2>
145 Value FunctionWrapperR(TR (*function)(T0, T1, T2), const std::vector<Value>& arguments)
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."));
152 return function(static_cast<T0>(arguments[0]),
153 static_cast<T1>(arguments[1]),
154 static_cast<T2>(arguments[2]));
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))
160 return boost::bind(&FunctionWrapperR<TR, T0, T1, T2>, function, _1);
163 template<typename T0, typename T1, typename T2, typename T3>
164 Value FunctionWrapperV(void (*function)(T0, T1, T2, T3), const std::vector<Value>& arguments)
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."));
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]));
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))
182 return boost::bind(&FunctionWrapperV<T0, T1, T2, T3>, function, _1);
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)
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."));
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]));
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))
202 return boost::bind(&FunctionWrapperR<TR, T0, T1, T2, T3>, function, _1);
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)
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."));
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]));
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))
225 return boost::bind(&FunctionWrapperV<T0, T1, T2, T3, T4>, function, _1);
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)
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."));
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]));
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))
246 return boost::bind(&FunctionWrapperR<TR, T0, T1, T2, T3, T4>, function, _1);
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)
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."));
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]));
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))
270 return boost::bind(&FunctionWrapperV<T0, T1, T2, T3, T4, T5>, function, _1);
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)
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."));
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]));
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))
292 return boost::bind(&FunctionWrapperR<TR, T0, T1, T2, T3, T4, T5>, function, _1);
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)
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."));
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]));
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))
317 return boost::bind(&FunctionWrapperV<T0, T1, T2, T3, T4, T5, T6>, function, _1);
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)
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."));
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]));
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))
340 return boost::bind(&FunctionWrapperR<TR, T0, T1, T2, T3, T4, T5, T6>, function, _1);
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)
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."));
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]));
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))
366 return boost::bind(&FunctionWrapperV<T0, T1, T2, T3, T4, T5, T6, T7>, function, _1);
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)
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."));
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]));
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))
390 return boost::bind(&FunctionWrapperR<TR, T0, T1, T2, T3, T4, T5, T6, T7>, function, _1);
393 template<typename TR>
394 boost::function<TR (const std::vector<Value>& arguments)> WrapFunction(TR (*function)(const std::vector<Value>&))
396 return boost::bind<TR>(function, _1);
399 inline boost::function<Value (const std::vector<Value>& arguments)> WrapFunction(void (*function)(const std::vector<Value>&))
401 return boost::bind(&FunctionWrapperVA, function, _1);
406 #endif /* SCRIPTFUNCTION_H */