diff --git a/libs/ecs/include/psemek/ecs/container.hpp b/libs/ecs/include/psemek/ecs/container.hpp index 82fe81ef..a4d71204 100644 --- a/libs/ecs/include/psemek/ecs/container.hpp +++ b/libs/ecs/include/psemek/ecs/container.hpp @@ -8,7 +8,7 @@ #include #include #include -#include +#include #include #include #include @@ -389,11 +389,11 @@ namespace psemek::ecs template void finally(Function && function); - template - Index & index(Args && ... args); + template + Global & global(Args && ... args); - template - Index & index_factory(Factory && factory); + template + Global & global_factory(Factory && factory); template std::size_t memory_usage(); @@ -411,7 +411,7 @@ namespace psemek::ecs detail::table_container table_container_; detail::query_cache_container query_cache_container_; detail::component_registry component_registry_; - detail::index_container index_container_; + detail::globals_container globals_container_; int next_constructor_id_ = 0; int next_destructor_id_ = 0; @@ -823,16 +823,16 @@ namespace psemek::ecs finally_callbacks_.push_back(std::move(wrapper)); } - template - Index & container::index(Args && ... args) + template + Global & container::global(Args && ... args) { - return index_container_.get(*this, std::forward(args)...); + return globals_container_.get(*this, std::forward(args)...); } - template - Index & container::index_factory(Factory && factory) + template + Global & container::global_factory(Factory && factory) { - return index_container_.set(std::move(factory)); + return globals_container_.set(std::move(factory)); } template diff --git a/libs/ecs/include/psemek/ecs/detail/globals_container.hpp b/libs/ecs/include/psemek/ecs/detail/globals_container.hpp new file mode 100644 index 00000000..a4158799 --- /dev/null +++ b/libs/ecs/include/psemek/ecs/detail/globals_container.hpp @@ -0,0 +1,66 @@ +#pragma once + +#include +#include +#include +#include +#include + +#include + +namespace psemek::ecs +{ + + struct container; + +} + +namespace psemek::ecs::detail +{ + + struct globals_container + { + template + Global & get(container & container, Args && ... args) + { + auto uuid = Global::uuid(); + if (auto it = storage_.find(uuid); it != storage_.end()) + return *reinterpret_cast(it->second()); + + if constexpr (std::is_constructible_v) + { + auto ptr = std::make_unique(container, std::forward(args)...); + auto result = ptr.get(); + storage_.insert({uuid, [ptr = std::move(ptr)]{ return ptr.get(); }}); + return *result; + } + else if constexpr (std::is_constructible_v) + { + auto ptr = std::make_unique(std::forward(args)...); + auto result = ptr.get(); + storage_.insert({uuid, [ptr = std::move(ptr)]{ return ptr.get(); }}); + return *result; + } + else + { + throw util::exception("Global " + util::type_name() + " is neither present nor constructible"); + } + } + + template + Global & set(Factory && factory) + { + auto uuid = Global::uuid(); + if (storage_.contains(uuid)) + throw util::exception("Global " + util::type_name() + " is already set"); + + auto result = factory(); + storage_[Global::uuid()] = std::move(factory); + return *result; + } + + private: + util::hash_map> storage_; + }; + +} diff --git a/libs/ecs/include/psemek/ecs/detail/index_container.hpp b/libs/ecs/include/psemek/ecs/detail/index_container.hpp deleted file mode 100644 index d0e71160..00000000 --- a/libs/ecs/include/psemek/ecs/detail/index_container.hpp +++ /dev/null @@ -1,66 +0,0 @@ -#pragma once - -#include -#include -#include -#include -#include - -#include - -namespace psemek::ecs -{ - - struct container; - -} - -namespace psemek::ecs::detail -{ - - struct index_container - { - template - Index & get(container & container, Args && ... args) - { - auto uuid = Index::uuid(); - if (auto it = storage_.find(uuid); it != storage_.end()) - return *reinterpret_cast(it->second()); - - if constexpr (std::is_constructible_v) - { - auto ptr = std::make_unique(container, std::forward(args)...); - auto result = ptr.get(); - storage_.insert({uuid, [ptr = std::move(ptr)]{ return ptr.get(); }}); - return *result; - } - else if constexpr (std::is_constructible_v) - { - auto ptr = std::make_unique(std::forward(args)...); - auto result = ptr.get(); - storage_.insert({uuid, [ptr = std::move(ptr)]{ return ptr.get(); }}); - return *result; - } - else - { - throw util::exception("Index " + util::type_name() + " is neither present nor constructible"); - } - } - - template - Index & set(Factory && factory) - { - auto uuid = Index::uuid(); - if (storage_.contains(uuid)) - throw util::exception("Index " + util::type_name() + " is already set"); - - auto result = factory(); - storage_[Index::uuid()] = std::move(factory); - return *result; - } - - private: - util::hash_map> storage_; - }; - -}