Luwra
Minimal-overhead Lua wrapper for C++
Namespaces | Classes | Typedefs | Enumerations | Functions
luwra Namespace Reference

Namespaces

namespace  internal
 

Classes

struct  Function
 A callable Lua value. More...
 
struct  Function< void >
 A callable Lua value without a return value. More...
 
struct  Pushable
 Arbitrary pushable value. More...
 
struct  Reference
 Handle for a reference. More...
 
struct  RefLifecycle
 Lifecycle of a reference. More...
 
struct  ReturnValue
 A version of Value for pushing return values onto the stack. ReturnValue inherits push implementations from Value. More...
 
struct  ReturnValue< const Type >
 Alias for ReturnValue<Type> More...
 
struct  ReturnValue< const volatile Type >
 Alias for ReturnValue<Type> More...
 
struct  ReturnValue< std::pair< First, Second > >
 Enables std::pair as return type. More...
 
struct  ReturnValue< std::tuple< Contents... > >
 Enables std::tuple as return type. More...
 
struct  ReturnValue< Type & >
 Alias for ReturnValue<Type> More...
 
struct  ReturnValue< Type && >
 Alias for ReturnValue<Type> More...
 
struct  ReturnValue< volatile Type >
 Alias for ReturnValue<Type> More...
 
struct  StateWrapper
 Wrapper for a Lua state. More...
 
struct  Table
 Allows you to inspect and modify Lua tables. More...
 
struct  Value
 Enables reading/pushing for an arbitrary type. More...
 
struct  Value< bool >
 Enables reading/pushing booleans. More...
 
struct  Value< CFunction >
 Enables pushing for C functions. More...
 
struct  Value< char * >
 Alias for Value<const char*> More...
 
struct  Value< char[N]>
 Alias for Value<const char*> More...
 
struct  Value< const char * >
 Enables reading/pushing strings as C strings. More...
 
struct  Value< const char[N]>
 Alias for Value<const char*> More...
 
struct  Value< const Type >
 Alias for Value<Type> More...
 
struct  Value< const void * >
 Enables reading constant light data. More...
 
struct  Value< const volatile Type >
 Alias for Value<Type> More...
 
struct  Value< double >
 
struct  Value< float >
 
struct  Value< Function< Ret > >
 Enables reading/pushing Lua functions. More...
 
struct  Value< internal::InferValueType >
 Enables reading of type-infered values. More...
 
struct  Value< internal::Path< Parent, Key > >
 
struct  Value< internal::TableAccessor< Accessor > >
 
struct  Value< long double >
 
struct  Value< LuaType >
 Enables reading the type of a value. More...
 
struct  Value< Pushable >
 Enables pushing for Pushables More...
 
struct  Value< Reference >
 Enables referencing/dereferencing values. More...
 
struct  Value< RefLifecycle >
 
struct  Value< signed char >
 
struct  Value< signed int >
 
struct  Value< signed long int >
 
struct  Value< signed long long int >
 
struct  Value< signed short >
 
struct  Value< State * >
 Enables reading Lua threads. More...
 
struct  Value< std::function< Ret(Args...)> >
 Enables reading Lua functions as std::function More...
 
struct  Value< std::list< Type > >
 Enables pushing for std::list assuming Type is pushable. More...
 
struct  Value< std::map< Key, Type > >
 Enables pushing and reading for std::map assuming Key and Type are pushable or readable. More...
 
struct  Value< std::nullptr_t >
 Enables reading/pushing nil More...
 
struct  Value< std::string >
 Enables reading/pushing string as std::string. More...
 
struct  Value< std::vector< Type > >
 Enables pushing for std::vector assuming Type is also pushable. More...
 
struct  Value< Table >
 Enables reading/pushing tables. More...
 
struct  Value< Type & >
 Alias for Value<Type> More...
 
struct  Value< Type && >
 Alias for Value<Type> More...
 
struct  Value< unsigned char >
 
struct  Value< unsigned int >
 
struct  Value< unsigned long int >
 
struct  Value< unsigned long long int >
 
struct  Value< unsigned short >
 
struct  Value< UserType * >
 Enables reading and pushing the arbitrary type UserType. More...
 
struct  Value< void * >
 Enables reading/pushing light data. More...
 
struct  Value< volatile Type >
 Alias for Value<Type> More...
 

Typedefs

using CFunction = lua_CFunction
 
using Integer = lua_Integer
 
using MemberMap = std::map< Pushable, Pushable >
 Allows mixed-type map of members.
 
using Number = lua_Number
 
using State = lua_State
 

Enumerations

enum class  LuaType {
  Nil = LUA_TNIL ,
  Number = LUA_TNUMBER ,
  Boolean = LUA_TBOOLEAN ,
  String = LUA_TSTRING ,
  Table = LUA_TTABLE ,
  Function = LUA_TFUNCTION ,
  Userdata = LUA_TUSERDATA ,
  LightUserdata = LUA_TLIGHTUSERDATA ,
  Thread = LUA_TTHREAD
}
 Indicates the type of a Lua value. More...
 

Functions

