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