UserData class reference
[Base module]
Declaration
#include <QtLua/UserData>
namespace QtLua {
class UserData;
};
This class is a member of the QtLua namespace.
Description
This class is the base class for C++ objects which may be exposed to lua script as a lua userdata value.
All lua meta operations on userdata values are mapped to virtual functions in this class which may be reimplemented in derived classes. Lua errors can be raised from these functions by throwing a String type exception. See Error handling and exceptions.
Objects derived from this class are subject to lua garbage collection and must be handled by the Ref smart pointer class in C++ code.
UserData base class declaration example:
// code from examples/cpp/userdata/ref.cc:26
class MyObject : public QtLua::UserData
{
public:
QTLUA_REFTYPE(MyObject);
MyObject(int a)
: a_(a) {}
private:
int a_;
};
UserData objects allocation examples:
// code from examples/cpp/userdata/ref.cc:48
QtLua::UserData::ptr ud = QTLUA_REFNEW(QtLua::UserData, );
MyObject::ptr my = QTLUA_REFNEW(MyObject, 42);
Members
Types
Functions
- virtual ~UserData()
- virtual String get_type_name() const
- virtual String get_value_str() const
- virtual Value::List meta_call(State &ls, const Value::List &args)
- 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 Value meta_operation(State &ls, Value::Operation op, const Value &a, const Value &b)
- virtual Ref<Iterator> new_iterator(State &ls)
- virtual bool operator<(const UserData &ud)
- Refobj<UserData> & operator=(const Refobj<UserData> &r)
- virtual bool operator==(const UserData &ud)
- virtual bool support(Value::Operation c) const
Protected functions
- virtual void completion_patch(String &path, String &entry, int &offset)
- int ref_count() const
- virtual void ref_drop(int count)
Static function
- static template String type_name()
Protected static function
- static void meta_call_check_args(const Value::List &args, int min_count, int max_count, ...)
Members detail
No documentation available
This member access is protected.
This function may be reimplemented to further modify completion result on console line when completed to a UserData value. This is usefull to append a dot or a pair of brackets access operator to the userdata value name for instance.
Parameters list:
- path: Completion result tables path to userdata value.
- entry: Completion result userdata name. May append to this string directly.
- offset: Cursor offset. May be decreased to place cursor between inserted brackets for instance.
Shortcut for Ref smart pointer class to UserData type provided for convenience
virtual String get_type_name() const
This function returns an object type name. The default implementation returns the C++ object type name. This is used for error messages and pretty printing.
The return value is Pretty print object type.
virtual String get_value_str() const
This function returns an string value describing object value or content. The default implementation returns an hexadecimal object pointer. This is used for mainly for pretty printing.
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 Value::OpCall as supported.
Parameters list:
- args: List of passed arguments.
The return value is List of returned values.
static void meta_call_check_args(const Value::List &args, int min_count, int max_count, ...)
This member access is protected.
This helper function can be used to check arguments types passed to the UserData::meta_call functions. This function throw an error message if checking fails.
More advanced arguments checking and conversion features are available in the Function base class which may be more appropriate when a userdata object is to be used as a function.
Parameters list:
- args: list of passed arguments.
- min_count: Minimum expected arguments count.
- max_count: Maximum expected arguments count or 0 if no limit.
- ...: List of Value::ValueType matching expected arguments type. At least max(min_count, max_count) types must be passed. Value::TNone may be used as wildcard. Last specified type is expected for all arguments above min_count when max_count == 0.
This function returns true if either the Value::OpIndex operation or the Value::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 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 Value::OpIndex as supported.
Parameters list:
- key: Value used as table index.
The return value is Table access result value.
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 Value::OpNewindex as supported.
Parameters list:
- key: Value used as table index.
- value: Value to put in table.
This function is called when a lua operator is used with a UserData object. The default implementation throws an error message. The UserData::support function must be reimplemented along with this function.
Parameters list:
- op: Specify invoked lua operator (see Value::Operation).
- a: First value involved in operation.
- b: Second value involved in operation for binary operators.
The return value is Operation result value.
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 Value::OpIterate as supported.
The return value is an Iterator based iterator object.
virtual bool operator<(const UserData &ud)
Userdata compare less than, default implementation compares the this pointers
virtual bool operator==(const UserData &ud)
Userdata compare for equality, default implementation compares the this pointers
Shortcut for Ref smart pointer class to UserData type provided for convenience
virtual bool support(Value::Operation c) const
Check given operation support.
See also Value::support function.
static template <typename X> String type_name()
Get a bare C++ typename from type