1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2 
3 #ifndef FUNCTIONWRAPPER_H
4 #define FUNCTIONWRAPPER_H
5 
6 #include "base/i2-base.hpp"
7 #include "base/value.hpp"
8 #include <boost/function_types/function_type.hpp>
9 #include <boost/function_types/parameter_types.hpp>
10 #include <boost/function_types/result_type.hpp>
11 #include <boost/function_types/function_arity.hpp>
12 #include <vector>
13 
14 namespace icinga
15 {
16 
17 template<typename FuncType>
18 typename std::enable_if<
19     std::is_class<FuncType>::value &&
20     std::is_same<typename boost::function_types::result_type<decltype(&FuncType::operator())>::type, Value>::value &&
21 	boost::function_types::function_arity<decltype(&FuncType::operator())>::value == 2,
22     std::function<Value (const std::vector<Value>&)>>::type
WrapFunction(FuncType function)23 WrapFunction(FuncType function)
24 {
25 	static_assert(std::is_same<typename boost::mpl::at_c<typename boost::function_types::parameter_types<decltype(&FuncType::operator())>, 1>::type, const std::vector<Value>&>::value, "Argument type must be const std::vector<Value>");
26 	return function;
27 }
28 
WrapFunction(void (* function)(const std::vector<Value> &))29 inline std::function<Value (const std::vector<Value>&)> WrapFunction(void (*function)(const std::vector<Value>&))
30 {
31 	return [function](const std::vector<Value>& arguments) {
32 		function(arguments);
33 		return Empty;
34 	};
35 }
36 
37 template<typename Return>
WrapFunction(Return (* function)(const std::vector<Value> &))38 std::function<Value (const std::vector<Value>&)> WrapFunction(Return (*function)(const std::vector<Value>&))
39 {
40 	return [function](const std::vector<Value>& values) -> Value { return function(values); };
41 }
42 
43 template <std::size_t... Indices>
44 struct indices {
45 	using next = indices<Indices..., sizeof...(Indices)>;
46 };
47 
48 template <std::size_t N>
49 struct build_indices {
50 	using type = typename build_indices<N-1>::type::next;
51 };
52 
53 template <>
54 struct build_indices<0> {
55 	using type = indices<>;
56 };
57 
58 template <std::size_t N>
59 using BuildIndices = typename build_indices<N>::type;
60 
61 struct UnpackCaller
62 {
63 private:
64 	template <typename FuncType, size_t... I>
Invokeicinga::UnpackCaller65 	auto Invoke(FuncType f, const std::vector<Value>& args, indices<I...>) -> decltype(f(args[I]...))
66 	{
67 		return f(args[I]...);
68 	}
69 
70 public:
71 	template <typename FuncType, int Arity>
operator ()icinga::UnpackCaller72 	auto operator() (FuncType f, const std::vector<Value>& args) -> decltype(Invoke(f, args, BuildIndices<Arity>{}))
73 	{
74 		return Invoke(f, args, BuildIndices<Arity>{});
75 	}
76 };
77 
78 template<typename FuncType, int Arity, typename ReturnType>
79 struct FunctionWrapper
80 {
Invokeicinga::FunctionWrapper81 	static Value Invoke(FuncType function, const std::vector<Value>& arguments)
82 	{
83 		return UnpackCaller().operator()<FuncType, Arity>(function, arguments);
84 	}
85 };
86 
87 template<typename FuncType, int Arity>
88 struct FunctionWrapper<FuncType, Arity, void>
89 {
Invokeicinga::FunctionWrapper90 	static Value Invoke(FuncType function, const std::vector<Value>& arguments)
91 	{
92 		UnpackCaller().operator()<FuncType, Arity>(function, arguments);
93 		return Empty;
94 	}
95 };
96 
97 template<typename FuncType>
98 typename std::enable_if<
99 	std::is_function<typename std::remove_pointer<FuncType>::type>::value && !std::is_same<FuncType, Value(*)(const std::vector<Value>&)>::value,
100 	std::function<Value (const std::vector<Value>&)>>::type
WrapFunction(FuncType function)101 WrapFunction(FuncType function)
102 {
103 	return [function](const std::vector<Value>& arguments) {
104 		constexpr size_t arity = boost::function_types::function_arity<typename std::remove_pointer<FuncType>::type>::value;
105 
106 		if (arity > 0) {
107 			if (arguments.size() < arity)
108 				BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function."));
109 			else if (arguments.size() > arity)
110 				BOOST_THROW_EXCEPTION(std::invalid_argument("Too many arguments for function."));
111 		}
112 
113 		using ReturnType = decltype(UnpackCaller().operator()<FuncType, arity>(*static_cast<FuncType *>(nullptr), std::vector<Value>()));
114 
115 		return FunctionWrapper<FuncType, arity, ReturnType>::Invoke(function, arguments);
116 	};
117 }
118 
119 template<typename FuncType>
120 typename std::enable_if<
121     std::is_class<FuncType>::value &&
122     !(std::is_same<typename boost::function_types::result_type<decltype(&FuncType::operator())>::type, Value>::value &&
123 	boost::function_types::function_arity<decltype(&FuncType::operator())>::value == 2),
124     std::function<Value (const std::vector<Value>&)>>::type
WrapFunction(FuncType function)125 WrapFunction(FuncType function)
126 {
127 	static_assert(!std::is_same<typename boost::mpl::at_c<typename boost::function_types::parameter_types<decltype(&FuncType::operator())>, 1>::type, const std::vector<Value>&>::value, "Argument type must be const std::vector<Value>");
128 
129 	using FuncTypeInvoker = decltype(&FuncType::operator());
130 
131 	return [function](const std::vector<Value>& arguments) {
132 		constexpr size_t arity = boost::function_types::function_arity<FuncTypeInvoker>::value - 1;
133 
134 		if (arity > 0) {
135 			if (arguments.size() < arity)
136 				BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function."));
137 			else if (arguments.size() > arity)
138 				BOOST_THROW_EXCEPTION(std::invalid_argument("Too many arguments for function."));
139 		}
140 
141 		using ReturnType = decltype(UnpackCaller().operator()<FuncType, arity>(*static_cast<FuncType *>(nullptr), std::vector<Value>()));
142 
143 		return FunctionWrapper<FuncType, arity, ReturnType>::Invoke(function, arguments);
144 	};
145 }
146 
147 }
148 
149 #endif /* FUNCTIONWRAPPER_H */
150