AsyncFw 1.2
Async Framework is c++ runtime with timers, poll notifiers, sockets, coroutines, etc.
 
Loading...
Searching...
No Matches
Task.h
1/*
2Copyright (c) 2026 Alexandr Kuzmuk
3
4This file is part of the AsyncFw project. Licensed under the MIT License.
5See {Link: LICENSE file https://mit-license.org} in the project root for full license information.
6*/
7
8#pragma once
9
10#include <memory>
11#include "../core/AbstractThread.h"
12#include "../core/AnyData.h"
13
14namespace AsyncFw {
16class AbstractTask : public AsyncFw::AbstractThread::AbstractTask, public AnyData {
17public:
18 AbstractTask();
19 ~AbstractTask();
20 virtual bool running() = 0;
21};
22
24template <typename M>
25class Task : public AbstractTask {
26public:
27 Task(M &&method, AbstractThread *thread = nullptr) : method(std::move(method)), thread_(thread) {}
28 void operator()() override {
29 running_ = true;
30 if (!thread_) {
31 method(&data_);
32 running_ = false;
33 return;
34 }
35 thread_->invokeMethod([_m = std::move(method), _r = std::make_shared<std::atomic_bool>(&running_), _d = std::make_shared<std::any>(data_)]() {
36 _m(_d.get());
37 *_r = false;
38 });
39 }
40 bool running() override { return running_; }
41
42private:
43 M method;
44 AbstractThread *thread_;
45 std::atomic_bool running_;
46};
47} // namespace AsyncFw
The AbstractThread class provides the base functionality for thread management.
Definition AbstractThread.h:46
AbstractFunction<> AbstractTask
The AbstractTask type.
Definition AbstractThread.h:56