template<typename Callable , typename... ExtraArgs>
internal::ReturnTypeOf< Callable > apply (State *state, int pos, Callable &&func, ExtraArgs &&... args)
 Retrieve values from the stack in order to invoke a Callable with them.
 
template<typename UserType , typename... Args>
UserType & construct (State *state, Args &&... args)
 Construct a user type value on the stack.
 
template<typename Iterable >
bool equal (State *state, const Iterable &indices)
 Check if 0 or more values are equal.
 
bool equal (State *state, int index1, int index2)
 Check if two values are equal.
 
template<typename Type , typename Key >
Type getField (State *state, int index, Key &&key)
 Retrieve a field from a table.
 
template<typename Type >
Type getGlobal (State *state, const char *name)
 Retrieve a value from the global namespace.
 
template<typename Callable , typename... ExtraArgs>
size_t map (State *state, int pos, Callable &&func, ExtraArgs &&... args)
 Retrieve values from the stack in order to invoke a Callable with them, then push the result onto the stack.
 
template<typename... Types>
void push (State *state, Types &&... values)
 Push zero or more values onto the stack.
 
template<typename... Types>
size_t pushReturn (State *state, Types &&... values)
 Push zero or more return values onto the stack.
 
template<typename Type = internal::InferValueType>
auto read (State *state, int index) -> decltype(Value< Type >::read(state, index))
 Read a value off the stack.
 
template<typename Sig >
void registerUserType (State *state, const char *ctor_name, const MemberMap &props=MemberMap(), const MemberMap &meta=MemberMap())
 Same as the other registerUserType but registers a constructor in the global namespace.
 
template<typename UserType >
void registerUserType (State *state, const MemberMap &props=MemberMap(), const MemberMap &meta=MemberMap())
 Register the metatable for a user type. This function allows you to register properties which are shared across all instances of the user type.
 
void setFields (State *state, int index, const MemberMap &fields)
 Apply key-value pairs to a table.
 
template<typename Key , typename Type >
void setFields (State *state, int index, Key &&key, Type &&value)
 Set the field of a table.
 
template<typename Key1 , typename Type1 , typename Key2 , typename Type2 , typename... Pairs>
void setFields (State *state, int index, Key1 &&key1, Type1 &&value1, Key2 &&key2, Type2 &&value2, Pairs &&... rest)
 Set multiple fields at once. Allows you to provide multiple key-value pairs.
 
template<typename Type >
void setGlobal (State *state, const char *name, Type &&value)
 Register a value in the global namespace.
 
void setMetatable (State *state, const char *name)
 Set a registered metatable for the table on top of the stack.
 

Typedef Documentation

◆ CFunction

using luwra::CFunction = typedef lua_CFunction

◆ Integer

using luwra::Integer = typedef lua_Integer

◆ MemberMap

using luwra::MemberMap = typedef std::map<Pushable, Pushable>

Allows mixed-type map of members.

Example:

MemberMap members {
{"foo", 13.37},
{1, "bar"},
{2, "baz"}
};

◆ Number

using luwra::Number = typedef lua_Number

◆ State

using luwra::State = typedef lua_State

Enumeration Type Documentation

◆ LuaType

enum class luwra::LuaType
strong

Indicates the type of a Lua value.

Enumerator
Nil 
Number 
Boolean 
String 
Table 
Function 
Userdata 
LightUserdata 
Thread 

Function Documentation

◆ apply()

template<typename Callable , typename... ExtraArgs>
internal::ReturnTypeOf< Callable > luwra::apply ( State state,
int  pos,
Callable &&  func,
ExtraArgs &&...  args 
)
inline

Retrieve values from the stack in order to invoke a Callable with them.

Parameters
stateLua state
posIndex of the first stack value
funcA Callable
argsExtra arguments passed to func before the stack values
Returns
Result of calling func

Invoke func with args... followed by the values retrieved from the stack.

Example 1:

double sum(double a, double b) {
return a + b;
}
push(state, 37.13);
push(state, 13.37);
// All parameters are extracted from the stack.
double result = apply(state, 1, sum);

Example 2:

push(state, 37.13);
push(state, 13.37);
// Only parameters 'b' and 'c' are extracted from the stack.
double result = apply(state, 1, [](double a, double b, double c) {
return a + b + c;
}, -0.5);

◆ construct()

template<typename UserType , typename... Args>
UserType & luwra::construct ( State state,
Args &&...  args 
)
inline

Construct a user type value on the stack.

Template Parameters
UserTypeType which you would like to construct
Parameters
stateLua state
argsConstructor arguments
Returns
Reference to the constructed value

Creates a full userdata which holds an instance of the user type. A metatable that is specific to UserType will be attached to the userdata. You can manage the metatable with registerUserType<UserType>.

Example:

struct A {
A(const char* a, int b);
};
A& a = construct<A>(state, "Hello World", 11);

◆ equal() [1/2]

template<typename Iterable >
bool luwra::equal ( State state,
const Iterable &  indices 
)
inline

Check if 0 or more values are equal.

Parameters
stateLua state
indicesIndices of values to compare

◆ equal() [2/2]

