diff options
author | Kelly Rauchenberger <fefferburbia@gmail.com> | 2018-02-07 14:13:32 -0500 |
---|---|---|
committer | Kelly Rauchenberger <fefferburbia@gmail.com> | 2018-02-07 14:13:32 -0500 |
commit | 97d7fb425da906947cc45e11fadb35f708da50d6 (patch) | |
tree | ae1a4a5bbbce97e5dcf50e7390f005fe9be61356 /src/system_manager.h | |
parent | da3df061699203eccc9a0c98becaee3ce8050a4f (diff) | |
download | therapy-97d7fb425da906947cc45e11fadb35f708da50d6.tar.gz therapy-97d7fb425da906947cc45e11fadb35f708da50d6.tar.bz2 therapy-97d7fb425da906947cc45e11fadb35f708da50d6.zip |
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.
Diffstat (limited to 'src/system_manager.h')
-rw-r--r-- | src/system_manager.h | 43 |
1 files changed, 25 insertions, 18 deletions
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 @@ | |||
5 | #include <memory> | 5 | #include <memory> |
6 | #include <map> | 6 | #include <map> |
7 | #include <typeindex> | 7 | #include <typeindex> |
8 | #include <stdexcept> | ||
8 | #include "system.h" | 9 | #include "system.h" |
9 | 10 | ||
10 | class SystemManager { | 11 | class SystemManager { |
11 | private: | 12 | private: |
12 | std::list<std::unique_ptr<System>> loop; | ||
13 | std::map<std::type_index, System*> systems; | ||
14 | 13 | ||
15 | public: | 14 | std::list<std::unique_ptr<System>> loop; |
16 | template <class T, class... Args> | 15 | std::map<std::type_index, System*> systems; |
17 | void emplaceSystem(Game& game, Args&&... args) | ||
18 | { | ||
19 | std::unique_ptr<T> ptr = std::unique_ptr<T>(new T(game, std::forward<Args>(args)...)); | ||
20 | std::type_index systemType = typeid(T); | ||
21 | 16 | ||
22 | systems[systemType] = ptr.get(); | 17 | public: |
23 | loop.push_back(std::move(ptr)); | ||
24 | } | ||
25 | 18 | ||
26 | template <class T> | 19 | template <class T, class... Args> |
27 | T& getSystem() | 20 | void emplaceSystem(Game& game, Args&&... args) |
28 | { | 21 | { |
29 | std::type_index systemType = typeid(T); | 22 | std::unique_ptr<T> ptr(new T(game, std::forward<Args>(args)...)); |
23 | std::type_index systemType = typeid(T); | ||
24 | |||
25 | systems[systemType] = ptr.get(); | ||
26 | loop.push_back(std::move(ptr)); | ||
27 | } | ||
30 | 28 | ||
31 | assert(systems.count(systemType) == 1); | 29 | template <class T> |
30 | T& getSystem() | ||
31 | { | ||
32 | std::type_index systemType = typeid(T); | ||
32 | 33 | ||
33 | return *((T*)systems[systemType]); | 34 | if (!systems.count(systemType)) |
35 | { | ||
36 | throw std::invalid_argument("Cannot get non-existent system"); | ||
34 | } | 37 | } |
38 | |||
39 | return *dynamic_cast<T*>(systems[systemType]); | ||
40 | } | ||
41 | |||
35 | }; | 42 | }; |
36 | 43 | ||
37 | #endif /* end of include guard: SYSTEM_MANAGER_H_544E6056 */ | 44 | #endif /* end of include guard: SYSTEM_MANAGER_H_544E6056 */ |