]> git.seodisparate.com - EntityComponentMetaSystem/commitdiff
Added IndexOf
authorStephen Seo <seo.disparate@gmail.com>
Thu, 25 Feb 2016 03:27:04 +0000 (12:27 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Thu, 25 Feb 2016 03:27:04 +0000 (12:27 +0900)
src/EC/Meta.hpp
src/test/MetaTest.cpp

index e314acfc12dddd509d369cdf573dfc2501325c93..ab62b74aa421eaf5179c9db4b282acf7d13c118f 100644 (file)
@@ -33,6 +33,25 @@ namespace EC
 
         template <typename T, typename TTypeList>
         using Contains = std::integral_constant<bool, ContainsHelper<T, TTypeList>::value >;
+
+        template <typename T, typename... Types>
+        struct IndexOf
+        {
+        };
+
+        template <typename T, typename... Types>
+        struct IndexOf<T, TypeList<T, Types...> > :
+            std::integral_constant<std::size_t, 0>
+        {
+        };
+
+        template <typename T, typename Type, typename... Types>
+        struct IndexOf<T, TypeList<Type, Types...> > :
+            std::integral_constant<std::size_t, 1 +
+                IndexOf<T, TypeList<Types...> >::value
+            >
+        {
+        };
     }
 }
 
index e8f64ef3a2e61cf3fbc2d22c13508865a3266c49..26c32fa9065ca4297e0997fabb7c08e5ded94b8b 100644 (file)
@@ -3,14 +3,16 @@
 
 #include <EC/Meta.hpp>
 
+struct C0 {};
+struct C1 {};
+struct C2 {};
+struct C3 {};
+
+using listAll = EC::Meta::TypeList<C0, C1, C2, C3>;
+using listSome = EC::Meta::TypeList<C1, C3>;
+
 TEST(Meta, Contains)
 {
-    struct C0 {};
-    struct C1 {};
-    struct C2 {};
-    struct C3 {};
-
-    using listAll = EC::Meta::TypeList<C0, C1, C2, C3>;
 
     int size = listAll::size;
     EXPECT_EQ(size, 4);
@@ -24,7 +26,6 @@ TEST(Meta, Contains)
     result = EC::Meta::Contains<C3, listAll>::value;
     EXPECT_TRUE(result);
 
-    using listSome = EC::Meta::TypeList<C1, C3>;
     size = listSome::size;
     EXPECT_EQ(size, 2);
 
@@ -37,3 +38,21 @@ TEST(Meta, Contains)
     result = EC::Meta::Contains<C3, listSome>::value;
     EXPECT_TRUE(result);
 }
+
+TEST(Meta, IndexOf)
+{
+    int index = EC::Meta::IndexOf<C0, listAll>::value;
+    EXPECT_EQ(index, 0);
+    index = EC::Meta::IndexOf<C1, listAll>::value;
+    EXPECT_EQ(index, 1);
+    index = EC::Meta::IndexOf<C2, listAll>::value;
+    EXPECT_EQ(index, 2);
+    index = EC::Meta::IndexOf<C3, listAll>::value;
+    EXPECT_EQ(index, 3);
+
+    index = EC::Meta::IndexOf<C1, listSome>::value;
+    EXPECT_EQ(index, 0);
+    index = EC::Meta::IndexOf<C3, listSome>::value;
+    EXPECT_EQ(index, 1);
+}
+