Engine²
Open-source game engine written in C++.
Loading...
Searching...
No Matches
WrappedSystem.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "BaseFunction.hpp"
4#include "Demangle.hpp"
5
6namespace Engine {
7class Core;
8
13template <typename TSystem, typename TErrorCallback>
14class WrappedSystem : public FunctionUtils::BaseFunction<void, Core &> {
15 public:
20 explicit WrappedSystem(TSystem system, TErrorCallback errorCallback)
21 : _system(system), _errorCallback(errorCallback)
22 {
25 }
26
28 ~WrappedSystem() override = default;
29
33 void operator()(Engine::Core &core) const override
34 {
35 try
36 {
37 return _system(core);
38 }
39 catch (const std::exception &) // NOSONAR
40 {
41 _errorCallback(core);
42 throw;
43 }
44 }
45
49 FunctionUtils::FunctionID GetID() const override { return _id; }
50
54 std::string GetName() const override { return _name; }
55
61 static FunctionUtils::FunctionID GetCallableID(const TSystem &callable)
62 {
63 if constexpr (std::is_class_v<TSystem>)
64 {
65 return typeid(callable).hash_code();
66 }
67 else
68 {
69 return std::hash<TSystem>{}(callable);
70 }
71 }
72
78 static std::string GetCallableName(const TSystem &callable)
79 {
80 if constexpr (std::is_class_v<TSystem>)
81 {
82 return FunctionUtils::DemangleTypeName(typeid(callable));
83 }
84 else
85 {
86 return std::to_string(GetCallableID(callable));
87 }
88 }
89
90 private:
92 [[no_unique_address]] TSystem _system;
93
95 [[no_unique_address]] TErrorCallback _errorCallback;
96
99
101 std::string _name;
102};
103} // namespace Engine
The core is the place where all the data of the engine is stored. It contains the registry (entities)...
Definition Core.hpp:33
WrappedSystem(TSystem system, TErrorCallback errorCallback)
Constructor for WrappedSystem.
Definition WrappedSystem.hpp:20
FunctionUtils::FunctionID GetID() const override
Returns the unique ID of the system.
Definition WrappedSystem.hpp:49
std::string _name
Name of the system.
Definition WrappedSystem.hpp:101
~WrappedSystem() override=default
Destructor for WrappedSystem.
TSystem _system
The wrapped system.
Definition WrappedSystem.hpp:92
static FunctionUtils::FunctionID GetCallableID(const TSystem &callable)
Get the ID of the system.
Definition WrappedSystem.hpp:61
TErrorCallback _errorCallback
The error callback to be called if the system throws an exception.
Definition WrappedSystem.hpp:95
void operator()(Engine::Core &core) const override
Calls the system.
Definition WrappedSystem.hpp:33
static std::string GetCallableName(const TSystem &callable)
Get the name of the system.
Definition WrappedSystem.hpp:78
FunctionUtils::FunctionID _id
Unique ID for the system.
Definition WrappedSystem.hpp:98
std::string GetName() const override
Get the name of the system.
Definition WrappedSystem.hpp:54
Base class for all functions contained in a FunctionContainer.
Definition BaseFunction.hpp:10
Definition Core.hpp:17
std::size_t FunctionID
FunctionID class to represent a unique identifier for functions.
Definition FunctionID.hpp:9
std::string DemangleTypeName(const std::type_info &typeInfo)
Definition Demangle.hpp:13