From f59f78385d1de44d914af5b629b7e310211300e8 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Mon, 21 May 2018 19:19:43 +0900 Subject: [PATCH] Use "if constexpr" provided by C++17 --- src/CMakeLists.txt | 4 +- src/EC/Bitset.hpp | 2 +- src/EC/Manager.hpp | 106 +++++++++++++++++++++++---------------------- 3 files changed, 57 insertions(+), 55 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 0244e2b..84ecbda 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -31,7 +31,7 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wpedantic") set(CMAKE_CXX_FLAGS_DEBUG "-O0 -g") set(CMAKE_CXX_FLAGS_RELEASE "-O3 -D NDEBUG") -target_compile_features(EntityComponentSystem INTERFACE cxx_std_14) +target_compile_features(EntityComponentSystem INTERFACE cxx_std_17) if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) message(STATUS "Setting build type to 'Debug', none was specified.") @@ -53,7 +53,7 @@ if(GTEST_FOUND) add_executable(UnitTests ${UnitTests_SOURCES}) target_link_libraries(UnitTests EntityComponentSystem ${GTEST_LIBRARIES}) target_include_directories(UnitTests PUBLIC ${GTEST_INCLUDE_DIR}) - target_compile_features(UnitTests PUBLIC cxx_std_14) + target_compile_features(UnitTests PUBLIC cxx_std_17) enable_testing() add_test(NAME UnitTests COMMAND UnitTests) diff --git a/src/EC/Bitset.hpp b/src/EC/Bitset.hpp index 4317313..efc3bc8 100644 --- a/src/EC/Bitset.hpp +++ b/src/EC/Bitset.hpp @@ -66,7 +66,7 @@ namespace EC Bitset bitset; EC::Meta::forEach([&bitset] (auto t) { - if(EC::Meta::Contains::value) + if constexpr (EC::Meta::Contains::value) { bitset[EC::Meta::IndexOf::value] = true; diff --git a/src/EC/Manager.hpp b/src/EC/Manager.hpp index 4b5e4e4..c6dc5ec 100644 --- a/src/EC/Manager.hpp +++ b/src/EC/Manager.hpp @@ -32,7 +32,6 @@ #include "Meta/Matching.hpp" #include "Meta/ForEachWithIndex.hpp" #include "Meta/ForEachDoubleTuple.hpp" -#include "Meta/IndexOf.hpp" #include "Bitset.hpp" namespace EC @@ -67,7 +66,7 @@ namespace EC template struct Storage { - using type = std::tuple..., std::vector >; + using type = std::tuple...>; }; using ComponentsStorage = typename EC::Meta::Morph >::type; @@ -242,15 +241,10 @@ namespace EC template Component* getEntityData(const std::size_t& index) { - constexpr auto componentIndex = EC::Meta::IndexOf< - Component, Components>::value; - if(componentIndex < Components::size) + if constexpr (EC::Meta::Contains::value) { - // Cast required due to compiler thinking that an invalid - // Component is needed even though the enclosing if statement - // prevents this from ever happening. - return (Component*) &std::get( - componentsStorage).at(index); + return &std::get >(componentsStorage) + .at(index); } else { @@ -295,15 +289,10 @@ namespace EC template const Component* getEntityData(const std::size_t& index) const { - constexpr auto componentIndex = EC::Meta::IndexOf< - Component, Components>::value; - if(componentIndex < Components::size) + if constexpr (EC::Meta::Contains::value) { - // Cast required due to compiler thinking that an invalid - // Component is needed even though the enclosing if statement - // prevents this from ever happening. - return (Component*) &std::get( - componentsStorage).at(index); + return &std::get >(componentsStorage) + .at(index); } else { @@ -395,27 +384,25 @@ namespace EC template void addComponent(const std::size_t& entityID, Args&&... args) { - if(!EC::Meta::Contains::value - || !isAlive(entityID)) + if constexpr (!EC::Meta::Contains::value) { return; } + else + { + if(!isAlive(entityID)) + { + return; + } + Component component(std::forward(args)...); - Component component(std::forward(args)...); + std::get( + entities[entityID] + ).template getComponentBit() = true; - std::get( - entities[entityID] - ).template getComponentBit() = true; - - constexpr auto index = - EC::Meta::IndexOf::value; - - // Cast required due to compiler thinking that vector at - // index = Components::size is being used, even if the previous - // if statement will prevent this from ever happening. - (*((std::vector*)(&std::get( - componentsStorage - ))))[entityID] = std::move(component); + std::get >(componentsStorage)[entityID] + = std::move(component); + } } /*! @@ -432,15 +419,20 @@ namespace EC template void removeComponent(const std::size_t& entityID) { - if(!EC::Meta::Contains::value - || !isAlive(entityID)) + if constexpr (!EC::Meta::Contains::value) { return; } - - std::get( - entities[entityID] - ).template getComponentBit() = false; + else + { + if(!isAlive(entityID)) + { + return; + } + std::get( + entities[entityID] + ).template getComponentBit() = false; + } } /*! @@ -454,15 +446,20 @@ namespace EC template void addTag(const std::size_t& entityID) { - if(!EC::Meta::Contains::value - || !isAlive(entityID)) + if constexpr (!EC::Meta::Contains::value) { return; } - - std::get( - entities[entityID] - ).template getTagBit() = true; + else + { + if(!isAlive(entityID)) + { + return; + } + std::get( + entities[entityID] + ).template getTagBit() = true; + } } /*! @@ -478,15 +475,20 @@ namespace EC template void removeTag(const std::size_t& entityID) { - if(!EC::Meta::Contains::value - || !isAlive(entityID)) + if constexpr (!EC::Meta::Contains::value) { return; } - - std::get( - entities[entityID] - ).template getTagBit() = false; + else + { + if(!isAlive(entityID)) + { + return; + } + std::get( + entities[entityID] + ).template getTagBit() = false; + } } private: