Function class reference
[Base module]
Declaration
#include <QtLua/Function>
namespace QtLua {
class Function;
};
This class is a member of the QtLua namespace.
This abstract class contains pure virtuals.
Description
This class is a convenient base class for exposing functions like objects to lua scripts. It's based on the UserData class and is handled by lua as an userdata value with redefined call operation.
Basic argument checking can be done using the UserData::meta_call_check_args function. More argument checking and conversion can be performed with the Function::get_arg family of functions. See Qt/Lua types conversion for supported types and conversion operations.
The QTLUA_FUNCTION macro is provided to easily declare a Function sub class:
// code from examples/cpp/userdata/function.cc:25
QTLUA_FUNCTION(foo, "This function returns \"test\"",
"usage: foo()")
{
return QtLua::Value(ls, "test");
}
Function objects can be exposed as a lua values:
// code from examples/cpp/userdata/function.cc:59
QtLua::State state;
static QtLua_Function_foo foo;
QtLua::Value f(&state, foo);
A convenience constructor is provided to register functions as global lua variables:
// code from examples/cpp/userdata/function.cc:47
QtLua::State state;
QTLUA_FUNCTION_REGISTER(&state, "bar.", foo);
Functions can also be registered on a Plugin objects.
Members
Inherited members
- 24 members inherited from UserData
Types
Functions
- virtual String get_description() const
- virtual String get_help() const
Protected function
- virtual ValueBase::List meta_call(State *ls, const ValueBase::List &args) = 0;
Static functions
- static template X get_arg(const ValueBase::List &args, int n, const X &default_)
- static template X get_arg(const ValueBase::List &args, int n)
- static template X * get_arg_cl(const ValueBase::List &args, int n)
- static template X * get_arg_qobject(const ValueBase::List &args, int n)
- static template Ref<X> get_arg_ud(const ValueBase::List &args, int n)
Macros
Members detail
This macro declares a new a Function class named QtLua_Function_name with functions to handle description, help and function call. User provided code is used for reimplementation of the UserData::meta_call function.
// code from examples/cpp/userdata/function.cc:25
QTLUA_FUNCTION(foo, "This function returns \"test\"",
"usage: foo()")
{
return QtLua::Value(ls, "test");
}
This macro expands to:
QTLUA_FUNCTION_DECL(name)
QTLUA_FUNCTION_BODY(name, description, help)
This macro contains functions definition for QTLUA_FUNCTION.
This macro expands to:
QtLua::String QtLua_Function_##name
::get_description() const { return description; }
QtLua::String QtLua_Function_##name
::get_help() const { return help; }
QtLua_Function_##name
::QtLua_Function_##name() { }
QtLua_Function_##name
::QtLua_Function_##name(QtLua::State *ls, const QtLua::String &path)
{ register_(ls, path); }
QtLua::Value::List QtLua_Function_##name
::meta_call(QtLua::State *ls, const QtLua::Value::List &args)
This macro contains class declaration for QTLUA_FUNCTION.
This macro expands to:
class QtLua_Function_##name : public QtLua::Function
{
QtLua::Value::List meta_call(QtLua::State *ls, const QtLua::Value::List &args);
QtLua::String get_description() const;
QtLua::String get_help() const;
public:
QtLua_Function_##name();
QtLua_Function_##name(QtLua::State *ls, const QtLua::String &path);
};
This macro declares and registers a Function object on a QtLua State object as a global variable.
This macro expands to:
static QtLua_Function_##name name(state, prefix #name)
This macro declares and registers a Function object on a QtLua State object as a global variable.
This macro expands to:
static QtLua_Function_##name name(state, path)
Shortcut for Ref smart pointer class to Function type provided for convenience
static template <typename X> X get_arg(const ValueBase::List &args, int n, const X &default_)
This function may be called from the Function::meta_call function to perform lua to C++ argument conversion and checking.
It checks if the argument is available and tries to convert argument to X type and throw if conversion fails. A default value is returned if no argument exists at specified index.
Parameters list:
- args: arguments list
- n: argument index in list
- default_: default value to return if no argument available
The return value is C++ converted value
Example:
// code from examples/cpp/userdata/function.cc:25
QTLUA_FUNCTION(foo, "This function returns \"test\"",
"usage: foo()")
{
QtLua::String a = get_arg<QtLua::String>(args, 0);
int b = get_arg<int>(args, 1, 42);
See also Qt/Lua types conversion section and Function::get_arg function.
static template <typename X> X get_arg(const ValueBase::List &args, int n)
This function does the same as the Function::get_arg function but throws if the argument is not available instead of returning a default value.
See also Function::get_arg function and Function::get_arg_ud function.
static template <typename X> X * get_arg_cl(const ValueBase::List &args, int n)
This function may be called from the Function::meta_call function to perform lua to C++ argument conversion and checking.
It checks if the argument is available and if it is an UserData object and tries to dynamic_cast it to the specified class. This function throws an exception if the result is null.
Parameters list:
- args: arguments list
- n: argument index in list
The return value is pointer to X type.
See also Qt/Lua types conversion section and Function::get_arg function.
static template <typename X> X * get_arg_qobject(const ValueBase::List &args, int n)
This function may be called from the Function::meta_call function to perform lua to C++ argument conversion and checking.
It checks if the argument is available and if it is a QObject wrapper and tries to cast to the requested QObject based class using the ValueBase::to_qobject_cast function.
Parameters list:
- args: arguments list
- n: argument index in list
The return value is pointer to X type.
See also Qt/Lua types conversion section and Function::get_arg function.
This function may be called from the Function::meta_call function to perform lua to C++ argument conversion and checking.
It checks if the argument is available and if it is an UserData object and tries to cast it using the ValueBase::to_userdata_cast function.
Parameters list:
- args: arguments list
- n: argument index in list
The return value is Ref pointer to X type.
See also Qt/Lua types conversion section and Function::get_arg function.
virtual String get_description() const
This function may be reimplemented to return a short description of the function.
virtual String get_help() const
This function may be reimplemented to return a function usage help string.
This member access is protected.
This pure virtual function shadows the meta_call
virtual function defined in the UserData
base class.
Documentation inherited from base class:
This function is called when a function invokation operation is performed on a userdata object. The default implementation throws an error message. The UserData::support function must be reimplemented along with this function to report ValueBase::OpCall as supported.
Parameters list:
- args: List of passed arguments.
The return value is List of returned values.
Shortcut for Ref smart pointer class to Function type provided for convenience