Pigweed
C/C++ API Reference
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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
24class CoroOrElseTask : public Task {
25 public:
29 : coro_(std::move(coro)), or_else_(std::move(or_else)) {}
30
34 void SetCoro(Coro<Status>&& coro) {
35 PW_ASSERT(!IsRegistered());
36 coro_ = std::move(coro);
37 }
38
42 void SetErrorHandler(pw::Function<void(Status)>&& or_else) {
43 PW_ASSERT(!IsRegistered());
44 or_else_ = std::move(or_else);
45 }
46
47 private:
48 Poll<> DoPend(Context& cx) final {
49 Poll<Status> result = coro_.Pend(cx);
50 if (result.IsPending()) {
51 return Pending();
52 }
53 if (!result->ok()) {
54 or_else_(*result);
55 }
56 return Ready();
57 }
58
59 Coro<Status> coro_;
60 pw::Function<void(Status)> or_else_;
61};
62
63} // namespace pw::async2
Definition: status.h:85
Definition: context.h:49
Definition: coro.h:463
Definition: coro_or_else_task.h:24
void SetErrorHandler(pw::Function< void(Status)> &&or_else)
Definition: coro_or_else_task.h:42
CoroOrElseTask(Coro< Status > &&coro, pw::Function< void(Status)> &&or_else)
Definition: coro_or_else_task.h:28
Poll DoPend(Context &cx) final
Definition: coro_or_else_task.h:48
void SetCoro(Coro< Status > &&coro)
Definition: coro_or_else_task.h:34
Definition: poll.h:54
constexpr bool IsPending() const noexcept
Returns whether or not this value is Pending.
Definition: poll.h:128
Definition: task.h:61
bool IsRegistered() const
fit::function_impl< function_internal::config::kInlineCallableSize, !function_internal::config::kEnableDynamicAllocation, FunctionType, PW_FUNCTION_DEFAULT_ALLOCATOR_TYPE > Function
Definition: function.h:74