]> git.seodisparate.com - EntityComponentMetaSystem/commitdiff
Add static_assert, components must be def-const
authorStephen Seo <seo.disparate@gmail.com>
Tue, 11 Sep 2018 03:16:04 +0000 (12:16 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Tue, 11 Sep 2018 03:16:04 +0000 (12:16 +0900)
EC::Manager will fail to compile if any given Component is not default
constructible, so a static_assert and ctest was added.

.gitignore
src/CMakeLists.txt
src/EC/Manager.hpp
src/test/WillFailCompileTest.cpp [new file with mode: 0644]

index e76c31bec31bbe493ba8566943839a1ea772cf29..ae041a3bf5920932a5a8ed798d8dddb4d788f6e8 100644 (file)
@@ -2,4 +2,5 @@ build*/
 *.o
 *.swp
 doxygen_html/
-
+compile_commands.json
+tags
index 56e7b471deedc61c7c8eef90997ea8d6e8907e40..0244e2b32f5426ac971e06b4926f563a32444b26 100644 (file)
@@ -16,6 +16,9 @@ set(EntityComponentSystem_HEADERS
     EC/Manager.hpp
     EC/EC.hpp)
 
+set(WillFailCompile_SOURCES
+    test/WillFailCompileTest.cpp)
+
 add_library(EntityComponentSystem INTERFACE)
 target_link_libraries(EntityComponentSystem INTERFACE pthread)
 target_include_directories(EntityComponentSystem INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})
@@ -56,3 +59,11 @@ if(GTEST_FOUND)
     add_test(NAME UnitTests COMMAND UnitTests)
 endif()
 
+add_executable(WillFailCompile ${WillFailCompile_SOURCES})
+set_target_properties(WillFailCompile PROPERTIES
+    EXCLUDE_FROM_ALL True
+    EXCLUDE_FROM_DEFAULT_BUILD True)
+add_test(NAME WillFailCompile_0
+    COMMAND ${CMAKE_COMMAND} --build . --target WillFailCompile --config $<CONFIGURATION>
+    WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
+set_tests_properties(WillFailCompile_0 PROPERTIES WILL_FAIL True)
index c8b8a7130a09fc411656ad805525df384ffd6bf8..4191ce936bd8f8044e18233cfb8a04c333e1b936 100644 (file)
@@ -22,6 +22,7 @@
 #include <algorithm>
 #include <thread>
 #include <mutex>
+#include <type_traits>
 
 #ifndef NDEBUG
   #include <iostream>
@@ -59,6 +60,10 @@ namespace EC
         using BitsetType = EC::Bitset<ComponentsList, TagsList>;
 
     private:
+        using ComponentsTuple = EC::Meta::Morph<ComponentsList, std::tuple<> >;
+        static_assert(std::is_default_constructible<ComponentsTuple>::value,
+            "All components must be default constructible");
+
         template <typename... Types>
         struct Storage
         {
@@ -66,6 +71,7 @@ namespace EC
         };
         using ComponentsStorage =
             typename EC::Meta::Morph<ComponentsList, Storage<> >::type;
+
         // Entity: isAlive, ComponentsTags Info
         using EntitiesTupleType = std::tuple<bool, BitsetType>;
         using EntitiesType = std::vector<EntitiesTupleType>;
diff --git a/src/test/WillFailCompileTest.cpp b/src/test/WillFailCompileTest.cpp
new file mode 100644 (file)
index 0000000..ca62ad3
--- /dev/null
@@ -0,0 +1,24 @@
+#include <EC/Manager.hpp>
+
+struct NoDef
+{
+    NoDef(int a) : a(a) {}
+
+    int a;
+};
+
+struct WithDef
+{
+    WithDef() : a(0) {}
+
+    int a;
+};
+
+using EC::Meta::TypeList;
+
+int main()
+{
+    // should fail to compile because "NoDef" is not default constructible
+    EC::Manager<TypeList<WithDef, NoDef>, TypeList<>> manager;
+    return 0;
+}