]> git.seodisparate.com - EntityComponentMetaSystem/commitdiff
Added forEach to EC/Meta
authorStephen Seo <seo.disparate@gmail.com>
Sun, 13 Mar 2016 05:06:57 +0000 (14:06 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Sun, 13 Mar 2016 05:06:57 +0000 (14:06 +0900)
src/EC/Meta/ForEach.hpp [new file with mode: 0644]
src/EC/Meta/Meta.hpp
src/test/MetaTest.cpp

diff --git a/src/EC/Meta/ForEach.hpp b/src/EC/Meta/ForEach.hpp
new file mode 100644 (file)
index 0000000..9f329b0
--- /dev/null
@@ -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
+
index 0123c8e9d735630b429409c4d611e91de80e23ee..8463a6fb3e4b1fbacda93ea5f5e5bbe9529e5eb5 100644 (file)
@@ -6,4 +6,5 @@
 #include "ContainsAll.hpp"
 #include "IndexOf.hpp"
 #include "Morph.hpp"
+#include "ForEach.hpp"
 
index 84638f5c1e4f0e90c61f25206a5274e78acf47f3..b2c12e93033f073d142fb85c66ec26180eea8e50 100644 (file)
@@ -181,3 +181,21 @@ TEST(Meta, TypeListGet)
     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]);
+}
+