Cuberite
A lightweight, fast and extensible game server for Minecraft
Public Member Functions | Private Types | List of all members
cLuaState::cTableRef Class Reference

Represents a stored Lua table with callback functions that C++ code can call. More...

#include <LuaState.h>

Inheritance diagram for cLuaState::cTableRef:
Inheritance graph
[legend]
Collaboration diagram for cLuaState::cTableRef:
Collaboration graph
[legend]

Public Member Functions

template<typename... Args>
bool CallTableFn (const char *a_FnName, Args &&... args)
 Calls the Lua function stored under the specified name in the referenced table, if still available. More...
 
template<typename... Args>
bool CallTableFnWithSelf (const char *a_FnName, Args &&... args)
 Calls the Lua function stored under the specified name in the referenced table, if still available. More...
 
 cTableRef (void)
 
bool RefStack (cLuaState &a_LuaState, int a_StackPos)
 Set the contained reference to the table in the specified Lua state's stack position. More...
 
- Public Member Functions inherited from cLuaState::cTrackedRef
void Clear (void)
 Frees the contained reference, if any. More...
 
 cTrackedRef (void)
 Creates an unbound ref instance. More...
 
bool IsSameLuaState (cLuaState &a_LuaState)
 Returns true if the reference resides in the specified Lua state. More...
 
bool IsValid (void)
 Returns true if the contained reference is valid. More...
 
bool RefStack (cLuaState &a_LuaState, int a_StackPos)
 Set the contained reference to the object at the specified Lua state's stack position. More...
 
 ~cTrackedRef ()
 

Private Types

using Super = cTrackedRef
 

Additional Inherited Members

- Protected Member Functions inherited from cLuaState::cTrackedRef
 cTrackedRef (const cTrackedRef &)=delete
 This class cannot be copied, because it is tracked in the LuaState by-ptr. More...
 
 cTrackedRef (cTrackedRef &&)=delete
 This class cannot be moved, because it is tracked in the LuaState by-ptr. More...
 
cRefGetRef ()
 Returns the internal reference. More...
 
void Invalidate (void)
 Invalidates the callback, without untracking it from the cLuaState. More...
 
- Protected Attributes inherited from cLuaState::cTrackedRef
std::atomic< cCriticalSection * > m_CS
 The mutex protecting m_Ref against multithreaded access. More...
 
cRef m_Ref
 Reference to the Lua callback. More...
 

Detailed Description

Represents a stored Lua table with callback functions that C++ code can call.

Is thread-safe and unload-safe. When Lua state is unloaded, the CallFn() will return failure instead of calling into non-existent code. To receive the cTableRef instance from the Lua side, use RefStack() or (better) cLuaState::GetStackValue() with a cTableRefPtr. Note that instances of this class are tracked in the canon LuaState instance, so that they can be invalidated when the LuaState is unloaded; due to multithreading issues they can only be tracked by-ptr, which has an unfortunate effect of disabling the copy and move constructors.

Definition at line 367 of file LuaState.h.

Member Typedef Documentation

◆ Super

Definition at line 370 of file LuaState.h.

Constructor & Destructor Documentation

◆ cTableRef()

cLuaState::cTableRef::cTableRef ( void  )
inline

Definition at line 373 of file LuaState.h.

Member Function Documentation

◆ CallTableFn()

template<typename... Args>
bool cLuaState::cTableRef::CallTableFn ( const char *  a_FnName,
Args &&...  args 
)
inline

Calls the Lua function stored under the specified name in the referenced table, if still available.

Returns true if callback has been called. Returns false if the Lua state isn't valid anymore, or the function doesn't exist.

Definition at line 379 of file LuaState.h.

◆ CallTableFnWithSelf()

template<typename... Args>
bool cLuaState::cTableRef::CallTableFnWithSelf ( const char *  a_FnName,
Args &&...  args 
)
inline

Calls the Lua function stored under the specified name in the referenced table, if still available.

A "self" parameter is injected in front of all the given parameters and is set to the callback table. Returns true if callback has been called. Returns false if the Lua state isn't valid anymore, or the function doesn't exist.

Definition at line 399 of file LuaState.h.

◆ RefStack()

bool cLuaState::cTableRef::RefStack ( cLuaState a_LuaState,
int  a_StackPos 
)

Set the contained reference to the table in the specified Lua state's stack position.

If another table has been previously contained, it is unreferenced first. Returns true on success, false on failure (not a table at the specified stack pos).

Definition at line 357 of file LuaState.cpp.


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