14 template<
typename Value,
typename ErrorType = ice::ErrorCode>
26 if constexpr (std::is_same_v<ice::ErrorCode, ErrorType>)
32 template<
typename OtherValue>
requires (std::is_convertible_v<OtherValue, Value>)
39 Expected(
Expected&& other)
noexcept requires (std::is_nothrow_move_constructible_v<Value>)
40 : _state{ other._state }
44 new (ice::addressof(
_value)) Value { ice::forward<Value>(other) };
52 template<
typename OtherValue>
requires (std::is_convertible_v<OtherValue, Value>)
53 auto operator=(OtherValue&&
value)
noexcept ->
Expected&
requires (std::is_nothrow_move_assignable_v<Value>)
55 if (std::exchange(_state,
ice::u8(1u)) == 1u)
60 new (ice::addressof(
_value)) Value { ice::forward<OtherValue>(
value) };
66 if (std::exchange(_state,
ice::u8(2u)) == 1u)
78 if (ice::addressof(other) !=
this)
80 if (_state == 1u && other._state == 1u)
82 _value = ice::move(other._value);
84 else if (other._state == 1u)
87 new (ice::addressof(
_value)) Value{ ice::move(other._value) };
111 bool valid() const noexcept {
return _state != 0u; }
113 bool failed() const noexcept {
return _state == 2; }
115 template<
typename Self>
116 auto value(
this Self&& self)
noexcept ->
auto&&
119 return ice::forward<Self>(self)._value;
122 auto error() const noexcept -> ErrorType
133 inline explicit operator bool() const noexcept
138 inline operator Value&() &
noexcept {
return this->
value(); }
139 inline operator Value&&() &&
noexcept {
return ice::move(*this).value(); }
170 bool valid() const noexcept {
return true; }
182 template<
typename ErrorType>
requires(std::is_base_of_v<ice::ErrorCode, ErrorType>)
183 inline bool operator==(ErrorType
error)
const noexcept
185 return _value ==
error;
188 inline operator bool() const noexcept
190 return _value ==
true;
201template<
typename Val>
202struct fmt::formatter<
ice::Expected<Val, ice::ErrorCode>>
204 template<
typename ParseContext>
205 constexpr auto parse(ParseContext& ctx)
210 template<
typename FormatContext>
213 if (value.succeeded())
215 return fmt::format_to(ctx.out(),
"{}",
ice::ErrorCode{ ice::S_Ok });
219 return fmt::format_to(ctx.out(),
"{}", value.error());
#define ICE_ASSERT_CORE(expression)
Definition assert_core.hxx:43
Expected(bool issuccess) noexcept
Definition expected.hxx:160
bool valid() const noexcept
Definition expected.hxx:170
Expected(ice::ErrorCode error) noexcept
Definition expected.hxx:165
Expected() noexcept
Definition expected.hxx:156
auto value() const noexcept -> ice::ErrorCode
Definition expected.hxx:172
auto error() const noexcept -> ice::ErrorCode
Definition expected.hxx:177
Definition expected.hxx:16
ErrorType _error
Definition expected.hxx:145
auto operator=(Expected &&other) noexcept -> Expected &
Definition expected.hxx:76
auto error() const noexcept -> ice::ErrorCode
Definition expected.hxx:122
Expected(Expected &&other) noexcept
Definition expected.hxx:39
bool failed() const noexcept
Definition expected.hxx:113
~Expected() noexcept
Definition expected.hxx:103
bool succeeded() const noexcept
Definition expected.hxx:112
Expected(OtherValue &&value) noexcept
Definition expected.hxx:33
auto value(this Self &&self) noexcept -> auto &&
Definition expected.hxx:116
Expected() noexcept
Definition expected.hxx:18
bool operator==(ErrorType error) const noexcept
Definition expected.hxx:128
Expected(ErrorType error) noexcept
Definition expected.hxx:22
Value _value
Definition expected.hxx:146
bool valid() const noexcept
Definition expected.hxx:111
auto operator=(ErrorType error) noexcept -> Expected &
Definition expected.hxx:64
SPDX-License-Identifier: MIT.
Definition array.hxx:12
static constexpr ice::ErrorCodeError E_Fail
Definition error_codes.hxx:13
static constexpr ice::ErrorCodeError E_Error
Definition error_codes.hxx:14
static constexpr ice::ErrorCodeSuccess S_Ok
Definition error_codes.hxx:10
ice::Expected< ice::ErrorCode > Result
Definition expected.hxx:197
std::uint8_t u8
Definition types.hxx:24