Albert
Loading...
Searching...
No Matches
scopedfuture.h
1// SPDX-FileCopyrightText: 2026 Manuel Schneider
2
3#pragma once
4#include <QFuture>
5#include <albert/export.h>
6#include <optional>
7
8namespace albert::detail
9{
11template<typename T>
12class ALBERT_EXPORT ScopedFuture
13{
14public:
15 explicit ScopedFuture(QFuture<T> f) : future_opt(std::move(f)) {}
16 ~ScopedFuture() { waitForFinished(); }
17
18 void waitForFinished()
19 {
20 if (future_opt && !future_opt->isFinished())
21 future_opt->waitForFinished();
22 }
23
24 ScopedFuture(const ScopedFuture &) = delete;
25 ScopedFuture(ScopedFuture &&other) noexcept
26 : future_opt(std::move(other.future_opt))
27 { other.future_opt.reset(); };
28
29 ScopedFuture &operator=(const ScopedFuture &) = delete;
30 ScopedFuture &operator=(ScopedFuture &&other) noexcept
31 {
32 waitForFinished();
33 future_opt = std::move(other.future_opt);
34 other.future_opt.reset();
35 return *this;
36 }
37
38 auto&& operator*(this auto&& self) {
39 return *std::forward<decltype(self)>(self).future_opt;
40 }
41
42 auto* operator->(this auto&& self) {
43 return &*std::forward<decltype(self)>(self).future_opt;
44 }
45
46 auto&& get(this auto&& self) {
47 return *std::forward<decltype(self)>(self).future_opt;
48 }
49 void reset() { future_opt.reset(); }
50
51private:
52 std::optional<QFuture<T>> future_opt;
53};
54
55} // namespace albert