Engine²
Open-source game engine written in C++.
Loading...
Searching...
No Matches
SchedulerContainer.inl
Go to the documentation of this file.
2
3template <typename TScheduler, typename... Args>
5{
6 if (this->_schedulers.contains(std::type_index(typeid(TScheduler))))
7 {
8 Log::Warning(fmt::format("Scheduler already exists: {}", typeid(TScheduler).name()));
9 return;
10 }
11 Log::Debug(fmt::format("Adding scheduler: {}", typeid(TScheduler).name()));
12 std::shared_ptr<TScheduler> scheduler = std::make_shared<TScheduler>(core, std::forward<Args>(args)...);
13 this->_schedulers[std::type_index(typeid(TScheduler))] = scheduler;
14 this->_orderedSchedulers.push_back(scheduler);
15}
16
17template <typename TScheduler> inline TScheduler &Engine::SchedulerContainer::GetScheduler()
18{
19 auto it = this->_schedulers.find(std::type_index(typeid(TScheduler)));
20 if (it == this->_schedulers.end())
21 {
22 throw SchedulerError(fmt::format("Scheduler not found: {}", typeid(TScheduler).name()));
23 }
24 return *static_cast<TScheduler *>(it->second.get());
25}
26
27template <typename TBefore, typename TAfter> inline void Engine::SchedulerContainer::Before()
28{
29 _dirty = true;
30 _dependencies[std::type_index(typeid(TAfter))].insert(std::type_index(typeid(TBefore)));
31}
32
33template <typename TAfter, typename TBefore> inline void Engine::SchedulerContainer::After()
34{
36}
37
38template <typename TBefore, typename TAfter> void Engine::SchedulerContainer::RemoveDependencyBefore()
39{
40 _dirty = true;
41 auto it = _dependencies.find(std::type_index(typeid(TAfter)));
42 if (it != _dependencies.end())
43 {
44 it->second.erase(std::type_index(typeid(TBefore)));
45 if (it->second.empty())
46 _dependencies.erase(it);
47 }
48 else
49 {
50 Log::Warning(fmt::format("Dependency not found: {} -> {}", typeid(TBefore).name(), typeid(TAfter).name()));
51 }
52}
53
54template <typename TAfter, typename TBefore> void Engine::SchedulerContainer::RemoveDependencyAfter()
55{
57}
58
60{
61 Update();
62 for (const auto &scheduler : _orderedSchedulers)
63 {
64 scheduler->RunSystems();
65
66 if (!scheduler->ShouldRunNextScheduler())
67 {
68 break;
69 }
70 }
71}
72
73inline bool Engine::SchedulerContainer::Contains(std::type_index id) const { return this->_schedulers.contains(id); }
74
75template <typename TScheduler> inline bool Engine::SchedulerContainer::Contains() const
76{
77 return Contains(std::type_index(typeid(TScheduler)));
78}
79
80inline std::shared_ptr<Engine::Scheduler::AScheduler> Engine::SchedulerContainer::GetScheduler(std::type_index id)
81{
82 auto it = this->_schedulers.find(id);
83 if (it == this->_schedulers.end())
84 throw SchedulerError(fmt::format("Scheduler not found: {}", id.name()));
85 return it->second;
86}
The core is the place where all the data of the engine is stored. It contains the registry (entities)...
Definition Core.hpp:33
std::unordered_map< std::type_index, std::unordered_set< std::type_index > > _dependencies
A map of the dependencies between schedulers. The key is the type index of a scheduler,...
Definition SchedulerContainer.hpp:140
std::list< std::shared_ptr< Scheduler::AScheduler > > _orderedSchedulers
A list of the the ordered list of schedulers.
Definition SchedulerContainer.hpp:143
void Update()
Updates the order of schedulers based on their dependencies. This function performs a topological sor...
Definition SchedulerContainer.cpp:89
void Before()
Add a dependency between two schedulers. It will set the first scheduler to be before the second one.
Definition SchedulerContainer.inl:27
void After()
Add a dependency between two schedulers. It will set the first scheduler to be after the second one.
Definition SchedulerContainer.inl:33
void RunSchedulers()
Runs all schedulers in the container. This function iterates through the ordered list of schedulers a...
Definition SchedulerContainer.inl:59
bool Contains() const
Checks if a scheduler of the specified type exists in the container.
Definition SchedulerContainer.inl:75
std::map< std::type_index, std::shared_ptr< Scheduler::AScheduler > > _schedulers
A map of the schedulers.
Definition SchedulerContainer.hpp:136
bool _dirty
A flag indicating whether the order of schedulers needs to be updated.
Definition SchedulerContainer.hpp:133
void RemoveDependencyAfter()
Remove a dependency between two schedulers. It will remove the dependency that the first scheduler is...
Definition SchedulerContainer.inl:54
TScheduler & GetScheduler()
Retrieves a scheduler of the specified type.
Definition SchedulerContainer.inl:17
void RemoveDependencyBefore()
Remove a dependency between two schedulers. It will remove the dependency that the first scheduler is...
Definition SchedulerContainer.inl:38
void AddScheduler(Core &core, Args &&...args)
Adds a scheduler to the container.
Definition SchedulerContainer.inl:4
Custom exception class for scheduler-related errors.
Definition SchedulerContainer.hpp:19
void Warning(const T &msg) noexcept
Definition Logger.hpp:49
void Debug(const T &msg) noexcept
Definition Logger.hpp:45