beatwave icon indicating copy to clipboard operation
beatwave copied to clipboard

Generalize tuple iteration process

Open rexim opened this issue 8 years ago • 6 comments

Right now it can only invoke Animated<T>::tick(). Make it take an std::function and allow it to do what it wants with the animated object.

rexim avatar Sep 07 '16 06:09 rexim

Well, it is possible to do something like this

#include <iostream>
#include <tuple>

template <typename T>
struct Animated
{
    void tick(int v)
    {
        std::cout << "tick(int): " << v << "\n";
    };
};

template <typename ...Tp>
using AnimatedTuple = std::tuple<Animated<Tp>...>;

template<std::size_t I = 0, typename Func, typename... Tp>
inline auto iterateTick(AnimatedTuple<Tp...>&, int32_t)
    -> typename std::enable_if<I == sizeof...(Tp), void>::type
{}

template<std::size_t I = 0, typename Func, typename... Tp>
inline auto iterateTick(AnimatedTuple<Tp...>& properties, int32_t deltaTime)
    -> typename std::enable_if<I < sizeof...(Tp), void>::type
{
    Func()(std::get<I>(properties));
    std::get<I>(properties).tick(deltaTime);
    iterateTick<I + 1, Tp...>(properties, deltaTime);
}

struct Functor
{
    template <typename T>
    void operator()(T)
    {
        // do something
    }
};

int main()
{
    AnimatedTuple<int> m_properties;
    iterateTick<0, Functor, int>(m_properties, 10);
    return 0;
}

If you want to use lambda (or std::function?) instead of struct, I think, it's necessary to use C++14 with its generic lambdas...

Newlifer avatar Sep 15 '16 08:09 Newlifer

@Newlifer why putting codez into issuez and not into PRz?

rexim avatar Sep 15 '16 08:09 rexim

@rexim coz it'z a discuss.

Newlifer avatar Sep 15 '16 08:09 Newlifer

@Newlifer Sorry, I'm not discussing the code that I'm not able to comment line precisely as I can do in Pull Requests (which were design specifically for this kind of discussion).

rexim avatar Sep 15 '16 08:09 rexim

@rexim But my code doesn't discuss with you! D:

Newlifer avatar Sep 15 '16 08:09 Newlifer

Since Travis uses GCC 4.8.4 at the moment we can only have experimental support for C++14: http://stackoverflow.com/a/31965806/2951870 Which is meh. That means we need to "update" GCC on Travis again #54. I think we gonna use functors for now.

@Newlifer thanks for pointing that out. Didn't know about C++14 generic lambdas. They are surely useful.

rexim avatar Sep 15 '16 20:09 rexim