Engine²
Open-source game engine written in C++.
Loading...
Searching...
No Matches
FunctionContainer.inl
Go to the documentation of this file.
3#include "Logger.hpp"
4
5template <typename TReturn, typename... TArgs>
6template <typename TCallable>
8{
10 std::string name;
11
12 if constexpr (is_derived_from_function_type<TCallable>::value)
13 {
14 id = callable.GetID();
15 name = callable.GetName();
16 }
17 else
18 {
21 }
22
23 if (_idToIterator.contains(id))
24 {
25 Log::Warning(fmt::format("Function already exists: {}", name)); // TODO: be able to change container thing name
26 return id;
27 }
28
29 auto function = std::make_unique<CallableFunction<TCallable, TReturn, TArgs...>>(callable);
30 _orderedFunctions.push_back(std::move(function));
31
32 auto it = std::prev(_orderedFunctions.end());
33 _idToIterator[id] = it;
34
35 return id;
36}
37
38template <typename TReturn, typename... TArgs>
40 std::unique_ptr<BaseFunction<TReturn, TArgs...>> &&function)
41{
42 FunctionUtils::FunctionID id = function->GetID();
43
44 if (_idToIterator.contains(id))
45 {
46 Log::Warning("Function already exists: " + function->GetName()); // TODO: be able to change container thing name
47 return id;
48 }
49
50 _orderedFunctions.push_back(std::move(function));
51
52 auto it = std::prev(_orderedFunctions.end());
53 _idToIterator[id] = it;
54
55 return id;
56}
57
58template <typename TReturn, typename... TArgs>
59std::unique_ptr<typename FunctionUtils::FunctionContainer<TReturn, TArgs...>::FunctionType>
61{
62 auto mapIt = _idToIterator.find(id);
63 if (mapIt == _idToIterator.end())
64 {
65 Log::Warning("Function not found");
66 return nullptr;
67 }
68
69 auto listIt = mapIt->second;
70 auto func = std::move(*listIt);
71
72 _orderedFunctions.erase(listIt);
73 _idToIterator.erase(mapIt);
74
75 return func;
76}
Base class for all functions contained in a FunctionContainer.
Definition BaseFunction.hpp:10
Abstract class that holds a callable object, to be later stored in a container.
Definition CallableFunction.hpp:13
static FunctionID GetCallableID(TCallable callable)
Get the ID of the callable object.
Definition CallableFunction.hpp:55
static std::string GetCallableName(TCallable callable)
Definition CallableFunction.hpp:67
Container for functions, allowing for dynamic storage and invocation.
Definition FunctionContainer.hpp:14
FunctionID AddFunction(TCallable callable)
Adds a function to the container.
std::unordered_map< FunctionID, typename std::list< std::unique_ptr< FunctionType > >::iterator > _idToIterator
Definition FunctionContainer.hpp:113
std::unique_ptr< FunctionType > DeleteFunction(FunctionID id)
Deletes a function from the container.
Definition FunctionContainer.inl:60
std::list< std::unique_ptr< FunctionType > > _orderedFunctions
Definition FunctionContainer.hpp:111
std::size_t FunctionID
FunctionID class to represent a unique identifier for functions.
Definition FunctionID.hpp:9
void Warning(const T &msg) noexcept
Definition Logger.hpp:49