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 <optional>
17
18#include "pw_async2/context.h"
19#include "pw_async2/coro.h"
20#include "pw_async2/fallible_coro_task.h"
21#include "pw_function/function.h"
22
23namespace pw::async2 {
24
26
28class [[deprecated("Use CoroTask or FallibleCoroTask instead")]]
29CoroOrElseTask final : public Task {
30 public:
34 : coro_task_(std::in_place,
35 std::move(coro),
36 [this] { or_else_(Status::Internal()); }),
37 or_else_(std::move(or_else)) {}
38
39 ~CoroOrElseTask() override { Deregister(); }
40
44 void SetCoro(Coro<Status>&& coro) {
45 PW_ASSERT(!IsRegistered());
46 coro_task_.emplace(std::move(coro),
47 [this] { or_else_(Status::Internal()); });
48 }
49
53 void SetErrorHandler(pw::Function<void(Status)>&& or_else) {
54 PW_ASSERT(!IsRegistered());
55 or_else_ = std::move(or_else);
56 }
57
58 private:
59 Poll<> DoPend(Context& cx) final { return coro_task_->Pend(cx); }
60
61 std::optional<FallibleCoroTask<Status>> coro_task_;
62 pw::Function<void(Status)> or_else_;
63};
64
66
67} // namespace pw::async2
Definition: status.h:120
Definition: context.h:46
Definition: coro.h:546
Definition: coro_or_else_task.h:29
void SetErrorHandler(pw::Function< void(Status)> &&or_else)
Definition: coro_or_else_task.h:53
CoroOrElseTask(Coro< Status > &&coro, pw::Function< void(Status)> &&or_else)
Definition: coro_or_else_task.h:33
Poll DoPend(Context &cx) final
Definition: coro_or_else_task.h:59
void SetCoro(Coro< Status > &&coro)
Definition: coro_or_else_task.h:44
Definition: poll.h:138
Definition: task.h:78
fit::function_impl< function_internal::config::kInlineCallableSize, !function_internal::config::kEnableDynamicAllocation, FunctionType, PW_FUNCTION_DEFAULT_ALLOCATOR_TYPE > Function
Definition: function.h:73