mini-llvm 0.1.0
Loading...
Searching...
No Matches
Expected.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2
3#pragma once
4
5#include <memory>
6#include <optional>
7#include <type_traits>
8#include <utility>
9#include <variant>
10
11namespace mini_llvm {
12
13template <typename E>
15public:
16 using error_type = E;
17
18 constexpr Unexpected(E error) : error_(std::move(error)) {}
19
20 template <typename F>
21 requires std::is_convertible_v<F, E>
22 constexpr Unexpected(F error) : error_(std::move(error)) {}
23
24 template <typename F>
25 requires std::is_convertible_v<F, E>
26 constexpr Unexpected(Unexpected<F> error) : error_(*std::move(error)) {}
27
28 constexpr E &operator*() & noexcept {
29 return error_;
30 }
31
32 constexpr const E &operator*() const & noexcept {
33 return error_;
34 }
35
36 constexpr E &&operator*() && noexcept {
37 return std::move(error_);
38 }
39
40 constexpr E *operator->() noexcept {
41 return std::addressof(error_);
42 }
43
44 constexpr const E *operator->() const noexcept {
45 return std::addressof(error_);
46 }
47
48 constexpr E &error() & noexcept {
49 return error_;
50 }
51
52 constexpr const E &error() const & noexcept {
53 return error_;
54 }
55
56 constexpr E &&error() && noexcept {
57 return std::move(error_);
58 }
59
60private:
61 E error_;
62};
63
64template <typename T, typename E>
65class [[nodiscard]] Expected {
66public:
67 using value_type = T;
68 using error_type = E;
69
70 constexpr Expected() = default;
71
72 constexpr Expected(T value) : valueOrError_(std::move(value)) {}
73
74 template <typename U>
75 requires std::is_convertible_v<U, T>
76 constexpr Expected(U value) : valueOrError_(static_cast<T>(std::move(value))) {}
77
78 constexpr Expected(Unexpected<E> error) : valueOrError_(std::move(error)) {}
79
80 template <typename F>
81 requires std::is_convertible_v<F, E>
82 constexpr Expected(Unexpected<F> error) : valueOrError_(static_cast<Unexpected<E>>(std::move(error))) {}
83
84 explicit constexpr operator bool() const noexcept {
85 return std::holds_alternative<T>(valueOrError_);
86 }
87
88 constexpr T &operator*() & noexcept {
89 return std::get<T>(valueOrError_);
90 }
91
92 constexpr const T &operator*() const & noexcept {
93 return std::get<T>(valueOrError_);
94 }
95
96 constexpr T &&operator*() && noexcept {
97 return std::get<T>(std::move(valueOrError_));
98 }
99
100 constexpr T *operator->() noexcept {
101 return std::addressof(std::get<T>(valueOrError_));
102 }
103
104 constexpr const T *operator->() const noexcept {
105 return std::addressof(std::get<T>(valueOrError_));
106 }
107
108 constexpr T &value() & noexcept {
109 return std::get<T>(valueOrError_);
110 }
111
112 constexpr const T &value() const & noexcept {
113 return std::get<T>(valueOrError_);
114 }
115
116 constexpr T &&value() && noexcept {
117 return std::get<T>(std::move(valueOrError_));
118 }
119
120 constexpr E &error() & noexcept {
121 return *std::get<Unexpected<E>>(valueOrError_);
122 }
123
124 constexpr const E &error() const & noexcept {
125 return *std::get<Unexpected<E>>(valueOrError_);
126 }
127
128 constexpr E &&error() && noexcept {
129 return *std::get<Unexpected<E>>(std::move(valueOrError_));
130 }
131
132private:
133 std::variant<T, Unexpected<E>> valueOrError_;
134};
135
136template <typename E>
137class [[nodiscard]] Expected<void, E> {
138public:
139 constexpr Expected() : error_(std::nullopt) {}
140
141 constexpr Expected(Unexpected<E> error) : error_(std::move(error)) {}
142
143 template <typename F>
144 requires std::is_convertible_v<F, E>
145 constexpr Expected(Unexpected<F> error) : error_(static_cast<Unexpected<E>>(std::move(error))) {}
146
147 explicit constexpr operator bool() const {
148 return !error_;
149 }
150
151 constexpr E &error() & noexcept {
152 return **error_;
153 }
154
155 constexpr const E &error() const & noexcept {
156 return **error_;
157 }
158
159 constexpr E &&error() && noexcept {
160 return **std::move(error_);
161 }
162
163private:
164 std::optional<Unexpected<E>> error_;
165};
166
167} // namespace mini_llvm
constexpr Expected()
Definition Expected.h:139
constexpr E && error() &&noexcept
Definition Expected.h:159
constexpr const E & error() const &noexcept
Definition Expected.h:155
constexpr E & error() &noexcept
Definition Expected.h:151
constexpr Expected(Unexpected< E > error)
Definition Expected.h:141
constexpr Expected(Unexpected< F > error)
Definition Expected.h:145
constexpr const T * operator->() const noexcept
Definition Expected.h:104
constexpr T & value() &noexcept
Definition Expected.h:108
constexpr T && value() &&noexcept
Definition Expected.h:116
constexpr T & operator*() &noexcept
Definition Expected.h:88
constexpr T && operator*() &&noexcept
Definition Expected.h:96
constexpr Expected(T value)
Definition Expected.h:72
constexpr const T & value() const &noexcept
Definition Expected.h:112
constexpr E && error() &&noexcept
Definition Expected.h:128
constexpr Expected(Unexpected< E > error)
Definition Expected.h:78
constexpr Expected()=default
T value_type
Definition Expected.h:67
constexpr const T & operator*() const &noexcept
Definition Expected.h:92
constexpr E & error() &noexcept
Definition Expected.h:120
constexpr const E & error() const &noexcept
Definition Expected.h:124
E error_type
Definition Expected.h:68
constexpr Expected(U value)
Definition Expected.h:76
constexpr Expected(Unexpected< F > error)
Definition Expected.h:82
constexpr T * operator->() noexcept
Definition Expected.h:100
Definition Expected.h:14
constexpr E & error() &noexcept
Definition Expected.h:48
E error_type
Definition Expected.h:16
constexpr Unexpected(E error)
Definition Expected.h:18
constexpr Unexpected(F error)
Definition Expected.h:22
constexpr const E & error() const &noexcept
Definition Expected.h:52
constexpr Unexpected(Unexpected< F > error)
Definition Expected.h:26
constexpr E && error() &&noexcept
Definition Expected.h:56
constexpr E * operator->() noexcept
Definition Expected.h:40
constexpr const E * operator->() const noexcept
Definition Expected.h:44
constexpr E && operator*() &&noexcept
Definition Expected.h:36
constexpr const E & operator*() const &noexcept
Definition Expected.h:32
constexpr E & operator*() &noexcept
Definition Expected.h:28
Definition GraphColoringAllocator.h:13