beatwave
beatwave copied to clipboard
Generalize tuple iteration process
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.
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 why putting codez into issuez and not into PRz?
@rexim coz it'z a discuss.
@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 But my code doesn't discuss with you! D:
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.