samedi 1 février 2020

arduino parameter pack working without tuple

I'm trying to do a sketch that can return a parameter pack. I found a reference here: tuple to parameter pack

I modify it to be even more generic and can return any type of object to a void function pointer.

That said, now I'm testing with arduino DUE and this board support tuple. But the arduino uno do not.

So, base on this article: variadic data structure I decide to use my own micro-tuple data-structure that will be supported by UNO. The structure work by itself.

Here the code so far:

#include <tuple>

template<size_t idx, typename T>
struct microTupleGetHelper;

template<typename ... T>
struct microTuple
{
};

template<int ...> struct seq {};

template<int N, int ...S> struct gens : gens<N - 1, N - 1, S...> { };

template<int ...S> struct gens<0, S...>{ typedef seq<S...> type; };



template<typename T, typename ... Rest>
struct microTuple<T, Rest ...>
{
microTuple(const T& first, const Rest& ... rest)
    : first(first)
    , rest(rest...)
{}

T first;
microTuple<Rest ... > rest;

template<size_t idx>
auto get() ->  decltype(microTupleGetHelper<idx, microTuple<T,Rest...>>::get(*this))
{
    return microTupleGetHelper<idx, microTuple<T,Rest...>>::get(*this);
}
};

template<typename T, typename ... Rest>
struct microTupleGetHelper<0, microTuple<T, Rest ... >>
{
    static T get(microTuple<T, Rest...>& data)
    {
        return data.first;
    }
};

template<size_t idx, typename T, typename ... Rest>
struct microTupleGetHelper<idx, microTuple<T, Rest ... >>
{
    static auto get(microTuple<T, Rest...>& data) ->  decltype(microTupleGetHelper<idx-1, 
microTuple<Rest ...>>::get(data.rest))
    {
        return microTupleGetHelper<idx-1, microTuple<Rest ...>>::get(data.rest);
    }
};



template <typename ...Args>
struct paramsPack
{
//std::tuple<Args...> params;
microTuple<Args...> params;

void (*func)(Args...);

/*    template<int ...S>
auto callFunc(seq<S...>) -> decltype(this->func(std::get<S>(this->params) ...))
{
    return func(std::get<S>(params) ...);
}*/

template<int ...S>
auto callFunc(seq<S...>) -> decltype(this->func(this->params.get<S>() ...))
{
    return func(params.get<S>() ...)
}

auto delayed_dispatch() -> decltype(this->callFunc(typename gens<sizeof...(Args)>::type()))
{
    return this->callFunc(typename gens<sizeof...(Args)>::type()); // Item #1
}

};

the error come in auto callFunc(..), it stand:

microTuple:72:73: error: expected primary-expression before ')' token

 auto callFunc(seq<S...>) -> decltype(this->func(this->params.get<S>() ...))

                                                                     ^

Aucun commentaire:

Enregistrer un commentaire