Cuberite
A lightweight, fast and extensible game server for Minecraft
|
A light-weight, type-erased reference to a function object. More...
#include <FunctionRef.h>
Public Member Functions | |
template<class FunctionObject , typename std::enable_if< !std::is_same< typename std::decay< FunctionObject >::type, cFunctionRef >::value, int >::type = 0> | |
cFunctionRef (FunctionObject &&a_FunctionObject) | |
Construct from a function object. More... | |
Ret | operator() (Args... a_Args) |
Call the referenced function object. More... | |
Private Types | |
using | cCallFunction = Ret(*)(void *, Args...) |
Static Private Member Functions | |
template<class ObjectType > | |
static Ret | ObjectFunctionCaller (void *a_Callable, Args... a_Args) |
Function that performs the call. More... | |
Private Attributes | |
void * | m_CallableData |
Type erased reference to a callable. More... | |
cCallFunction | m_CallFunction |
Function that knows how to call the type erased reference. More... | |
A light-weight, type-erased reference to a function object.
cFunctionRef
is used whenever you call a normal function with a lambda. e.g. one of the cWorld::DoWith
functions,
m_World->DoWithChunkAt(BlockPos, [](cChunk & a_Chunk) -> bool { ... } );
It looks like you're calling it with a lambda but that would require DoWithChunkAt
to be a template. The function is really being called with cFunctionRef<bool(cChunk&)>
which is constructed from the lambda via a templated constructor. This gives you a generic pointer to the lambda which doesn't depend on the type of the function object it references.
std::function
but doesn't copy the function object. This means that mutable function objects will be modified for the caller but would not be if using a std::function
(See #3990 for implications of this).cFunctionRef
has no empty state but is non-owning and so is safe to call as long as the referred object is still alive. Definition at line 36 of file FunctionRef.h.
|
private |
Definition at line 74 of file FunctionRef.h.
|
inline |
Construct from a function object.
Definition at line 45 of file FunctionRef.h.
|
inlinestaticprivate |
Function that performs the call.
Definition at line 64 of file FunctionRef.h.
|
inline |
Call the referenced function object.
Definition at line 55 of file FunctionRef.h.
|
private |
Type erased reference to a callable.
Definition at line 77 of file FunctionRef.h.
|
private |
Function that knows how to call the type erased reference.
Definition at line 80 of file FunctionRef.h.