UserObject class reference
[Base module]

Declaration  

#include <QtLua/UserObject>

namespace QtLua {
template <typename T> class UserObject;
};

This class is a member of the QtLua namespace.

Description  

This base class can be used to create C++ objects with named properties accessible from lua script. This is a lightweight alternative to writting a QObject based class when only set/get properties mechanism is needed. The class doesn't need to be a QObject and doesn't require moc pre-processing.

Each property must be described in a table and can have an associated lua_set* and/or lua_get* accessor function. Lua accessors functions must be able to convert between a Value object and property field type. Property members and lua accessor functions can be user defined or declared using the QTLUA_PROPERTY family of macros as shown in the example below:

// code from examples/cpp/userdata/userobject.cc:24

class Test : public QtLua::UserObject<Test>
{
QTLUA_USEROBJECT(Test);

QTLUA_PROPERTY(int, _value);

public:
Test(int value)
: _value(value)
{
}

};

QTLUA_PROPERTIES_TABLE(Test,
QTLUA_PROPERTY_ENTRY(Test, "value", _value)
);

In the next example our class already needs to inherit from an other UserData based class for some reasons. We declare the UserObject class as a member and forward table accesses to it. This example also shows how to write lua accessors by hand:

// code from examples/cpp/userdata/userobject2.cc:24

class Test : public QtLua::UserData
{
QTLUA_USEROBJECT(Test);

QtLua::UserObject<Test> _uo;
int _value;

QtLua::Value meta_index(QtLua::State *ls, const QtLua::Value &key)
{
return _uo.meta_index(ls, key);
}

bool meta_contains(QtLua::State *ls, const QtLua::Value &key)
{
return _uo.meta_contains(ls, key);
}

void meta_newindex(QtLua::State *ls, const QtLua::Value &key, const QtLua::Value &value)
{
return _uo.meta_newindex(ls, key, value);
}

QtLua::Ref<QtLua::Iterator> new_iterator(QtLua::State *ls)
{
return _uo.new_iterator(ls);
}

bool support(QtLua::Value::Operation c) const
{
return _uo.support(c) || QtLua::UserData::support(c);
}

QtLua::Value lua_get_value(QtLua::State *ls)
{
return QtLua::Value(ls, _value);
}

void lua_set_value(QtLua::State *ls, const QtLua::Value &value)
{
_value = value;
}

public:
Test(int value)
: _uo(this), // pass pointer to the object which holds properties
_value(value)
{
_uo.ref_delegate(this);
}

};

QTLUA_PROPERTIES_TABLE(Test,
QTLUA_PROPERTY_ENTRY_U(Test, "value", lua_get_value, lua_set_value)
);

Members  

Inherited members  

Types  

Functions  

Macros  

Members detail  

UserObject()  

This constructor must be used when the class which contains properties inherit from UserObject.

UserObject(T *obj)  

This constructor must be used when the class which contains properties does not inherit from UserObject. Pointer to properties object must be provided.

#define QTLUA_PROPERTIES_TABLE(class_name, ...)  

This macro must be used once at global scope to list available properties and specify allowed access.

#define QTLUA_PROPERTY(type, member)  

Declare a member of given type and define lua accessor functions for the specified member. This is a convenience macro, member and accessor functions can be defined directly.

This macro expands to:

type member;
QTLUA_PROPERTY_ACCESSORS(member);

#define QTLUA_PROPERTY_ACCESSORS(member)  

Define simple inline accessors function for the specified member

This macro expands to:

#define QTLUA_PROPERTY_ACCESSORS_F(member)  

Define lua accessors functions for the specified member which rely on regular C++ accessors.

This macro expands to:

#define QTLUA_PROPERTY_ACCESSOR_F_GET(member)  

Define a lua get accessor function for the specified member which relies on a regular C++ get accessor functions.

This macro expands to:

