ECS index API wip: support creating & storing indices
This commit is contained in:
parent
080893ee96
commit
8fcaef4ba1
2 changed files with 56 additions and 0 deletions
|
|
@ -8,6 +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/accessor.hpp>
|
||||
#include <psemek/ecs/exceptions.hpp>
|
||||
#include <psemek/util/range.hpp>
|
||||
|
|
@ -375,11 +376,15 @@ namespace psemek::ecs
|
|||
template <typename ... Components, typename Function>
|
||||
void watch(Function && function);
|
||||
|
||||
template <typename Index, typename ... Args>
|
||||
Index & index(Args && ... args);
|
||||
|
||||
private:
|
||||
detail::entity_list entity_list_;
|
||||
detail::table_container table_container_;
|
||||
detail::query_cache_container query_cache_container_;
|
||||
detail::component_registry component_registry_;
|
||||
detail::index_container index_container_;
|
||||
|
||||
std::vector<util::uuid> uuid_helper_;
|
||||
util::hash_set<util::uuid> uuid_set_helper_;
|
||||
|
|
@ -656,4 +661,10 @@ namespace psemek::ecs
|
|||
callback_caches_.push_back(cache);
|
||||
}
|
||||
|
||||
template <typename Index, typename ... Args>
|
||||
Index & container::index(Args && ... args)
|
||||
{
|
||||
return index_container_.get<Index>(*this, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
45
libs/ecs/include/psemek/ecs/detail/index_container.hpp
Normal file
45
libs/ecs/include/psemek/ecs/detail/index_container.hpp
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
#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 <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.get());
|
||||
|
||||
if constexpr (std::is_constructible_v<Index, ecs::container &, Args && ...>)
|
||||
{
|
||||
auto ptr = std::make_shared<Index>(container, std::forward<Args>(args)...);
|
||||
storage_.insert({uuid, ptr});
|
||||
return *ptr;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw util::exception("Index " + util::type_name<Index>() + " is neither present nor constructible");
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
util::hash_map<util::uuid, std::shared_ptr<void>> storage_;
|
||||
};
|
||||
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue