Added forEach to EC/Meta

This commit is contained in:
Stephen Seo 2016-03-13 14:06:57 +09:00
parent eeb22ede25
commit bee0c9d26d
3 changed files with 51 additions and 0 deletions

32
src/EC/Meta/ForEach.hpp Normal file
View file

@ -0,0 +1,32 @@
#ifndef EC_META_FOR_EACH_HPP
#define EC_META_FOR_EACH_HPP
#include <tuple>
#include <utility>
#include "Morph.hpp"
namespace EC
{
namespace Meta
{
template <typename Function, typename TTuple, std::size_t... Indices>
constexpr void forEachHelper(Function&& function, TTuple tuple, std::index_sequence<Indices...>)
{
return (void)std::initializer_list<int>{(function(std::get<Indices>(tuple)), 0)...};
}
template <typename TTypeList, typename Function>
constexpr void forEach(Function&& function)
{
using TTuple = EC::Meta::Morph<TTypeList, std::tuple<> >;
using TTupleSize = std::tuple_size<TTuple>;
using IndexSeq = std::make_index_sequence<TTupleSize::value>;
return forEachHelper(std::forward<Function>(function), TTuple{}, IndexSeq{});
}
}
}
#endif

View file

@ -6,4 +6,5 @@
#include "ContainsAll.hpp" #include "ContainsAll.hpp"
#include "IndexOf.hpp" #include "IndexOf.hpp"
#include "Morph.hpp" #include "Morph.hpp"
#include "ForEach.hpp"

View file

@ -181,3 +181,21 @@ TEST(Meta, TypeListGet)
EXPECT_TRUE(isSame); EXPECT_TRUE(isSame);
} }
TEST(Meta, ForEach)
{
EC::Bitset<ListComponentsAll, ListTagsAll> bitset;
auto setBits = [&bitset] (auto t) {
bitset[EC::Meta::IndexOf<decltype(t), ListAll>::value] = true;
};
EC::Meta::forEach<ListComponentsSome>(setBits);
EXPECT_FALSE(bitset[0]);
EXPECT_TRUE(bitset[1]);
EXPECT_FALSE(bitset[2]);
EXPECT_TRUE(bitset[3]);
EXPECT_FALSE(bitset[4]);
EXPECT_FALSE(bitset[5]);
}