Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 542
AndiR Avatar asked Nov 29 '25 14:11

AndiR


1 Answers

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);
}
like image 107
Jonas Avatar answered Dec 02 '25 03:12

Jonas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!