2018-12-31 20:04:05 +01:00
|
|
|
//
|
2024-01-01 01:07:21 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "td/utils/common.h"
|
|
|
|
#include "td/utils/logging.h"
|
|
|
|
#include "td/utils/ScopeGuard.h"
|
|
|
|
#include "td/utils/Slice.h"
|
|
|
|
#include "td/utils/StackAllocator.h"
|
|
|
|
#include "td/utils/StringBuilder.h"
|
|
|
|
|
|
|
|
#include <cerrno>
|
|
|
|
#include <cstring>
|
2018-09-27 03:19:03 +02:00
|
|
|
#include <memory>
|
2018-12-31 20:04:05 +01:00
|
|
|
#include <new>
|
2018-04-12 10:08:20 +02:00
|
|
|
#include <type_traits>
|
2018-12-31 20:04:05 +01:00
|
|
|
#include <utility>
|
|
|
|
|
2023-03-21 11:30:42 +01:00
|
|
|
#define TRY_STATUS(status) \
|
|
|
|
{ \
|
|
|
|
auto try_status = (status); \
|
|
|
|
if (try_status.is_error()) { \
|
|
|
|
return try_status.move_as_error_unsafe(); \
|
|
|
|
} \
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2019-07-21 20:07:07 +02:00
|
|
|
|
2023-03-21 11:30:42 +01:00
|
|
|
#define TRY_STATUS_PREFIX(status, prefix) \
|
|
|
|
{ \
|
|
|
|
auto try_status = (status); \
|
|
|
|
if (try_status.is_error()) { \
|
|
|
|
return try_status.move_as_error_prefix_unsafe(prefix); \
|
|
|
|
} \
|
2019-07-06 13:29:15 +02:00
|
|
|
}
|
2019-07-21 20:07:07 +02:00
|
|
|
|
2023-03-21 11:30:42 +01:00
|
|
|
#define TRY_STATUS_PROMISE(promise_name, status) \
|
|
|
|
{ \
|
|
|
|
auto try_status = (status); \
|
|
|
|
if (try_status.is_error()) { \
|
|
|
|
return promise_name.set_error(try_status.move_as_error_unsafe()); \
|
|
|
|
} \
|
2020-06-24 13:47:36 +02:00
|
|
|
}
|
|
|
|
|
2023-03-21 11:30:42 +01:00
|
|
|
#define TRY_STATUS_PROMISE_PREFIX(promise_name, status, prefix) \
|
|
|
|
{ \
|
|
|
|
auto try_status = (status); \
|
|
|
|
if (try_status.is_error()) { \
|
|
|
|
return promise_name.set_error(try_status.move_as_error_prefix_unsafe(prefix)); \
|
|
|
|
} \
|
2020-06-24 13:47:36 +02:00
|
|
|
}
|
|
|
|
|
2019-07-24 04:10:32 +02:00
|
|
|
#define TRY_RESULT(name, result) TRY_RESULT_IMPL(TD_CONCAT(TD_CONCAT(r_, name), __LINE__), auto name, result)
|
|
|
|
|
2020-06-24 13:47:36 +02:00
|
|
|
#define TRY_RESULT_PROMISE(promise_name, name, result) \
|
|
|
|
TRY_RESULT_PROMISE_IMPL(promise_name, TD_CONCAT(TD_CONCAT(r_, name), __LINE__), auto name, result)
|
|
|
|
|
2019-09-02 20:11:56 +02:00
|
|
|
#define TRY_RESULT_ASSIGN(name, result) TRY_RESULT_IMPL(TD_CONCAT(r_response, __LINE__), name, result)
|
2019-07-21 20:07:07 +02:00
|
|
|
|
2020-06-24 13:47:36 +02:00
|
|
|
#define TRY_RESULT_PROMISE_ASSIGN(promise_name, name, result) \
|
|
|
|
TRY_RESULT_PROMISE_IMPL(promise_name, TD_CONCAT(TD_CONCAT(r_, name), __LINE__), name, result)
|
|
|
|
|
2019-07-06 13:29:15 +02:00
|
|
|
#define TRY_RESULT_PREFIX(name, result, prefix) \
|
2019-07-24 04:10:32 +02:00
|
|
|
TRY_RESULT_PREFIX_IMPL(TD_CONCAT(TD_CONCAT(r_, name), __LINE__), auto name, result, prefix)
|
|
|
|
|
|
|
|
#define TRY_RESULT_PREFIX_ASSIGN(name, result, prefix) \
|
2019-07-06 13:29:15 +02:00
|
|
|
TRY_RESULT_PREFIX_IMPL(TD_CONCAT(TD_CONCAT(r_, name), __LINE__), name, result, prefix)
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2020-06-24 13:47:36 +02:00
|
|
|
#define TRY_RESULT_PROMISE_PREFIX(promise_name, name, result, prefix) \
|
|
|
|
TRY_RESULT_PROMISE_PREFIX_IMPL(promise_name, TD_CONCAT(TD_CONCAT(r_, name), __LINE__), auto name, result, prefix)
|
|
|
|
|
|
|
|
#define TRY_RESULT_PROMISE_PREFIX_ASSIGN(promise_name, name, result, prefix) \
|
|
|
|
TRY_RESULT_PROMISE_PREFIX_IMPL(promise_name, TD_CONCAT(TD_CONCAT(r_, name), __LINE__), name, result, prefix)
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
#define TRY_RESULT_IMPL(r_name, name, result) \
|
|
|
|
auto r_name = (result); \
|
|
|
|
if (r_name.is_error()) { \
|
2023-03-21 11:30:42 +01:00
|
|
|
return r_name.move_as_error_unsafe(); \
|
2018-12-31 20:04:05 +01:00
|
|
|
} \
|
2023-03-21 11:30:42 +01:00
|
|
|
name = r_name.move_as_ok_unsafe();
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2019-07-06 13:29:15 +02:00
|
|
|
#define TRY_RESULT_PREFIX_IMPL(r_name, name, result, prefix) \
|
|
|
|
auto r_name = (result); \
|
|
|
|
if (r_name.is_error()) { \
|
2023-03-21 11:30:42 +01:00
|
|
|
return r_name.move_as_error_prefix_unsafe(prefix); \
|
2019-07-06 13:29:15 +02:00
|
|
|
} \
|
2023-03-21 11:30:42 +01:00
|
|
|
name = r_name.move_as_ok_unsafe();
|
2019-07-06 13:29:15 +02:00
|
|
|
|
2020-06-24 13:47:36 +02:00
|
|
|
#define TRY_RESULT_PROMISE_IMPL(promise_name, r_name, name, result) \
|
|
|
|
auto r_name = (result); \
|
|
|
|
if (r_name.is_error()) { \
|
2023-03-21 11:30:42 +01:00
|
|
|
return promise_name.set_error(r_name.move_as_error_unsafe()); \
|
2020-06-24 13:47:36 +02:00
|
|
|
} \
|
2023-03-21 11:30:42 +01:00
|
|
|
name = r_name.move_as_ok_unsafe();
|
2020-06-24 13:47:36 +02:00
|
|
|
|
|
|
|
#define TRY_RESULT_PROMISE_PREFIX_IMPL(promise_name, r_name, name, result, prefix) \
|
|
|
|
auto r_name = (result); \
|
|
|
|
if (r_name.is_error()) { \
|
2023-03-21 11:30:42 +01:00
|
|
|
return promise_name.set_error(r_name.move_as_error_prefix_unsafe(prefix)); \
|
2020-06-24 13:47:36 +02:00
|
|
|
} \
|
2023-03-21 11:30:42 +01:00
|
|
|
name = r_name.move_as_ok_unsafe();
|
2020-06-24 13:47:36 +02:00
|
|
|
|
2023-03-21 11:30:42 +01:00
|
|
|
#define LOG_STATUS(status) \
|
|
|
|
{ \
|
|
|
|
auto log_status = (status); \
|
|
|
|
if (log_status.is_error()) { \
|
|
|
|
LOG(ERROR) << log_status.move_as_error_unsafe(); \
|
|
|
|
} \
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2018-02-12 17:03:04 +01:00
|
|
|
#ifndef TD_STATUS_NO_ENSURE
|
|
|
|
#define ensure() ensure_impl(__FILE__, __LINE__)
|
|
|
|
#define ensure_error() ensure_error_impl(__FILE__, __LINE__)
|
|
|
|
#endif
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
#if TD_PORT_POSIX
|
|
|
|
#define OS_ERROR(message) \
|
2020-02-26 16:15:19 +01:00
|
|
|
[&] { \
|
2018-12-31 20:04:05 +01:00
|
|
|
auto saved_errno = errno; \
|
|
|
|
return ::td::Status::PosixError(saved_errno, (message)); \
|
|
|
|
}()
|
|
|
|
#define OS_SOCKET_ERROR(message) OS_ERROR(message)
|
|
|
|
#elif TD_PORT_WINDOWS
|
|
|
|
#define OS_ERROR(message) \
|
2020-02-26 16:15:19 +01:00
|
|
|
[&] { \
|
2018-12-31 20:04:05 +01:00
|
|
|
auto saved_error = ::GetLastError(); \
|
|
|
|
return ::td::Status::WindowsError(saved_error, (message)); \
|
|
|
|
}()
|
|
|
|
#define OS_SOCKET_ERROR(message) \
|
2020-02-26 16:15:19 +01:00
|
|
|
[&] { \
|
2018-12-31 20:04:05 +01:00
|
|
|
auto saved_error = ::WSAGetLastError(); \
|
|
|
|
return ::td::Status::WindowsError(saved_error, (message)); \
|
|
|
|
}()
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
#if TD_PORT_POSIX
|
|
|
|
CSlice strerror_safe(int code);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if TD_PORT_WINDOWS
|
|
|
|
string winerror_to_string(int code);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
class Status {
|
2020-06-15 03:23:47 +02:00
|
|
|
enum class ErrorType : int8 { General, Os };
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
public:
|
|
|
|
Status() = default;
|
|
|
|
|
2023-05-17 12:45:26 +02:00
|
|
|
bool is_static() const {
|
|
|
|
if (is_ok()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return get_info().static_flag;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Status clone() const TD_WARN_UNUSED_RESULT {
|
|
|
|
if (is_ok()) {
|
|
|
|
return Status();
|
|
|
|
}
|
|
|
|
auto info = get_info();
|
|
|
|
if (info.static_flag) {
|
2023-03-14 20:43:28 +01:00
|
|
|
return clone_static(-999);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
return Status(false, info.error_type, info.error_code, message());
|
|
|
|
}
|
|
|
|
|
|
|
|
static Status OK() TD_WARN_UNUSED_RESULT {
|
|
|
|
return Status();
|
|
|
|
}
|
|
|
|
|
|
|
|
static Status Error(int err, Slice message = Slice()) TD_WARN_UNUSED_RESULT {
|
2020-06-15 03:23:47 +02:00
|
|
|
return Status(false, ErrorType::General, err, message);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static Status Error(Slice message) TD_WARN_UNUSED_RESULT {
|
|
|
|
return Error(0, message);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if TD_PORT_WINDOWS
|
|
|
|
static Status WindowsError(int saved_error, Slice message) TD_WARN_UNUSED_RESULT {
|
2020-06-15 03:23:47 +02:00
|
|
|
return Status(false, ErrorType::Os, saved_error, message);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if TD_PORT_POSIX
|
|
|
|
static Status PosixError(int32 saved_errno, Slice message) TD_WARN_UNUSED_RESULT {
|
2020-06-15 03:23:47 +02:00
|
|
|
return Status(false, ErrorType::Os, saved_errno, message);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
template <int Code>
|
|
|
|
static Status Error() {
|
2020-06-15 03:23:47 +02:00
|
|
|
static Status status(true, ErrorType::General, Code, "");
|
2023-03-14 20:43:28 +01:00
|
|
|
return status.clone_static(Code);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
StringBuilder &print(StringBuilder &sb) const {
|
|
|
|
if (is_ok()) {
|
|
|
|
return sb << "OK";
|
|
|
|
}
|
|
|
|
Info info = get_info();
|
|
|
|
switch (info.error_type) {
|
2020-06-15 03:23:47 +02:00
|
|
|
case ErrorType::General:
|
2018-12-31 20:04:05 +01:00
|
|
|
sb << "[Error";
|
|
|
|
break;
|
2020-06-15 03:23:47 +02:00
|
|
|
case ErrorType::Os:
|
2018-12-31 20:04:05 +01:00
|
|
|
#if TD_PORT_POSIX
|
|
|
|
sb << "[PosixError : " << strerror_safe(info.error_code);
|
|
|
|
#elif TD_PORT_WINDOWS
|
|
|
|
sb << "[WindowsError : " << winerror_to_string(info.error_code);
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
sb << " : " << code() << " : " << message() << "]";
|
|
|
|
return sb;
|
|
|
|
}
|
|
|
|
|
|
|
|
string to_string() const {
|
|
|
|
auto buf = StackAllocator::alloc(4096);
|
|
|
|
StringBuilder sb(buf.as_slice());
|
|
|
|
print(sb);
|
|
|
|
return sb.as_cslice().str();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Default interface
|
|
|
|
bool is_ok() const TD_WARN_UNUSED_RESULT {
|
|
|
|
return !is_error();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_error() const TD_WARN_UNUSED_RESULT {
|
|
|
|
return ptr_ != nullptr;
|
|
|
|
}
|
|
|
|
|
2018-07-08 21:00:54 +02:00
|
|
|
#ifdef TD_STATUS_NO_ENSURE
|
|
|
|
void ensure() const {
|
|
|
|
if (!is_ok()) {
|
|
|
|
LOG(FATAL) << "Unexpected Status " << to_string();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void ensure_error() const {
|
|
|
|
if (is_ok()) {
|
|
|
|
LOG(FATAL) << "Unexpected Status::OK";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
2018-02-12 17:03:04 +01:00
|
|
|
void ensure_impl(CSlice file_name, int line) const {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!is_ok()) {
|
2018-06-26 21:51:00 +02:00
|
|
|
LOG(FATAL) << "Unexpected Status " << to_string() << " in file " << file_name << " at line " << line;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
2018-02-12 17:03:04 +01:00
|
|
|
void ensure_error_impl(CSlice file_name, int line) const {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (is_ok()) {
|
2018-02-12 17:03:04 +01:00
|
|
|
LOG(FATAL) << "Unexpected Status::OK in file " << file_name << " at line " << line;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
2018-07-08 21:00:54 +02:00
|
|
|
#endif
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
void ignore() const {
|
|
|
|
// nop
|
|
|
|
}
|
|
|
|
|
|
|
|
int32 code() const {
|
|
|
|
if (is_ok()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return get_info().error_code;
|
|
|
|
}
|
|
|
|
|
|
|
|
CSlice message() const {
|
|
|
|
if (is_ok()) {
|
|
|
|
return CSlice("OK");
|
|
|
|
}
|
|
|
|
return CSlice(ptr_.get() + sizeof(Info));
|
|
|
|
}
|
|
|
|
|
|
|
|
string public_message() const {
|
|
|
|
if (is_ok()) {
|
|
|
|
return "OK";
|
|
|
|
}
|
|
|
|
Info info = get_info();
|
|
|
|
switch (info.error_type) {
|
2020-06-15 03:23:47 +02:00
|
|
|
case ErrorType::General:
|
2018-12-31 20:04:05 +01:00
|
|
|
return message().str();
|
2020-06-15 03:23:47 +02:00
|
|
|
case ErrorType::Os:
|
2018-12-31 20:04:05 +01:00
|
|
|
#if TD_PORT_POSIX
|
|
|
|
return strerror_safe(info.error_code).str();
|
|
|
|
#elif TD_PORT_WINDOWS
|
|
|
|
return winerror_to_string(info.error_code);
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const Status &error() const {
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Status move() TD_WARN_UNUSED_RESULT {
|
|
|
|
return std::move(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
Status move_as_error() TD_WARN_UNUSED_RESULT {
|
|
|
|
return std::move(*this);
|
|
|
|
}
|
|
|
|
|
2023-03-21 11:30:42 +01:00
|
|
|
Status move_as_error_unsafe() TD_WARN_UNUSED_RESULT {
|
|
|
|
return std::move(*this);
|
|
|
|
}
|
|
|
|
|
2020-06-26 01:24:13 +02:00
|
|
|
Status move_as_ok() = delete;
|
2020-06-24 13:47:36 +02:00
|
|
|
|
2023-03-21 11:30:42 +01:00
|
|
|
Status move_as_ok_unsafe() = delete;
|
|
|
|
|
2020-06-24 13:47:36 +02:00
|
|
|
Status move_as_error_prefix(const Status &status) const TD_WARN_UNUSED_RESULT {
|
|
|
|
return status.move_as_error_suffix(message());
|
|
|
|
}
|
|
|
|
|
2021-05-20 22:13:02 +02:00
|
|
|
Status move_as_error_prefix(Slice prefix) const TD_WARN_UNUSED_RESULT;
|
|
|
|
|
2023-03-21 11:30:42 +01:00
|
|
|
Status move_as_error_prefix_unsafe(Slice prefix) const TD_WARN_UNUSED_RESULT;
|
|
|
|
|
2021-05-20 22:13:02 +02:00
|
|
|
Status move_as_error_suffix(Slice suffix) const TD_WARN_UNUSED_RESULT;
|
2019-07-06 13:29:15 +02:00
|
|
|
|
2023-03-21 11:30:42 +01:00
|
|
|
Status move_as_error_suffix_unsafe(Slice suffix) const TD_WARN_UNUSED_RESULT;
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
private:
|
|
|
|
struct Info {
|
|
|
|
bool static_flag : 1;
|
|
|
|
signed int error_code : 23;
|
|
|
|
ErrorType error_type;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Deleter {
|
|
|
|
void operator()(char *ptr) {
|
|
|
|
if (!get_info(ptr).static_flag) {
|
|
|
|
delete[] ptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
std::unique_ptr<char[], Deleter> ptr_;
|
|
|
|
|
|
|
|
Status(Info info, Slice message) {
|
|
|
|
size_t size = sizeof(Info) + message.size() + 1;
|
|
|
|
ptr_ = std::unique_ptr<char[], Deleter>(new char[size]);
|
|
|
|
char *ptr = ptr_.get();
|
|
|
|
reinterpret_cast<Info *>(ptr)[0] = info;
|
|
|
|
ptr += sizeof(Info);
|
|
|
|
std::memcpy(ptr, message.begin(), message.size());
|
|
|
|
ptr += message.size();
|
|
|
|
*ptr = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Status(bool static_flag, ErrorType error_type, int error_code, Slice message)
|
|
|
|
: Status(to_info(static_flag, error_type, error_code), message) {
|
2019-07-06 13:29:15 +02:00
|
|
|
if (static_flag) {
|
|
|
|
TD_LSAN_IGNORE(ptr_.get());
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2023-03-14 20:43:28 +01:00
|
|
|
Status clone_static(int code) const TD_WARN_UNUSED_RESULT {
|
|
|
|
LOG_CHECK(ptr_ != nullptr && get_info().static_flag) << ptr_.get() << ' ' << code;
|
2018-12-31 20:04:05 +01:00
|
|
|
Status result;
|
|
|
|
result.ptr_ = std::unique_ptr<char[], Deleter>(ptr_.get());
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Info to_info(bool static_flag, ErrorType error_type, int error_code) {
|
|
|
|
const int MIN_ERROR_CODE = -(1 << 22) + 1;
|
|
|
|
const int MAX_ERROR_CODE = (1 << 22) - 1;
|
|
|
|
Info tmp;
|
|
|
|
tmp.static_flag = static_flag;
|
|
|
|
tmp.error_type = error_type;
|
|
|
|
|
|
|
|
if (error_code < MIN_ERROR_CODE) {
|
|
|
|
LOG(ERROR) << "Error code value is altered from " << error_code;
|
|
|
|
error_code = MIN_ERROR_CODE;
|
|
|
|
}
|
|
|
|
if (error_code > MAX_ERROR_CODE) {
|
|
|
|
LOG(ERROR) << "Error code value is altered from " << error_code;
|
|
|
|
error_code = MAX_ERROR_CODE;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if TD_GCC
|
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wconversion"
|
|
|
|
#endif
|
|
|
|
tmp.error_code = error_code;
|
|
|
|
#if TD_GCC
|
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
#endif
|
|
|
|
CHECK(error_code == tmp.error_code);
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
Info get_info() const {
|
|
|
|
return get_info(ptr_.get());
|
|
|
|
}
|
|
|
|
static Info get_info(char *ptr) {
|
|
|
|
return reinterpret_cast<Info *>(ptr)[0];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class T = Unit>
|
|
|
|
class Result {
|
|
|
|
public:
|
2020-06-24 13:47:36 +02:00
|
|
|
using ValueT = T;
|
2018-07-08 21:00:54 +02:00
|
|
|
Result() : status_(Status::Error<-1>()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-04-12 10:08:20 +02:00
|
|
|
template <class S, std::enable_if_t<!std::is_same<std::decay_t<S>, Result>::value, int> = 0>
|
2018-12-31 20:04:05 +01:00
|
|
|
Result(S &&x) : status_(), value_(std::forward<S>(x)) {
|
|
|
|
}
|
2019-07-06 13:29:15 +02:00
|
|
|
struct emplace_t {};
|
|
|
|
template <class... ArgsT>
|
2021-12-09 22:27:13 +01:00
|
|
|
Result(emplace_t, ArgsT &&...args) : status_(), value_(std::forward<ArgsT>(args)...) {
|
2019-07-06 13:29:15 +02:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
Result(Status &&status) : status_(std::move(status)) {
|
|
|
|
CHECK(status_.is_error());
|
|
|
|
}
|
|
|
|
Result(const Result &) = delete;
|
|
|
|
Result &operator=(const Result &) = delete;
|
2021-10-18 13:36:15 +02:00
|
|
|
Result(Result &&other) noexcept : status_(std::move(other.status_)) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (status_.is_ok()) {
|
|
|
|
new (&value_) T(std::move(other.value_));
|
|
|
|
other.value_.~T();
|
|
|
|
}
|
2018-07-08 21:00:54 +02:00
|
|
|
other.status_ = Status::Error<-2>();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2021-10-18 13:36:15 +02:00
|
|
|
Result &operator=(Result &&other) noexcept {
|
2019-02-04 05:05:44 +01:00
|
|
|
CHECK(this != &other);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (status_.is_ok()) {
|
|
|
|
value_.~T();
|
|
|
|
}
|
|
|
|
if (other.status_.is_ok()) {
|
|
|
|
#if TD_GCC
|
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
|
|
|
|
#endif
|
|
|
|
new (&value_) T(std::move(other.value_));
|
|
|
|
#if TD_GCC
|
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
#endif
|
|
|
|
other.value_.~T();
|
|
|
|
}
|
|
|
|
status_ = std::move(other.status_);
|
2018-07-08 21:00:54 +02:00
|
|
|
other.status_ = Status::Error<-3>();
|
2018-12-31 20:04:05 +01:00
|
|
|
return *this;
|
|
|
|
}
|
2019-07-06 13:29:15 +02:00
|
|
|
template <class... ArgsT>
|
2021-12-09 22:27:13 +01:00
|
|
|
void emplace(ArgsT &&...args) {
|
2019-07-06 13:29:15 +02:00
|
|
|
if (status_.is_ok()) {
|
|
|
|
value_.~T();
|
|
|
|
}
|
|
|
|
new (&value_) T(std::forward<ArgsT>(args)...);
|
|
|
|
status_ = Status::OK();
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
~Result() {
|
|
|
|
if (status_.is_ok()) {
|
|
|
|
value_.~T();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-08 21:00:54 +02:00
|
|
|
#ifdef TD_STATUS_NO_ENSURE
|
|
|
|
void ensure() const {
|
|
|
|
status_.ensure();
|
|
|
|
}
|
|
|
|
void ensure_error() const {
|
|
|
|
status_.ensure_error();
|
|
|
|
}
|
|
|
|
#else
|
2018-02-12 17:03:04 +01:00
|
|
|
void ensure_impl(CSlice file_name, int line) const {
|
|
|
|
status_.ensure_impl(file_name, line);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-02-12 17:03:04 +01:00
|
|
|
void ensure_error_impl(CSlice file_name, int line) const {
|
|
|
|
status_.ensure_error_impl(file_name, line);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-07-08 21:00:54 +02:00
|
|
|
#endif
|
2018-12-31 20:04:05 +01:00
|
|
|
void ignore() const {
|
|
|
|
status_.ignore();
|
|
|
|
}
|
|
|
|
bool is_ok() const {
|
|
|
|
return status_.is_ok();
|
|
|
|
}
|
|
|
|
bool is_error() const {
|
|
|
|
return status_.is_error();
|
|
|
|
}
|
|
|
|
const Status &error() const {
|
|
|
|
CHECK(status_.is_error());
|
|
|
|
return status_;
|
|
|
|
}
|
|
|
|
Status move_as_error() TD_WARN_UNUSED_RESULT {
|
|
|
|
CHECK(status_.is_error());
|
|
|
|
SCOPE_EXIT {
|
2018-07-08 21:00:54 +02:00
|
|
|
status_ = Status::Error<-4>();
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
return std::move(status_);
|
|
|
|
}
|
2023-03-21 11:30:42 +01:00
|
|
|
Status move_as_error_unsafe() TD_WARN_UNUSED_RESULT {
|
2019-07-06 13:29:15 +02:00
|
|
|
SCOPE_EXIT {
|
2019-07-21 20:07:07 +02:00
|
|
|
status_ = Status::Error<-5>();
|
2019-07-06 13:29:15 +02:00
|
|
|
};
|
2023-03-21 11:30:42 +01:00
|
|
|
return std::move(status_);
|
|
|
|
}
|
|
|
|
Status move_as_error_prefix(Slice prefix) TD_WARN_UNUSED_RESULT {
|
|
|
|
SCOPE_EXIT {
|
|
|
|
status_ = Status::Error<-6>();
|
|
|
|
};
|
2019-07-06 13:29:15 +02:00
|
|
|
return status_.move_as_error_prefix(prefix);
|
|
|
|
}
|
2023-03-21 11:30:42 +01:00
|
|
|
Status move_as_error_prefix_unsafe(Slice prefix) TD_WARN_UNUSED_RESULT {
|
|
|
|
SCOPE_EXIT {
|
|
|
|
status_ = Status::Error<-7>();
|
|
|
|
};
|
|
|
|
return status_.move_as_error_prefix_unsafe(prefix);
|
|
|
|
}
|
2020-06-24 13:47:36 +02:00
|
|
|
Status move_as_error_prefix(const Status &prefix) TD_WARN_UNUSED_RESULT {
|
|
|
|
SCOPE_EXIT {
|
2023-03-21 11:30:42 +01:00
|
|
|
status_ = Status::Error<-8>();
|
2020-06-24 13:47:36 +02:00
|
|
|
};
|
|
|
|
return status_.move_as_error_prefix(prefix);
|
|
|
|
}
|
2023-03-21 11:30:42 +01:00
|
|
|
|
2020-06-24 13:47:36 +02:00
|
|
|
Status move_as_error_suffix(Slice suffix) TD_WARN_UNUSED_RESULT {
|
|
|
|
SCOPE_EXIT {
|
2023-03-21 11:30:42 +01:00
|
|
|
status_ = Status::Error<-9>();
|
2020-06-24 13:47:36 +02:00
|
|
|
};
|
|
|
|
return status_.move_as_error_suffix(suffix);
|
|
|
|
}
|
2023-03-21 11:30:42 +01:00
|
|
|
Status move_as_error_suffix_unsafe(Slice suffix) TD_WARN_UNUSED_RESULT {
|
|
|
|
SCOPE_EXIT {
|
|
|
|
status_ = Status::Error<-10>();
|
|
|
|
};
|
|
|
|
return status_.move_as_error_suffix_unsafe(suffix);
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
const T &ok() const {
|
2019-02-12 17:17:20 +01:00
|
|
|
LOG_CHECK(status_.is_ok()) << status_;
|
2018-12-31 20:04:05 +01:00
|
|
|
return value_;
|
|
|
|
}
|
|
|
|
T &ok_ref() {
|
2019-02-12 17:17:20 +01:00
|
|
|
LOG_CHECK(status_.is_ok()) << status_;
|
2018-12-31 20:04:05 +01:00
|
|
|
return value_;
|
|
|
|
}
|
2018-04-06 21:37:30 +02:00
|
|
|
const T &ok_ref() const {
|
2019-02-12 17:17:20 +01:00
|
|
|
LOG_CHECK(status_.is_ok()) << status_;
|
2018-04-06 21:37:30 +02:00
|
|
|
return value_;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
T move_as_ok() {
|
2019-02-12 17:17:20 +01:00
|
|
|
LOG_CHECK(status_.is_ok()) << status_;
|
2018-12-31 20:04:05 +01:00
|
|
|
return std::move(value_);
|
|
|
|
}
|
2023-03-21 11:30:42 +01:00
|
|
|
T move_as_ok_unsafe() {
|
|
|
|
return std::move(value_);
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
Result<T> clone() const TD_WARN_UNUSED_RESULT {
|
|
|
|
if (is_ok()) {
|
|
|
|
return Result<T>(ok()); // TODO: return clone(ok());
|
|
|
|
}
|
|
|
|
return error().clone();
|
|
|
|
}
|
|
|
|
void clear() {
|
|
|
|
*this = Result<T>();
|
|
|
|
}
|
|
|
|
|
2020-06-24 13:47:36 +02:00
|
|
|
template <class F>
|
2020-06-26 01:24:13 +02:00
|
|
|
Result<decltype(std::declval<F>()(std::declval<T>()))> move_map(F &&f) {
|
2020-06-24 13:47:36 +02:00
|
|
|
if (is_error()) {
|
2023-03-21 11:30:42 +01:00
|
|
|
return move_as_error_unsafe();
|
2020-06-24 13:47:36 +02:00
|
|
|
}
|
2023-03-21 11:30:42 +01:00
|
|
|
return f(move_as_ok_unsafe());
|
2020-06-24 13:47:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class F>
|
|
|
|
decltype(std::declval<F>()(std::declval<T>())) move_fmap(F &&f) {
|
|
|
|
if (is_error()) {
|
2023-03-21 11:30:42 +01:00
|
|
|
return move_as_error_unsafe();
|
2020-06-24 13:47:36 +02:00
|
|
|
}
|
2023-03-21 11:30:42 +01:00
|
|
|
return f(move_as_ok_unsafe());
|
2020-06-24 13:47:36 +02:00
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
private:
|
|
|
|
Status status_;
|
|
|
|
union {
|
|
|
|
T value_;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
inline Result<Unit>::Result(Status &&status) : status_(std::move(status)) {
|
|
|
|
// no assert
|
|
|
|
}
|
|
|
|
|
|
|
|
inline StringBuilder &operator<<(StringBuilder &string_builder, const Status &status) {
|
|
|
|
return status.print(string_builder);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|