.. index:: pair: class; EE::Task .. _doxid-class_e_e_1_1_task: template class EE::Task ======================= .. toctree:: :hidden: Overview ~~~~~~~~ The result of an operation that completes later. :ref:`More...` .. ref-code-block:: cpp :class: doxyrest-overview-code-block #include template class Task { public: // typedefs typedef T :target:`ValueType`; // construction :ref:`Task`(); :target:`Task`(Task&&); :target:`Task`(const Task&); // methods Task& :target:`operator=`(Task&&); Task& :target:`operator=`(const Task&); bool :target:`valid`() const; :ref:`TaskState` :target:`state`() const; bool :target:`isReady`() const; bool :target:`isPending`() const; :ref:`TaskState` :ref:`wait`() const; template :ref:`TaskState` :ref:`waitFor`(const std::chrono::duration& timeout) const; T :ref:`takeValue`(); auto :ref:`get`(); template auto :ref:`then`(:ref:`Executor`& executor, F&& fn); template auto :ref:`then`(F&& fn); template void :ref:`onComplete`(F&& fn); static Task :ref:`create`(:ref:`TaskResolver`& resolve); template static Task :ref:`ready`(Args&&... args); }; .. _details-class_e_e_1_1_task: Detailed Documentation ~~~~~~~~~~~~~~~~~~~~~~ The result of an operation that completes later. The consumer's end. The producer completes it through the :ref:`TaskResolver ` that :ref:`create() ` fills in. :ref:`Task ` is move-only and carries no domain error semantics: fallible operations return :ref:`Task ` >. Attaching a continuation consumes the task (``std::move( task ).then( ... )``), so only one continuation chain can ever exist. * then( executor, fn ): runs fn through the executor (for example the :ref:`UI ` thread's), even if the task has already completed. This is the normal way to use a result. * then( fn ): runs fn on whichever thread completes the task, or right away on the calling thread if it already has. For cheap transformations that touch no thread-bound state. * fn may return a value, void, or a :ref:`Task `; nested tasks are flattened into :ref:`Task `. * If the task is abandoned, fn is not called and the returned task is abandoned too. * onComplete( fn ) also observes abandonment: fn receives std::nullopt. Construction ------------ .. index:: pair: function; Task .. _doxid-class_e_e_1_1_task_1a4e4d2d17e8e6b12f3f85e6073230e99d: .. ref-code-block:: cpp :class: doxyrest-title-code-block Task() Creates an invalid task. Its :ref:`state() ` is Abandoned. Methods ------- .. index:: pair: function; wait .. _doxid-class_e_e_1_1_task_1acf8b201f4268b6e83e5db3cbf6bca89d: .. ref-code-block:: cpp :class: doxyrest-title-code-block :ref:`TaskState` wait() const Blocks until the task is Ready or Abandoned. This is a low-level bridge for worker-thread adapters and tests. It must never be called on the :ref:`UI ` thread, nor on the thread that is expected to resolve the task. .. index:: pair: function; waitFor .. _doxid-class_e_e_1_1_task_1a5d46eace4576d3ac725492155b3b4e7b: .. ref-code-block:: cpp :class: doxyrest-title-code-block template :ref:`TaskState` waitFor(const std::chrono::duration& timeout) const Blocks until the task completes or the timeout expires. .. rubric:: Returns: The state on return. .. index:: pair: function; takeValue .. _doxid-class_e_e_1_1_task_1aba131eb7b0057279a5848a7b337b6230: .. ref-code-block:: cpp :class: doxyrest-title-code-block T takeValue() Extracts the value of a Ready task and consumes it. Precondition: :ref:`isReady() `. .. index:: pair: function; get .. _doxid-class_e_e_1_1_task_1a0ed4b345da7902b60215b36f22ae01a0: .. ref-code-block:: cpp :class: doxyrest-title-code-block auto get() Blocks until completion and returns the value, or std::nullopt if abandoned. Same threading restrictions as :ref:`wait() `. .. index:: pair: function; then .. _doxid-class_e_e_1_1_task_1afd1872c589904c3ec68dba689a6eb4a6: .. ref-code-block:: cpp :class: doxyrest-title-code-block template auto then(:ref:`Executor`& executor, F&& fn) Runs fn( value ) through executor once the task completes, even if it already has. The executor must outlive the task chain. See the class documentation. .. index:: pair: function; then .. _doxid-class_e_e_1_1_task_1ae3fdfd59ad07733c367f4f5dd342d609: .. ref-code-block:: cpp :class: doxyrest-title-code-block template auto then(F&& fn) Runs fn( value ) on the thread that completes the task, or right away on the calling thread if it already has. See the class documentation. .. index:: pair: function; onComplete .. _doxid-class_e_e_1_1_task_1a5c1be4dd1c0a788ff6b5b0482cf24afa: .. ref-code-block:: cpp :class: doxyrest-title-code-block template void onComplete(F&& fn) Observes the outcome without producing a new task, on the thread that completes the task (or right away on the calling thread if it already has). fn( std::optional value ) receives the value, or std::nullopt if the task was abandoned; for Task, fn( bool completed ) receives false if it was abandoned. .. index:: pair: function; create .. _doxid-class_e_e_1_1_task_1aca423d9dddbdf5a8ac467eba9a244939: .. ref-code-block:: cpp :class: doxyrest-title-code-block static Task create(:ref:`TaskResolver`& resolve) .. ref-code-block:: cpp TaskResolver resolve; auto task = Task::create( resolve ); pool.execute( [resolve] { resolve( 42 ); } ); return task; .. rubric:: Returns: A pending task. resolve is set to the resolver that completes it (a resolver it held before is released). .. index:: pair: function; ready .. _doxid-class_e_e_1_1_task_1a840027a76560f1fe55a62085714d79a9: .. ref-code-block:: cpp :class: doxyrest-title-code-block template static Task ready(Args&&... args) .. rubric:: Returns: A task that is already Ready with the given value.