inline QtLua::Value lua_get_##member(QtLua::State *ls)
{
return QtLua::Value(ls, get_##member());
}

#define QTLUA_PROPERTY_ACCESSOR_F_SET(member)  

Define a lua set accessor function for the specified member which relies on a regular C++ set accessor functions.

This macro expands to:

inline void lua_set_##member(QtLua::State *ls, const QtLua::Value &value)
{
set_##member(value);
}

#define QTLUA_PROPERTY_ACCESSOR_GET(member)  

Define a lua get accessor function for the specified member

This macro expands to:

inline QtLua::Value lua_get_##member(QtLua::State *ls)
{
return QtLua::Value(ls, member);
}

#define QTLUA_PROPERTY_ACCESSOR_SET(member)  

Define a lua set accessor function for the specified member

This macro expands to:

inline void lua_set_##member(QtLua::State *ls, const QtLua::Value &value)
{
member = value;
}

#define QTLUA_PROPERTY_ENTRY(class_name, name, member)  

Property table entry with get and set accessors.

#define QTLUA_PROPERTY_ENTRY_GET(class_name, name, member)  

Property table entry with get accessor only.

#define QTLUA_PROPERTY_ENTRY_SET(class_name, name, member)  

Property table entry with set accessor only.

#define QTLUA_PROPERTY_ENTRY_U(class_name, name, get, set)  

Property table entry with user defined lua accessor functions.

#define QTLUA_PROPERTY_ENTRY_U_GET(class_name, name, get)  

Property table entry with user defined lua get accessor function only.

#define QTLUA_PROPERTY_ENTRY_U_SET(class_name, name, set)  

Property table entry with user defined lua set accessor function only.

#define QTLUA_PROPERTY_GET(type, member)  

Declare a member of given type and define a lua get accessor function for the specified member.

This macro expands to:

type member;
QTLUA_PROPERTY_ACCESSOR_GET(member);

See also QTLUA_PROPERTY macro.

#define QTLUA_PROPERTY_SET(type, member)  

Declare a member of given type and define a lua set accessor function for the specified member.

This macro expands to:

type member;
QTLUA_PROPERTY_ACCESSOR_SET(member);

See also QTLUA_PROPERTY macro.

#define QTLUA_USEROBJECT(class_name)  

This macro must appears once in class body which holds property declarations.

typedef Ref<const UserObject, UserObject> const_ptr  

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

virtual bool meta_contains(State *ls, const Value &key)  

This virtual function overrides the meta_contains virtual function defined in the UserData base class.

Documentation inherited from base class:

This function returns true if either the ValueBase::OpIndex operation or the ValueBase::OpNewindex operation is supported and an entry is associated to the given key.

The default implementation returns !meta_index(ls, key).is_nil() or false if UserData::meta_index throws.

virtual Value meta_index(State *ls, const Value &key)  

This virtual function overrides the meta_index virtual function defined in the UserData base class.

Documentation inherited from base class:

This function is called when a table read access operation is attempted 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::OpIndex as supported.

Parameters list:

  • key: Value used as table index.

The return value is Table access result value.

virtual void meta_newindex(State *ls, const Value &key, const Value &value)  

This virtual function overrides the meta_newindex virtual function defined in the UserData base class.

Documentation inherited from base class:

This function is called when a table write access operation is attempted 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::OpNewindex as supported.

Parameters list:

  • key: Value used as table index.
  • value: Value to put in table.

virtual Ref<Iterator> new_iterator(State *ls)  

This virtual function overrides the new_iterator virtual function defined in the UserData base class.

Documentation inherited from base class:

This function may return an Iterator object used to iterate over an userdata object. The default implementation throws an error message. The UserData::support function must be reimplemented along with this function to report ValueBase::OpIterate as supported.

The return value is an Iterator based iterator object.

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

virtual bool support(ValueBase::Operation c) const  

This virtual function overrides the support virtual function defined in the UserData base class.

Documentation inherited from base class:

Check given operation support.

See also ValueBase::support function.

Valid XHTML 1.0 StrictGenerated by diaxen on Sat Mar 30 15:29:55 2013 using MkDoc