From f782b81ba10c9b7a1e221b16de0aaa7b6c521729 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Wed, 25 Apr 2018 17:00:07 -0400 Subject: EntityManager const-correctness --- src/entity_manager.cpp | 4 ++-- src/entity_manager.h | 29 +++++++++++++++++++---------- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/entity_manager.cpp b/src/entity_manager.cpp index f792e17..0aaaf8e 100644 --- a/src/entity_manager.cpp +++ b/src/entity_manager.cpp @@ -5,7 +5,7 @@ template <> std::set EntityManager::getEntitiesWithComponents<>( - std::set& componentTypes) + std::set& componentTypes) const { if (cachedComponents.count(componentTypes) == 1) { @@ -15,7 +15,7 @@ std::set EntityManager::getEntitiesWithComponents<>( std::set& cache = cachedComponents[componentTypes]; for (id_type entity = 0; entity < entities.size(); entity++) { - EntityData& data = entities[entity]; + const EntityData& data = entities[entity]; bool cacheEntity = true; for (auto& componentType : componentTypes) diff --git a/src/entity_manager.h b/src/entity_manager.h index 1e8d31c..b2ef0de 100644 --- a/src/entity_manager.h +++ b/src/entity_manager.h @@ -27,13 +27,15 @@ private: database_type entities; std::vector slotAvailable; std::set allEntities; - std::map, std::set> cachedComponents; + + mutable std::map, std::set> + cachedComponents; id_type nextEntityID = 0; template std::set getEntitiesWithComponentsHelper( - std::set& componentTypes) + std::set& componentTypes) const { componentTypes.insert(typeid(T)); @@ -42,7 +44,7 @@ private: template std::set getEntitiesWithComponents( - std::set& componentTypes) + std::set& componentTypes) const { return getEntitiesWithComponentsHelper(componentTypes); } @@ -168,14 +170,14 @@ public: } template - T& getComponent(id_type entity) + const T& getComponent(id_type entity) const { if ((entity >= entities.size()) || slotAvailable[entity]) { throw std::invalid_argument("Cannot get non-existent entity"); } - EntityData& data = entities[entity]; + const EntityData& data = entities[entity]; std::type_index componentType = typeid(T); if (!data.components.count(componentType)) @@ -183,25 +185,32 @@ public: throw std::invalid_argument("Cannot get non-existent component"); } - return *dynamic_cast(data.components[componentType].get()); + return *dynamic_cast(data.components.at(componentType).get()); } template - bool hasComponent(id_type entity) + T& getComponent(id_type entity) + { + return const_cast( + static_cast(*this).getComponent(entity)); + } + + template + bool hasComponent(id_type entity) const { if ((entity >= entities.size()) || slotAvailable[entity]) { throw std::invalid_argument("Cannot get non-existent entity"); } - EntityData& data = entities[entity]; + const EntityData& data = entities[entity]; std::type_index componentType = typeid(T); return data.components.count(componentType); } template - std::set getEntitiesWithComponents() + std::set getEntitiesWithComponents() const { std::set componentTypes; @@ -216,6 +225,6 @@ public: template <> std::set EntityManager::getEntitiesWithComponents<>( - std::set& componentTypes); + std::set& componentTypes) const; #endif /* end of include guard: ENTITY_MANAGER_H_C5832F11 */ -- cgit 1.4.1