dfx 0.1.0
Linux-based dynamic dataflow executor
Loading...
Searching...
No Matches
PackagedTask.hpp
1// SPDX-FileCopyrightText: 2025-2026 Vincent Leroy
2// SPDX-License-Identifier: MIT
3//
4// This file is part of dfx.
5//
6// Licensed under the MIT License. See the LICENSE file in the project root
7// for full license information.
8
9#pragma once
10
11// Standard includes
12#include <concepts>
13#include <future>
14
15// Project includes
16#include <dfx-runtime-api/Task.hpp>
17
18namespace dfx::Runtime
19{
29template<typename R>
30class PackagedTask : public Api::Task
31{
32public:
41 template<typename Func>
42 requires (!std::same_as<PackagedTask, std::remove_cvref_t<Func>>)
43 explicit PackagedTask(Func && func, int priority = 0)
44 : Task(priority)
45 , _task { std::forward<Func>(func) }
46 {}
47
52 std::future<R> getFuture()
53 { return _task.get_future(); }
54
59 void run() override
60 { _task(); }
61
62private:
63 std::packaged_task<R()> _task;
64};
65
76template<typename Func, typename ... Args>
77auto makePackagedTask(Func && func, Args && ... args)
78{
79 using ReturnType = std::invoke_result_t<Func>;
80 return std::make_unique<PackagedTask<ReturnType>>(std::forward<Func>(func), std::forward<Args>(args)...);
81}
82} // !namespace dfx::Runtime
Polymorphic unit of work executed by the runtime (typically by ThreadPool).
Definition Task.hpp:48
Task(int priority=0)
Construct a task with a given scheduling priority.
Definition Task.hpp:55
int priority() const noexcept
Task scheduling priority.
Definition Task.hpp:67
PackagedTask(Func &&func, int priority=0)
Constructs a new PackagedTask object.
Definition PackagedTask.hpp:43
void run() override
Executes the wrapped task.
Definition PackagedTask.hpp:59
std::future< R > getFuture()
Gets a future associated with the task's result.
Definition PackagedTask.hpp:52
Definition Node.hpp:48
auto makePackagedTask(Func &&func, Args &&... args)
Helper factory function to create a PackagedTask wrapped in a std::unique_ptr.
Definition PackagedTask.hpp:77