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 20 namespace pw::async2 { 21 22 /// A ``Task`` that delegates to a provided ``Coro<Status>>`` and executes 23 /// an ``or_else`` handler function on failure. 24 class CoroOrElseTask : public Task { 25 public: 26 /// Create a new ``Task`` which runs ``coro``, invoking ``or_else`` on 27 /// any non-OK status. CoroOrElseTask(Coro<Status> && coro,pw::Function<void (Status)> && or_else)28 CoroOrElseTask(Coro<Status>&& coro, pw::Function<void(Status)>&& or_else) 29 : coro_(std::move(coro)), or_else_(std::move(or_else)) {} 30 31 /// *Non-atomically* sets `coro`. 32 /// 33 /// The task must not be `Post`ed when `coro` is changed. SetCoro(Coro<Status> && coro)34 void SetCoro(Coro<Status>&& coro) { 35 PW_ASSERT(!IsRegistered()); 36 coro_ = std::move(coro); 37 } 38 39 /// *Non-atomically* sets `or_else`. 40 /// 41 /// The task must not be `Post`ed when `or_else` is changed. SetErrorHandler(pw::Function<void (Status)> && or_else)42 void SetErrorHandler(pw::Function<void(Status)>&& or_else) { 43 PW_ASSERT(!IsRegistered()); 44 or_else_ = std::move(or_else); 45 } 46 47 private: DoPend(Context & cx)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 64