3#include "NonCopyable.hpp"
12#include <unordered_map>
40 virtual void Update(
const float deltaT) = 0;
48 virtual void Draw()
const;
62 SystemManager() =
default;
64 SystemManager(
const SystemManager&) =
delete;
65 SystemManager& operator=(
const SystemManager&) =
delete;
84 template <
typename SystemT,
typename... Args>
85 requires std::is_base_of_v<System, SystemT>
86 std::shared_ptr<SystemT>
AddSystem(
const u32 priority = 1, Args&&... args)
88 std::unique_lock lock(m_mutex);
90 auto ptr = std::make_shared<SystemT>(std::forward<Args>(args)...);
92 m_systems.push_back(std::make_pair(priority, ptr));
93 m_systemsMap.emplace(
typeid(SystemT), ptr);
95 std::sort(m_systems.begin(), m_systems.end(), [](
const auto& a,
const auto& b)
97 return a.first < b.first;
111 template <
typename SystemT>
112 requires std::is_base_of_v<System, SystemT>
115 std::unique_lock lock(m_mutex);
117 auto it = m_systemsMap.find(
typeid(SystemT));
118 if (it == m_systemsMap.end())
123 std::shared_ptr<System> ptr = it->second;
126 for (u32 i = 0; i < m_systems.size(); ++i)
128 if (m_systems[i].second == ptr)
137 m_systems.erase(m_systems.begin() + index);
149 template <
typename SystemT>
150 requires std::is_base_of_v<System, SystemT>
153 std::shared_lock lock(m_mutex);
155 auto it = m_systemsMap.find(
typeid(SystemT));
156 if (it != m_systemsMap.end())
158 return std::dynamic_pointer_cast<SystemT>(it->second);
178 void Update(
const float deltaT);
185 std::shared_mutex m_mutex;
187 std::vector<std::pair<u32, std::shared_ptr<System>>> m_systems;
188 std::unordered_map<std::type_index, std::shared_ptr<System>> m_systemsMap;
Core engine.
Definition Engine.hpp:90
std::shared_ptr< SystemT > AddSystem(const u32 priority=1, Args &&... args)
Constructs a system and adds it to the manager.
Definition SystemManager.hpp:86
std::shared_ptr< SystemT > GetSystem()
Returns a shared pointer to a managed system.
Definition SystemManager.hpp:151
void RemoveSystem()
Removes a system from the manager.
Definition SystemManager.hpp:113
void ClearSystems()
Removes and destroys all managed systems.
Definition SystemManager.cpp:27
Base class for all systems.
Definition SystemManager.hpp:27
virtual void Update(const float deltaT)=0
Updates the system.
virtual void Draw() const
Renders the system.
Definition SystemManager.cpp:3