VR-Engage  2.2
Loading...
Searching...
No Matches
makVre::DtLuaState Class Reference

Detailed Description

This class encapsulates a Lua state and provides methods for executing scripts, accessing Lua objects, and converting between C++ and Lua data types. It also manages the encoder/decoder registry for type conversion.

#include <luaState.h>

Inheritance diagram for makVre::DtLuaState:
[legend]

Public Member Functions

 DtLuaState ()
 
 DtLuaState (lua_State &L)
 
virtual ~DtLuaState ()
 
DtLuaStateoperator= (const DtLuaState &orig)
 
lua_State * state ()
 
virtual bool hasObject (const std::string &name)
 
virtual DtLuaObject findObject (const std::string &name)
 
virtual bool doFile (const std::string &filename)
 
virtual bool doScript (const std::string &data)
 
template<typename RT>
RT get (int stackIndex)
 
template<typename RT>
RT get (const std::string &name, bool resolveDots=true)
 
template<typename T>
void push (const T &data)
 
template<typename T>
bool store (const std::string &name, const T &data)
 
virtual DtLuaObject createTable (int narr=0, int nrec=0)
 
virtual void clearStack ()
 
virtual void stackDump ()
 
virtual void forceGarbageCollection ()
 
virtual bool getItemOnStack (const std::string &name, bool resolveDots=true)
 
virtual bool topStackValueOk ()
 
virtual int stackSize ()
 

Static Public Member Functions

template<typename T>
static void addEncoder (DtBaseEncoder *encoder)
 
template<typename T>
static void addDecoder (DtBaseDecoder *decoder)
 
template<typename T>
static void addEncoder (DtBaseEncoder *encoder)
 
template<typename T>
static void addDecoder (DtBaseDecoder *decoder)
 

Protected Types

using EncoderMap = std::map<std::string, DtBaseEncoder*>
 
using DecoderMap = std::map<std::string, DtBaseDecoder*>
 

Protected Member Functions

virtual void printValue (int index)
 
virtual void printTable (int index)
 

Static Protected Member Functions

static void initEncoders ()
 
static void initDecoders ()
 

Protected Attributes

lua_State * myState
 
bool myNeedToCloseState
 

Static Protected Attributes

static EncoderMap theEncoders
 
static DecoderMap theDecoders
 
static bool theInitializedEncoderDecoders
 

Private Member Functions

 DtLuaState (const DtLuaState &orig)
 

Member Typedef Documentation

◆ EncoderMap

using makVre::DtLuaState::EncoderMap = std::map<std::string, DtBaseEncoder*>
protected

Type definition for the encoder registry map.

◆ DecoderMap

using makVre::DtLuaState::DecoderMap = std::map<std::string, DtBaseDecoder*>
protected

Type definition for the decoder registry map.

Constructor & Destructor Documentation

◆ DtLuaState() [1/3]

makVre::DtLuaState::DtLuaState ( const DtLuaState & orig)
private

Copy constructor (not implemented)

Copying a Lua state is not supported

References DtLuaState().

Referenced by DtLuaState(), and operator=().

◆ DtLuaState() [2/3]

makVre::DtLuaState::DtLuaState ( )

Default constructor.

Creates a new Lua state and initializes it with standard libraries

◆ DtLuaState() [3/3]

makVre::DtLuaState::DtLuaState ( lua_State & L)

Constructor with existing Lua state.

Parameters
LReference to an existing Lua state to use

◆ ~DtLuaState()

virtual makVre::DtLuaState::~DtLuaState ( )
virtual

Virtual destructor.

Closes the Lua state if it was created by this object

Member Function Documentation

◆ operator=()

DtLuaState & makVre::DtLuaState::operator= ( const DtLuaState & orig)

Assignment operator.

Parameters
origThe Lua state to copy from
Returns
Reference to this object

Copies the Lua state reference but does not copy the actual Lua state

References DtLuaState().

◆ state()

lua_State * makVre::DtLuaState::state ( )

Gets the underlying Lua state.

Returns
Pointer to the Lua state

◆ hasObject()

virtual bool makVre::DtLuaState::hasObject ( const std::string & name)
virtual

Checks if an object exists in the Lua state.

Parameters
nameName of the object to check for
Returns
True if the object exists, false otherwise

◆ findObject()

virtual DtLuaObject makVre::DtLuaState::findObject ( const std::string & name)
virtual

Finds and returns a Lua object.

Parameters
nameName of the object to find
Returns
The Lua object if found

◆ doFile()

virtual bool makVre::DtLuaState::doFile ( const std::string & filename)
virtual

Executes a Lua script from a file.

Parameters
filenamePath to the Lua script file
Returns
True if execution succeeded, false otherwise

◆ doScript()

virtual bool makVre::DtLuaState::doScript ( const std::string & data)
virtual

Executes a Lua script from a string.

Parameters
dataThe Lua script code as a string
Returns
True if execution succeeded, false otherwise

References get(), and push().

◆ get() [1/2]

template<typename RT>
RT makVre::DtLuaState::get ( int stackIndex)

Gets a value from the Lua stack and converts it to a C++ type.

Template Parameters
RTReturn type to convert the Lua value to
Parameters
stackIndexIndex on the Lua stack
Returns
The value converted to the requested C++ type
Exceptions
std::stringif a decoder for the requested type is not found

