Rename ecs::index -> ecs::global
All checks were successful
Run tests / Run tests (push) Successful in 7m23s

This commit is contained in:
Nikita Lisitsa 2026-08-19 12:38:23 +03:00
parent 4aa18c4fda
commit c22c257c96
3 changed files with 78 additions and 78 deletions

View file

@ -8,7 +8,7 @@
#include <psemek/ecs/detail/component_uuid_helper.hpp>
#include <psemek/ecs/detail/without.hpp>
#include <psemek/ecs/detail/component_registry.hpp>
#include <psemek/ecs/detail/index_container.hpp>
#include <psemek/ecs/detail/globals_container.hpp>
#include <psemek/ecs/detail/recursion_depth_guard.hpp>
#include <psemek/ecs/accessor.hpp>
#include <psemek/ecs/inspect.hpp>
@ -389,11 +389,11 @@ namespace psemek::ecs
template <typename Function>
void finally(Function && function);
template <typename Index, typename ... Args>
Index & index(Args && ... args);
template <typename Global, typename ... Args>
Global & global(Args && ... args);
template <typename Index, typename Factory>
Index & index_factory(Factory && factory);
template <typename Global, typename Factory>
Global & global_factory(Factory && factory);
template <typename ... Components>
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 <typename Index, typename ... Args>
Index & container::index(Args && ... args)
template <typename Global, typename ... Args>
Global & container::global(Args && ... args)
{
return index_container_.get<Index>(*this, std::forward<Args>(args)...);
return globals_container_.get<Global>(*this, std::forward<Args>(args)...);
}
template <typename Index, typename Factory>
Index & container::index_factory(Factory && factory)
template <typename Global, typename Factory>
Global & container::global_factory(Factory && factory)
{
return index_container_.set<Index>(std::move(factory));
return globals_container_.set<Global>(std::move(factory));
}
template <typename ... Components>

View file

@ -0,0 +1,66 @@
#pragma once
#include <psemek/util/uuid.hpp>
#include <psemek/util/hash_table.hpp>
#include <psemek/util/type_name.hpp>
#include <psemek/util/exception.hpp>
#include <psemek/util/function.hpp>
#include <memory>
namespace psemek::ecs
{
struct container;
}
namespace psemek::ecs::detail
{
struct globals_container
{
template <typename Global, typename ... Args>
Global & get(container & container, Args && ... args)
{
auto uuid = Global::uuid();
if (auto it = storage_.find(uuid); it != storage_.end())
return *reinterpret_cast<Global *>(it->second());
if constexpr (std::is_constructible_v<Global, ecs::container &, Args && ...>)
{
auto ptr = std::make_unique<Global>(container, std::forward<Args>(args)...);
auto result = ptr.get();
storage_.insert({uuid, [ptr = std::move(ptr)]{ return ptr.get(); }});
return *result;
}
else if constexpr (std::is_constructible_v<Global, Args && ...>)
{
auto ptr = std::make_unique<Global>(std::forward<Args>(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<Global>() + " is neither present nor constructible");
}
}
template <typename Global, typename Factory>
Global & set(Factory && factory)
{
auto uuid = Global::uuid();
if (storage_.contains(uuid))
throw util::exception("Global " + util::type_name<Global>() + " is already set");
auto result = factory();
storage_[Global::uuid()] = std::move(factory);
return *result;
}
private:
util::hash_map<util::uuid, util::function<void*()>> storage_;
};
}

View file

@ -1,66 +0,0 @@
#pragma once
#include <psemek/util/uuid.hpp>
#include <psemek/util/hash_table.hpp>
#include <psemek/util/type_name.hpp>
#include <psemek/util/exception.hpp>
#include <psemek/util/function.hpp>
#include <memory>
namespace psemek::ecs
{
struct container;
}
namespace psemek::ecs::detail
{
struct index_container
{
template <typename Index, typename ... Args>
Index & get(container & container, Args && ... args)
{
auto uuid = Index::uuid();
if (auto it = storage_.find(uuid); it != storage_.end())
return *reinterpret_cast<Index *>(it->second());
if constexpr (std::is_constructible_v<Index, ecs::container &, Args && ...>)
{
auto ptr = std::make_unique<Index>(container, std::forward<Args>(args)...);
auto result = ptr.get();
storage_.insert({uuid, [ptr = std::move(ptr)]{ return ptr.get(); }});
return *result;
}
else if constexpr (std::is_constructible_v<Index, Args && ...>)
{
auto ptr = std::make_unique<Index>(std::forward<Args>(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<Index>() + " is neither present nor constructible");
}
}
template <typename Index, typename Factory>
Index & set(Factory && factory)
{
auto uuid = Index::uuid();
if (storage_.contains(uuid))
throw util::exception("Index " + util::type_name<Index>() + " is already set");
auto result = factory();
storage_[Index::uuid()] = std::move(factory);
return *result;
}
private:
util::hash_map<util::uuid, util::function<void*()>> storage_;
};
}