AndiR
AndiR

Reputation: 191

generic call of a std::function from QVariantList

I'm looking for a generic way to call a std::function with arguments from a QVariantList. This Version works but has the drawback the template parameters must be specified:

template <typename... T>
struct VariantFunc {
    static void Invoke(std::function<void(T...)> f, const QVariantList& args)
    {
        InvokeHelper(f, args);
    }

private:
    template <typename T1>
    static void InvokeHelper(std::function<void(T1)> f, const QVariantList& args)
    {
        f(args.at(0).value<T1>());
    }   
    template <typename T1, typename T2>
    static void InvokeHelper(std::function<void(T1, T2)> f, const QVariantList& args)
    {
        f(args.at(0).value<T1>(), args.at(1).value<T2>());
    }
    template <typename T1, typename T2, typename T3>
    static void InvokeHelper(std::function<void(T1, T2, T3)> f, const QVariantList& args)
    {
        f(args.at(0).value<T1>(), args.at(1).value<T2>(), args.at(2).value<T3>());
    }
};

auto args = QVariantList() << 100 << QString("hello") << QJsonValue(1234);
auto f = [](int i, QString s, QJsonValue j) { qDebug() << i << s << j;  };
VariantFunc<int, QString, QJsonValue>::Invoke(f, args);

I'd like to have such an implementation:

struct VariantFunc2 {
    template<typename Func>
    static void Invoke(Func f, const QVariantList& args)
    {
        // ???
    }
};

auto args = QVariantList() << 100 << QString("hello") << QJsonValue(1234);
auto f = [](int i, QString s, QJsonValue j) { qDebug() << i << s << j;  };
VariantFunc2::Invoke(f, args);

Is there a way to do this in C++11? Obviously the deduction guides for std::function provides a solution, but not with C++11.

Upvotes: 3

Views: 969

Answers (3)

Jonas
Jonas

Reputation: 7017

Here is a C++14 implementation. It does not use std::function, to avoid the overhead associated with said class. Instead it uses perfect forwarding of the callable. Additionally, the Invoke function returns the result of invoking the callable.

template <typename F>
struct function_traits;

template <typename R, class... Args>
struct function_traits<R(*)(Args...)> : public function_traits<R(Args...)> {};

template <typename R, class... Args>
struct function_traits<R(Args...)> {};

template <typename C, class R, class... Args>
struct function_traits<R(C::*)(Args...) const> : public function_traits<R(C&, Args...)>
{
    using args = std::tuple<Args...>;
};

template <typename F, typename Args, std::size_t ... I>
auto Invoke_impl(F&& f, const QVariantList& args, std::index_sequence<I ...>)
{
    return f(args.at(I).value<std::remove_const_t<std::remove_reference_t<std::tuple_element_t<I, Args>>>>() ...);
}

template <typename Func>
auto Invoke(Func&& f, const QVariantList& args)
{
    using F = typename std::remove_reference<Func>::type;
    using Args = typename function_traits<decltype(&F::operator())>::args;
    using Indices = std::make_index_sequence<std::tuple_size<Args>::value>;
    return Invoke_impl<Func, Args>(std::forward<Func>(f), args, Indices{});
}

int main()
{
    QString msg = "From Lambda";
    auto f = [msg](int i, const QString& s, const QJsonValue& j)
    {
        qDebug() << msg << i << s << j;
        return 5;
    };
    auto args = QVariantList() << 11 << "hello" << QJsonValue(123.456);
    qDebug() << Invoke(f, args);
}

Upvotes: 2

AndiR
AndiR

Reputation: 191

The Key is the correct type deduction from functor.

namespace FunctorHelper {
template <typename R, typename... Args>
struct VariantFunc {
    static R invoke(const std::function<R(Args...)>& f, const QVariantList& args)
    {
        std::index_sequence_for<Args...> idxs;
        return invoke_helper(f, args, idxs);
    }

private:
    template <std::size_t... I>
    static R invoke_helper(const std::function<R(Args...)>& f, const QVariantList& args, std::index_sequence<I...>)
    {
        return f(args.at(I).value<std::remove_const<std::remove_reference<Args>::type>::type>()...);
    }
};

template <typename F>
struct function_traits;

// function pointer
template <typename R, class... Args>
struct function_traits<R(*)(Args...)> : public function_traits<R(Args...)>
{};

template <typename R, class... Args>
struct function_traits<R(Args...)>
{};

// const member function pointer
template <typename C, class R, class... Args>
struct function_traits<R(C::*)(Args...) const> : public function_traits<R(C&, Args...)>
{
    static constexpr std::size_t arity = sizeof...(Args);

    static R deduce_invoke(const std::function<R(Args...)>& f, const QVariantList& args)
    {
        return VariantFunc<R, Args...>::invoke(f, args);
    }
};
} // namespace

template <typename Func>
void Invoke(Func f, const QVariantList& args)
{
    using call_type = FunctorHelper::function_traits<decltype(&Func::operator())>;
    call_type::deduce_invoke(f, args);
}

void main()
{
    QString msg = "From Lambda";
    auto f = [msg](int i, const QString& s, const QJsonValue& j)
    {
        qDebug() << msg << i << s << j;
    };
    auto args = QVariantList() << 11 << "hello" << QJsonValue(123.456);
    Invoke(f, args);
    // "From Lambda" 11 "hello" QJsonValue(double, 123.456)
}

Upvotes: 0

Caleth
Caleth

Reputation: 62719

Wrapping it in a class just makes this harder. You want the pack T... to be a template parameter of Invoke

namespace detail {
template <typename... T, std::size_t... I>
void invoke_helper(std::function<void(T...)> f, const QVariantList& args, std::index_sequence<I...>) 
{
    f(args.at(I).value<T>()...);
}

template <typename... T>
void deduce_invoke(std::function<void(T...)> f, const QVariantList& args)
{
    std::index_sequence_for<T...> idxs;
    invoke_helper<T...>(std::move(f), args, idxs);
}

}

template <typename Func>
void Invoke(Func&& f, const QVariantList& args)
{
    detail::deduce_invoke(std::function{f}, args);
}

This will deduce T... for unambiguous cases.

This shares more similarity with std::apply than with std::invoke, so I think Apply would be a better name.

Upvotes: 0

Related Questions