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_async/function_dispatcher.h" 17 18 namespace pw::async { 19 20 /// HeapDispatcher wraps an existing Dispatcher and allocates Task objects on 21 /// the heap before posting them to the existing Dispatcher. After Tasks run, 22 /// they are automatically freed. 23 class HeapDispatcher final : public FunctionDispatcher { 24 public: HeapDispatcher(Dispatcher & dispatcher)25 HeapDispatcher(Dispatcher& dispatcher) : dispatcher_(dispatcher) {} 26 ~HeapDispatcher() override = default; 27 28 // FunctionDispatcher overrides: 29 Status PostAt(TaskFunction&& task_func, 30 chrono::SystemClock::time_point time) override; 31 32 // Dispatcher overrides: PostAt(Task & task,chrono::SystemClock::time_point time)33 inline void PostAt(Task& task, 34 chrono::SystemClock::time_point time) override { 35 return dispatcher_.PostAt(task, time); 36 } Cancel(Task & task)37 inline bool Cancel(Task& task) override { return dispatcher_.Cancel(task); } 38 39 // VirtualSystemClock overrides: now()40 inline chrono::SystemClock::time_point now() override { 41 return dispatcher_.now(); 42 } 43 44 private: 45 Dispatcher& dispatcher_; 46 }; 47 48 } // namespace pw::async 49