summaryrefslogtreecommitdiffstatshomepage
path: root/3rdparty/sol2/docs/source/api/proxy.rst
blob: 897dbbaf7ddbe7ed1009f57c7084a009f69f94ad (plain) (blame)
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
proxy, (protected\_)function_result - proxy_base derivatives
============================================================
``table[x]`` and ``function(...)`` conversion struct
----------------------------------------------------

.. code-block:: c++

	template <typename Recurring>
	struct proxy_base;

	template <typename Table, typename Key>
	struct proxy : proxy_base<...>;

	struct stack_proxy: proxy_base<...>;

	struct function_result : proxy_base<...>;

	struct protected_function_result: proxy_base<...>;


These classes provide implicit assignment operator ``operator=`` (for ``set``) and an implicit conversion operator ``operator T`` (for ``get``) to support items retrieved from the underlying Lua implementation, specifically :doc:`sol::table<table>` and the results of function calls on :doc:`sol::function<function>` and :doc:`sol::protected_function<protected_function>`.

.. _proxy:

proxy
-----

``proxy`` is returned by lookups into :doc:`sol::table<table>` and table-like entities. Because it is templated on key and table type, it would be hard to spell: you can capture it using the word ``auto`` if you feel like you need to carry it around for some reason before using it. ``proxy`` evaluates its arguments lazily, when you finally call ``get`` or ``set`` on it. Here are some examples given the following lua script. 

.. code-block:: lua
	:linenos:
	:caption: lua nested table script

	bark = { 
		woof = {
			[2] = "arf!" 
		} 
	}


After loading that file in or putting it in a string and reading the string directly in lua (see :doc:`state`), you can start kicking around with it in C++ like so:

.. code-block:: c++
	:linenos:

	sol::state lua;

	// produces proxy, implicitly converts to std::string, quietly destroys proxy
	std::string x = lua["bark"]["woof"][2];


``proxy`` lazy evaluation:

.. code-block:: c++
	:linenos:
	:caption: multi-get

	auto x = lua["bark"];
	auto y = x["woof"];
	auto z = x[2];
	// retrivies value inside of lua table above
	std::string value = z; // "arf!"
	// Can change the value later...
	z = 20;
	// Yay, lazy-evaluation!
	int changed_value = z; // now it's 20!


We don't recommend the above to be used across classes or between function: it's more of something you can do to save a reference to a value you like, call a script or run a lua function, and then get it afterwards. You can also set functions (and function objects :ref:`*<note 1>`) this way, and retrieve them as well.

.. code-block:: c++
	:linenos:

	lua["bark_value"] = 24;
	lua["chase_tail"] = floof::chase_tail; // chase_tail is a free function


members
-------

.. code-block:: c++
	:caption: functions: [overloaded] implicit conversion get
	:name: implicit-get

	requires( sol::is_primitive_type<T>::value == true )
	template <typename T>
	operator T() const;
	
	requires( sol::is_primitive_type<T>::value == false )
	template <typename T>
	operator T&() const;

Gets the value associated with the keys the proxy was generated and convers it to the type ``T``. Note that this function will always return ``T&``, a non-const reference, to types which are not based on :doc:`sol::reference<reference>` and not a :doc:`primitive lua type<types>`

.. code-block:: c++
	:caption: function: get a value
	:name: regular-get

	template <typename T>
	decltype(auto) get( ) const;

Gets the value associated with the keys and converts it to the type ``T``.

.. code-block:: c++
	:caption: function: optionally get a value
	:name: regular-get-or

	template <typename T, typename Otherwise>
	optional<T> get_or( Otherwise&& otherise ) const;

Gets the value associated with the keys and converts it to the type ``T``. If it is not of the proper type, it will return a ``sol::nullopt`` instead.

``operator[]`` proxy-only members
---------------------------------

.. code-block:: c++
	:caption: function: valid
	:name: proxy-valid

	bool valid () const;

Returns whether this proxy actually refers to a valid object. It uses :ref:`sol::stack::probe_get_field<stack-probe-get-field>` to determine whether or not its valid.

.. code-block:: c++
	:caption: functions: [overloaded] implicit set
	:name: implicit-set

	requires( sol::detail::Function<Fx> == false )
	template <typename T>
	proxy& operator=( T&& value );
	
	requires( sol::detail::Function<Fx> == true )
	template <typename Fx>
	proxy& operator=( Fx&& function );

Sets the value associated with the keys the proxy was generated with to ``value``. If this is a function, calls ``set_function``. If it is not, just calls ``set``. Does not exist on :ref:`function_result<function-result>` or :ref:`protected_function_result<protected-function-result>`. See :ref:`note<note 1>` for caveats.

.. code-block:: c++
	:caption: function: set a callable
	:name: regular-set-function

	template <typename Fx>
	proxy& set_function( Fx&& fx );

Sets the value associated with the keys the proxy was generated with to a function ``fx``. Does not exist on :ref:`function_result<function-result>` or :ref:`protected_function_result<protected-function-result>`.


.. code-block:: c++
	:caption: function: set a value
	:name: regular-set

	template <typename T>
	proxy& set( T&& value );

Sets the value associated with the keys the proxy was generated with to ``value``. Does not exist on :ref:`function_result<function-result>` or :ref:`protected_function_result<protected-function-result>`.

stack_proxy
-----------

``sol::stack_proxy`` is what gets returned by :doc:`sol::variadic_args<variadic_args>` and other parts of the framework. It is similar to proxy, but is meant to alias a stack index and not a named variable.

.. _function-result:

function_result
---------------

``function_result`` is a temporary-only, intermediate-only implicit conversion worker for when :doc:`function<function>` is called. It is *NOT* meant to be stored or captured with ``auto``. It provides fast access to the desired underlying value. It does not implement ``set`` / ``set_function`` / templated ``operator=``, as is present on :ref:`proxy<proxy>`.


.. _protected-function-result:

protected_function_result
-------------------------

``protected_function_result`` is a nicer version of ``function_result`` that can be used to detect errors. Its gives safe access to the desired underlying value. It does not implement ``set`` / ``set_function`` / templated ``operator=`` as is present on :ref:`proxy<proxy>`.


.. _note 1:

on function objects and proxies
-------------------------------

Consider the following:

.. code-block:: cpp
	:linenos:
	:caption: Note 1 Case

	struct doge {
		int bark;

		void operator()() {
			bark += 1;
		}
	};

	sol::state lua;
	lua["object"] = doge{}; // bind constructed doge to "object"
	// but it binds as a function

When you use the ``lua["object"] = doge{};`` from above, keep in mind that Sol detects if this is a function *callable with any kind of arguments*. Since ``doge`` has overriden ``return_type operator()( argument_types... )`` on itself, it results in satisfying the ``requires`` constraint from above. This means that if you have a user-defined type you want to bind as a :doc:`userdata with usertype semantics<usertype>` with this syntax, it might get bound as a function and not as a user-defined type (d'oh!). use ``lua["object"].set(doge)`` directly to avoid this, or ``lua["object"].set_function(doge{})`` to perform this explicitly.