Nameless Engine
Loading...
Searching...
No Matches
ne::NodeFunction< FunctionReturnType(FunctionArgs...)> Class Template Reference

#include <NodeFunction.hpp>

Public Member Functions

 NodeFunction (size_t iNodeId, const std::function< FunctionReturnType(FunctionArgs...)> &callback)
 
 NodeFunction (const NodeFunction &other)=default
 
NodeFunctionoperator= (const NodeFunction &other)=default
 
 NodeFunction (NodeFunction &&other) noexcept=default
 
NodeFunctionoperator= (NodeFunction &&other) noexcept=default
 
bool operator() (FunctionArgs &&... args)
 
bool isNodeSpawned ()
 

Private Attributes

std::function< FunctionReturnType(FunctionArgs...)> callback
 
size_t iNodeId = 0
 

Detailed Description

template<typename FunctionReturnType, typename... FunctionArgs>
class ne::NodeFunction< FunctionReturnType(FunctionArgs...)>

std::function wrapper used for Node functions/lambdas with an additional check (compared to the usual std::function): once the callback is called this class will first check if the node, the callback function points to, is still spawned or not, and if not spawned then the callback function will not be called to avoid running functions on despawned nodes or hitting deleted memory.

Constructor & Destructor Documentation

◆ NodeFunction() [1/3]

template<typename FunctionReturnType , typename... FunctionArgs>
ne::NodeFunction< FunctionReturnType(FunctionArgs...)>::NodeFunction ( size_t  iNodeId,
const std::function< FunctionReturnType(FunctionArgs...)> &  callback 
)
inline

Constructor.

Warning
Don't capture gc pointers in std::function (as it's used under the hood).
Parameters
iNodeIdID of the spawned node that "contains" the callback.
callbackPoints to the function/lambda of the spawned node with ID that was specified as the previous argument.

◆ NodeFunction() [2/3]

template<typename FunctionReturnType , typename... FunctionArgs>
ne::NodeFunction< FunctionReturnType(FunctionArgs...)>::NodeFunction ( const NodeFunction< FunctionReturnType(FunctionArgs...)> &  other)
default

Copy constructor.

Parameters
otherOther object.

◆ NodeFunction() [3/3]

template<typename FunctionReturnType , typename... FunctionArgs>
ne::NodeFunction< FunctionReturnType(FunctionArgs...)>::NodeFunction ( NodeFunction< FunctionReturnType(FunctionArgs...)> &&  other)
defaultnoexcept

Move constructor.

Parameters
otherOther object.

Member Function Documentation

◆ isNodeSpawned()

template<typename FunctionReturnType , typename... FunctionArgs>
bool ne::NodeFunction< FunctionReturnType(FunctionArgs...)>::isNodeSpawned ( )
inline

Checks if the node, the callback function points to, is still spawned or not.

Remarks
You don't need to call this function before calling the callback to check if the callback is still valid or not - it will be done automatically under the hood.
Returns
true if the node is still spawned, false otherwise.

◆ operator()()

template<typename FunctionReturnType , typename... FunctionArgs>
bool ne::NodeFunction< FunctionReturnType(FunctionArgs...)>::operator() ( FunctionArgs &&...  args)
inline

Calls the stores callback function with the specified arguments.

Remarks
If the node that the callback function points to is no longer spawned the callback function will not be called.
If you are calling the callback function in a multi-threaded environment (for ex. from a non-main thread) then once the callback function has started executing it's up to you to guarantee that the node, the callback function points to, will not be despawned while the callback function is executing (if it matters for you).
Parameters
argsArguments to pass to callback function call.
Returns
true if the node, the callback function points to, was despawned and the callback was not called to avoid running logic on despawned/deleted node, otherwise false.

◆ operator=() [1/2]

template<typename FunctionReturnType , typename... FunctionArgs>
NodeFunction & ne::NodeFunction< FunctionReturnType(FunctionArgs...)>::operator= ( const NodeFunction< FunctionReturnType(FunctionArgs...)> &  other)
default

Copy assignment.

Parameters
otherOther object.
Returns
Result of copy assignment.

◆ operator=() [2/2]

template<typename FunctionReturnType , typename... FunctionArgs>
NodeFunction & ne::NodeFunction< FunctionReturnType(FunctionArgs...)>::operator= ( NodeFunction< FunctionReturnType(FunctionArgs...)> &&  other)
defaultnoexcept

Move assignment.

Parameters
otherOther object.
Returns
Result of move assignment.

Member Data Documentation

◆ callback

template<typename FunctionReturnType , typename... FunctionArgs>
std::function<FunctionReturnType(FunctionArgs...)> ne::NodeFunction< FunctionReturnType(FunctionArgs...)>::callback
private

Points to the function of the spawned node with ID iNodeId.

◆ iNodeId

template<typename FunctionReturnType , typename... FunctionArgs>
size_t ne::NodeFunction< FunctionReturnType(FunctionArgs...)>::iNodeId = 0
private

ID of the spawned node that contains callback function.


The documentation for this class was generated from the following file: