Policies

Sometimes it is necessary to control how luabind passes arguments and return value, to do this we have policies. All policies use an index to associate them with an argument in the function signature. These indices are result and _N (where N >= 1). When dealing with member functions _1 refers to the this pointer.

adopt

Motivation

Used to transfer ownership across language boundaries.

Defined in

#include <luabind/adopt_policy.hpp>

Synopsis

adopt(index)

Parameters

Parameter Purpose
index The index which should transfer ownership, _N or result

Example

X* create()
{
    return new X;
}
module(L)
[
    def("create", &create, adopt(result))
];

dependency

Motivation

The dependency policy is used to create life-time dependencies between values. This is needed for example when returning internal references to some class.

Defined in

#include <luabind/dependency_policy.hpp>

Synopsis

dependency(nurse_index, patient_index)

Parameters

Parameter Purpose
nurse_index The index which will keep the patient alive.
patient_index The index which will be kept alive.

Example

struct X
{
    B member;
    B& get() { return member; }
};
module(L)
[
    class_<X>("X")
        .def("get", &X::get, dependency(result, _1))
];

out_value

Motivation

This policy makes it possible to wrap functions that take non-const references or pointer to non-const as it’s parameters with the intention to write return values to them. Since it’s impossible to pass references to primitive types in lua, this policy will add another return value with the value after the call. If the function already has one return value, one instance of this policy will add another return value (read about multiple return values in the lua manual).

Defined in

#include <luabind/out_value_policy.hpp>

Synopsis

out_value(index, policies = none)

Parameters

Parameter Purpose
index The index of the parameter to be used as an out parameter.
policies The policies used internally to convert the out parameter to/from Lua. _1 means to C++, _2 means from C++.

Example

void f1(float& val) { val = val + 10.f; }
void f2(float* val) { *val = *val + 10.f; }
module(L)
[
    def("f", &f, out_value(_1))
];
Lua 5.0  Copyright (C) 1994-2003 Tecgraf, PUC-Rio
> print(f1(10))
20
> print(f2(10))
20

pure_out_value

Motivation

This works exactly like out_value, except that it will pass a default constructed object instead of converting an argument from Lua. This means that the parameter will be removed from the lua signature.

Defined in

#include <luabind/out_value_policy.hpp>

Synopsis

pure_out_value(index, policies = none)

Parameters

Parameter Purpose
index The index of the parameter to be used as an out parameter.
policies The policies used internally to convert the out parameter to Lua. _1 is used as the internal index.

Example

Note that no values are passed to the calls to f1 and f2.

void f1(float& val) { val = 10.f; }
void f2(float\* val) { \*val = 10.f; }
module(L)
[
    def("f", &f, pure_out_value(_1))
];
Lua 5.0  Copyright (C) 1994-2003 Tecgraf, PUC-Rio
> print(f1())
10
> print(f2())
10

return_reference_to

Motivation

It is very common to return references to arguments or the this-pointer to allow for chaining in C++.

struct A
{
    float val;

    A& set(float v)
    {
        val = v;
        return *this;
    }
};

When luabind generates code for this, it will create a new object for the return-value, pointing to the self-object. This isn’t a problem, but could be a bit inefficient. When using the return_reference_to-policy we have the ability to tell luabind that the return-value is already on the lua stack.

Defined in

#include <luabind/return_reference_to_policy.hpp>

Synopsis

return_reference_to(index)

Parameters

Parameter Purpose
index The argument index to return a reference to, any argument but not result.

Example

struct A
{
    float val;

    A& set(float v)
    {
        val = v;
        return *this;
    }
};
module(L)
[
    class_<A>("A")
        .def(constructor<>())
        .def("set", &A::set, return_reference_to(_1))
];

Warning

This policy ignores all type information and should be used only it situations where the parameter type is a perfect match to the return-type (such as in the example).

copy

Motivation

This will make a copy of the parameter. This is the default behavior when passing parameters by-value. Note that this can only be used when passing from C++ to Lua. This policy requires that the parameter type has an accessible copy constructor.

Defined in

#include <luabind/copy_policy.hpp>

Synopsis

copy(index)

Parameters

Parameter Purpose
index The index to copy. result when used while wrapping C++ functions. _N when passing arguments to Lua.

Example

X* get()
{
    static X instance;
    return &instance;
}
module(L)
[
    def("create", &create, copy(result))
];

discard_result

Motivation

This is a very simple policy which makes it possible to throw away the value returned by a C++ function, instead of converting it to Lua.

Defined in

#include <luabind/discard_result_policy.hpp>

Synopsis

discard_result

Example

struct X
{
    X& set(T n)
    {
        ...
        return *this;
    }
};
module(L)
[
    class_<X>("X")
        .def("set", &simple::set, discard_result)
];

return_stl_iterator

Motivation

This policy converts an STL container to a generator function that can be used in lua to iterate over the container. It works on any container that defines begin() and end() member functions (they have to return iterators).

Defined in

#include <luabind/iterator_policy.hpp>

Synopsis

return_stl_iterator

Example

struct X
{
    std::vector<std::string> names;
};
module(L)
[
    class_<A>("A")
        .def_readwrite("names", &X::names, return_stl_iterator)
];
> a = A()
> for name in a.names do
>  print(name)
> end

raw

Note

raw() has been deprecated. lua_State* parameters are automatically handled by luabind.

Motivation

This converter policy will pass through the lua_State* unmodified. This can be useful for example when binding functions that need to return a luabind::object. The parameter will be removed from the function signature, decreasing the function arity by one.

Defined in

#include <luabind/raw_policy.hpp>

Synopsis

raw(index)

Parameters

Parameter Purpose
index The index of the lua_State* parameter.

Example

void greet(lua_State* L)
{
    lua_pushstring(L, "hello");
}
module(L)
[
    def("greet", &greet, raw(_1))
];
> print(greet())
hello

yield

Motivation

Makes a C++ function yield when returning.

Defined in

#include <luabind/yield_policy.hpp>

Synopsis

yield

Example

void do_thing_that_takes_time()
{
    // ...
}
module(L)
[
    def("do_thing_that_takes_time", &do_thing_that_takes_time, yield)
];