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
- 24 members inherited from UserData
Types
Functions
- UserObject()
- UserObject(T *obj)
- virtual bool meta_contains(State *ls, const Value &key)
- virtual Value meta_index(State *ls, const Value &key)
- virtual void meta_newindex(State *ls, const Value &key, const Value &value)
- virtual Ref<Iterator> new_iterator(State *ls)
- virtual bool support(ValueBase::Operation c) const
Macros
- QTLUA_PROPERTIES_TABLE
- QTLUA_PROPERTY
- QTLUA_PROPERTY_ACCESSORS
- QTLUA_PROPERTY_ACCESSORS_F
- QTLUA_PROPERTY_ACCESSOR_F_GET
- QTLUA_PROPERTY_ACCESSOR_F_SET
- QTLUA_PROPERTY_ACCESSOR_GET
- QTLUA_PROPERTY_ACCESSOR_SET
- QTLUA_PROPERTY_ENTRY
- QTLUA_PROPERTY_ENTRY_GET
- QTLUA_PROPERTY_ENTRY_SET
- QTLUA_PROPERTY_ENTRY_U
- QTLUA_PROPERTY_ENTRY_U_GET
- QTLUA_PROPERTY_ENTRY_U_SET
- QTLUA_PROPERTY_GET
- QTLUA_PROPERTY_SET
- QTLUA_USEROBJECT
Members detail
This constructor must be used when the class which contains properties inherit from UserObject.
This constructor must be used when the class which contains properties does not inherit from UserObject. Pointer to properties object must be provided.
This macro must be used once at global scope to list available properties and specify allowed access.
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 simple inline accessors function for the specified member
This macro expands to:
QTLUA_PROPERTY_ACCESSOR_GET(member)
QTLUA_PROPERTY_ACCESSOR_SET(member)
Define lua accessors functions for the specified member which rely on regular C++ accessors.
This macro expands to:
QTLUA_PROPERTY_ACCESSOR_F_GET(member)
QTLUA_PROPERTY_ACCESSOR_F_SET(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 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 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 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;
}
Property table entry with get and set accessors.
Property table entry with get accessor only.
Property table entry with set accessor only.
Property table entry with user defined lua accessor functions.
Property table entry with user defined lua get accessor function only.
Property table entry with user defined lua set accessor function only.
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.
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.
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
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.
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.
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.
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.
typedef Ref<UserObject, UserObject> ptr
Shortcut for Ref smart pointer class to UserObject type provided for convenience