| 1 | /*++ |
| 2 | |
| 3 | Copyright (c) Microsoft. All rights reserved. |
| 4 | |
| 5 | Module Name: |
| 6 | |
| 7 | Task.h |
| 8 | |
| 9 | Abstract: |
| 10 | |
| 11 | Declaration of a task for function composition and chaining. |
| 12 | |
| 13 | --*/ |
| 14 | #pragma once |
| 15 | #include "CLIExecutionContext.h" |
| 16 | #include <functional> |
| 17 | |
| 18 | using namespace wsl::windows::wslc::execution; |
| 19 | |
| 20 | namespace wsl::windows::wslc::task { |
| 21 | |
| 22 | struct Task |
| 23 | { |
| 24 | using Func = std::function<void(CLIExecutionContext&)>; |
| 25 | |
| 26 | Task(void (*f)(CLIExecutionContext&)) : m_func(f) |
| 27 | { |
| 28 | } |
| 29 | |
| 30 | Task(Func f) : m_func(std::move(f)) |
| 31 | { |
| 32 | } |
| 33 | |
| 34 | Task() = default; |
| 35 | virtual ~Task() = default; |
| 36 | |
| 37 | Task(const Task&) = default; |
| 38 | Task& operator=(const Task&) = default; |
| 39 | virtual void operator()(CLIExecutionContext& context) const |
| 40 | { |
| 41 | m_func(context); |
| 42 | } |
| 43 | |
| 44 | private: |
| 45 | Func m_func = nullptr; |
| 46 | }; |
| 47 | |
| 48 | inline CLIExecutionContext& operator<<(CLIExecutionContext& context, const Task& task) |
| 49 | { |
| 50 | return task(context), context; |
| 51 | } |
| 52 | |
| 53 | inline CLIExecutionContext& operator<<(CLIExecutionContext& context, void (*f)(CLIExecutionContext&)) |
| 54 | { |
| 55 | return context << Task(f); |
| 56 | } |
| 57 | |
| 58 | } // namespace wsl::windows::wslc::task |