Simple Lua
A Simple C++ Lua Wrapper
Loading...
Searching...
No Matches
SL::Runtime Struct Reference

Represents a single Lua runtime. More...

#include <Runtime.hpp>

Public Types

enum class  ErrorCode {
  None , TypeMismatch , VariableDoesntExist , NotFunction ,
  FunctionError
}
 
template<typename T >
using Result = Util::Result<T, Util::Error<ErrorCode>>
 

Public Member Functions

SL_SYMBOL Runtime (const std::string &filename)
 Construct a Lua runtime from a script.
 
SL_SYMBOL Runtime (Runtime &&r)
 
 Runtime (const Runtime &)=delete
 
SL_SYMBOL Result< void > registerFunction (const std::string &table_name, const std::string &func_name, SL::Function function)
 Registers a C++ function for use in the Lua runtime.
 
template<typename T >
SL_SYMBOL Result< T > getGlobal (const std::string &name)
 Get a global variable by name from runtime.
 
template<typename T >
SL_SYMBOL Result< void > setGlobal (const std::string &name, const T &value)
 Set a global variable from a value to a name.
 
template<typename... Return, typename... Args>
Result< std::tuple< Return... > > runFunction (const std::string &name, Args &&... args)
 Invokes a Lua function from this environment.
 
SL_SYMBOL bool good () const
 
SL_SYMBOL operator bool () const
 
const auto & filename () const
 

Static Public Member Functions

template<typename... Libraries>
static Runtime create (const std::string &filename)
 Creates a runtime with the given Libraries loaded into it.
 

Detailed Description

Represents a single Lua runtime.

Constructor & Destructor Documentation

◆ Runtime()

SL_SYMBOL SL::Runtime::Runtime ( const std::string & filename)

Construct a Lua runtime from a script.

Parameters
filenameFile path to the script

Member Function Documentation

◆ create()

template<typename... Libraries>
Runtime SL::Runtime::create ( const std::string & filename)
static

Creates a runtime with the given Libraries loaded into it.

Template Parameters
LibrariesList of library types that are derived from SL::Lib::Base.
Parameters
filenameName of the file to load into the runtime
Returns
Runtime The created runtime

◆ getGlobal()

template<typename T >
SL_SYMBOL Result< T > SL::Runtime::getGlobal ( const std::string & name)

Get a global variable by name from runtime.

Template Parameters
TType of the global variable (supported types in Lua namespace)
Parameters
nameName of the variable in the script
Returns
Result<T> The value of the variable or error

◆ registerFunction()

SL_SYMBOL Result< void > SL::Runtime::registerFunction ( const std::string & table_name,
const std::string & func_name,
SL::Function function )

Registers a C++ function for use in the Lua runtime.

Parameters
table_nameName of the table to register function in
func_nameName of the function to call in Lua
functionPointer to a static function
Returns
Result<void> Returns if an error has occured

◆ runFunction()

template<typename... Return, typename... Args>
Runtime::Result< std::tuple< Return... > > SL::Runtime::runFunction ( const std::string & name,
Args &&... args )
inline

Invokes a Lua function from this environment.

Template Parameters
ReturnExpected return types from the function
ArgsArguments to pass into the function
Parameters
nameName of the function
argsValues of the arguments
Returns
Result<std::tuple<Return...>> Contains the values returned from the function or error

◆ setGlobal()

template<typename T >
SL_SYMBOL Result< void > SL::Runtime::setGlobal ( const std::string & name,
const T & value )

Set a global variable from a value to a name.

Template Parameters
TType of the global variable (supported types in Lua namespace)
Parameters
nameName of the global variable
valueValue of the global variable
Returns
Result<void> The status of the operation

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