Skip to content

Commit 2b42c34

Browse files
committed
Add move constructors and assignment operators
Implements move constructors and assignment operators for the ProgressBar class. Also moves existing constructor implementation in cpp file.
1 parent dda9fd2 commit 2b42c34

File tree

2 files changed

+76
-28
lines changed

2 files changed

+76
-28
lines changed

cpp/core/progressbar.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
#include "progressbar.h"
1818
#include "app.h"
19+
#include <thread>
20+
#include <utility>
1921

2022
// MSYS2 supports VT100, and file redirection is handled explicitly so this can be used globally
2123
#define CLEAR_LINE_CODE "\033[0K"
@@ -168,4 +170,65 @@ bool ProgressBar::set_update_method() {
168170
return stderr_to_file;
169171
}
170172

173+
ProgressBar::ProgressBar(const std::string &text, size_t target, int log_level)
174+
: first_time(true),
175+
last_value(0),
176+
show(std::this_thread::get_id() == ::MR::App::main_thread_ID && !progressbar_active &&
177+
App::log_level >= log_level),
178+
_text(text),
179+
_ellipsis("..."),
180+
_value(0),
181+
current_val(0),
182+
next_percent(0),
183+
next_time(0.0),
184+
_multiplier(0.0),
185+
_text_has_been_modified(false) {
186+
if (show) {
187+
set_max(target);
188+
progressbar_active = true;
189+
}
190+
}
191+
192+
ProgressBar::ProgressBar(ProgressBar &&other) noexcept
193+
: show(other.show),
194+
_text(std::move(other._text)),
195+
_ellipsis(std::move(other._ellipsis)),
196+
_value(other._value),
197+
current_val(other.current_val),
198+
next_percent(other.next_percent),
199+
next_time(other.next_time),
200+
_multiplier(other._multiplier),
201+
timer(other.timer),
202+
_text_has_been_modified(other._text_has_been_modified),
203+
first_time(other.first_time),
204+
last_value(other.last_value) {
205+
other.show = false;
206+
}
207+
208+
ProgressBar &MR::ProgressBar::operator=(ProgressBar &&other) noexcept {
209+
if (this == &other) {
210+
return *this;
211+
}
212+
213+
// If the current object is managing an active progress bar, finish it.
214+
if (show) {
215+
done();
216+
}
217+
218+
_text = std::move(other._text);
219+
_ellipsis = std::move(other._ellipsis);
220+
show = other.show;
221+
_value = other._value;
222+
current_val = other.current_val;
223+
next_percent = other.next_percent;
224+
next_time = other.next_time;
225+
_multiplier = other._multiplier;
226+
timer = other.timer;
227+
_text_has_been_modified = other._text_has_been_modified;
228+
first_time = other.first_time;
229+
last_value = other.last_value;
230+
other.show = false;
231+
return *this;
232+
}
233+
171234
} // namespace MR

cpp/core/progressbar.h

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@
1818

1919
#include <chrono>
2020
#include <condition_variable>
21+
#include <cstddef>
2122
#include <memory>
2223
#include <mutex>
2324
#include <string>
2425
#include <thread>
26+
#include <utility>
2527

2628
#include "app.h"
2729
#include "debug.h"
@@ -55,9 +57,11 @@ namespace MR {
5557
class ProgressBar {
5658
public:
5759
//! Create an unusable ProgressBar.
58-
ProgressBar() : show(false) {}
60+
explicit ProgressBar() = default;
5961
ProgressBar(const ProgressBar &p) = delete;
60-
ProgressBar(ProgressBar &&p) = default;
62+
ProgressBar &operator=(const ProgressBar &p) = delete;
63+
ProgressBar(ProgressBar &&other) noexcept;
64+
ProgressBar &operator=(ProgressBar &&other) noexcept;
6165

6266
FORCE_INLINE ~ProgressBar() { done(); }
6367

@@ -149,42 +153,23 @@ class ProgressBar {
149153
;
150154
static void *data;
151155

152-
mutable bool first_time;
153-
mutable size_t last_value;
156+
mutable bool first_time = false;
157+
mutable size_t last_value = 0;
154158

155159
private:
156-
const bool show;
160+
bool show = false;
157161
std::string _text, _ellipsis;
158-
size_t _value, current_val, next_percent;
159-
double next_time;
160-
float _multiplier;
162+
size_t _value = 0, current_val = 0, next_percent = 0;
163+
double next_time = 0.0;
164+
float _multiplier = 0.0F;
161165
Timer timer;
162-
bool _text_has_been_modified;
166+
bool _text_has_been_modified = false;
163167

164168
FORCE_INLINE void display_now() { display_func(*this); }
165169

166170
static bool progressbar_active;
167171
};
168172

169-
FORCE_INLINE ProgressBar::ProgressBar(const std::string &text, size_t target, int log_level)
170-
: first_time(true),
171-
last_value(0),
172-
show(std::this_thread::get_id() == ::MR::App::main_thread_ID && !progressbar_active &&
173-
App::log_level >= log_level),
174-
_text(text),
175-
_ellipsis("..."),
176-
_value(0),
177-
current_val(0),
178-
next_percent(0),
179-
next_time(0.0),
180-
_multiplier(0.0),
181-
_text_has_been_modified(false) {
182-
if (show) {
183-
set_max(target);
184-
progressbar_active = true;
185-
}
186-
}
187-
188173
inline void ProgressBar::set_max(size_t target) {
189174
if (!show)
190175
return;

0 commit comments

Comments
 (0)