Better ecs::entity_accessor interface

This commit is contained in:
Nikita Lisitsa 2023-08-22 23:15:56 +03:00
parent 82682c0317
commit c59a28433a

View file

@ -14,7 +14,7 @@ namespace psemek::ecs
{}
template <typename Component>
Component & get()
Component * get_if()
{
util::uuid const uuid = Component::uuid();
@ -22,12 +22,26 @@ namespace psemek::ecs
for (std::size_t i = 0; i < component_uuids.size(); ++i)
if (uuid == component_uuids[i])
return *reinterpret_cast<Component *>(table_->get_component_pointers()[i].data + detail::stride<Component>() * row_);
return reinterpret_cast<Component *>(table_->get_component_pointers()[i].data + detail::stride<Component>() * row_);
return nullptr;
}
template <typename Component>
Component & get()
{
if (auto ptr = get_if<Component>())
return *ptr;
assert(false);
__builtin_unreachable();
}
template <typename Component>
bool contains() const
{
return get_if<Component>() != nullptr;
}
private:
detail::table * table_;
std::uint32_t row_;