]> granicus.if.org Git - icinga2/commitdiff
Workaround for GCC bug 61321
authorGunnar Beutner <gunnar.beutner@icinga.com>
Tue, 19 Dec 2017 07:42:24 +0000 (08:42 +0100)
committerGunnar Beutner <gunnar.beutner@icinga.com>
Wed, 20 Dec 2017 07:02:17 +0000 (08:02 +0100)
lib/base/functionwrapper.hpp
lib/config/vmops.hpp
lib/icinga/macroprocessor.cpp
lib/icinga/macroprocessor.hpp

index a8cbce913195c4cfc4c2e7a2d9a881d9cd2e6d0d..2ee22d9b208227230eab34f3b94ed54a2ff9ded9 100644 (file)
@@ -23,6 +23,9 @@
 #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>
 
@@ -31,7 +34,14 @@ using namespace std::placeholders;
 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;
 }
@@ -43,12 +53,11 @@ inline std::function<Value (const std::vector<Value>&)> WrapFunction(void (*func
                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>
@@ -69,52 +78,78 @@ struct build_indices<0> {
 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)
@@ -123,7 +158,9 @@ std::function<Value (const std::vector<Value>&)> WrapFunction(FuncType function,
                                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);
        };
 }
 
index 97c5d6358543b23d5524c7db98c217bf623d3576..323ba51ee47e58b30c05b994e257b47662f9b0ed 100644 (file)
@@ -113,7 +113,7 @@ public:
        {
                auto evaluatedClosedVars = EvaluateClosedVars(frame, closedVars);
 
-               auto wrapper = [argNames, evaluatedClosedVars, expression](const std::vector<Value>& arguments) {
+               auto wrapper = [argNames, evaluatedClosedVars, expression](const std::vector<Value>& arguments) -> Value {
                        if (arguments.size() < argNames.size())
                                BOOST_THROW_EXCEPTION(std::invalid_argument("Too few arguments for function"));
 
index e3d81202f174d29d39ea84fb9c5ae05a71f6f5d0..9789313f7601dceefa437105e2623041b5e94a24 100644 (file)
@@ -180,30 +180,6 @@ bool MacroProcessor::ResolveMacro(const String& macro, const ResolverList& resol
        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)
@@ -214,12 +190,27 @@ Value MacroProcessor::EvaluateFunction(const Function::Ptr& func, const Resolver
                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);
 }
index 1b2c015da0e26374e52e402282edf69a4e98c6c7..8924162aee0742e8dfc52efafcb34be1a163354d 100644 (file)
@@ -63,12 +63,6 @@ private:
            String *missingMacro, const EscapeCallback& escapeFn,
            const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros,
            int recursionLevel = 0);
-       static Value 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);
-       static Value InternalResolveArgumentsShim(const std::vector<Value>& args, const ResolverList& resolvers,
-           const CheckResult::Ptr& cr, const Dictionary::Ptr& resolvedMacros,
-           bool useResolvedMacros, int recursionLevel);
        static Value EvaluateFunction(const Function::Ptr& func, const ResolverList& resolvers,
            const CheckResult::Ptr& cr, const MacroProcessor::EscapeCallback& escapeFn,
            const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros, int recursionLevel);