Engine²
Open-source game engine written in C++.
Loading...
Searching...
No Matches
CallableFunction.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "BaseFunction.hpp"
4#include "Demangle.hpp"
5#include <string>
6#include <typeinfo>
7
8namespace FunctionUtils {
12template <typename TCallable, typename TReturn, typename... TArgs>
13class CallableFunction : public BaseFunction<TReturn, TArgs...> {
14 public:
19 explicit CallableFunction(TCallable callable) : _callable(callable)
20 {
23 }
24
28 ~CallableFunction() override = default;
29
35 TReturn operator()(TArgs... args) const override { return _callable(args...); }
36
41 FunctionID GetID() const override { return _id; }
42
47 std::string GetName() const override { return _name; }
48
55 static FunctionID GetCallableID(TCallable callable)
56 {
57 if constexpr (std::is_class_v<TCallable>)
58 {
59 return typeid(callable).hash_code();
60 }
61 else
62 {
63 return std::hash<TCallable>{}(callable);
64 }
65 }
66
67 static std::string GetCallableName(TCallable callable)
68 {
69 if constexpr (std::is_class_v<TCallable>)
70 {
71 return FunctionUtils::DemangleTypeName(typeid(callable));
72 }
73 else
74 {
75 return std::to_string(GetCallableID(callable));
76 }
77 }
78
79 private:
80 [[no_unique_address]] TCallable _callable;
82 std::string _name;
83};
84} // namespace FunctionUtils
Base class for all functions contained in a FunctionContainer.
Definition BaseFunction.hpp:10
static FunctionID GetCallableID(TCallable callable)
Get the ID of the callable object.
Definition CallableFunction.hpp:55
~CallableFunction() override=default
Destructor for CallableFunction.
FunctionID GetID() const override
Returns the unique ID of the callable object.
Definition CallableFunction.hpp:41
std::string _name
Definition CallableFunction.hpp:82
TCallable _callable
Definition CallableFunction.hpp:80
TReturn operator()(TArgs... args) const override
Calls the stored callable object with the provided arguments.
Definition CallableFunction.hpp:35
static std::string GetCallableName(TCallable callable)
Definition CallableFunction.hpp:67
std::string GetName() const override
Returns the name of the callable object.
Definition CallableFunction.hpp:47
CallableFunction(TCallable callable)
Constructor for CallableFunction.
Definition CallableFunction.hpp:19
FunctionID _id
Definition CallableFunction.hpp:81
Definition BaseFunction.hpp:6
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