sol2/docs/source/api/coroutine.rst

93 lines
3.8 KiB
ReStructuredText
Raw Normal View History

2016-03-12 06:47:15 +08:00
coroutine
=========
*resumable/yielding functions from Lua*
2016-03-12 06:47:15 +08:00
A ``coroutine`` is a :doc:`reference<reference>` to a function in Lua that can be called multiple times to yield a specific result. It is a cooperative function. It is run on the :doc:`lua_State<state>` that was used to create it (see :doc:`thread<thread>` for an example on how to get a coroutine that runs on a stack space separate from your usual "main" stack space :doc:`lua_State<state>`).
2016-03-12 06:47:15 +08:00
The ``coroutine`` object is entirely similar to the :doc:`protected_function<protected_function>` object, with additional member functions to check if a coroutine has yielded (:doc:`call_status::yielded<types>`) and is thus runnable again, whether it has completed (:ref:`call_status::ok<call-status>`) and thus cannot yield anymore values, or whether it has suffered an error (see :ref:`status()<thread-status>`'s and :ref:`call_status<call-status>`'s error codes).
2016-03-12 06:47:15 +08:00
For example, you can work with a coroutine like this:
.. literalinclude:: ../../../examples/source/docs/coroutine_main.cpp
2016-03-12 06:47:15 +08:00
:caption: co.lua
2019-07-18 17:06:28 +08:00
:name: co-lua
:lines: 8-15
:linenos:
2016-03-12 06:47:15 +08:00
This is a function that yields. We set the ``counter`` value in C++, and then use the coroutine to get a few values:
2016-03-12 06:47:15 +08:00
.. literalinclude:: ../../../examples/source/docs/coroutine_main.cpp
:caption: coroutine_main.cpp
:name: coroutine_main
:lines: 1-6,18-19,21,25-
:linenos:
2016-03-12 06:47:15 +08:00
Note that this code doesn't check for errors: to do so, you can call the function and assign it as ``auto result = loop_coroutine();``, then check ``result.valid()`` as is the case with :doc:`protected_function<protected_function>`.
2016-03-12 06:47:15 +08:00
Finally, you can run this coroutine on another stack space (NOT a different computer thread: Lua uses the term 'thread' a bit strangely, as we follow its usage of the term, but it is NOT a separate thread) by doing the following:
2016-03-12 06:47:15 +08:00
.. literalinclude:: ../../../examples/source/docs/coroutine_thread.cpp
:caption: coroutine_thread.cpp
2019-07-18 17:06:28 +08:00
:name: yield-main-thread
:lines: 1-6,18-19,21,25-
:linenos:
2016-03-12 06:47:15 +08:00
The following are the members of ``sol::coroutine``:
members
-------
.. code-block:: cpp
:caption: function: constructor
2019-07-18 17:06:28 +08:00
:name: sol-coroutine-constructor
coroutine(lua_State* L, int index = -1);
Grabs the coroutine at the specified index given a ``lua_State*``.
2016-03-12 06:47:15 +08:00
.. code-block:: cpp
:caption: returning the coroutine's status
2019-07-18 17:06:28 +08:00
:name: sol-coroutine-status
2016-03-12 06:47:15 +08:00
call_status status() const noexcept;
2016-03-12 06:47:15 +08:00
Returns the status of a coroutine.
.. code-block:: cpp
:caption: checks for an error
2019-07-18 17:06:28 +08:00
:name: sol-coroutine-error
2016-03-12 06:47:15 +08:00
bool error() const noexcept;
2016-03-12 06:47:15 +08:00
Checks if an error occured when the coroutine was run.
.. _runnable:
.. code-block:: cpp
:caption: runnable and explicit operator bool
2019-07-18 17:06:28 +08:00
:name: sol-coroutine-runnable
2016-03-12 06:47:15 +08:00
bool runnable () const noexcept;
explicit operator bool() const noexcept;
2016-03-12 06:47:15 +08:00
These functions allow you to check if a coroutine can still be called (has more values to yield and has not errored). If you have a coroutine object ``coroutine my_co = /*...*/``, you can either check ``runnable()`` or do ``if ( my_co ) { /* use coroutine */ }``.
.. code-block:: cpp
:caption: calling a coroutine
2019-07-18 17:06:28 +08:00
:name: sol-coroutine-operator-call
2016-03-12 06:47:15 +08:00
template<typename... Args>
protected_function_result operator()( Args&&... args );
2016-03-12 06:47:15 +08:00
template<typename... Ret, typename... Args>
decltype(auto) call( Args&&... args );
2016-03-12 06:47:15 +08:00
template<typename... Ret, typename... Args>
decltype(auto) operator()( types<Ret...>, Args&&... args );
2016-03-12 06:47:15 +08:00
Calls the coroutine. The second ``operator()`` lets you specify the templated return types using the ``my_co(sol::types<int, std::string>, ...)`` syntax. Check ``status()`` afterwards for more information about the success of the run or just check the coroutine object in an ifs tatement, as shown :ref:`above<runnable>`.