C/C++ API Reference
Loading...
Searching...
No Matches
coro_or_else_task.h
1// Copyright 2023 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14#pragma once
15
16#include "pw_async2/coro.h"
17#include "pw_async2/dispatcher.h"
18#include "pw_function/function.h"
19
20namespace pw::async2 {
21
23
26class CoroOrElseTask : public Task {
27 public:
31 : coro_(std::move(coro)), or_else_(std::move(or_else)) {}
32
36 void SetCoro(Coro<Status>&& coro) {
37 PW_ASSERT(!IsRegistered());
38 coro_ = std::move(coro);
39 }
40
44 void SetErrorHandler(pw::Function<void(Status)>&& or_else) {
45 PW_ASSERT(!IsRegistered());
46 or_else_ = std::move(or_else);
47 }
48
49 private:
50 Poll<> DoPend(Context& cx) final {
51 Poll<Status> result = coro_.Pend(cx);
52 if (result.IsPending()) {
53 return Pending();
54 }
55 if (!result->ok()) {
56 or_else_(*result);
57 }
58 return Ready();
59 }
60
61 Coro<Status> coro_;
62 pw::Function<void(Status)> or_else_;
63};
64
66
67} // namespace pw::async2
Definition: status.h:109
Definition: context.h:55
Definition: coro.h:469
Definition: coro_or_else_task.h:26
void SetErrorHandler(pw::Function< void(Status)> &&or_else)
Definition: coro_or_else_task.h:44
CoroOrElseTask(Coro< Status > &&coro, pw::Function< void(Status)> &&or_else)
Definition: coro_or_else_task.h:30
Poll DoPend(Context &cx) final
Definition: coro_or_else_task.h:50
void SetCoro(Coro< Status > &&coro)
Definition: coro_or_else_task.h:36
Definition: poll.h:60
Definition: task.h:63
bool IsRegistered() const
constexpr bool IsPending() const noexcept
Returns whether or not this value is Pending.
Definition: poll.h:136
constexpr PendingType Pending()
Returns a value indicating that an operation was not yet able to complete.
Definition: poll.h:271
constexpr Poll Ready()
Returns a value indicating completion.
Definition: poll.h:255
fit::function_impl< function_internal::config::kInlineCallableSize, !function_internal::config::kEnableDynamicAllocation, FunctionType, PW_FUNCTION_DEFAULT_ALLOCATOR_TYPE > Function
Definition: function.h:73