115 operator int()
const {
return value(); }
122 template <
typename E,
typename = std::enable_if_t<std::is_enum_v<E>>>
128template <
typename E = Errno,
bool include_message = true>
130 template <
typename T,
typename P,
typename = std::enable_if_t<std::is_convertible_v<P, E>>>
134 template <
typename T>
150 template <
typename P,
typename = std::enable_if_t<std::is_convertible_v<P, E>>>
153 template <
typename T>
171 return !(lhs == rhs);
185 template <
typename T>
190 std::string
str()
const {
return ""; }
194template <
typename E = Errno,
bool include_message =
true,
195 typename = std::enable_if_t<!std::is_same_v<E, int>>>
199 template <
typename P,
typename = std::enable_if_t<std::is_convertible_v<P, E>>>
203 template <
typename T,
typename P,
typename = std::enable_if_t<std::is_convertible_v<E, P>>>
209 template <
typename T,
typename P,
typename = std::enable_if_t<std::is_convertible_v<E, P>>>
215 template <
typename T>
217 static_assert(include_message,
"<< not supported when include_message = false");
219 if constexpr (std::is_same_v<std::remove_cv_t<std::remove_reference_t<T>>,
ResultError<E>>) {
223 return (*
this) << t.message();
231 const std::string
str()
const {
232 static_assert(include_message,
"str() not supported when include_message = false");
233 std::string
str =
ss_.str();
236 return code_.print();
238 return std::move(
str) +
": " +
code_.print();
248 template <
typename T,
typename...
Args>
251 template <
typename T,
typename...
Args>
259 std::conditional_t<include_message, std::stringstream, internal::DoNothingStream>
ss_;
275template <
typename T,
typename E,
typename...
Args>
277 if constexpr (std::is_same_v<std::remove_cv_t<std::remove_reference_t<T>>,
ResultError<E>>) {
283template <
typename T,
typename...
Args>
288template <
typename T,
typename...
Args>
293#define Errorf(fmt, ...) android::base::ErrorfImpl(FMT_STRING(fmt), ##__VA_ARGS__)
294#define ErrnoErrorf(fmt, ...) android::base::ErrnoErrorfImpl(FMT_STRING(fmt), ##__VA_ARGS__)
296template <
typename T,
typename E = Errno,
bool include_message = true>
304using Code = std::decay_t<decltype(std::declval<U>().
error().code())>;
307using ErrorType = std::decay_t<decltype(std::declval<U>().error())>;
310constexpr bool IsNumeric = std::is_integral_v<U> || std::is_floating_point_v<U> ||
311 (std::is_enum_v<U> && std::is_convertible_v<U, size_t>);
320 operator const T() const && {
334template <
typename T,
typename =
void>
338 std::enable_if_t<impl::IsNumeric<impl::Code<T>>>
341#pragma push_macro("SPECIALIZED_CONVERSION")
342#define SPECIALIZED_CONVERSION(type)\
343 operator const expected<type, ErrorType<T>>() const &&\
344 { return unexpected(std::move(this->error_));}
365#undef SPECIALIZED_CONVERSION
366#pragma pop_macro("SPECIALIZED_CONVERSION")
374concept Universal = std::is_same_v<U, U>;
378template <
typename T,
typename E,
bool include_message>
390 static bool IsOk(
const V& val) {
return val.
ok(); }
394 if constexpr (std::is_same_v<T, void>) {
398 return std::move(val.value());
419 template <impl::Universal U>
421 template <
typename U>
434#define ASSERT_RESULT_OK(stmt) \
435 if (const auto& tmp = (stmt); !tmp.ok()) \
436 FAIL() << "Value of: " << #stmt << "\n" \
437 << " Actual: " << tmp.error().message() << "\n" \
438 << "Expected: is ok\n"
440#define EXPECT_RESULT_OK(stmt) \
441 if (const auto& tmp = (stmt); !tmp.ok()) \
442 ADD_FAILURE() << "Value of: " << #stmt << "\n" \
443 << " Actual: " << tmp.error().message() << "\n" \
444 << "Expected: is ok\n"
Error(P &&code)
Definition: result.h:201
Error()
Definition: result.h:198
Error & operator<<(T &&t)
Definition: result.h:216
const std::string str() const
Definition: result.h:231
Error & operator=(Error &&)=delete
friend Error ErrorfImpl(const T &&fmt, const Args &... args)
Definition: result.h:284
Error(bool has_code, E code, const std::string &message)
Definition: result.h:255
std::conditional_t< include_message, std::stringstream, internal::DoNothingStream > ss_
Definition: result.h:259
const bool has_code_
Definition: result.h:261
E code_
Definition: result.h:260
Error & operator=(const Error &)=delete
Error(const Error &)=delete
friend Error ErrnoErrorfImpl(const T &&fmt, const Args &... args)
Definition: result.h:289
Definition: expected.h:86
constexpr const E & error() const &
Definition: expected.h:341
constexpr bool ok() const noexcept
Definition: expected.h:334
#define SPECIALIZED_CONVERSION(type)
Definition: result.h:342
std::decay_t< decltype(std::declval< U >().error())> ErrorType
Definition: result.h:307
constexpr bool IsNumeric
Definition: result.h:310
std::decay_t< decltype(std::declval< U >().error().code())> Code
Definition: result.h:304
Error< Errno > ErrnoError()
Definition: result.h:264
constexpr bool operator==(const expected< T1, E1 > &x, const expected< T2, E2 > &y)
Definition: expected.h:389
std::ostream & operator<<(std::ostream &os, const ResultError< E > &t)
Definition: result.h:175
Error< Errno > ErrnoErrorfImpl(const T &&fmt, const Args &... args)
Definition: result.h:289
E ErrorCode(E code)
Definition: result.h:269
constexpr bool operator!=(const expected< T1, E1 > &x, const expected< T2, E2 > &y)
Definition: expected.h:396
Error< Errno > ErrorfImpl(const T &&fmt, const Args &... args)
Definition: result.h:284
unexpected(E) -> unexpected< E >
EventFormat format
Definition: kernel_log_server.cc:57
std::vector< std::string_view > Args
Definition: incremental.h:28
Definition: logging.h:464
std::string print() const
Definition: result.h:116
int val_
Definition: result.h:118
Errno(int e)
Definition: result.h:113
Errno()
Definition: result.h:112
int value() const
Definition: result.h:114
OkOrFail(const OkOrFail &other)=delete
impl::Code< V > C
Definition: result.h:383
OkOrFail(const OkOrFail &&other)=delete
static const OkOrFail< V > Fail(V &&v)
Definition: result.h:403
OkOrFail(Err &&v)
Definition: result.h:385
static T Unwrap(V &&val)
Definition: result.h:393
static bool IsOk(const V &val)
Definition: result.h:390
impl::ErrorType< V > Err
Definition: result.h:382
static std::string ErrorMessage(const V &val)
Definition: result.h:427
static bool IsOk(const T &)
ResultError(P &&code)
Definition: result.h:151
E code_
Definition: result.h:161
const E & code() const
Definition: result.h:158
const E & code() const
Definition: result.h:141
ResultError(T &&message, P &&code)
Definition: result.h:131
const std::string & message() const
Definition: result.h:140
E code_
Definition: result.h:145
std::string message_
Definition: result.h:144
ErrorType< T > error_
Definition: result.h:318
std::true_type IsNumericT
Definition: result.h:368
DoNothingStream & operator<<(T &&)
Definition: result.h:186
std::string str() const
Definition: result.h:190