]> git.seodisparate.com - EntityComponentMetaSystem/commitdiff
Added EC/Bitset.hpp and EC/Meta/ContainsAll.hpp
authorStephen Seo <seo.disparate@gmail.com>
Fri, 4 Mar 2016 12:12:37 +0000 (21:12 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Fri, 4 Mar 2016 12:12:37 +0000 (21:12 +0900)
Also added EC/EC.hpp and EC/Meta/Meta.hpp that loads all
headers in that directory.

src/CMakeLists.txt
src/EC/Bitset.hpp [new file with mode: 0644]
src/EC/EC.hpp [new file with mode: 0644]
src/EC/Meta/Contains.hpp
src/EC/Meta/ContainsAll.hpp [new file with mode: 0644]
src/EC/Meta/Meta.hpp
src/EC/Meta/TypeList.hpp
src/test/MetaTest.cpp

index b32d9074055fa2457bae7cf39083b852e991c0d6..d454bc81d4240c79f07a8082c71f1f76c08826fe 100644 (file)
@@ -4,7 +4,11 @@ project(EntityComponentSystem)
 set(EntityComponentSystem_HEADERS
     EC/Meta/TypeList.hpp
     EC/Meta/Contains.hpp
-    EC/Meta/IndexOf.hpp)
+    EC/Meta/ContainsAll.hpp
+    EC/Meta/IndexOf.hpp
+    EC/Meta/Meta.hpp
+    EC/Bitset.hpp
+    EC/EC.hpp)
 
 add_library(EntityComponentSystem INTERFACE)
 target_include_directories(EntityComponentSystem INTERFACE ${CMAKE_SOURCE_DIR})
diff --git a/src/EC/Bitset.hpp b/src/EC/Bitset.hpp
new file mode 100644 (file)
index 0000000..6ddd1d3
--- /dev/null
@@ -0,0 +1,29 @@
+
+#ifndef EC_BITSET_HPP
+#define EC_BITSET_HPP
+
+#include <bitset>
+#include "Meta/TypeList.hpp"
+
+namespace EC
+{
+    template <typename ComponentsList, typename TagsList>
+    struct Bitset :
+        public std::bitset<ComponentsList::size + TagsList::size>
+    {
+        template <typename Component>
+        constexpr auto getComponentBit()
+        {
+            return (*this)[EC::Meta::IndexOf<Component, ComponentsList>::value];
+        }
+
+        template <typename Tag>
+        constexpr auto getTagBit()
+        {
+            return (*this)[ComponentsList::size + EC::Meta::IndexOf<Tag, TagsList>::value];
+        }
+    };
+}
+
+#endif
+
diff --git a/src/EC/EC.hpp b/src/EC/EC.hpp
new file mode 100644 (file)
index 0000000..9e7b952
--- /dev/null
@@ -0,0 +1,3 @@
+
+#include "Bitset.hpp"
+
index 4dc32c8701438ca0621ec3c1ee2581cf4300a15f..32f05be3012d1209645f36d7fe218ead5481b300 100644 (file)
@@ -2,6 +2,7 @@
 #ifndef EC_META_CONTAINS_HPP
 #define EC_META_CONTAINS_HPP
 
+#include <type_traits>
 #include "TypeList.hpp"
 
 namespace EC
@@ -25,7 +26,7 @@ namespace EC
         };
 
         template <typename T, typename TTypeList>
-        using Contains = std::integral_constant<bool, ContainsHelper<T, TTypeList>::value >;
+        using Contains = std::integral_constant<bool, ContainsHelper<T, TTypeList>::value>;
     }
 }
 
