icinga2/lib/base/functionwrapper.hpp

407 lines
16 KiB
C++
Raw Normal View History

2013-03-25 20:47:02 +01:00
/******************************************************************************
* Icinga 2 *
2015-01-22 12:00:23 +01:00
* Copyright (C) 2012-2015 Icinga Development Team (http://www.icinga.org) *
2013-03-25 20:47:02 +01:00
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; either version 2 *
* of the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software Foundation *
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
******************************************************************************/
#ifndef SCRIPTFUNCTIONWRAPPER_H
#define SCRIPTFUNCTIONWRAPPER_H
2014-05-25 16:23:35 +02:00
#include "base/i2-base.hpp"
#include "base/value.hpp"
2013-03-25 20:47:02 +01:00
#include <vector>
#include <boost/function.hpp>
#include <boost/bind.hpp>
namespace icinga
{
Value FunctionWrapperVV(void (*function)(void), const std::vector<Value>& arguments);
Value FunctionWrapperVA(void (*function)(const std::vector<Value>&), const std::vector<Value>& arguments);
2013-03-25 20:47:02 +01:00
boost::function<Value (const std::vector<Value>& arguments)> I2_BASE_API WrapFunction(void (*function)(void));
2013-03-25 20:47:02 +01:00
template<typename TR>
Value FunctionWrapperR(TR (*function)(void), const std::vector<Value>&)
2013-03-25 20:47:02 +01:00
{
return function();
}
template<typename TR>
boost::function<Value (const std::vector<Value>& arguments)> WrapFunction(TR (*function)(void))
2013-03-25 20:47:02 +01:00
{
return boost::bind(&FunctionWrapperR<TR>, function, _1);
2013-03-25 20:47:02 +01:00
}
template<typename T0>
Value FunctionWrapperV(void (*function)(T0), const std::vector<Value>& arguments)
2013-03-25 20:47:02 +01:00
{
if (arguments.size() < 1)
BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function."));
else if (arguments.size() > 1)
BOOST_THROW_EXCEPTION(std::invalid_argument("Too many arguments for function."));
2013-03-25 20:47:02 +01:00
function(static_cast<T0>(arguments[0]));
return Empty;
}
template<typename T0>
boost::function<Value (const std::vector<Value>& arguments)> WrapFunction(void (*function)(T0))
2013-03-25 20:47:02 +01:00
{
return boost::bind(&FunctionWrapperV<T0>, function, _1);
2013-03-25 20:47:02 +01:00
}
template<typename TR, typename T0>
Value FunctionWrapperR(TR (*function)(T0), const std::vector<Value>& arguments)
2013-03-25 20:47:02 +01:00
{
if (arguments.size() < 1)
BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function."));
else if (arguments.size() > 1)
BOOST_THROW_EXCEPTION(std::invalid_argument("Too many arguments for function."));
2013-03-25 20:47:02 +01:00
return function(static_cast<T0>(arguments[0]));
}
template<typename TR, typename T0>
boost::function<Value (const std::vector<Value>& arguments)> WrapFunction(TR (*function)(T0))
2013-03-25 20:47:02 +01:00
{
return boost::bind(&FunctionWrapperR<TR, T0>, function, _1);
2013-03-25 20:47:02 +01:00
}
template<typename T0, typename T1>
Value FunctionWrapperV(void (*function)(T0, T1), const std::vector<Value>& arguments)
2013-03-25 20:47:02 +01:00
{
if (arguments.size() < 2)
BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function."));
else if (arguments.size() > 2)
BOOST_THROW_EXCEPTION(std::invalid_argument("Too many arguments for function."));
2013-03-25 20:47:02 +01:00
function(static_cast<T0>(arguments[0]),
static_cast<T1>(arguments[1]));
return Empty;
}
template<typename T0, typename T1>
boost::function<Value (const std::vector<Value>& arguments)> WrapFunction(void (*function)(T0, T1))
2013-03-25 20:47:02 +01:00
{
return boost::bind(&FunctionWrapperV<T0, T1>, function, _1);
2013-03-25 20:47:02 +01:00
}
template<typename TR, typename T0, typename T1>
Value FunctionWrapperR(TR (*function)(T0, T1), const std::vector<Value>& arguments)
2013-03-25 20:47:02 +01:00
{
if (arguments.size() < 2)
BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function."));
else if (arguments.size() > 2)
BOOST_THROW_EXCEPTION(std::invalid_argument("Too many arguments for function."));
2013-03-25 20:47:02 +01:00
return function(static_cast<T0>(arguments[0]),
static_cast<T1>(arguments[1]));
}
template<typename TR, typename T0, typename T1>
boost::function<Value (const std::vector<Value>& arguments)> WrapFunction(TR (*function)(T0, T1))
2013-03-25 20:47:02 +01:00
{
return boost::bind(&FunctionWrapperR<TR, T0, T1>, function, _1);
2013-03-25 20:47:02 +01:00
}
template<typename T0, typename T1, typename T2>
Value FunctionWrapperV(void (*function)(T0, T1, T2), const std::vector<Value>& arguments)
2013-03-25 20:47:02 +01:00
{
if (arguments.size() < 3)
BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function."));
else if (arguments.size() > 3)
BOOST_THROW_EXCEPTION(std::invalid_argument("Too many arguments for function."));
2013-03-25 20:47:02 +01:00
function(static_cast<T0>(arguments[0]),
static_cast<T1>(arguments[1]),
static_cast<T2>(arguments[2]));
return Empty;
}
template<typename T0, typename T1, typename T2>
boost::function<Value (const std::vector<Value>& arguments)> WrapFunction(void (*function)(T0, T1, T2))
2013-03-25 20:47:02 +01:00
{
return boost::bind(&FunctionWrapperV<T0, T1, T2>, function, _1);
2013-03-25 20:47:02 +01:00
}
template<typename TR, typename T0, typename T1, typename T2>
Value FunctionWrapperR(TR (*function)(T0, T1, T2), const std::vector<Value>& arguments)
2013-03-25 20:47:02 +01:00
{
if (arguments.size() < 3)
BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function."));
else if (arguments.size() > 3)
BOOST_THROW_EXCEPTION(std::invalid_argument("Too many arguments for function."));
2013-03-25 20:47:02 +01:00
return function(static_cast<T0>(arguments[0]),
static_cast<T1>(arguments[1]),
static_cast<T2>(arguments[2]));
}
template<typename TR, typename T0, typename T1, typename T2>
boost::function<Value (const std::vector<Value>& arguments)> WrapFunction(TR (*function)(T0, T1, T2))
2013-03-25 20:47:02 +01:00
{
return boost::bind(&FunctionWrapperR<TR, T0, T1, T2>, function, _1);
2013-03-25 20:47:02 +01:00
}
template<typename T0, typename T1, typename T2, typename T3>
Value FunctionWrapperV(void (*function)(T0, T1, T2, T3), const std::vector<Value>& arguments)
2013-03-25 20:47:02 +01:00
{
if (arguments.size() < 4)
BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function."));
else if (arguments.size() > 4)
BOOST_THROW_EXCEPTION(std::invalid_argument("Too many arguments for function."));
2013-03-25 20:47:02 +01:00
function(static_cast<T0>(arguments[0]),
static_cast<T1>(arguments[1]),
static_cast<T2>(arguments[2]),
static_cast<T3>(arguments[3]));
return Empty;
}
template<typename T0, typename T1, typename T2, typename T3>
boost::function<Value (const std::vector<Value>& arguments)> WrapFunction(void (*function)(T0, T1, T2, T3))
2013-03-25 20:47:02 +01:00
{
return boost::bind(&FunctionWrapperV<T0, T1, T2, T3>, function, _1);
2013-03-25 20:47:02 +01:00
}
template<typename TR, typename T0, typename T1, typename T2, typename T3>
Value FunctionWrapperR(TR (*function)(T0, T1, T2, T3), const std::vector<Value>& arguments)
2013-03-25 20:47:02 +01:00
{
if (arguments.size() < 4)
BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function."));
else if (arguments.size() > 4)
BOOST_THROW_EXCEPTION(std::invalid_argument("Too many arguments for function."));
2013-03-25 20:47:02 +01:00
return function(static_cast<T0>(arguments[0]),
static_cast<T1>(arguments[1]),
static_cast<T2>(arguments[2]),
static_cast<T3>(arguments[3]));
}
template<typename TR, typename T0, typename T1, typename T2, typename T3>
boost::function<Value (const std::vector<Value>& arguments)> WrapFunction(TR (*function)(T0, T1, T2, T3))
2013-03-25 20:47:02 +01:00
{
return boost::bind(&FunctionWrapperR<TR, T0, T1, T2, T3>, function, _1);
2013-03-25 20:47:02 +01:00
}
template<typename T0, typename T1, typename T2, typename T3, typename T4>
Value FunctionWrapperV(void (*function)(T0, T1, T2, T3, T4), const std::vector<Value>& arguments)
{
if (arguments.size() < 5)
BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function."));
else if (arguments.size() > 5)
BOOST_THROW_EXCEPTION(std::invalid_argument("Too many arguments for function."));
function(static_cast<T0>(arguments[0]),
static_cast<T1>(arguments[1]),
static_cast<T2>(arguments[2]),
static_cast<T3>(arguments[3]),
static_cast<T4>(arguments[4]));
return Empty;
}
template<typename T0, typename T1, typename T2, typename T3, typename T4>
boost::function<Value (const std::vector<Value>& arguments)> WrapFunction(void (*function)(T0, T1, T2, T3, T4))
{
return boost::bind(&FunctionWrapperV<T0, T1, T2, T3, T4>, function, _1);
}
template<typename TR, typename T0, typename T1, typename T2, typename T3, typename T4>
Value FunctionWrapperR(TR (*function)(T0, T1, T2, T3, T4), const std::vector<Value>& arguments)
{
if (arguments.size() < 5)
BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function."));
else if (arguments.size() > 5)
BOOST_THROW_EXCEPTION(std::invalid_argument("Too many arguments for function."));
return function(static_cast<T0>(arguments[0]),
static_cast<T1>(arguments[1]),
static_cast<T2>(arguments[2]),
static_cast<T3>(arguments[3]),
static_cast<T4>(arguments[4]));
}
template<typename TR, typename T0, typename T1, typename T2, typename T3, typename T4>
boost::function<Value (const std::vector<Value>& arguments)> WrapFunction(TR (*function)(T0, T1, T2, T3, T4))
{
return boost::bind(&FunctionWrapperR<TR, T0, T1, T2, T3, T4>, function, _1);
}
template<typename T0, typename T1, typename T2, typename T3, typename T4, typename T5>
Value FunctionWrapperV(void (*function)(T0, T1, T2, T3, T4, T5), const std::vector<Value>& arguments)
{
if (arguments.size() < 6)
BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function."));
else if (arguments.size() > 6)
BOOST_THROW_EXCEPTION(std::invalid_argument("Too many arguments for function."));
function(static_cast<T0>(arguments[0]),
static_cast<T1>(arguments[1]),
static_cast<T2>(arguments[2]),
static_cast<T3>(arguments[3]),
static_cast<T4>(arguments[4]),
static_cast<T5>(arguments[5]));
return Empty;
}
template<typename T0, typename T1, typename T2, typename T3, typename T4, typename T5>
boost::function<Value (const std::vector<Value>& arguments)> WrapFunction(void (*function)(T0, T1, T2, T3, T4, T5))
{
return boost::bind(&FunctionWrapperV<T0, T1, T2, T3, T4, T5>, function, _1);
}
template<typename TR, typename T0, typename T1, typename T2, typename T3, typename T4, typename T5>
Value FunctionWrapperR(TR (*function)(T0, T1, T2, T3, T4, T5), const std::vector<Value>& arguments)
{
if (arguments.size() < 6)
BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function."));
else if (arguments.size() > 6)
BOOST_THROW_EXCEPTION(std::invalid_argument("Too many arguments for function."));
return function(static_cast<T0>(arguments[0]),
static_cast<T1>(arguments[1]),
static_cast<T2>(arguments[2]),
static_cast<T3>(arguments[3]),
static_cast<T4>(arguments[4]),
static_cast<T5>(arguments[5]));
}
template<typename TR, typename T0, typename T1, typename T2, typename T3, typename T4, typename T5>
boost::function<Value (const std::vector<Value>& arguments)> WrapFunction(TR (*function)(T0, T1, T2, T3, T4, T5))
{
return boost::bind(&FunctionWrapperR<TR, T0, T1, T2, T3, T4, T5>, function, _1);
}
template<typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
Value FunctionWrapperV(void (*function)(T0, T1, T2, T3, T4, T5, T6), const std::vector<Value>& arguments)
{
if (arguments.size() < 7)
BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function."));
else if (arguments.size() > 7)
BOOST_THROW_EXCEPTION(std::invalid_argument("Too many arguments for function."));
function(static_cast<T0>(arguments[0]),
static_cast<T1>(arguments[1]),
static_cast<T2>(arguments[2]),
static_cast<T3>(arguments[3]),
static_cast<T4>(arguments[4]),
static_cast<T5>(arguments[5]),
static_cast<T6>(arguments[6]));
return Empty;
}
template<typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
boost::function<Value (const std::vector<Value>& arguments)> WrapFunction(void (*function)(T0, T1, T2, T3, T4, T5, T6))
{
return boost::bind(&FunctionWrapperV<T0, T1, T2, T3, T4, T5, T6>, function, _1);
}
template<typename TR, typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
Value FunctionWrapperR(TR (*function)(T0, T1, T2, T3, T4, T5, T6), const std::vector<Value>& arguments)
{
if (arguments.size() < 7)
BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function."));
else if (arguments.size() > 7)
BOOST_THROW_EXCEPTION(std::invalid_argument("Too many arguments for function."));
return function(static_cast<T0>(arguments[0]),
static_cast<T1>(arguments[1]),
static_cast<T2>(arguments[2]),
static_cast<T3>(arguments[3]),
static_cast<T4>(arguments[4]),
static_cast<T5>(arguments[5]),
static_cast<T6>(arguments[6]));
}
template<typename TR, typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
boost::function<Value (const std::vector<Value>& arguments)> WrapFunction(TR (*function)(T0, T1, T2, T3, T4, T5, T6))
{
return boost::bind(&FunctionWrapperR<TR, T0, T1, T2, T3, T4, T5, T6>, function, _1);
}
template<typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7>
Value FunctionWrapperV(void (*function)(T0, T1, T2, T3, T4, T5, T6, T7), const std::vector<Value>& arguments)
{
if (arguments.size() < 8)
BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function."));
else if (arguments.size() > 8)
BOOST_THROW_EXCEPTION(std::invalid_argument("Too many arguments for function."));
function(static_cast<T0>(arguments[0]),
static_cast<T1>(arguments[1]),
static_cast<T2>(arguments[2]),
static_cast<T3>(arguments[3]),
static_cast<T4>(arguments[4]),
static_cast<T5>(arguments[5]),
static_cast<T6>(arguments[6]),
static_cast<T7>(arguments[7]));
return Empty;
}
template<typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7>
boost::function<Value (const std::vector<Value>& arguments)> WrapFunction(void (*function)(T0, T1, T2, T3, T4, T5, T6, T7))
{
return boost::bind(&FunctionWrapperV<T0, T1, T2, T3, T4, T5, T6, T7>, function, _1);
}
template<typename TR, typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7>
Value FunctionWrapperR(TR (*function)(T0, T1, T2, T3, T4, T5, T6, T7), const std::vector<Value>& arguments)
{
if (arguments.size() < 8)
BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function."));
else if (arguments.size() > 8)
BOOST_THROW_EXCEPTION(std::invalid_argument("Too many arguments for function."));
return function(static_cast<T0>(arguments[0]),
static_cast<T1>(arguments[1]),
static_cast<T2>(arguments[2]),
static_cast<T3>(arguments[3]),
static_cast<T4>(arguments[4]),
static_cast<T5>(arguments[5]),
static_cast<T6>(arguments[6]),
static_cast<T7>(arguments[7]));
}
template<typename TR, typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7>
boost::function<Value (const std::vector<Value>& arguments)> WrapFunction(TR (*function)(T0, T1, T2, T3, T4, T5, T6, T7))
{
return boost::bind(&FunctionWrapperR<TR, T0, T1, T2, T3, T4, T5, T6, T7>, function, _1);
}
template<typename TR>
boost::function<TR (const std::vector<Value>& arguments)> WrapFunction(TR (*function)(const std::vector<Value>&))
{
return boost::bind<TR>(function, _1);
}
2013-03-25 20:47:02 +01:00
inline boost::function<Value (const std::vector<Value>& arguments)> WrapFunction(void (*function)(const std::vector<Value>&))
{
return boost::bind(&FunctionWrapperVA, function, _1);
}
2013-03-25 20:47:02 +01:00
}
#endif /* SCRIPTFUNCTION_H */