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)
58 std::string
MakeMsg(std::string&& msg,
char const*, std::int32_t);
60 std::string
MakeMsg(std::string&& msg,
char const* file, std::int32_t line);
70 std::unique_ptr<detail::ResultImpl> impl_{
nullptr};
74 explicit
Result(
std::
string msg) : impl_{std::make_unique<detail::ResultImpl>(std::move(msg))} {}
75 explicit Result(std::string msg, std::error_code errc)
76 : impl_{
std::make_unique<detail::ResultImpl>(
std::move(msg),
std::move(errc))} {}
78 : impl_{
std::make_unique<detail::ResultImpl>(
std::move(msg),
std::move(prev.impl_))} {}
80 : impl_{
std::make_unique<detail::ResultImpl>(
std::move(msg),
std::move(errc),
81 std::move(prev.impl_))} {}
88 [[nodiscard]]
bool OK() const noexcept(true) {
return !impl_; }
89 [[nodiscard]] std::string
Report()
const {
return OK() ?
"" : impl_->Report(); }
93 [[nodiscard]]
auto Code()
const {
return OK() ? std::error_code{} : impl_->Code(); }
95 if (
OK() && that.OK()) {
98 if ((
OK() && !that.OK()) || (!
OK() && that.OK())) {
101 return *impl_ == *that.impl_;
109 return std::forward<Result>(rhs);
112 return std::forward<Result>(lhs);
114 lhs.impl_->Concat(std::move(rhs.impl_));
115 return std::forward<Result>(lhs);
132 [[nodiscard]]
inline auto Fail(std::string msg, std::error_code errc,
147 [[nodiscard]]
inline auto Fail(std::string msg, std::error_code errc,
Result&& prev,
151 std::forward<Result>(prev)};
155 template <
typename Fn>
156 [[nodiscard]] std::enable_if_t<std::is_invocable_v<Fn>, Result>
operator<<(Result&& r, Fn&& fn) {
158 return std::forward<Result>(r);
Definition: intrusive_ptr.h:207
std::string MakeMsg(std::string &&msg, char const *, std::int32_t)
std::enable_if_t< std::is_invocable_v< Fn >, Result > operator<<(Result &&r, Fn &&fn)
Definition: result.h:156
auto Fail(std::string msg, char const *file=__builtin_FILE(), std::int32_t line=__builtin_LINE())
Return failure.
Definition: result.h:125
void SafeColl(Result const &rc)
auto Success() noexcept(true)
Return success.
Definition: result.h:121
Result operator+(Result &&lhs, Result &&rhs)
Definition: result.h:107
#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:68
Result(std::string msg, std::error_code errc, Result &&prev)
Definition: result.h:79
std::string Report() const
Definition: result.h:89
Result(Result const &that)=delete
bool operator==(Result const &that) const noexcept(true)
Definition: result.h:94
friend Result operator+(Result &&lhs, Result &&rhs)
Definition: result.h:107
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:93
Result(std::string msg, std::error_code errc)
Definition: result.h:75
Result & operator=(Result const &that)=delete
Result(Result &&that)=default
Result(std::string msg, Result &&prev)
Definition: result.h:77
bool OK() const noexcept(true)
Definition: result.h:88
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