Blue Brain BioExplorer
Progress.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 
25 
26 #include <functional>
27 #include <mutex>
28 #include <string>
29 
30 namespace core
31 {
36 class Progress : public BaseObject
37 {
38 public:
39  Progress() = default;
40  explicit Progress(const std::string& operation)
41  : _operation(operation)
42  {
43  }
44 
46  void update(const std::string& operation, const float amount)
47  {
48  std::lock_guard<std::mutex> lock_(_mutex);
49  _updateValue(_operation, operation);
50  _updateValue(_amount, amount);
51  }
52 
54  void increment(const std::string& operation, const float increment)
55  {
56  std::lock_guard<std::mutex> lock_(_mutex);
57  _updateValue(_operation, operation);
58  _updateValue(_amount, _amount + increment);
59  }
60 
65  void consume(std::function<void(std::string, float)> callback)
66  {
67  std::lock_guard<std::mutex> lock_(_mutex);
68  if (isModified())
69  {
70  callback(_operation, _amount);
71  resetModified();
72  }
73  }
74 
75 private:
76  std::string _operation;
77  float _amount{0.f};
78  std::mutex _mutex;
79 };
80 } // namespace core
void _updateValue(T &member, const T &newValue, const bool triggerCallback=true)
Definition: BaseObject.h:87
void resetModified()
Definition: BaseObject.h:64
bool isModified() const
Definition: BaseObject.h:60
void update(const std::string &operation, const float amount)
Definition: Progress.h:46
void increment(const std::string &operation, const float increment)
Definition: Progress.h:54
Progress()=default
Progress(const std::string &operation)
Definition: Progress.h:40
void consume(std::function< void(std::string, float)> callback)
Definition: Progress.h:65