Nameless Engine
Loading...
Searching...
No Matches
NodeFunction.hpp
1#pragma once
2
3// Standard.
4#include <functional>
5#include <optional>
6
7// Custom.
8#include "game/GameManager.h"
9
10namespace ne {
11 template <typename FunctionReturnType, typename... FunctionArgs> class NodeFunction;
12
20 template <typename FunctionReturnType, typename... FunctionArgs>
21 class NodeFunction<FunctionReturnType(FunctionArgs...)> {
22 public:
23 NodeFunction() = default;
24
34 NodeFunction(size_t iNodeId, const std::function<FunctionReturnType(FunctionArgs...)>& callback) {
35 static_assert(
36 std::is_same_v<FunctionReturnType, void>,
37 "return type must be `void` - this is a current limitation");
38
39 this->iNodeId = iNodeId;
40 this->callback = callback;
41 }
42
48 NodeFunction(const NodeFunction& other) = default;
49
57 NodeFunction& operator=(const NodeFunction& other) = default;
58
64 NodeFunction(NodeFunction&& other) noexcept = default;
65
73 NodeFunction& operator=(NodeFunction&& other) noexcept = default;
74
91 bool operator()(FunctionArgs&&... args) {
92 if (isNodeSpawned()) {
93 callback(std::forward<FunctionArgs>(args)...);
94 return false;
95 }
96
97 return true;
98 }
99
109 // Get game manager.
110 const auto pGameManager = GameManager::get();
111 if (pGameManager == nullptr) [[unlikely]] {
112 return false;
113 }
114
115 // Make sure the game manager is not being destroyed.
116 if (pGameManager->isBeingDestroyed()) [[unlikely]] {
117 // Exit now because it might be dangerous to continue if we are on a non-main thread.
118 return false;
119 }
120
121 return pGameManager->isNodeSpawned(iNodeId);
122 }
123
124 private:
126 std::function<FunctionReturnType(FunctionArgs...)> callback;
127
129 size_t iNodeId = 0;
130 };
131} // namespace ne
static GameManager * get()
Definition: GameManager.cpp:299
NodeFunction(const NodeFunction &other)=default
bool operator()(FunctionArgs &&... args)
Definition: NodeFunction.hpp:91
std::function< FunctionReturnType(FunctionArgs...)> callback
Definition: NodeFunction.hpp:126
NodeFunction(size_t iNodeId, const std::function< FunctionReturnType(FunctionArgs...)> &callback)
Definition: NodeFunction.hpp:34
NodeFunction & operator=(const NodeFunction &other)=default
NodeFunction(NodeFunction &&other) noexcept=default
NodeFunction & operator=(NodeFunction &&other) noexcept=default
bool isNodeSpawned()
Definition: NodeFunction.hpp:108
Definition: NodeFunction.hpp:11