mardi 2 mai 2017

Can't pass parameters by "variables" to variadic-based template

I have written code for storing some tasks with it's parameters for later execution. Code:

class TaskInterface
{
public:
    virtual void Execute() = 0;
};

namespace TaskHelper
{
    template <std::size_t... Types>
    struct index {};

    template <std::size_t N, std::size_t... Types>
    struct gen_seq : gen_seq<N - 1, N - 1, Types...> {};

    template <std::size_t... Types>
    struct gen_seq<0, Types...> : index<Types...>{};
}

template <typename ReturnType, typename... Types>
class SimpleTask : public TaskInterface
{
public:
    template <typename Function>
    SimpleTask(Function&& func, Types&&... args)
        : m_function(std::forward<Function>(func)),
        m_args(std::make_tuple(std::forward<Types>(args)...)) {
    }

    void Execute() override final
    {
        func(m_args);
    }

private:
    std::function<ReturnType(Types...)> m_function;
    std::tuple<Types...> m_args;

    template <typename... Args, std::size_t... Is>
    void func(std::tuple<Args...>& tup, TaskHelper::index<Is...>)
    {
        m_function(std::get<Is>(tup)...);
    }

    template <typename... Args>
    void func(std::tuple<Args...>& tup)
    {
        func(tup, TaskHelper::gen_seq<sizeof...(Args)>{});
    }
};

template < typename ReturnType, class Class, typename... Types>
class MemberTask : public TaskInterface
{
public:
    typedef ReturnType(Class::*Method)(Types...);

    MemberTask(Class* object, Method method, Types&&... args) :
        m_object(object), m_method(method), m_args(std::make_tuple(std::forward<Types>(args)...)) {
    };

    void Execute() override final
    {
        func(m_args);
    };

private:
    Class* m_object;
    Method m_method;
    std::tuple<Types...> m_args;

    template <typename... Args, std::size_t... Is>
    void func(std::tuple<Args...>& tup, TaskHelper::index<Is...>)
    {
        (m_object->*m_method)(std::get<Is>(tup)...);
    }

    template <typename... Args>
    void func(std::tuple<Args...>& tup)
    {
        func(tup, TaskHelper::gen_seq<sizeof...(Args)>{});
    }
};

template <typename Function, typename... Arguments>
TaskInterface* CreateSimpleTask(Function&& func, Arguments&&... args)
{
    return new SimpleTask<typename std::result_of<decltype(func)(Arguments...)>::type, Arguments...>(std::forward<Function>(func), std::forward<Arguments>(args)...);
}

template <class Class, typename Method, typename... Arguments>
TaskInterface* CreateMemberTask(Class* obj, Method method, Arguments&&... args)
{
    return new MemberTask<typename std::result_of<decltype(method)(Class)>::type, Class, Arguments...>(std::forward<Class*>(obj), std::forward<Method>(method), std::forward<Arguments>(args)...);
}

class Test {
public:
    Test() { id = ++m_id; }
    bool doIt(int n) {
        std::cout << "doIt of " << n * id;
        return true;
    };

private:
    static int m_id;
    int id;
};

int Test::m_id = 0;


double test1(int xs)
{
    xs *= 555;
    return 66.02l;
}

But the problem is I can create these tasks only in this ways:

TaskInterface* st = CreateSimpleTask(test1, 5);

Test t;
TaskInterface* mt = CreateMemberTask(&t, &Test::doIt, 66);

And can't in these way:

// error C2664: 'double (int)' : cannot convert argument 1 from 'int *' to 'int'
int xxxx;
TaskBase* st = CreateSimpleTask(test1, &xxxx);

or for MemberTask:

// cannot convert argument 2 from 'bool (__thiscall Test::* )(std::string)' to 'bool (__thiscall Test::* )(std::string &)'
std::string ss = "sdfsdf";
TaskBase* mt = CreateMemberTask(&t, &Test::doIt, ss);

How can I modify my classes in order to pass parameters not only by "value", but also by "variables"? Or all my "architecture" is fully wrong for this purpose?

Aucun commentaire:

Enregistrer un commentaire