Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generalize tuple iteration process #57

Open
rexim opened this issue Sep 7, 2016 · 6 comments
Open

Generalize tuple iteration process #57

rexim opened this issue Sep 7, 2016 · 6 comments

Comments

@rexim
Copy link
Owner

rexim commented Sep 7, 2016

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 rexim added the refactor label Sep 7, 2016
@rexim rexim self-assigned this Sep 7, 2016
rexim added a commit that referenced this issue Sep 7, 2016
@rexim rexim removed the in progress label Sep 13, 2016
rexim added a commit that referenced this issue Sep 13, 2016
@Newlifer
Copy link
Contributor

Newlifer commented Sep 15, 2016

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...

@rexim
Copy link
Owner Author

rexim commented Sep 15, 2016

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

@Newlifer
Copy link
Contributor

@rexim coz it'z a discuss.

@rexim
Copy link
Owner Author

rexim commented Sep 15, 2016

@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).

@Newlifer
Copy link
Contributor

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

@rexim
Copy link
Owner Author

rexim commented Sep 15, 2016

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants