Blue Brain BioExplorer
Task.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-2024, EPFL/Blue Brain Project
3  *
4  * Responsible Author: Daniel.Nachbaur@epfl.ch
5  *
6  * This file is part of Blue Brain BioExplorer <https://github.com/BlueBrain/BioExplorer>
7  *
8  * This library is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU Lesser General Public License version 3.0 as published
10  * by the Free Software Foundation.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
15  * details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21 
22 #pragma once
23 
27 
28 namespace core
29 {
36 {
37 public:
38  virtual ~AbstractTask() = default;
39 
47  void cancel(std::function<void()> done = {})
48  {
49  if (_cancelled)
50  return;
51  _cancelDone = done;
52  _cancelled = true;
53  _cancelToken.cancel();
54  _cancel();
55  }
56 
61  void finishCancel()
62  {
63  if (_cancelDone)
64  _cancelDone();
65  }
66 
68  bool canceled() const { return _cancelled; }
73  virtual void schedule() = 0;
74 
76  Progress progress{"Scheduling task ..."};
77 
78 protected:
79  async::cancellation_token _cancelToken;
80  std::function<void()> _cancelDone;
81  std::atomic_bool _cancelled{false};
82 
83 private:
84  virtual void _cancel() {}
85 };
86 
94 template <typename T>
95 class Task : public AbstractTask
96 {
97 public:
98  using Type = async::task<T>;
99 
103  Task() = default;
104 
106  template <typename F>
107  Task(F&& functor)
108  {
109  _task = async::spawn(_setupFunctor(std::move(functor)));
110  }
111 
113  void schedule() override
114  { /* task is already running after construction */
115  }
116 
121  T result() { return _task.get(); }
123  auto& get() { return _task; }
124 
125 protected:
127 
128  template <typename F>
129  auto&& _setupFunctor(F&& functor)
130  {
131  if (std::is_base_of<TaskFunctor, F>::value)
132  {
133  auto& taskFunctor = static_cast<TaskFunctor&>(functor);
134  taskFunctor.setProgressFunc(
135  std::bind(&Progress::update, std::ref(progress), std::placeholders::_1, std::placeholders::_3));
136  taskFunctor.setCancelToken(_cancelToken);
137  }
138  return std::move(functor);
139  }
140 };
141 
146 template <typename T>
147 class DeferredTask : public Task<T>
148 {
149 public:
150  template <typename F>
151  DeferredTask(F&& functor)
152  {
153  Task<T>::_task = _e.get_task().then(Task<T>::template _setupFunctor(std::move(functor)));
154  }
155 
156  void schedule() final { _e.set(); }
157 
158 private:
159  async::event_task<void> _e;
160 };
161 } // namespace core
virtual void schedule()=0
bool canceled() const
Definition: Task.h:68
void finishCancel()
Definition: Task.h:61
std::atomic_bool _cancelled
Definition: Task.h:81
Progress progress
Definition: Task.h:76
std::function< void()> _cancelDone
Definition: Task.h:80
async::cancellation_token _cancelToken
Definition: Task.h:79
void cancel(std::function< void()> done={})
Definition: Task.h:47
virtual ~AbstractTask()=default
void schedule() final
Definition: Task.h:156
DeferredTask(F &&functor)
Definition: Task.h:151
void update(const std::string &operation, const float amount)
Definition: Progress.h:46
void setProgressFunc(const ProgressFunc &progressFunc)
Definition: TaskFunctor.h:48
T result()
Definition: Task.h:121
async::task< T > Type
Definition: Task.h:98
auto && _setupFunctor(F &&functor)
Definition: Task.h:129
Task(F &&functor)
Definition: Task.h:107
void schedule() override
Definition: Task.h:113
auto & get()
Definition: Task.h:123
Task()=default
Type _task
Definition: Task.h:126