Compare commits

...

32 Commits

Author SHA1 Message Date
Stephen Seo b7117c09b3 Merge branch 'master' into cxx17
Run UnitTests / build-and-run-unittests (push) Successful in 39s Details
2024-01-20 19:45:14 +09:00
Stephen Seo f6fa8a6173 Merge branch 'master' into cxx17
Run UnitTests / build-and-run-unittests (push) Successful in 14s Details
2024-01-20 19:21:39 +09:00
Stephen Seo 107c1094c9 Merge branch 'master' into cxx17
Run UnitTests / build-and-run-unittests (push) Waiting to run Details
2024-01-20 18:00:53 +09:00
Stephen Seo 7894955ff5 Merge branch 'master' into cxx17
Run UnitTests / build-and-run-unittests (push) Successful in 14s Details
2024-01-19 15:21:45 +09:00
Stephen Seo bc23918637 Merge branch 'master' into cxx17 2022-06-17 11:02:36 +09:00
Stephen Seo 6a4be98216 Use optional instead of unique_ptr in ThreadPool 2022-06-16 14:09:56 +09:00
Stephen Seo 3e6fd6c1a5 Merge branch 'master' into cxx17 2022-06-16 14:01:26 +09:00
Stephen Seo b2cd215faf Merge branch 'master' into cxx17 2022-01-20 17:33:30 +09:00
Stephen Seo f009ef1135 Merge branch 'master' into cxx17 2022-01-20 14:34:31 +09:00
Stephen Seo 56d7ddd3ba Use "if constexpr" where possible in ThreadPool 2021-09-09 15:55:23 +09:00
Stephen Seo 0a7929b0a4 Merge branch 'master' into cxx17 2021-09-09 15:54:30 +09:00
Stephen Seo 60f35e8270 Merge branch 'master' into cxx17 2021-09-08 15:56:44 +09:00
Stephen Seo 9732ac80bd Merge branch 'master' into cxx17 2021-09-08 15:12:04 +09:00
Stephen Seo cfccb5f863 Merge branch 'master' into cxx17 2021-09-08 14:58:54 +09:00
Stephen Seo dd83a492c8 Merge branch 'master' into cxx17 2021-09-08 14:14:43 +09:00
Stephen Seo 4752a67380 Merge branch 'master' into cxx17 2021-09-08 10:49:49 +09:00
Stephen Seo eca91d3670 Merge branch 'master' into cxx17 2021-09-08 10:46:39 +09:00
Stephen Seo 5baa4e92b7 Merge branch 'master' into cxx17 2021-09-07 20:25:10 +09:00
Stephen Seo 39500e1fdc Merge branch 'master' into cxx17 2021-09-07 20:16:36 +09:00
Stephen Seo 12ea26a69a Merge branch 'master' into cxx17 2021-09-07 18:29:17 +09:00
Stephen Seo 8d44b8e2bc Modify .clangd specifying use of C++17 2021-09-07 18:18:08 +09:00
Stephen Seo af12aa577b Merge branch 'master' into cxx17 2021-09-07 18:17:59 +09:00
Stephen Seo 155df42657 Merge branch 'master' into cxx17 2021-09-07 18:16:29 +09:00
Stephen Seo bfd5dba0b2 Merge branch 'master' into cxx17 2021-09-07 12:24:48 +09:00
Stephen Seo 481f223c7d Add "if constexpr" where possible in Manager 2021-09-07 12:13:28 +09:00
Stephen Seo 20df53837b Use "if constexpr" in ThreadPool 2021-09-07 12:11:36 +09:00
Stephen Seo eb638f325e Merge branch 'master' into cxx17 2021-09-07 12:10:07 +09:00
Stephen Seo 95697ae7b6 Merge branch 'master' into cxx17 2021-05-17 12:34:26 +09:00
Stephen Seo f6dbe5414e Merge branch 'master' into cxx17 2021-05-16 20:08:25 +09:00
Stephen Seo 5f1e6f1b5a Merge branch 'master' into cxx17 2019-11-06 16:44:38 +09:00
Stephen Seo 25c864dd8a Merge branch 'master' into cxx17 2019-11-06 16:37:46 +09:00
Stephen Seo f59f78385d Use "if constexpr" provided by C++17 2018-11-22 14:32:32 +09:00
5 changed files with 55 additions and 59 deletions

