File: return_reference_to.rst

package info (click to toggle)
luabind 0.9.1%2Bdfsg-5
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 1,460 kB
  • sloc: cpp: 13,761; makefile: 120; sh: 24; ansic: 11
file content (79 lines) | stat: -rwxr-xr-x 1,655 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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
~~~~~~~~~~

.. parsed-literal::

    #include <luabind/return_reference_to_policy.hpp>

Synopsis
~~~~~~~~

.. parsed-literal::

    return_reference_to(index)

Parameters
~~~~~~~~~~

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

Example
~~~~~~~

.. parsed-literal::

    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).