9 #include <system_error>
18 std::unique_ptr<ResultImpl>
prev{
nullptr};
35 if ((
prev && !that.prev) || (!
prev && that.prev)) {
40 auto cur_eq =
message == that.message &&
errc == that.errc;
41 if (
prev && that.prev) {
43 auto prev_eq = *
prev == *that.prev;
44 return cur_eq && prev_eq;
49 [[nodiscard]] std::string
Report()
const;
50 [[nodiscard]] std::error_code
Code()
const;
52 void Concat(std::unique_ptr<ResultImpl> rhs);
55 #if (!defined(__GNUC__) && !defined(__clang__)) || defined(__MINGW32__)
56 #define __builtin_FILE() nullptr
57 #define __builtin_LINE() (-1)
60 std::string
MakeMsg(std::string&& msg,
char const* file, std::int32_t line);
69 std::unique_ptr<detail::ResultImpl> impl_{
nullptr};
73 explicit
Result(
std::
string msg) : impl_{std::make_unique<detail::ResultImpl>(std::move(msg))} {}
74 explicit Result(std::string msg, std::error_code errc)
75 : impl_{
std::make_unique<detail::ResultImpl>(
std::move(msg),
std::move(errc))} {}
77 : impl_{
std::make_unique<detail::ResultImpl>(
std::move(msg),
std::move(prev.impl_))} {}
79 : impl_{
std::make_unique<detail::ResultImpl>(
std::move(msg),
std::move(errc),
80 std::move(prev.impl_))} {}
87 [[nodiscard]]
bool OK() const noexcept(true) {
return !impl_; }
88 [[nodiscard]] std::string
Report()
const {
return OK() ?
"" : impl_->Report(); }
92 [[nodiscard]]
auto Code()
const {
return OK() ? std::error_code{} : impl_->Code(); }
94 if (
OK() && that.OK()) {
97 if ((
OK() && !that.OK()) || (!
OK() && that.OK())) {
100 return *impl_ == *that.impl_;
108 return std::forward<Result>(rhs);
111 return std::forward<Result>(lhs);
113 lhs.impl_->Concat(std::move(rhs.impl_));
114 return std::forward<Result>(lhs);
131 [[nodiscard]]
inline auto Fail(std::string msg, std::error_code errc,
146 [[nodiscard]]
inline auto Fail(std::string msg, std::error_code errc,
Result&& prev,
150 std::forward<Result>(prev)};
154 template <
typename Fn>
155 [[nodiscard]] std::enable_if_t<std::is_invocable_v<Fn>, Result>
operator<<(Result&& r, Fn&& fn) {
157 return std::forward<Result>(r);
Definition: intrusive_ptr.h:207
std::string MakeMsg(std::string &&msg, char const *file, std::int32_t line)
std::enable_if_t< std::is_invocable_v< Fn >, Result > operator<<(Result &&r, Fn &&fn)
Definition: result.h:155
auto Fail(std::string msg, char const *file=__builtin_FILE(), std::int32_t line=__builtin_LINE())
Return failure.
Definition: result.h:124
void SafeColl(Result const &rc)
auto Success() noexcept(true)
Return success.
Definition: result.h:120
Result operator+(Result &&lhs, Result &&rhs)
Definition: result.h:106
#define __builtin_LINE()
Definition: result.h:57
#define __builtin_FILE()
Definition: result.h:56
An error type that's easier to handle than throwing dmlc exception. We can record and propagate the s...
Definition: result.h:67
Result(std::string msg, std::error_code errc, Result &&prev)
Definition: result.h:78
std::string Report() const
Definition: result.h:88
Result(Result const &that)=delete
bool operator==(Result const &that) const noexcept(true)
Definition: result.h:93
friend Result operator+(Result &&lhs, Result &&rhs)
Definition: result.h:106
Result & operator=(Result &&that)=default
Result() noexcept(true)=default
auto Code() const
Return the root system error. This might return success if there's no system error.
Definition: result.h:92
Result(std::string msg, std::error_code errc)
Definition: result.h:74
Result & operator=(Result const &that)=delete
Result(Result &&that)=default
Result(std::string msg, Result &&prev)
Definition: result.h:76
bool OK() const noexcept(true)
Definition: result.h:87
void Concat(std::unique_ptr< ResultImpl > rhs)
std::string message
Definition: result.h:15
ResultImpl & operator=(ResultImpl const &that)=delete
ResultImpl(std::string msg)
Definition: result.h:26
ResultImpl(std::string msg, std::error_code errc)
Definition: result.h:27
ResultImpl & operator=(ResultImpl &&that)=default
std::error_code Code() const
ResultImpl(std::string msg, std::unique_ptr< ResultImpl > prev)
Definition: result.h:29
ResultImpl(ResultImpl &&that)=default
std::error_code errc
Definition: result.h:16
std::string Report() const
bool operator==(ResultImpl const &that) const noexcept(true)
Definition: result.h:34
ResultImpl(ResultImpl const &that)=delete
ResultImpl(std::string msg, std::error_code errc, std::unique_ptr< ResultImpl > prev)
Definition: result.h:31
std::unique_ptr< ResultImpl > prev
Definition: result.h:18