From 97d7fb425da906947cc45e11fadb35f708da50d6 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Wed, 7 Feb 2018 14:13:32 -0500 Subject: Changed EntityManager to dense vector This should improve speed, because entity lookup will be O(1) instead of O(log n). Deletion is also O(1). Insert stays at potentially O(n), but still should be overall faster than the previous method. Also replaced some asserts with exceptions. Also made Component polymorphic so that deletion actually works properly. --- src/system_manager.h | 43 +++++++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 18 deletions(-) (limited to 'src/system_manager.h') diff --git a/src/system_manager.h b/src/system_manager.h index 087b71c..e2c98cb 100644 --- a/src/system_manager.h +++ b/src/system_manager.h @@ -5,33 +5,40 @@ #include #include #include +#include #include "system.h" class SystemManager { - private: - std::list> loop; - std::map systems; +private: - public: - template - void emplaceSystem(Game& game, Args&&... args) - { - std::unique_ptr ptr = std::unique_ptr(new T(game, std::forward(args)...)); - std::type_index systemType = typeid(T); + std::list> loop; + std::map systems; - systems[systemType] = ptr.get(); - loop.push_back(std::move(ptr)); - } +public: - template - T& getSystem() - { - std::type_index systemType = typeid(T); + template + void emplaceSystem(Game& game, Args&&... args) + { + std::unique_ptr ptr(new T(game, std::forward(args)...)); + std::type_index systemType = typeid(T); + + systems[systemType] = ptr.get(); + loop.push_back(std::move(ptr)); + } - assert(systems.count(systemType) == 1); + template + T& getSystem() + { + std::type_index systemType = typeid(T); - return *((T*)systems[systemType]); + if (!systems.count(systemType)) + { + throw std::invalid_argument("Cannot get non-existent system"); } + + return *dynamic_cast(systems[systemType]); + } + }; #endif /* end of include guard: SYSTEM_MANAGER_H_544E6056 */ -- cgit 1.4.1