Engine²
Open-source game engine written in C++.
Loading...
Searching...
No Matches
SchedulerContainer.hpp
Go to the documentation of this file.
1#pragma once
2
4#include <list>
5#include <map>
6#include <memory>
7#include <queue>
8#include <stdexcept>
9#include <typeindex>
10#include <unordered_map>
11#include <unordered_set>
12#include <vector>
13
14namespace Engine {
15class Core;
16
19class SchedulerError : public std::exception {
20 public:
23 explicit SchedulerError(const std::string &message) : msg(fmt::format("Scheduler error: {}", message)) {};
24
27 const char *what() const throw() override { return this->msg.c_str(); };
28
29 private:
31 std::string msg;
32};
33
40 public:
42 SchedulerContainer() = default;
43
46
52 template <typename TScheduler, typename... Args> void AddScheduler(Core &core, Args &&...args);
53
56 template <typename TScheduler> TScheduler &GetScheduler();
57
61 std::shared_ptr<Scheduler::AScheduler> GetScheduler(std::type_index id);
62
66 void RunSchedulers();
67
71 template <typename TScheduler> void DeleteScheduler() { DeleteScheduler(std::type_index(typeid(TScheduler))); }
72
75 void DeleteScheduler(std::type_index id);
76
80 template <typename TBefore, typename TAfter> void Before();
81
85 template <typename TAfter, typename TBefore> void After();
86
91 template <typename TBefore, typename TAfter> void RemoveDependencyBefore();
92
97 template <typename TAfter, typename TBefore> void RemoveDependencyAfter();
98
102 template <typename TScheduler> bool Contains() const;
103
107 bool Contains(std::type_index id) const;
108
113
114 private:
117 void Update();
118
120 void TopologicalSort();
121
128 void ProcessDependencies(std::type_index current, std::queue<std::type_index> &q,
129 std::map<std::type_index, size_t> &inDegree) const;
130
131 private:
133 bool _dirty = false;
134
136 std::map<std::type_index, std::shared_ptr<Scheduler::AScheduler>> _schedulers;
137
140 std::unordered_map<std::type_index, std::unordered_set<std::type_index>> _dependencies;
141
143 std::list<std::shared_ptr<Scheduler::AScheduler>> _orderedSchedulers;
144};
145} // namespace Engine
146
147#include "SchedulerContainer.inl"
The core is the place where all the data of the engine is stored. It contains the registry (entities)...
Definition Core.hpp:33
void TopologicalSort()
Apply a topological sort between all schedulers based on their dependencies.
Definition SchedulerContainer.cpp:46
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 DeleteScheduler()
Deletes a scheduler of the specified type.
Definition SchedulerContainer.hpp:71
void Update()
Updates the order of schedulers based on their dependencies. This function performs a topological sor...
Definition SchedulerContainer.cpp:89
SchedulerContainer()=default
Default constructor for SchedulerContainer.
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
void SetErrorPolicyForAllSchedulers(Scheduler::SchedulerErrorPolicy policy)
Sets the error policy for all schedulers in the container.
Definition SchedulerContainer.cpp:97
bool _dirty
A flag indicating whether the order of schedulers needs to be updated.
Definition SchedulerContainer.hpp:133
~SchedulerContainer()=default
Destructor for SchedulerContainer.
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 ProcessDependencies(std::type_index current, std::queue< std::type_index > &q, std::map< std::type_index, size_t > &inDegree) const
Helper function to process the dependencies of a scheduler during the topological sort....
Definition SchedulerContainer.cpp:30
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
std::string msg
The error message associated with the exception.
Definition SchedulerContainer.hpp:31
const char * what() const override
Gets the error message.
Definition SchedulerContainer.hpp:27
SchedulerError(const std::string &message)
Constructor for SchedulerError.
Definition SchedulerContainer.hpp:23
SchedulerErrorPolicy
Enum that defines how the scheduler will handle errors.
Definition IScheduler.hpp:11
Definition Core.hpp:17