Engine²
Open-source game engine written in C++.
Loading...
Searching...
No Matches
Core.ipp
Go to the documentation of this file.
1#include "Logger.hpp"
2#include "core/Core.hpp"
6
7namespace Engine {
8
9template <typename TResource> inline TResource &Core::RegisterResource(TResource &&resource)
10{
11 return this->_registry->ctx().insert_or_assign<TResource>(std::forward<TResource>(resource));
12}
13
14template <typename TResource> inline TResource &Core::GetResource()
15{
16 if (!this->_registry->ctx().contains<TResource>())
17 {
19 fmt::format("Resource not found in the core registry: {}", typeid(TResource).name()));
20 }
21 return this->_registry->ctx().get<TResource>();
22}
23
24template <typename TResource> inline bool Core::HasResource() const
25{
26 return this->_registry->ctx().contains<TResource>();
27}
28
29template <typename TResource> inline void Core::DeleteResource() { this->_registry->ctx().erase<TResource>(); }
30
31template <typename TResource> inline const TResource &Core::GetResource() const
32{
33 return this->_registry->ctx().get<TResource>();
34}
35
36template <CScheduler TScheduler, typename... Args> inline TScheduler &Core::RegisterScheduler(Args &&...args)
37{
38 this->_schedulers.AddScheduler<TScheduler>(*this, std::forward<Args>(args)...);
39 return this->_schedulers.GetScheduler<TScheduler>();
40}
41
42template <CScheduler TScheduler> void Core::DeleteScheduler()
43{
44 this->_schedulersToDelete.push_back(std::type_index(typeid(TScheduler)));
45}
46
47template <CScheduler TScheduler> inline TScheduler &Core::GetScheduler()
48{
49 return this->_schedulers.GetScheduler<TScheduler>();
50}
51
52inline Scheduler::AScheduler &Core::GetScheduler(std::type_index id)
53{
54 if (!this->_schedulers.Contains(id))
55 {
56 throw Exception::MissingSchedulerError(fmt::format("Scheduler not found in the core: {}", id.name()));
57 }
58 return *(this->_schedulers.GetScheduler(id));
59}
60
61template <CScheduler TScheduler, typename... Systems> inline decltype(auto) Core::RegisterSystem(Systems... systems)
62{
63 return this->_schedulers.GetScheduler<TScheduler>().AddSystems(systems...);
64}
65
66template <typename TSchedulerA, typename TSchedulerB> void Core::SetSchedulerBefore()
67{
68 this->_schedulers.Before<TSchedulerA, TSchedulerB>();
69}
70
71template <typename TSchedulerA, typename TSchedulerB> void Core::SetSchedulerAfter()
72{
73 this->_schedulers.After<TSchedulerA, TSchedulerB>();
74}
75
76template <typename TSchedulerA, typename TSchedulerB> void Core::RemoveDependencyAfter()
77{
78 this->_schedulers.RemoveDependencyAfter<TSchedulerA, TSchedulerB>();
79}
80
81template <typename TSchedulerA, typename TSchedulerB> void Core::RemoveDependencyBefore()
82{
83 this->_schedulers.RemoveDependencyBefore<TSchedulerA, TSchedulerB>();
84}
85
86template <typename... Systems> inline decltype(auto) Core::RegisterSystem(Systems... systems)
87{
88 if (!this->_schedulers.Contains(_defaultScheduler))
89 {
90 Log::Warning(fmt::format("Trying to register systems with a default scheduler that does not exist: {}",
91 _defaultScheduler.name()));
92 }
93 return this->_schedulers.GetScheduler(_defaultScheduler)->AddSystems(systems...);
94}
95
96template <CScheduler TScheduler, typename System, typename ErrorCallback>
97inline decltype(auto) Core::RegisterSystemWithErrorHandler(System system, ErrorCallback callback)
98{
99 return this->RegisterSystem<TScheduler>(WrappedSystem(system, callback));
100}
101
102template <typename System, typename ErrorCallback>
103inline decltype(auto) Core::RegisterSystemWithErrorHandler(System system, ErrorCallback callback)
104{
105 return this->RegisterSystem(WrappedSystem(system, callback));
106}
107
108template <CPlugin... TPlugins> void Core::AddPlugins() { (AddPlugin<TPlugins>(), ...); }
109
110template <CPlugin TPlugin> void Core::AddPlugin()
111{
112 if (this->_plugins.contains(std::type_index(typeid(TPlugin))))
113 {
114 Log::Warning(fmt::format("Plugin {} already added", typeid(TPlugin).name()));
115 }
116 this->_plugins.emplace(std::type_index(typeid(TPlugin)), std::make_unique<TPlugin>(*this));
117 this->_plugins.at(std::type_index(typeid(TPlugin)))->Bind();
118}
119
120template <CPlugin TPlugin> bool Core::HasPlugin() const
121{
122 return this->_plugins.contains(std::type_index(typeid(TPlugin)));
123}
124
125template <CScheduler TScheduler> void Core::SetDefaultScheduler()
126{
127 SetDefaultScheduler(std::type_index(typeid(TScheduler)));
128}
129
130inline void Core::SetDefaultScheduler(std::type_index scheduler)
131{
132 if (!this->_schedulers.Contains(scheduler))
133 {
134 Log::Warning(fmt::format("Trying to set a default scheduler that does not exist: {}", scheduler.name()));
135 }
136 this->_defaultScheduler = scheduler;
137}
138} // namespace Engine
void AddPlugin()
Adds a plugin.
Definition Core.ipp:110
std::unique_ptr< Registry > _registry
The registry that contains all components (and by definition, the entities).
Definition Core.hpp:227
TResource & GetResource()
Get a reference of a resource.
Definition Core.ipp:14
TScheduler & RegisterScheduler(Args &&...args)
Add a new scheduler to the registry.
Definition Core.ipp:36
void SetSchedulerBefore()
Sets the execution order of two schedulers, ensuring that TSchedulerA is executed before TSchedulerB.
Definition Core.ipp:66
bool HasResource() const
Check if a resource is registered in the core.
Definition Core.ipp:24
TScheduler & GetScheduler()
Get a scheduler from the registry by its type.
Definition Core.ipp:47
Engine::SchedulerContainer _schedulers
The container that contains all the schedulers.
Definition Core.hpp:229
void RemoveDependencyAfter()
Removes a dependency between two schedulers, ensuring that TSchedulerB is no longer dependent on TSch...
Definition Core.ipp:76
decltype(auto) RegisterSystemWithErrorHandler(System system, ErrorCallback callback)
Add a system to a specific scheduler, associated with a callback that should run if the system fails ...
Definition Core.ipp:97
void SetDefaultScheduler()
Sets the default scheduler.
Definition Core.ipp:125
decltype(auto) RegisterSystem(Systems... systems)
Add one or multiple systems associated with a specific scheduler.
Definition Core.ipp:61
std::type_index _defaultScheduler
The default scheduler type index. It is used when adding systems without specifying a scheduler.
Definition Core.hpp:231
bool HasPlugin() const
Checks if a plugin is present.
Definition Core.ipp:120
void AddPlugins()
Adds multiple plugins that will be call instantly through the Bind method to agregate their systems a...
Definition Core.ipp:108
void RemoveDependencyBefore()
Removes a dependency between two schedulers, ensuring that TSchedulerA is no longer dependent on TSch...
Definition Core.ipp:81
std::vector< std::type_index > _schedulersToDelete
The list of schedulers to delete at the end of the current loop.
Definition Core.hpp:233
void SetSchedulerAfter()
Sets the execution order of two schedulers by specifying that one scheduler should execute after anot...
Definition Core.ipp:71
std::unordered_map< std::type_index, std::unique_ptr< IPlugin > > _plugins
The plugins added to the core.
Definition Core.hpp:235
void DeleteScheduler()
Deletes a scheduler.
Definition Core.ipp:42
void DeleteResource()
Delete a resource from the core.
Definition Core.ipp:29
TResource & RegisterResource(TResource &&resource)
Store a resource instance.
Definition Core.ipp:9
Exception thrown when a requested resource is not found in the Core.
Definition MissingResourceError.hpp:8
Exception thrown when a requested scheduler is not found in the Core.
Definition MissingSchedulerError.hpp:8
AScheduler is an abstract class that implements the IScheduler interface. It provides common function...
Definition AScheduler.hpp:16
Wrapper around a system that allows to add an error callback to it.
Definition WrappedSystem.hpp:14
Definition Core.hpp:25
Definition Core.hpp:22
Definition Core.hpp:17
FunctionUtils::CallableFunction< TCallable, void, Core & > System
Definition System.hpp:20
void Warning(const T &msg) noexcept(false)
Definition Logger.hpp:32