From 8c462b83a159d6408cde1b1817cf452d34041806 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Tue, 11 Sep 2018 12:16:04 +0900 Subject: [PATCH] Add static_assert, components must be def-const EC::Manager will fail to compile if any given Component is not default constructible, so a static_assert and ctest was added. --- .gitignore | 3 ++- src/CMakeLists.txt | 11 +++++++++++ src/EC/Manager.hpp | 6 ++++++ src/test/WillFailCompileTest.cpp | 24 ++++++++++++++++++++++++ 4 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 src/test/WillFailCompileTest.cpp diff --git a/.gitignore b/.gitignore index e76c31b..ae041a3 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ build*/ *.o *.swp doxygen_html/ - +compile_commands.json +tags diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 56e7b47..0244e2b 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -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 $ + WORKING_DIRECTORY ${CMAKE_BINARY_DIR}) +set_tests_properties(WillFailCompile_0 PROPERTIES WILL_FAIL True) diff --git a/src/EC/Manager.hpp b/src/EC/Manager.hpp index c8b8a71..4191ce9 100644 --- a/src/EC/Manager.hpp +++ b/src/EC/Manager.hpp @@ -22,6 +22,7 @@ #include #include #include +#include #ifndef NDEBUG #include @@ -59,6 +60,10 @@ namespace EC using BitsetType = EC::Bitset; private: + using ComponentsTuple = EC::Meta::Morph >; + static_assert(std::is_default_constructible::value, + "All components must be default constructible"); + template struct Storage { @@ -66,6 +71,7 @@ namespace EC }; using ComponentsStorage = typename EC::Meta::Morph >::type; + // Entity: isAlive, ComponentsTags Info using EntitiesTupleType = std::tuple; using EntitiesType = std::vector; diff --git a/src/test/WillFailCompileTest.cpp b/src/test/WillFailCompileTest.cpp new file mode 100644 index 0000000..ca62ad3 --- /dev/null +++ b/src/test/WillFailCompileTest.cpp @@ -0,0 +1,24 @@ +#include + +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<>> manager; + return 0; +}