diff --git a/src/EC/Meta/ContainsAll.hpp b/src/EC/Meta/ContainsAll.hpp
new file mode 100644 (file)
index 0000000..cb3440c
--- /dev/null
@@ -0,0 +1,34 @@
+
+#ifndef EC_META_CONTAINS_ALL_HPP
+#define EC_META_CONTAINS_ALL_HPP
+
+#include "TypeList.hpp"
+#include "Contains.hpp"
+
+namespace EC
+{
+    namespace Meta
+    {
+        template <typename TTypeListA, typename TTypeListB>
+        struct ContainsAllHelper :
+            std::true_type
+        {
+        };
+
+        template <typename Type, typename... Types, typename TTypeListB>
+        struct ContainsAllHelper<TypeList<Type, Types...>, TTypeListB> :
+            std::conditional<
+                Contains<Type, TTypeListB>::value,
+                ContainsAllHelper<TypeList<Types...>, TTypeListB>,
+                std::false_type
+            >::type
+        {
+        };
+
+        template <typename TTypeListA, typename TTypeListB>
+        using ContainsAll = std::integral_constant<bool, ContainsAllHelper<TTypeListA, TTypeListB>::value>;
+    }
+}
+
+#endif
+
index 69c22064a50278623c093c4aefe1e5d6907a3144..c1578695deca739218945979d84a82d7d5d54cce 100644 (file)
@@ -1,5 +1,6 @@
 
 #include "TypeList.hpp"
 #include "Contains.hpp"
+#include "ContainsAll.hpp"
 #include "IndexOf.hpp"
 
index 560dbe910cb7b60068bee2db0f0079914dce7ac1..053add0f624934511b3f75e96c6bc7a1ce68b470 100644 (file)
@@ -2,8 +2,6 @@
 #ifndef EC_META_TYPE_LIST_HPP
 #define EC_META_TYPE_LIST_HPP
 
-#include <type_traits>
-
 namespace EC
 {
     namespace Meta
index 3bb4ea8c95015b6de0c2aaf7cb0f6ea834afc016..2564615be7e8d0e26453b6665df19f9bac5cbecb 100644 (file)
@@ -2,18 +2,23 @@
 #include <gtest/gtest.h>
 
 #include <EC/Meta/Meta.hpp>
+#include <EC/EC.hpp>
 
 struct C0 {};
 struct C1 {};
 struct C2 {};
 struct C3 {};
 
+struct T0 {};
+struct T1 {};
+
 using listAll = EC::Meta::TypeList<C0, C1, C2, C3>;
 using listSome = EC::Meta::TypeList<C1, C3>;
 
+using listTagsAll = EC::Meta::TypeList<T0, T1>;
+
 TEST(Meta, Contains)
 {
-
     int size = listAll::size;
     EXPECT_EQ(size, 4);
 
@@ -39,6 +44,18 @@ TEST(Meta, Contains)
     EXPECT_TRUE(result);
 }
 
+TEST(Meta, ContainsAll)
+{
+    bool contains = EC::Meta::ContainsAll<listSome, listAll>::value;
+    EXPECT_TRUE(contains);
+
+    contains = EC::Meta::ContainsAll<listAll, listSome>::value;
+    EXPECT_FALSE(contains);
+
+    contains = EC::Meta::ContainsAll<listAll, listAll>::value;
+    EXPECT_TRUE(contains);
+}
+
 TEST(Meta, IndexOf)
 {
     int index = EC::Meta::IndexOf<C0, listAll>::value;
@@ -56,3 +73,20 @@ TEST(Meta, IndexOf)
     EXPECT_EQ(index, 1);
 }
 
+TEST(Meta, Bitset)
+{
+    EC::Bitset<listAll, listTagsAll> bitset;
+    EXPECT_EQ(bitset.size(), listAll::size + listTagsAll::size);
+
+    bitset[1] = true;
+    EXPECT_TRUE(bitset.getComponentBit<C1>());
+    bitset.flip();
+    EXPECT_FALSE(bitset.getComponentBit<C1>());
+
+    bitset.reset();
+    bitset[4] = true;
+    EXPECT_TRUE(bitset.getTagBit<T0>());
+    bitset.flip();
+    EXPECT_FALSE(bitset.getTagBit<T0>());
+}
+