References DtASSERT, makVre::DtTypeDecoder< T >::get(), myState, and theDecoders.

Referenced by doScript(), and get().

◆ get() [2/2]

template<typename RT>
RT makVre::DtLuaState::get ( const std::string & name,
bool resolveDots = true )

Gets a named value from Lua and converts it to a C++ type.

Template Parameters
RTReturn type to convert the Lua value to
Parameters
nameName of the Lua variable/object to get
resolveDotsWhether to resolve dotted names (e.g., 'table.field')
Returns
The value converted to the requested C++ type
Exceptions
std::stringif the variable doesn't exist or a decoder for the type is not found

References get(), getItemOnStack(), and myState.

◆ push()

template<typename T>
void makVre::DtLuaState::push ( const T & data)

Pushes a C++ value onto the Lua stack.

Template Parameters
TType of the value to push
Parameters
dataThe value to push onto the Lua stack
Exceptions
std::stringif an encoder for the specified type is not found

References DtASSERT, myState, makVre::DtTypeEncoder< T >::push(), and theEncoders.

Referenced by doScript(), makVre::DtLuaObject::setField(), and makVre::DtLuaObject::setField().

◆ store()

template<typename T>
bool makVre::DtLuaState::store ( const std::string & name,
const T & data )

Stores a C++ value as a named variable in Lua.

Template Parameters
TType of the value to store
Parameters
nameName to give the variable in Lua
dataThe value to store
Returns
True if storage succeeded, false otherwise

◆ createTable()

virtual DtLuaObject makVre::DtLuaState::createTable ( int narr = 0,
int nrec = 0 )
virtual

Creates a new Lua table.

Parameters
narrPre-allocated array slots (optimization hint)
nrecPre-allocated record slots (optimization hint)
Returns
A Lua object representing the new table

◆ clearStack()

virtual void makVre::DtLuaState::clearStack ( )
virtual

Clears the Lua stack.

Removes all items from the Lua stack

◆ stackDump()

virtual void makVre::DtLuaState::stackDump ( )
virtual

Dumps the contents of the Lua stack for debugging.

Prints each item on the stack to standard output

◆ forceGarbageCollection()

virtual void makVre::DtLuaState::forceGarbageCollection ( )
virtual

Forces a full Lua garbage collection cycle.

◆ addEncoder() [1/2]

template<typename T>
static void makVre::DtLuaState::addEncoder ( DtBaseEncoder * encoder)
static

Registers an encoder for a specific C++ type.

Template Parameters
TThe C++ type to register an encoder for
Parameters
encoderPointer to the encoder implementation

◆ addDecoder() [1/2]

template<typename T>
static void makVre::DtLuaState::addDecoder ( DtBaseDecoder * decoder)
static

Registers a decoder for a specific C++ type.

Template Parameters
TThe C++ type to register a decoder for
Parameters
decoderPointer to the decoder implementation

◆ getItemOnStack()

virtual bool makVre::DtLuaState::getItemOnStack ( const std::string & name,
bool resolveDots = true )
virtual

Retrieves a named item and pushes it onto the stack.

Parameters
nameName of the item to retrieve
resolveDotsWhether to resolve dotted names (e.g., 'table.field')
Returns
True if the item was found and pushed, false otherwise

Referenced by get().

◆ topStackValueOk()

virtual bool makVre::DtLuaState::topStackValueOk ( )
virtual

Checks if the top stack value is valid.

Returns
True if the top stack value is valid, false otherwise

◆ stackSize()

virtual int makVre::DtLuaState::stackSize ( )
virtual

Gets the current size of the Lua stack.

Returns
Number of items on the stack

◆ printValue()

virtual void makVre::DtLuaState::printValue ( int index)
protectedvirtual

Prints a single value from the stack for debugging.

Parameters
indexIndex of the value to print

◆ printTable()

virtual void makVre::DtLuaState::printTable ( int index)
protectedvirtual

Prints a table from the stack for debugging.

Parameters
indexIndex of the table to print

◆ initEncoders()

static void makVre::DtLuaState::initEncoders ( )
staticprotected

Initializes the standard encoders.

◆ initDecoders()

static void makVre::DtLuaState::initDecoders ( )
staticprotected

Initializes the standard decoders.

◆ addEncoder() [2/2]

template<typename T>
static void makVre::DtLuaState::addEncoder ( DtBaseEncoder * encoder)
static

References theEncoders.

◆ addDecoder() [2/2]

template<typename T>
static void makVre::DtLuaState::addDecoder ( DtBaseDecoder * decoder)
static

References theDecoders.

Member Data Documentation

◆ myState

lua_State* makVre::DtLuaState::myState
protected

Pointer to the Lua state.

Referenced by get(), get(), and push().

◆ myNeedToCloseState

bool makVre::DtLuaState::myNeedToCloseState
protected

Flag indicating if the state should be closed on destruction.

◆ theEncoders

EncoderMap makVre::DtLuaState::theEncoders
staticprotected

Static map of type names to encoder implementations.

Referenced by addEncoder(), and push().

◆ theDecoders

DecoderMap makVre::DtLuaState::theDecoders
staticprotected

Static map of type names to decoder implementations.

Referenced by addDecoder(), and get().

◆ theInitializedEncoderDecoders

bool makVre::DtLuaState::theInitializedEncoderDecoders
staticprotected

Flag indicating if encoders and decoders have been initialized.


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