TLE92466ED Driver 2.0.0
API reference
Loading...
Searching...
No Matches
tle92466ed_expected.hpp
Go to the documentation of this file.
1
19#pragma once
20
21// ── Feature detection ────────────────────────────────────────────────────────
22#if defined(__has_include)
23#if __has_include(<version>)
24#include <version>
25#endif
26#endif
27
28// ── C++23 path: alias to standard library ────────────────────────────────────
29#if defined(__cpp_lib_expected) && __cpp_lib_expected >= 202211L
30
31#include <expected>
32
33namespace tle {
34
35template <typename T, typename E>
36using expected = std::expected<T, E>;
37
38template <typename E>
39using unexpected = std::unexpected<E>;
40
41} // namespace tle
42
43// ── C++20 fallback: lightweight polyfill ─────────────────────────────────────
44#else
45
46#include <cstdint>
47#include <type_traits>
48#include <utility>
49
50namespace tle {
51
56template <typename E> class unexpected {
58
59public:
60 constexpr explicit unexpected(const E &e) noexcept : error_(e) {}
61 constexpr explicit unexpected(E &&e) noexcept : error_(std::move(e)) {}
62
63 [[nodiscard]] constexpr const E &error() const &noexcept { return error_; }
64 [[nodiscard]] constexpr E &error() &noexcept { return error_; }
65 [[nodiscard]] constexpr E &&error() &&noexcept { return std::move(error_); }
66};
67
69template <typename E> unexpected(E) -> unexpected<E>;
70
71// Forward declaration
72template <typename T, typename E> class expected;
73
82template <typename T, typename E> class expected {
86
87public:
88 // ── Value constructors ────────────────────────────────────────────────────
89 constexpr expected(const T &v) noexcept // NOLINT(google-explicit-constructor)
90 : value_(v), error_{}, has_value_(true) {}
91
92 constexpr expected(T &&v) noexcept // NOLINT(google-explicit-constructor)
93 : value_(std::move(v)), error_{}, has_value_(true) {}
94
95 // ── Error constructors (from unexpected) ──────────────────────────────────
96 constexpr expected( // NOLINT(google-explicit-constructor)
98 : value_{}, error_(u.error()), has_value_(false) {}
99
100 constexpr expected( // NOLINT(google-explicit-constructor)
102 : value_{}, error_(std::move(u).error()), has_value_(false) {}
103
104 // ── Copy / Move ───────────────────────────────────────────────────────────
105 constexpr expected(const expected &) noexcept = default;
110
111 // ── Observers ─────────────────────────────────────────────────────────────
115 [[nodiscard]] constexpr bool has_value() const noexcept {
116 return has_value_;
117 }
118
119 [[nodiscard]] constexpr T &value() &noexcept { return value_; }
120 [[nodiscard]] constexpr const T &value() const &noexcept { return value_; }
121 [[nodiscard]] constexpr T &&value() &&noexcept {
122 return std::move(value_);
123 }
124
125 [[nodiscard]] constexpr E &error() &noexcept { return error_; }
126 [[nodiscard]] constexpr const E &error() const &noexcept { return error_; }
127 [[nodiscard]] constexpr E &&error() &&noexcept {
128 return std::move(error_);
129 }
130
131 [[nodiscard]] constexpr T &operator*() &noexcept { return value_; }
132 [[nodiscard]] constexpr const T &operator*() const &noexcept {
133 return value_;
134 }
135 [[nodiscard]] constexpr T &&operator*() &&noexcept {
136 return std::move(value_);
137 }
138
139 [[nodiscard]] constexpr T *operator->() noexcept { return &value_; }
140 [[nodiscard]] constexpr const T *operator->() const noexcept {
141 return &value_;
142 }
143
144 // ── Monadic helpers ───────────────────────────────────────────────────────
145 template <typename U>
146 [[nodiscard]] constexpr T value_or(U &&default_val) const &noexcept {
147 return has_value_ ? value_ : static_cast<T>(std::forward<U>(default_val));
148 }
149
150 // ── Converting constructor (e.g. expected<uint32_t,E> → expected<uint16_t,E>)
151 template <typename U,
152 typename = std::enable_if_t<!std::is_same_v<U, T> &&
153 std::is_convertible_v<U, T>>>
154 constexpr expected( // NOLINT(google-explicit-constructor)
156 : value_(other.has_value() ? static_cast<T>(*other) : T{}),
157 error_(other.has_value() ? E{} : other.error()),
159};
160
167template <typename E> class expected<void, E> {
170
171public:
172 // ── Success constructor ───────────────────────────────────────────────────
173 constexpr expected() noexcept : error_{}, has_value_(true) {}
174
175 // ── Error constructors (from unexpected) ──────────────────────────────────
176 constexpr expected( // NOLINT(google-explicit-constructor)
177 const unexpected<E> &u) noexcept
178 : error_(u.error()), has_value_(false) {}
179
180 constexpr expected( // NOLINT(google-explicit-constructor)
182 : error_(std::move(u).error()), has_value_(false) {}
183
184 // ── Copy / Move ───────────────────────────────────────────────────────────
185 constexpr expected(const expected &) noexcept = default;
190
191 // ── Observers ─────────────────────────────────────────────────────────────
195 [[nodiscard]] constexpr bool has_value() const noexcept {
196 return has_value_;
197 }
198
199 [[nodiscard]] constexpr E &error() &noexcept { return error_; }
200 [[nodiscard]] constexpr const E &error() const &noexcept { return error_; }
201 [[nodiscard]] constexpr E &&error() &&noexcept {
202 return std::move(error_);
203 }
204};
205
206} // namespace tle
207
208#endif // __cpp_lib_expected
constexpr bool has_value() const noexcept
Definition tle92466ed_expected.hpp:195
constexpr expected(unexpected< E > &&u) noexcept
Definition tle92466ed_expected.hpp:180
constexpr expected(const expected &) noexcept=default
constexpr E & error() &noexcept
Definition tle92466ed_expected.hpp:199
E error_
Definition tle92466ed_expected.hpp:168
constexpr expected() noexcept
Definition tle92466ed_expected.hpp:173
constexpr expected(const unexpected< E > &u) noexcept
Definition tle92466ed_expected.hpp:176
constexpr E && error() &&noexcept
Definition tle92466ed_expected.hpp:201
bool has_value_
Definition tle92466ed_expected.hpp:169
constexpr expected(expected &&) noexcept=default
constexpr const E & error() const &noexcept
Definition tle92466ed_expected.hpp:200
Expected type — holds either a value of type T or an error of type E.
Definition tle92466ed_expected.hpp:82
constexpr T && operator*() &&noexcept
Definition tle92466ed_expected.hpp:135
constexpr T && value() &&noexcept
Definition tle92466ed_expected.hpp:121
constexpr const T & operator*() const &noexcept
Definition tle92466ed_expected.hpp:132
constexpr T & value() &noexcept
Definition tle92466ed_expected.hpp:119
constexpr const E & error() const &noexcept
Definition tle92466ed_expected.hpp:126
bool has_value_
Definition tle92466ed_expected.hpp:85
constexpr bool has_value() const noexcept
Definition tle92466ed_expected.hpp:115
constexpr T value_or(U &&default_val) const &noexcept
Definition tle92466ed_expected.hpp:146
constexpr expected(const expected &) noexcept=default
constexpr expected(const T &v) noexcept
Definition tle92466ed_expected.hpp:89
constexpr expected(expected &&) noexcept=default
constexpr T & operator*() &noexcept
Definition tle92466ed_expected.hpp:131
constexpr T * operator->() noexcept
Definition tle92466ed_expected.hpp:139
constexpr expected(const expected< U, E > &other) noexcept
Definition tle92466ed_expected.hpp:154
constexpr expected(unexpected< E > &&u) noexcept
Definition tle92466ed_expected.hpp:100
E error_
Definition tle92466ed_expected.hpp:84
T value_
Definition tle92466ed_expected.hpp:83
constexpr expected(T &&v) noexcept
Definition tle92466ed_expected.hpp:92
constexpr const T * operator->() const noexcept
Definition tle92466ed_expected.hpp:140
constexpr E & error() &noexcept
Definition tle92466ed_expected.hpp:125
constexpr E && error() &&noexcept
Definition tle92466ed_expected.hpp:127
constexpr const T & value() const &noexcept
Definition tle92466ed_expected.hpp:120
constexpr expected(const unexpected< E > &u) noexcept
Definition tle92466ed_expected.hpp:96
Wrapper for an error value (mirrors std::unexpected)
Definition tle92466ed_expected.hpp:56
constexpr unexpected(const E &e) noexcept
Definition tle92466ed_expected.hpp:60
constexpr E & error() &noexcept
Definition tle92466ed_expected.hpp:64
constexpr E && error() &&noexcept
Definition tle92466ed_expected.hpp:65
constexpr unexpected(E &&e) noexcept
Definition tle92466ed_expected.hpp:61
E error_
Definition tle92466ed_expected.hpp:57
constexpr const E & error() const &noexcept
Definition tle92466ed_expected.hpp:63
Definition tle92466ed_expected.hpp:50
unexpected(E) -> unexpected< E >
Deduction guide.