View File

@ -1,2 +1,2 @@
CompileFlags:
Add: [-std=c++14]
Add: [-std=c++17]

View File

@ -35,7 +35,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.")
@ -59,7 +59,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)

View File

@ -66,7 +66,7 @@ namespace EC
Bitset<ComponentsList, TagsList> bitset;
EC::Meta::forEach<Contents>([&bitset] (auto t) {
if(EC::Meta::Contains<decltype(t), Combined>::value)
if constexpr (EC::Meta::Contains<decltype(t), Combined>::value)
{
bitset[EC::Meta::IndexOf<decltype(t), Combined>::value] =
true;

View File

@ -35,7 +35,6 @@
#include "Meta/Combine.hpp"
#include "Meta/ForEachDoubleTuple.hpp"
#include "Meta/ForEachWithIndex.hpp"
#include "Meta/IndexOf.hpp"
#include "Meta/Matching.hpp"
#include "ThreadPool.hpp"
@ -81,7 +80,7 @@ struct Manager {
template <typename... Types>
struct Storage {
using type = std::tuple<std::deque<Types>..., std::deque<char> >;
using type = std::tuple<std::deque<Types>...>;
};
using ComponentsStorage =
typename EC::Meta::Morph<ComponentsList, Storage<> >::type;
@ -194,7 +193,7 @@ struct Manager {
*/
Manager() : threadPool{}, idStackCounter(0) {
resize(EC_INIT_ENTITIES_SIZE);
if (ThreadCount >= 2) {
if constexpr (ThreadCount >= 2) {
threadPool = std::make_unique<ThreadPool<ThreadCount> >();
}
@ -360,14 +359,9 @@ struct Manager {
*/
template <typename Component>
Component* getEntityData(const std::size_t& index) {
constexpr auto componentIndex =
EC::Meta::IndexOf<Component, Components>::value;
if (componentIndex < Components::size) {
// 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<componentIndex>(componentsStorage)
.at(index);
if constexpr (EC::Meta::Contains<Component, Components>::value) {
return &std::get<std::deque<Component> >(componentsStorage)
.at(index);
} else {
return nullptr;
}
@ -408,14 +402,9 @@ struct Manager {
*/
template <typename Component>
const Component* getEntityData(const std::size_t& index) const {
constexpr auto componentIndex =
EC::Meta::IndexOf<Component, Components>::value;
if (componentIndex < Components::size) {
// 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<componentIndex>(componentsStorage)
.at(index);
if constexpr (EC::Meta::Contains<Component, Components>::value) {
return &std::get<std::deque<Component> >(componentsStorage)
.at(index);
} else {
return nullptr;
}
@ -501,23 +490,20 @@ struct Manager {
*/
template <typename Component, typename... Args>
void addComponent(const std::size_t& entityID, Args&&... args) {
if (!EC::Meta::Contains<Component, Components>::value ||
!isAlive(entityID)) {
if constexpr (!EC::Meta::Contains<Component, Components>::value) {
return;
} else {
if (!isAlive(entityID)) {
return;
}
Component component(std::forward<Args>(args)...);
std::get<BitsetType>(entities[entityID])
.template getComponentBit<Component>() = true;
std::get<std::deque<Component> >(componentsStorage)[entityID] =
std::move(component);
}
Component component(std::forward<Args>(args)...);
std::get<BitsetType>(entities[entityID])
.template getComponentBit<Component>() = true;
constexpr auto index = EC::Meta::IndexOf<Component, Components>::value;
// Cast required due to compiler thinking that deque<char> at
// index = Components::size is being used, even if the previous
// if statement will prevent this from ever happening.
(*((std::deque<Component>*)(&std::get<index>(
componentsStorage))))[entityID] = std::move(component);
}
/*!
@ -533,13 +519,15 @@ struct Manager {
*/
template <typename Component>
void removeComponent(const std::size_t& entityID) {
if (!EC::Meta::Contains<Component, Components>::value ||
!isAlive(entityID)) {
if constexpr (!EC::Meta::Contains<Component, Components>::value) {
return;
} else {
if (!isAlive(entityID)) {
return;
}
std::get<BitsetType>(entities[entityID])
.template getComponentBit<Component>() = false;
}
std::get<BitsetType>(entities[entityID])
.template getComponentBit<Component>() = false;
}
/*!
@ -552,12 +540,16 @@ struct Manager {
*/
template <typename Tag>
void addTag(const std::size_t& entityID) {
if (!EC::Meta::Contains<Tag, Tags>::value || !isAlive(entityID)) {
if constexpr (!EC::Meta::Contains<Tag, Tags>::value) {
return;
}
} else {
if (!isAlive(entityID)) {
return;
}
std::get<BitsetType>(entities[entityID]).template getTagBit<Tag>() =
true;
std::get<BitsetType>(entities[entityID]).template getTagBit<Tag>() =
true;
}
}
/*!
@ -572,12 +564,16 @@ struct Manager {
*/
template <typename Tag>
void removeTag(const std::size_t& entityID) {
if (!EC::Meta::Contains<Tag, Tags>::value || !isAlive(entityID)) {
if constexpr (!EC::Meta::Contains<Tag, Tags>::value) {
return;
}
} else {
if (!isAlive(entityID)) {
return;
}
std::get<BitsetType>(entities[entityID]).template getTagBit<Tag>() =
false;
std::get<BitsetType>(entities[entityID]).template getTagBit<Tag>() =
false;
}
}
/*!

View File

@ -6,8 +6,8 @@
#include <deque>
#include <functional>
#include <list>
#include <memory>
#include <mutex>
#include <optional>
#include <queue>
#include <thread>
#include <tuple>
@ -23,8 +23,8 @@ namespace Internal {
using TPFnType = std::function<void(void *)>;
using TPTupleType = std::tuple<TPFnType, void *>;
using TPQueueType = std::queue<TPTupleType>;
using ThreadPtr = std::unique_ptr<std::thread>;
using ThreadStackType = std::vector<std::tuple<ThreadPtr, std::thread::id>>;
using ThreadOpt = std::optional<std::thread>;
using ThreadStackType = std::vector<std::tuple<ThreadOpt, std::thread::id>>;
using ThreadStacksType = std::deque<ThreadStackType>;
using ThreadStacksMutexesT = std::deque<std::mutex>;
using ThreadCountersT = std::deque<std::atomic_uint>;
@ -75,14 +75,14 @@ class ThreadPool {
been executed.
*/
Internal::PointersT startThreads() {
if (MAXSIZE >= 2) {
if constexpr (MAXSIZE >= 2) {
checkStacks();
auto pointers = newStackEntry();
Internal::ThreadStackType *threadStack = std::get<0>(pointers);
std::mutex *threadStackMutex = std::get<1>(pointers);
std::atomic_uint *aCounter = std::get<2>(pointers);
for (unsigned int i = 0; i < MAXSIZE; ++i) {
std::thread *newThread = new std::thread(
std::thread newThread(
[](Internal::ThreadStackType *threadStack,
std::mutex *threadStackMutex,
Internal::TPQueueType *fnQueue, std::mutex *queueMutex,
@ -91,7 +91,7 @@ class ThreadPool {
{
std::lock_guard<std::mutex> lock(*threadStackMutex);
threadStack->push_back(
{Internal::ThreadPtr(nullptr),
{Internal::ThreadOpt{},
std::this_thread::get_id()});
}
@ -149,7 +149,7 @@ class ThreadPool {
std::this_thread::sleep_for(std::chrono::microseconds(15));
}
std::lock_guard<std::mutex> stackLock(*threadStackMutex);
std::get<0>(threadStack->at(i)).reset(newThread);
std::get<0>(threadStack->at(i)) = std::move(newThread);
}
return pointers;
} else {
@ -178,7 +178,7 @@ class ThreadPool {
all previously queued functions have been executed.
*/
void easyStartAndWait() {
if (MAXSIZE >= 2) {
if constexpr (MAXSIZE >= 2) {
Internal::PointersT pointers = startThreads();
do {
std::this_thread::sleep_for(std::chrono::microseconds(30));