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.
This commit is contained in:
Stephen Seo 2018-09-11 12:16:04 +09:00
parent 6bf239a43b
commit 8c462b83a1
4 changed files with 43 additions and 1 deletions

3
.gitignore vendored
View file

@ -2,4 +2,5 @@ build*/
*.o
*.swp
doxygen_html/
compile_commands.json
tags

View 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)

View 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>;

View file

@ -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;
}