bool luwra::equal ( State state,
int  index1,
int  index2 
)
inline

Check if two values are equal.

Parameters
stateLua state
index1Index of left-hand side value
index2Index of right-hand side value

◆ getField()

template<typename Type , typename Key >
Type luwra::getField ( State state,
int  index,
Key &&  key 
)
inline

Retrieve a field from a table.

Template Parameters
TypeExpected type of the value
Parameters
stateLua state
indexIndex of the table
keyKey value

◆ getGlobal()

template<typename Type >
Type luwra::getGlobal ( State state,
const char *  name 
)
inline

Retrieve a value from the global namespace.

Template Parameters
TypeExpected type of the value
Parameters
stateLua state
nameGlobal name

◆ map()

template<typename Callable , typename... ExtraArgs>
size_t luwra::map ( State state,
int  pos,
Callable &&  func,
ExtraArgs &&...  args 
)
inline

Retrieve values from the stack in order to invoke a Callable with them, then push the result onto the stack.

Parameters
stateLua state
posIndex of the first stack value
funcA Callable
argsExtra arguments passed to func before the stack values
Returns
Number of return values pushed onto the stack

Works similar to apply. This function pushes the result of func onto the stack.

◆ push()

template<typename... Types>
void luwra::push ( State state,
Types &&...  values 
)
inline

Push zero or more values onto the stack.

Parameters
stateLua state
valuesValues to push

◆ pushReturn()

template<typename... Types>
size_t luwra::pushReturn ( State state,
Types &&...  values 
)
inline

Push zero or more return values onto the stack.

Parameters
stateLua state
valuesValues to push
Returns
Number of Lua values that have been pushed onto the stack

◆ read()

template<typename Type = internal::InferValueType>
auto luwra::read ( State state,
int  index 
) -> decltype(Value<Type>::read(state, index))
inline

Read a value off the stack.

Template Parameters
TypeType of targeted value
Parameters
stateLua state
indexPosition of the value on the stack

◆ registerUserType() [1/2]

template<typename Sig >
void luwra::registerUserType ( State state,
const char *  ctor_name,
const MemberMap props = MemberMap(),
const MemberMap meta = MemberMap() 
)
inline

Same as the other registerUserType but registers a constructor in the global namespace.

Template Parameters
SigA signature in the form of UserType(CtorArgs...) where UserType is the user type for which you would like to register the metatable and CtorArgs... the parameter types of the constructor.
Parameters
stateLua state
ctor_nameConstructor name
propsProperties
metaMeta methods

◆ registerUserType() [2/2]

template<typename UserType >
void luwra::registerUserType ( State state,
const MemberMap props = MemberMap(),
const MemberMap meta = MemberMap() 
)
inline

Register the metatable for a user type. This function allows you to register properties which are shared across all instances of the user type.

Template Parameters
UserTypeType for which the metatable will be registered
Parameters
stateLua state
propsProperties of the user type
metaMeta methods of the user type

By default, a garbage-collector hook and string representation function are added as meta methods. Both can be overwritten.

Example:

struct A {
int foo;
A(int x);
void bar();
A __add(const A& x);
};
// Register the meta table
registerUserType<A>(
state,
{
LUWRA_MEMBER(A, foo),
LUWRA_MEMBER(A, bar)
},
{
LUWRA_MEMBER(A, __add)
}
);
// Register the constructor in the global namespace
setGlobal(state, "A", LUWRA_WRAP_CONSTRUCTOR(A, int));

in Lua

local x = A(13)
-- Retrieve 'foo'
x:foo()
-- Update 'foo'
x:foo(37)
-- Call 'bar'
x:bar()
-- Use meta method '__add'
local y = x + A(-16)

◆ setFields() [1/3]

void luwra::setFields ( State state,
int  index,
const MemberMap fields 
)
inline

Apply key-value pairs to a table.

Parameters
stateLua state
indexTable index
fieldsTable fields

◆ setFields() [2/3]

template<typename Key , typename Type >
void luwra::setFields ( State state,
int  index,
Key &&  key,
Type &&  value 
)
inline

Set the field of a table.

Parameters
stateLua state
indexIndex of the table
keyKey
valueValue

◆ setFields() [3/3]

template<typename Key1 , typename Type1 , typename Key2 , typename Type2 , typename... Pairs>
void luwra::setFields ( State state,
int  index,
Key1 &&  key1,
Type1 &&  value1,
Key2 &&  key2,
Type2 &&  value2,
Pairs &&...  rest 
)
inline

Set multiple fields at once. Allows you to provide multiple key-value pairs.

Parameters
stateLua state
indexIndex of the table
key1First key
value1First value
key2Second key
value2Second value
restRest of the key-value pairs

◆ setGlobal()

template<typename Type >
void luwra::setGlobal ( State state,
const char *  name,
Type &&  value 
)
inline

Register a value in the global namespace.

Parameters
stateLua state
nameGlobal name
valueGlobal value

◆ setMetatable()

void luwra::setMetatable ( State state,
const char *  name 
)
inline

Set a registered metatable for the table on top of the stack.

Parameters
stateLua state
nameMetatable name