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

Represents a stored callback to Lua that C++ code can call. More...

#include <LuaState.h>

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

Public Member Functions

template<typename... Args>
bool Call (Args &&... args)
 Calls the Lua callback, if still available. More...
 
 cCallback (void)
 
bool RefStack (cLuaState &a_LuaState, int a_StackPos)
 Set the contained callback to the function 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 ()
 

Protected Member Functions

 cCallback (cCallback &&)=delete
 This class cannot be moved, because it is tracked in the LuaState by-ptr. More...
 
 cCallback (const cCallback &)=delete
 This class cannot be copied, because it is tracked in the LuaState by-ptr. More...
 
- 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...
 

Private Types

using Super = cTrackedRef
 

Additional Inherited Members

- 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 callback to Lua that C++ code can call.

Is thread-safe and unload-safe. When the Lua state is unloaded, the callback returns an error instead of calling into non-existent code. To receive the callback instance from the Lua side, use RefStack() or (better) cLuaState::GetStackValue() with a cCallbackPtr. 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 283 of file LuaState.h.

Member Typedef Documentation

◆ Super

Definition at line 286 of file LuaState.h.

Constructor & Destructor Documentation

◆ cCallback() [1/3]

cLuaState::cCallback::cCallback ( void  )
inline

Definition at line 290 of file LuaState.h.

◆ cCallback() [2/3]

cLuaState::cCallback::cCallback ( const cCallback )
protecteddelete

This class cannot be copied, because it is tracked in the LuaState by-ptr.

Use cCallbackPtr for a copyable object.

◆ cCallback() [3/3]

cLuaState::cCallback::cCallback ( cCallback &&  )
protecteddelete

This class cannot be moved, because it is tracked in the LuaState by-ptr.

Use cCallbackPtr for a copyable object.

Member Function Documentation

◆ Call()

template<typename... Args>
bool cLuaState::cCallback::Call ( Args &&...  args)
inline

Calls the Lua callback, if still available.

Returns true if callback has been called. Returns false if the Lua state isn't valid anymore.

Definition at line 296 of file LuaState.h.

◆ RefStack()

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

Set the contained callback to the function in the specified Lua state's stack position.

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

Definition at line 319 of file LuaState.cpp.


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