Skip to content

Commit 95ef8d7

Browse files
author
Vladimir Vilimaitis
committed
Avoid std::expected for Linux CI
1 parent 127d104 commit 95ef8d7

2 files changed

Lines changed: 59 additions & 6 deletions

File tree

src_cpp/src/BackgroundRemovalEngine.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ Expected<std::unique_ptr<BackgroundRemovalEngine>> BackgroundRemovalEngine::crea
7272
.arg(QString::fromUtf8(error.what())));
7373
}
7474

75-
return engine;
75+
return std::move(engine);
7676
}
7777

7878
Expected<RemovalResult> BackgroundRemovalEngine::removeBackground(const QImage& image) {

src_cpp/src/Expected.h

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,73 @@
11
#pragma once
22

3-
#include <expected>
3+
#include <memory>
4+
#include <type_traits>
45
#include <utility>
6+
#include <variant>
57

68
#include <QString>
79

810
namespace rembg::native_app {
911

12+
class Failure {
13+
public:
14+
explicit Failure(QString message)
15+
: message_(std::move(message)) {}
16+
17+
private:
18+
template <class T>
19+
friend class Expected;
20+
21+
QString message_;
22+
};
23+
1024
template <class T>
11-
using Expected = std::expected<T, QString>;
25+
class Expected {
26+
public:
27+
template <class U>
28+
requires(
29+
!std::is_same_v<std::remove_cvref_t<U>, Expected>
30+
&& !std::is_same_v<std::remove_cvref_t<U>, Failure>
31+
&& std::is_constructible_v<T, U&&>
32+
)
33+
Expected(U&& value)
34+
: storage_(std::in_place_index<0>, std::forward<U>(value)) {}
35+
36+
Expected(Failure failure)
37+
: storage_(std::in_place_index<1>, std::move(failure.message_)) {}
38+
39+
Expected(const Expected&) = default;
40+
Expected(Expected&&) = default;
41+
Expected& operator=(const Expected&) = default;
42+
Expected& operator=(Expected&&) = default;
43+
~Expected() = default;
44+
45+
[[nodiscard]] bool has_value() const noexcept { return storage_.index() == 0; }
46+
[[nodiscard]] explicit operator bool() const noexcept { return has_value(); }
47+
48+
[[nodiscard]] T& value() & { return std::get<0>(storage_); }
49+
[[nodiscard]] const T& value() const& { return std::get<0>(storage_); }
50+
[[nodiscard]] T&& value() && { return std::move(std::get<0>(storage_)); }
51+
52+
[[nodiscard]] T& operator*() & { return value(); }
53+
[[nodiscard]] const T& operator*() const& { return value(); }
54+
[[nodiscard]] T&& operator*() && { return std::move(*this).value(); }
55+
56+
[[nodiscard]] T* operator->() { return std::addressof(value()); }
57+
[[nodiscard]] const T* operator->() const { return std::addressof(value()); }
58+
59+
[[nodiscard]] const QString& error() const& { return std::get<1>(storage_); }
60+
[[nodiscard]] QString&& error() && { return std::move(std::get<1>(storage_)); }
61+
62+
private:
63+
std::variant<T, QString> storage_;
64+
};
1265

13-
inline std::unexpected<QString> failure(QString message) {
14-
return std::unexpected<QString>(std::move(message));
66+
inline Failure failure(QString message) {
67+
return Failure(std::move(message));
1568
}
1669

17-
inline std::unexpected<QString> failure(const char* message) {
70+
inline Failure failure(const char* message) {
1871
return failure(QString::fromUtf8(message));
1972
}
2073

0 commit comments

Comments
 (0)