lubyk logo

Lubyk documentation

C++ Function definition.

(internal) A public C++ function or method definition.

.new (def)

Create a new function object with def settings. Some important fields in def:

name Function name.
db Storage engine (dub.MemoryStorage).
parent Parent or storage engine if the function is defined in root.
header Path to C++ header file where this function is declared.
params_list List of dub.Param definitions for the function arguments.
return_value Return type definition (see dub.MemoryStorage.makeType).
definition Full function name as defined (used in comment).
argsstring Arguments as a single string (used in comment).
location A String representing the function source file and line.
desc String description (will be used in a C++ comment in bindings).
static True if the function is static (static class function or C function).
xml An xml object representing the function definition.
member True if the function is a member function (static or not).
dtor True if the function is the destructor.
ctor True if the function is a constructor.
dub "dub" options as parsed from the "dub" C++ comment.
pure_virtual True if the function is a pure virtual.

Operator overloading

Read this table as 'operator[KEY]' translates into lua function '__[VALUE]'. For example operator+ becomes __add. This works for most methods except for some operators which do not have a lua equivalent (+=, -=, etc).

.OP_TO_NAME = {

['+'] = 'add'

local v = foo + bar

['-'] = 'sub'

local v = foo - bar

['- '] = 'unm'

local bar = -foo

['*'] = 'mul'

local v = foo * bar

['/'] = 'div'

local v = foo / bar

['=='] = 'eq'

if (foo == bar) ...

['<'] = 'lt'

if (foo < bar) ...

['<='] = 'le'

if (foo <= bar) ...

['()'] = 'call'

This is the call on the object itself. Example:

local foo = Foo()
foo() --> call

['[]'] = 'index'

Table access. When binding this method, integer access is very fast but we must also hand-code metatable access (to get methods) and attribute access.

It is thus better to avoid this operator for it has a non-negligible cost.

local x = foo[4]

['='] = 'sete'

This is not supported in lua due to the fact that all variables are references to objects and the equal sign is used to assign reference. We must therefore use 'set':

local foo, bar = Simple('I am foo'), Simple('I am bar')
-- foo = bar would make 'foo' a pointer to bar, not assign
-- values of bar into foo. We must use:
foo:set(bar)
--> C++ foo.operator=(bar)

['+='] = 'adde'

Mutable operator. No equivalent in Lua.

-- Add 50 into foo, mutating foo.
foo:adde(50)

['-='] = 'sube'

Mutable operator. No equivalent in Lua.

-- Remove 50 from foo, mutating foo.
foo:sube(50)

['*='] = 'mule'

Mutable operator. No equivalent in Lua.

-- Scale foo by 50, mutating foo.
foo:mule(50)

['/='] = 'dive'

Mutable operator. No equivalent in Lua.

-- Divide foo by 50, mutating foo.
foo:dive(50)

}

Accessors

:fullname ()

Return the full name of the function (with enclosing namespaces separated by '::'. Example: foo::Bar::drawLine.

:fullcname ()

Full C name for function. This is like fullname but with the C names in case binding names are different.

:nameWithArgs ()

String representation of the function name with arguments (used in comments). Example: drawLine(int x, int y, int x2, int y2, const foo.Pen &pen)

:neverThrows ()

Returns true if the function does not throw any C++ exception.

:setName (name)

Set function name and C name.

Iterators

:params ()

Return an iterator over the parameters of this function.