diff --git a/src/EC/Meta/ForEach.hpp b/src/EC/Meta/ForEach.hpp new file mode 100644 index 0000000..9f329b0 --- /dev/null +++ b/src/EC/Meta/ForEach.hpp @@ -0,0 +1,32 @@ + +#ifndef EC_META_FOR_EACH_HPP +#define EC_META_FOR_EACH_HPP + +#include +#include +#include "Morph.hpp" + +namespace EC +{ + namespace Meta + { + template + constexpr void forEachHelper(Function&& function, TTuple tuple, std::index_sequence) + { + return (void)std::initializer_list{(function(std::get(tuple)), 0)...}; + } + + template + constexpr void forEach(Function&& function) + { + using TTuple = EC::Meta::Morph >; + using TTupleSize = std::tuple_size; + using IndexSeq = std::make_index_sequence; + + return forEachHelper(std::forward(function), TTuple{}, IndexSeq{}); + } + } +} + +#endif + diff --git a/src/EC/Meta/Meta.hpp b/src/EC/Meta/Meta.hpp index 0123c8e..8463a6f 100644 --- a/src/EC/Meta/Meta.hpp +++ b/src/EC/Meta/Meta.hpp @@ -6,4 +6,5 @@ #include "ContainsAll.hpp" #include "IndexOf.hpp" #include "Morph.hpp" +#include "ForEach.hpp" diff --git a/src/test/MetaTest.cpp b/src/test/MetaTest.cpp index 84638f5..b2c12e9 100644 --- a/src/test/MetaTest.cpp +++ b/src/test/MetaTest.cpp @@ -181,3 +181,21 @@ TEST(Meta, TypeListGet) EXPECT_TRUE(isSame); } +TEST(Meta, ForEach) +{ + EC::Bitset bitset; + + auto setBits = [&bitset] (auto t) { + bitset[EC::Meta::IndexOf::value] = true; + }; + + EC::Meta::forEach(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]); +} +