#include "base/i2-base.hpp"
#include "base/value.hpp"
#include <vector>
+#include <boost/function_types/function_type.hpp>
+#include <boost/function_types/parameter_types.hpp>
+#include <boost/function_types/result_type.hpp>
#include <boost/function_types/function_arity.hpp>
#include <type_traits>
namespace icinga
{
-inline std::function<Value (const std::vector<Value>&)> WrapFunction(const std::function<Value(const std::vector<Value>&)>& function)
+template<typename FuncType>
+typename std::enable_if<
+ std::is_class<FuncType>::value &&
+ std::is_same<typename boost::function_types::result_type<decltype(&FuncType::operator())>::type, Value>::value &&
+ std::is_same<typename boost::mpl::at_c<boost::function_types::parameter_types<decltype(&FuncType::operator())>, 1>::type, const std::vector<Value>&>::value &&
+ boost::function_types::function_arity<decltype(&FuncType::operator())>::value == 2,
+ std::function<Value (const std::vector<Value>&)>>::type
+WrapFunction(FuncType function)
{
return function;
}
return Empty;
};
}
+
template<typename Return>
std::function<Value (const std::vector<Value>&)> WrapFunction(Return (*function)(const std::vector<Value>&))
{
- return [function](const std::vector<Value>& arguments) {
- return static_cast<Value>(function(arguments));
- };
+ return std::bind(function, _1);
}
template <std::size_t... Indices>
template <std::size_t N>
using BuildIndices = typename build_indices<N>::type;
-struct unpack_caller
+struct UnpackCaller
{
private:
template <typename FuncType, size_t... I>
- auto call(FuncType f, const std::vector<Value>& args, indices<I...>) -> decltype(f(args[I]...))
+ auto Invoke(FuncType f, const std::vector<Value>& args, indices<I...>) -> decltype(f(args[I]...))
{
return f(args[I]...);
}
public:
- template <typename FuncType>
- auto operator () (FuncType f, const std::vector<Value>& args)
- -> decltype(call(f, args, BuildIndices<boost::function_types::function_arity<typename boost::remove_pointer<FuncType>::type>::value>{}))
+ template <typename FuncType, int Arity>
+ auto operator() (FuncType f, const std::vector<Value>& args)
+ -> decltype(Invoke(f, args, BuildIndices<Arity>{}))
{
- return call(f, args, BuildIndices<boost::function_types::function_arity<typename boost::remove_pointer<FuncType>::type>::value>{});
+ return Invoke(f, args, BuildIndices<Arity>{});
}
};
-enum class enabler_t {};
+template<typename FuncType, int Arity, typename ReturnType>
+struct FunctionWrapper
+{
+ static Value Invoke(FuncType function, const std::vector<Value>& arguments)
+ {
+ return UnpackCaller().operator()<FuncType, Arity>(function, arguments);
+ }
+};
-template<bool T>
-using EnableIf = typename std::enable_if<T, enabler_t>::type;
+template<typename FuncType, int Arity>
+struct FunctionWrapper<FuncType, Arity, void>
+{
+ static Value Invoke(FuncType function, const std::vector<Value>& arguments)
+ {
+ UnpackCaller().operator()<FuncType, Arity>(function, arguments);
+ return Empty;
+ }
+};
template<typename FuncType>
-std::function<Value (const std::vector<Value>&)> WrapFunction(FuncType function,
- EnableIf<std::is_same<decltype(unpack_caller()(FuncType(), std::vector<Value>())), void>::value>* = 0)
+typename std::enable_if<
+ std::is_function<typename std::remove_pointer<FuncType>::type>::value && !std::is_same<FuncType, Value(*)(const std::vector<Value>&)>::value,
+ std::function<Value (const std::vector<Value>&)>>::type
+WrapFunction(FuncType function)
{
return [function](const std::vector<Value>& arguments) {
- constexpr int arity = boost::function_types::function_arity<typename boost::remove_pointer<FuncType>::type>::value;
+ constexpr size_t arity = boost::function_types::function_arity<typename std::remove_pointer<FuncType>::type>::value;
- if (arguments.size() < arity)
- BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function."));
- else if (arguments.size() > arity)
- BOOST_THROW_EXCEPTION(std::invalid_argument("Too many arguments for function."));
+ if (arity > 0) {
+ if (arguments.size() < arity)
+ BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function."));
+ else if (arguments.size() > arity)
+ BOOST_THROW_EXCEPTION(std::invalid_argument("Too many arguments for function."));
+ }
- unpack_caller()(function, arguments);
- return Empty;
+ using ReturnType = decltype(UnpackCaller().operator()<FuncType, arity>(*static_cast<FuncType *>(nullptr), std::vector<Value>()));
+
+ return FunctionWrapper<FuncType, arity, ReturnType>::Invoke(function, arguments);
};
}
template<typename FuncType>
-std::function<Value (const std::vector<Value>&)> WrapFunction(FuncType function,
- EnableIf<!std::is_same<decltype(unpack_caller()(FuncType(), std::vector<Value>())), void>::value>* = 0)
+typename std::enable_if<
+ std::is_class<FuncType>::value &&
+ !(std::is_same<typename boost::function_types::result_type<decltype(&FuncType::operator())>::type, Value>::value &&
+ std::is_same<typename boost::mpl::at_c<boost::function_types::parameter_types<decltype(&FuncType::operator())>, 1>::type, const std::vector<Value>&>::value &&
+ boost::function_types::function_arity<decltype(&FuncType::operator())>::value == 2),
+ std::function<Value (const std::vector<Value>&)>>::type
+WrapFunction(FuncType function)
{
+ using FuncTypeInvoker = decltype(&FuncType::operator());
+
return [function](const std::vector<Value>& arguments) {
- constexpr int arity = boost::function_types::function_arity<typename boost::remove_pointer<FuncType>::type>::value;
+ constexpr size_t arity = boost::function_types::function_arity<FuncTypeInvoker>::value - 1;
if (arity > 0) {
if (arguments.size() < arity)
BOOST_THROW_EXCEPTION(std::invalid_argument("Too many arguments for function."));
}
- return unpack_caller()(function, arguments);
+ using ReturnType = decltype(UnpackCaller().operator()<FuncType, arity>(*static_cast<FuncType *>(nullptr), std::vector<Value>()));
+
+ return FunctionWrapper<FuncType, arity, ReturnType>::Invoke(function, arguments);
};
}
return false;
}
-Value MacroProcessor::InternalResolveMacrosShim(const std::vector<Value>& args, const ResolverList& resolvers,
- const CheckResult::Ptr& cr, const MacroProcessor::EscapeCallback& escapeFn, const Dictionary::Ptr& resolvedMacros,
- bool useResolvedMacros, int recursionLevel)
-{
- if (args.size() < 1)
- BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function"));
-
- String missingMacro;
-
- return MacroProcessor::InternalResolveMacros(args[0], resolvers, cr, &missingMacro, escapeFn,
- resolvedMacros, useResolvedMacros, recursionLevel);
-}
-
-Value MacroProcessor::InternalResolveArgumentsShim(const std::vector<Value>& args, const ResolverList& resolvers,
- const CheckResult::Ptr& cr, const Dictionary::Ptr& resolvedMacros,
- bool useResolvedMacros, int recursionLevel)
-{
- if (args.size() < 2)
- BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function"));
-
- return MacroProcessor::ResolveArguments(args[0], args[1], resolvers, cr,
- resolvedMacros, useResolvedMacros, recursionLevel);
-}
-
Value MacroProcessor::EvaluateFunction(const Function::Ptr& func, const ResolverList& resolvers,
const CheckResult::Ptr& cr, const MacroProcessor::EscapeCallback& escapeFn,
const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros, int recursionLevel)
resolvers_this->Set(resolver.first, resolver.second);
}
- resolvers_this->Set("macro", new Function("macro (temporary)", std::bind(&MacroProcessor::InternalResolveMacrosShim,
- _1, std::cref(resolvers), cr, MacroProcessor::EscapeCallback(), resolvedMacros, useResolvedMacros,
- recursionLevel + 1), { "str" }));
- resolvers_this->Set("resolve_arguments", new Function("resolve_arguments (temporary)", std::bind(&MacroProcessor::InternalResolveArgumentsShim,
- _1, std::cref(resolvers), cr, resolvedMacros, useResolvedMacros,
- recursionLevel + 1)));
+ auto internalResolveMacrosShim = [resolvers, cr, resolvedMacros, useResolvedMacros, recursionLevel](const std::vector<Value>& args) {
+ if (args.size() < 1)
+ BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function"));
+
+ String missingMacro;
+
+ return MacroProcessor::InternalResolveMacros(args[0], resolvers, cr, &missingMacro, MacroProcessor::EscapeCallback(),
+ resolvedMacros, useResolvedMacros, recursionLevel);
+ };
+
+ resolvers_this->Set("macro", new Function("macro (temporary)", internalResolveMacrosShim, { "str" }));
+
+ auto internalResolveArgumentsShim = [resolvers, cr, resolvedMacros, useResolvedMacros, recursionLevel](const std::vector<Value>& args) {
+ if (args.size() < 2)
+ BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function"));
+
+ return MacroProcessor::ResolveArguments(args[0], args[1], resolvers, cr,
+ resolvedMacros, useResolvedMacros, recursionLevel + 1);
+ };
+
+ resolvers_this->Set("resolve_arguments", new Function("resolve_arguments (temporary)", internalResolveArgumentsShim, { "command", "args" }));
return func->InvokeThis(resolvers_this);
}