Blue Brain BioExplorer
TaskFunctor.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 
24 #ifdef __GNUC__
25 #pragma GCC diagnostic push
26 #pragma GCC diagnostic ignored "-Wshadow"
27 #endif
28 #include <async++.h>
29 #ifdef __GNUC__
30 #pragma GCC diagnostic pop
31 #endif
32 
33 #include <string>
34 
35 namespace core
36 {
42 {
43 public:
45  using ProgressFunc = std::function<void(std::string, float, float)>;
46 
48  void setProgressFunc(const ProgressFunc& progressFunc) { _progressFunc = progressFunc; }
49 
58  void progress(const std::string& message, const float increment, const float amount)
59  {
60  cancelCheck();
61  if (_progressFunc)
62  _progressFunc(message, increment, amount);
63  }
64 
66  void setCancelToken(async::cancellation_token& cancelToken) { _cancelToken = &cancelToken; }
67 
73  void cancelCheck() const
74  {
75  if (_cancelToken)
76  async::interruption_point(*_cancelToken);
77  }
78 
79 private:
80  async::cancellation_token* _cancelToken{nullptr};
81  ProgressFunc _progressFunc;
82 };
83 } // namespace core
void progress(const std::string &message, const float increment, const float amount)
Definition: TaskFunctor.h:58
void setCancelToken(async::cancellation_token &cancelToken)
Definition: TaskFunctor.h:66
void cancelCheck() const
Definition: TaskFunctor.h:73
void setProgressFunc(const ProgressFunc &progressFunc)
Definition: TaskFunctor.h:48
std::function< void(std::string, float, float)> ProgressFunc
Definition: TaskFunctor.h:45