dfx 0.1.0
Linux-based dynamic dataflow executor
Loading...
Searching...
No Matches
Task.hpp
1// SPDX-FileCopyrightText: 2025 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 <memory>
14
15// Project includes
17
18namespace dfx::Runtime
19{
20class Task;
21
23template<typename T>
24concept DerivedFromTask = std::derived_from<T, Task>;
25
47class Task
48{
49public:
55 Task(int priority = 0)
56 : _priority { priority }
57 {}
58
61
63 virtual ~Task() = default;
64
67 int priority() const noexcept { return _priority; }
68
74 virtual void run() = 0;
75
83 template<DerivedFromTask T>
84 T & as() { return static_cast<T &>(*this); }
85
93 template<DerivedFromTask T>
94 T const & as() const { return static_cast<T const &>(*this); }
95
96private:
97 // High value == high priority == work done sooner
98 int _priority;
99};
100
102using TaskPtr = std::unique_ptr<Task>;
103} // !namespace dfx::Runtime
Convenience macros to explicitly control copy and move semantics.
Polymorphic unit of work executed by the runtime (typically by ThreadPool).
Definition Task.hpp:48
virtual ~Task()=default
Polymorphic base requires a virtual destructor.
int priority() const noexcept
Task scheduling priority.
Definition Task.hpp:67
T & as()
Unchecked downcast to a concrete task type.
Definition Task.hpp:84
T const & as() const
Unchecked downcast to a concrete task type (const overload).
Definition Task.hpp:94
DISABLE_COPY_AND_MOVE(Task)
Tasks are neither copyable nor movable.
Task(int priority=0)
Construct a task with a given scheduling priority.
Definition Task.hpp:55
virtual void run()=0
Execute the task.
Convenience concept for constraining templates to Task-derived types.
Definition Task.hpp:24
Definition Node.hpp:47
std::unique_ptr< Task > TaskPtr
Unique ownership pointer for tasks.
Definition Task.hpp:102