Function class reference
[Functions export 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 function. See Qt/Lua types conversion for supported types and conversion operations.

This class allows definition of function description and help strings which are useful to the Console user.

Defining a new function object is done by inheriting from this class and implementing the Function::meta_call function:

// code from examples/cpp/userdata/function.cc:29

static class : public QtLua::Function
{
QtLua::Value::List meta_call(QtLua::State &ls, const QtLua::Value::List &args)
{
return QtLua::Value(ls, "foo");
}
} foo_function;

Function objects can be exposed as a lua values or registered on a Plugin object. The Function::register_ and Function::register_ functions offer convenient ways to register a Function object in a lua table or on a Plugin object.

Inheritance  

Members  

Inherited members  

Types  

Functions  

Protected function  

  • virtual Value::List meta_call(State &ls, const Value::List &args) = 0;

Static functions  

  • static template X get_arg(const Value::List &args, int n, const X &default_)
  • static template X get_arg(const Value::List &args, int n)
  • static template Ref<X> get_arg_ud(const Value::List &args, int n)

Members detail  

typedef Ref<const Function, Function> const_ptr  

Shortcut for Ref smart pointer class to Function type provided for convenience

static template <typename X> X get_arg(const Value::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:32

QtLua::Value::List meta_call(QtLua::State &ls, const QtLua::Value::List &args)
{
QtLua::String a = get_arg<QtLua::String>(args, 0);
int b = get_arg<int>(args, 1, 42);

return QtLua::Value(ls, "foo");
}

See also Qt/Lua types conversion section and Function::get_arg function.

static template <typename X> X get_arg(const Value::List &args, int n)  

This function does the same as the Function::get_arg function but throws if 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> Ref<X> get_arg_ud(const Value::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 cast it using the Value::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.

virtual Value::List meta_call(State &ls, const Value::List &args) = 0;  

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 should be reimplemented along with this function.

Parameters list:

  • args: List of passed arguments.

The return value is List of returned values.

typedef Ref<Function, Function> ptr  

Shortcut for Ref smart pointer class to Function type provided for convenience

void register_(State &ls, const String &path)  

This function is provided for convenience and may be used to register the QtLua::Functions object in lua global table or in package subtables. All intermediate tables in path will be created as needed.

Parameters list:

  • ls: QtLua state where function must be registered.
  • path: table path to function value.

Example:

// code from examples/cpp/userdata/function.cc:55

QtLua::State state;

foo_function.register_(state, "bar.foo");

void register_(Plugin &plugin, const String &name)  

This function registers the Function object on a Plugin object and must be called from the PluginInterface::register_members function.

Parameters list:

  • plugin: reference to the Plugin object which must be populated
  • name: name of the plugin member

Valid XHTML 1.0 StrictGenerated by diaxen on Mon Aug 15 03:23:06 2011 using MkDoc