Flow
Documentation for the Flow C++ Library
Loading...
Searching...
No Matches
flow_thread_task.h
Go to the documentation of this file.
1#pragma once
2#include <future>
3#include <memory>
4
5namespace flow {
6
10 class ThreadTask final {
12 virtual ~ThreadTaskInterface() = default;
13 virtual void executeImp() = 0;
14 };
15
16 template <typename Callable>
17 struct ThreadTaskImp final : public ThreadTaskInterface {
18 Callable task;
19
20 template<typename C>
21 ThreadTaskImp(C&& callable) : task(std::forward<C>(callable)) {}
22
23 virtual void executeImp() override {
24 task();
25 }
26 };
27
28 std::unique_ptr<ThreadTaskInterface> task_;
29
30 public:
31 template <typename FuncType>
32 explicit ThreadTask(std::packaged_task<FuncType> task)
33 : task_(std::make_unique<ThreadTaskImp<std::packaged_task<FuncType>>>(std::move(task))) {
34 }
35
36 ThreadTask(ThreadTask&&) = default;
37 ThreadTask(const ThreadTask&) = delete;
39 ThreadTask& operator=(const ThreadTask&) = delete;
40 ~ThreadTask() = default;
41
43 void execute() {
44 task_->executeImp();
45 }
46 };
47}
ThreadTask(ThreadTask &&)=default
ThreadTask(std::packaged_task< FuncType > task)
ThreadTask & operator=(const ThreadTask &)=delete
void execute()
Execute this task.
ThreadTask & operator=(ThreadTask &&)=default
~ThreadTask()=default
std::unique_ptr< ThreadTaskInterface > task_
ThreadTask(const ThreadTask &)=delete
virtual void executeImp() override