Support const ecs::accessor and const-qualified component types in ecs::accessor::get

This commit is contained in:
Nikita Lisitsa 2023-12-16 20:06:40 +03:00
parent 6833531edb
commit 6a13a06187

View file

@ -62,6 +62,9 @@ namespace psemek::ecs
template <typename Component> template <typename Component>
Component * get_if(); Component * get_if();
template <typename Component>
Component const * get_if() const;
/** Obtain the component of the specified type of the accessed entity. /** Obtain the component of the specified type of the accessed entity.
* *
* @tparam Component The type of component to obtain * @tparam Component The type of component to obtain
@ -73,6 +76,9 @@ namespace psemek::ecs
template <typename Component> template <typename Component>
Component & get(); Component & get();
template <typename Component>
Component const & get() const;
/** Check if the entity contains this component type. /** Check if the entity contains this component type.
* *
* @tparam Component The type of component to obtain * @tparam Component The type of component to obtain
@ -100,7 +106,13 @@ namespace psemek::ecs
template <typename Component> template <typename Component>
Component * accessor::get_if() Component * accessor::get_if()
{ {
util::uuid const uuid = Component::uuid(); return const_cast<Component *>(static_cast<accessor const *>(this)->get_if<Component>());
}
template <typename Component>
Component const * accessor::get_if() const
{
util::uuid const uuid = std::remove_const_t<Component>::uuid();
auto column = table_->column(uuid); auto column = table_->column(uuid);
if (!column) if (!column)
@ -114,13 +126,21 @@ namespace psemek::ecs
{ {
if (auto ptr = get_if<Component>()) if (auto ptr = get_if<Component>())
return *ptr; return *ptr;
throw component_not_found_exception(typeid(Component), table_->entity_handles()[row_]); throw component_not_found_exception(typeid(std::remove_const_t<Component>), table_->entity_handles()[row_]);
}
template <typename Component>
Component const & accessor::get() const
{
if (auto ptr = get_if<Component const>())
return *ptr;
throw component_not_found_exception(typeid(std::remove_const_t<Component>), table_->entity_handles()[row_]);
} }
template <typename Component> template <typename Component>
bool accessor::contains() const bool accessor::contains() const
{ {
return get_if<Component>() != nullptr; return get_if<Component const>() != nullptr;
} }
inline accessor::accessor(detail::table * table, std::uint32_t row) inline accessor::accessor(detail::table * table, std::uint32_t row)